Chapter 5 Flashcards
What is the purpose of using methods in Java?
Methods break a complex problem into manageable pieces (divide and conquer) and allow code reuse, making programs simpler and more organized.
What is a void method in Java?
A void method performs a task and terminates without returning any value.
What is a value-returning method in Java?
A value-returning method performs a task and sends a value back to the code that called it.
What components make up a method definition in Java?
A method definition consists of a header (including method modifiers, return type, method name, and parameters) and a body (the statements executed when the method is called).
What does the public method modifier mean in Java?
The public modifier makes the method accessible from outside the class.
What does the static method modifier mean in Java?
The static modifier means the method belongs to the class, not a specific instance of the class.
What is the purpose of documentation comments (/** … */) in methods?
Documentation comments explain the method’s purpose and can include @param tags to describe parameters.
What is the difference between arguments and parameters in Java methods?
Arguments are values passed to a method during the call, while parameters are variables that hold the argument values inside the method.
What is the significance of widening and narrowing conversions when passing arguments to methods?
Widening conversions (e.g., int to double) are handled automatically, but narrowing conversions (e.g., double to int) cause a compiler error unless explicitly cast.
How are primitive data types passed to methods in Java?
Primitive data types are passed by value, meaning only a copy of the data is passed to the method.
How are objects passed to methods in Java?
Objects are passed by reference, meaning the reference to the object is passed, but the object itself is not directly modified.
What are local variables in Java?
Local variables are declared inside a method, are not accessible outside it, and are destroyed when the method ends. They must be initialized before use.
How do you validate arguments in Java methods?
Use boolean methods to test if arguments meet specific conditions and return true or false.
What is functional decomposition in programming?
Functional decomposition is the process of breaking a large, complex problem into smaller, manageable pieces (methods).
What does the throws clause in a method header signify in Java?
The throws clause indicates that the method might throw an exception (e.g., IOException), and the caller must handle or declare it.