More Java Review Notes
Data Types
int = Integer (whole numbers)
double = Decimal numbers
boolean = True/False
char = A single keyboard character surrounded by ' '
String = Series of characters surrounded by " "
int = Integer (whole numbers)
double = Decimal numbers
boolean = True/False
char = A single keyboard character surrounded by ' '
String = Series of characters surrounded by " "
Variables & Casting
Variables are memory locations which hold data. Once a variable holds data of a certain type, it cannot change types except under specific conditions. To explicitly change the data type, you can use casting. For example:
Variables are memory locations which hold data. Once a variable holds data of a certain type, it cannot change types except under specific conditions. To explicitly change the data type, you can use casting. For example:
Methods
These are also called functions. They usually are named starting with lowercase letters and are verbs because they do something. Example:
These are also called functions. They usually are named starting with lowercase letters and are verbs because they do something. Example:
In the above method, named getData, there are two formal parameters: an integer called num and a boolean called status. The function returns a String variable which is locally defined as message within the function.
The Main Method
Every program must have a main method. This is where the program will start running. The Java main method is:
Every program must have a main method. This is where the program will start running. The Java main method is:
Control Structures
Control structures direct the flow of a program through its operations. Common control structures include the following:
if statements
If statements set a condition as to whether a line or block of code will run or not.
Example 1:
Control structures direct the flow of a program through its operations. Common control structures include the following:
if statements
If statements set a condition as to whether a line or block of code will run or not.
Example 1:
Note that there is no semi-colon after the "if" line, only at the end of the statement underneath it.
Example 2:
Example 2:
While Loops
When the number of times to run the loop is not known, a while loop should be used. Example:
When the number of times to run the loop is not known, a while loop should be used. Example:
For Loops
When the number of times to run the loop is known, a for loop should be used. Example:
When the number of times to run the loop is known, a for loop should be used. Example:
Operators
Operators can be mathematical, relational, or logical. They are often used in control structures to make decisions.
Mathematical Operators
Operators can be mathematical, relational, or logical. They are often used in control structures to make decisions.
Mathematical Operators
Relational Operators
Logical Operators
Classes
A class is a blueprint for objects. Multiple, individual objects can be created from one class. Most classes have the following parts:
1. A constructor that organizes an object with default settings
2. Variables
3. Methods
Example:
A class is a blueprint for objects. Multiple, individual objects can be created from one class. Most classes have the following parts:
1. A constructor that organizes an object with default settings
2. Variables
3. Methods
Example:
Objects
Objects are instances of a class. They have properties and can perform functions. An object could be created from the above class as follows:
Objects are instances of a class. They have properties and can perform functions. An object could be created from the above class as follows:
To use the function defined in the object myObject, identify the object name followed by the dot operator followed by the function name. Example:
This function call would result in "What's up?Hermaent" being output to the console.