Chapter 3/ Parameters and Objects Flashcards
Parameter
In Java, parameters sent to methods are passed-by-value. The “type” of data that a method can receive is referred to as a “parameter”.
When passing a parameter must have type declared.
ex.
public static void countSum(int number){
}
Formal Parameter
A variable that appears inside parentheses in the header of a method that os used to generalize the method’s behavior.
Actual Parameter
A specific value or expression that appears inside parentheses in a method call.
Character Literals
Character literals are enclosed in single quotation mart.
Ex.
‘>’ ‘=’
Parameters vs. Class Constants
Parameters are more flexible. Because you specify the value to be used each time you call the method.
Overloading of Methods
When there are more than one method with the same name, as long as they have different parameters.
The primarily requirement for overloading is that the methods must have different method signatures when defining them.
drawBox(3, 6);
drawBox();
Method Signature
The name of a statement along with its number and type of parameters.
Ex.
one method with the same name has zero parameters
and another one has two parameters.
The situations gets complicated when overloading involves the same number of parameters.
For example, println method is a series of overloaded methods. We can call this method by passing different parameters: String, int, double, etc.
Interactive Programs
Programs that wait for the user to type a response.
Console Input
Response typed by the user an interactive program pauses for input.
System.out -> Standart Output
When refer to System.out you accessing an object in the System class known as a standard output stream.
System.in
corresponding object for input.
The easier way to read console inputs with using Scanner objects.
Constructor (Construct)
the most of the objects are constructed by calling a special method Constructor.
Constructor - a method that creates and initializes an object.
Java constructor are called using a keyword ‘new’ followed by the object’s type and any necessary parameters.
Ex. construct a specific scanner object Scanner console = new Scanner(System.in);
Scanner
Import java.util.*; // a package containing Scanner public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Please enter a number: "); int num = console.nextInt(); System.out.print("Please enter a number: "); double num = console.nextDouble(); System.out.println(); }
Scanner Methods
next() -> reads and returns the next tokens a String
nextDouble() -> reads and returns a double value
nextInt() -> reads and returns an int value
nextLine() -> reads and returns the next line of input as a String
Token
a single element of input (one word, one number)