Java Basics: Identifiers, Data Types, Operators
Java Basics: Identifiers, Data Types, Operators
Content
1. Identifiers
OBJECT-ORIENTED LANGUAGE AND THEORY 2. Data Types
02-2. JAVA BASICS 3. Operators
4. Control Statements
5. Arrays
1 2
3 4
3 4
Page 1
1
5 6
5 6
7 8
7 8
Page 2
2
9 10
Integer Real
• Signed integer • Initially created with value 0.0
• Initially created with value 0
9 10
11 12
11 12
Page 3
3
13 14
13 14
15 16
15 16
Page 4
4
17 18
Escape sequence
2.3. Casting
• Characters for keyboard control
• \b backspace • Java is a strongly-typed language
• \f form feed • Casting a wrong type to a variable can lead to a compiler
• \n newline error or exceptions in JVM
• \r return (về đầu dòng)
• \t tab
• JVM can implicitly cast a data type to a larger
• Display special characters in a string data type
• \" quotation mark • To cast a variable to a narrower data type, we
• \’ apostrophe need to do it explicitly
• \\ backslash
double f;
int a, b; int d; long g;
short c; short e; f = g;
a = b + c; e = (short)d; g = f; //error
17 18
19 20
19 20
Page 5
5
21 22
21 22
23 24
Comments Command
• Java supports three types of comments: • Command ends with;
• // Comments in a single line
• Multiple commands can be written on one line
// Without line break
• /* Comments as a paragraph */ • A command can be written in multiple lines
• /** Javadoc * comments in form of Javadoc */
• Example:
[Link](
“This is part of the same line”);
23 24
Page 6
6
25 26
Content 3. Operators
1. Identifiers • An operator combines single values or child expressions
into a more complex expression. It can return a value.
2. Data Types • Java provides the following operators:
3. Operators • Arithmetic operators
• Bitwise operator, relational operators
4. Control Statements
• Logical operators
5. Arrays • Assignment operators
• Unary operators
25 26
27 28
• ==, !=, >, <, >=, <= • >>=, <<=, &=, |=, ^=
• Logical operators
• &&, ||, !
27 28
Page 7
7
29 30
29 30
31 32
31 32
Page 8
8
33 34
33 34
35 36
35 36
Page 9
9
37 38
37 38
39 40
39 40
Page 10
10
41 42
41 42
43 44
43 44
Page 11
11
45 46
Example
Example - Array
//Java Program to illustrate how to declare, instantiate, initialize c[ 0 ] -45
Array name (all the
//and traverse the Java array. elements of array c[ 1 ] 6
class Testarray { have the same
name, c) c[ 2 ] 0
public static void main(String args[]) {
int a[] = new int[5];// declaration and instantiation c[ 3 ] 72
a[0] = 10;// initialization c[ 4 ] 1543
a[1] = 20; [Link]: length of
a[2] = 70; the array c c[ 5 ] -89
a[3] = 40; c[ 6 ] 0
a[4] = 50;
c[ 7 ] 62
//traversing array
for (int i = 0; i < [Link]; i++)// length is the property of array c[ 8 ] -3
[Link](a[i]); c[ 9 ] 1
} Index (to access to the
} elements of array) c[ 10 ] 6453
c[ 11 ] 78
45 46
47 48
47 48
Page 12
12
49
Row 0
b[ 0 ][ 0 ] b[ 0 ][ 1 ] b[ 0 ][ 2 ] b[ 0 ][ 3 ]
Row 1
b[ 1 ][ 0 ] b[ 1 ][ 1 ] b[ 1 ][ 2 ] b[ 1 ][ 3 ]
Row 2
b[ 2 ][ 0 ] b[ 2 ][ 1 ] b[ 2 ][ 2 ] b[ 2 ][ 3 ]
Column index
Row index
Array name
49
Page 13
13