Part 01 - Java Basics 1 - Variable_2025
Part 01 - Java Basics 1 - Variable_2025
◼ Identifiers are:
◼ Text strings that represent variables, methods,
classes or labels
◼ Case-sensitive
◼ Characters can be digit, letter, '$' or '_'
◼ Identifiers cannot:
◼ Begin with a digit
◼ Be the same as a reserved word.
An_Identifier
a_2nd_Identifier
Go2
✓ An-Identifier
2nd_Identifier
goto
$10 10$
17/02/2025 Khoa CNTT – Trường ĐH Nông Lâm TP. HCM 5
Legal Identifiers
Categories:
1. byte Size: 1 byte
a. integer Range: -27 → 27 - 1
Categories:
a. integer
b. floating 1. float
Size: 4 bytes
Range: ±1.4 x 10-45 → ±3.4 x 1038
c. character
2. double Size: 8 bytes
d. boolean Range: ±4.9 x 10-324 → ±1.8 x 10308
Categories:
a. integer
b. floating
c. character char Size: 2 bytes
Range: \u0000 → \uFFFF
d. boolean
Categories:
a. integer
b. floating
c. character
Size: 1 byte
d. boolean boolean Range: true | false
byte Byte-length
1 byte : –128 to 127
integer
short Short integer 2 bytes : –32 768 to 32 767
int Integer 4 bytes : –2 147 483 648 to 2 147 483 647
char A Unicode
2 bytes
character
boolean A boolean value true or false
◼ float g = 49837849.029847F;
◼ Boolean Literals
◼ true, false
◼ Example:
char ch;
int i;
float f;
double d;
x = y;
int total;
➢ Notes:
◼ Can only be used to declare local variables inside methods
(functions) or code blocks
◼ You can't use local variable type inference with method
arguments,
◼ You cannot initialize a var variable to null.
◼ Note:
◼ only the Member variables acquire the default values if not
explicitly initialized
◼ You must initialize the local variables explicitly before you use
them in the code, otherwise you will receive a compiler error.
17/02/2025 Khoa CNTT – Trường ĐH Nông Lâm TP. HCM 34
Variable Initialization
//integers
byte largestByte = Byte.MAX_VALUE;
short largestShort = Short.MAX_VALUE;
int largestInteger = Integer.MAX_VALUE;
long largestLong = Long.MAX_VALUE;
//real numbers
float largestFloat = Float.MAX_VALUE;
double largestDouble =
Double.MAX_VALUE;
//other primitive types
char aChar = 'S';
boolean aBoolean = true;
17/02/2025 Khoa CNTT – Trường ĐH Nông Lâm TP. HCM 35
Variables declarations
1) int n = -100;
2) int x = 089, int a = 0x137E
3) unsigned int i = -100;
4) int = 2.9, b = 0x34G;
5) long m = 2, p = 4;
6) int 2k;
7) float y = y * 2;
8) char ch = “b”, m = ‘\n’;
System.out.println(”===============“);
System.out.println("Obj1:count is="+obj1.count);
System.out.println("Obj2: count is="+obj2.count);
}
}
→ the result of obj1.count =?? and obj2.count =???
◼ int[] scores;
◼ Creating an Array
◼ scores[0] = 75;
◼ System.out.print(scores[0])
Number
Data type Array variable’s name
elements
• Declaring Arrays:
– Declare arrays of primitive or class types:
char s[];
char[] s;
Point p[];
Point[] p;
– The declaration of an array creates space for a
reference
– Actual memory allocation is done dynamically
either by a new statement or by an array
initializer.
class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
//populate matrix
for (int i = 0; i < aMatrix.length; i++) {
aMatrix[i] = new int[5]; //create sub-array
for (int j = 0; j < aMatrix[i].length; j++) {
aMatrix[i][j] = i + j;
}
}
//print matrix
for (int i = 0; i < aMatrix.length; i++) {
for (int j = 0; j < aMatrix[i].length; j++) {
System.out.print(aMatrix[i][j] + " ");
}
System.out.println();
}
}
}
17/02/2025 Khoa CNTT – Trường ĐH Nông Lâm TP. HCM 50
TriangleArray Example
◼ Stack
◼ Local variables: The variables of primitive types
defined inside a method or as method parameters
◼ Local reference variables: The variables that
refer to an object and are defined inside a
method or as a method parameter. Remember
that an object that a local variable refers to lives
on the heap and not on the stack
◼ Method invocations (Parameters): When you
invoke (call) a method, the method is pushed
onto the stack (that is, placed on top of the
stack)
◼ Heap
◼ Instance variables: The variables of primitive
types defined inside a class but outside of all its
methods
◼ Instance reference variables: The variables
that refer to an object and are defined inside a
class but outside of all its methods
◼ Objects: Represent the entities in the real-
world problem that the Java program is trying to
solve. All the objects live on the heap, always
◼ Note: the object will not die with the local
reference variable
(Lưu ý rằng trong bài này, việc tính trung bình cộng cần
cho kết quả là số kiểu nguyên, nghĩa là nếu tổng giá trị
bằng 7 thì trung bình cộng cần in ra phải là 2 chứ không
phải 2.3333.
◼ Cho 2 số nguyên a, b. Viết một chương trình kiểm
tra xem số thứ nhất có chia hết cho số thứ hai hay
không, in ra kết quả kiểm tra