This webpage is NOT used ---------------------------------------- Tell Prof. Cheung if it shows up in website....
|
|
|
|
Also, you must use the correct syntax construct to write a variable definition
int NameOfVariable ;
|
Notes:
|
public class Var01
{
public static void main(String[] args)
{
int x; // Define integer variable with name "x"
System.out.println("Hello Class");
System.out.println(" The variable x contains this number:");
System.out.println(x); // Print varibale "x"
}
}
|
Notes:
|
If you compile the program, you will get an error:
Var01.java:11: variable x might not have been initialized
System.out.println(x); // Print the content in variable "x"
^
|
This is because the variable x does not have a value yet.
|
VariableName = Value ; |
Notes:
|
public class Var02
{
public static void main(String[] args)
{
int x; // Define integer variable with name "x"
x = 12345; // Assign 12345 to x
System.out.println("Hello Class");
System.out.println(" The variable x contains this number:");
System.out.println(x); // Print varibale "x"
}
}
|
Notes:
|
How to run the program:
|
Output:
Hello Class
The variable x contains this number:
12345
|
|
Most of the integer arithmetic operators should look familiar
| Operator symbol | Operation |
|---|---|
| + | addition |
| - | subtraction |
| * | multiplication |
| / | compute the quotient of a division |
| % | compute the remainder of a division |
Result of the integer operation 5 + 3 is 8 Result of the integer operation 5 - 3 is 2 Result of the integer operation 5 * 3 is 15 Result of the integer operation 5 / 3 is 1 (quotient !) Result of the integer operation 5 % 3 is 2 (remainder !) |
|
|
Schematically:
|
|
|
|
|
|
All it takes is a bit of imagination
|
(Context: remember the example of "you is a word" and "you are a student" ?)
|
Example:
|
|
|
| Logic value | Value used to encode the lo/types.html gic value |
|---|---|
| true | 1 (integer) |
| false | 0 (integer) |
In other words:
|