5. JAVA
Mr.Warawut
Khangkhan
F F a-z, A-Z, _, $ F
F
Chapter 2 Basic Programming Concepts
a-z, A-Z, _, $
F F F F ˈ
(Case Sensitive)
F (Reserved Word)
Literal Words (True, False, Null)
5
6. Mr.Warawut
Chapter 2 Basic Programming Concepts
Khangkhan
6
F RESERVED WORD
7. (DECLARATION)
Mr.Warawut
Khangkhan
ˈ F F (Data
Type) F
Chapter 2 Basic Programming Concepts
dataType varName [= value];
dataType = F
varName =
value = F
7
9. Mr.Warawut Khangkhan Chapter 2 Basic Programming Concepts
Basic Programming Concepts
DATA TYPE
9
10. F (DATA TYPE)
Mr.Warawut
Khangkhan
Primitive Data Type - F
F F
Chapter 2 Basic Programming Concepts
Class Type –
Interface
Array Type – F ˈ F F F ˈ
10
11. F
(PRIMITIVE DATA TYPE)
Mr.Warawut
Khangkhan
F (Integer)
F
Chapter 2 Basic Programming Concepts
(Floating Point)
F (Character)
F F (Boolean)
11
12. INTEGER TYPE
Mr.Warawut
Khangkhan
Data Type Size Range
(Byte)
Chapter 2 Basic Programming Concepts
byte 1 -128 to +127
short 2 -32,768 to +32,767
int 4 -2,147,483,648 to
+2,147,483,647
long 8 -9,223,372,036,854,775,808
to
+9,223,372,036,854,775,807
12
13. EX. INTEGER TYPE
Mr.Warawut
Khangkhan
byte A = 65;
short B = 32767;
Chapter 2 Basic Programming Concepts
int C = 2147286444;
long D = 21472864448L;
long E = 21472864438l;
13
14. FLOATING POINT TYPE
Mr.Warawut
Khangkhan
Data Type Size Range
(Byte)
Chapter 2 Basic Programming Concepts
float 4 -3.40292347E+38 to
+3.40292347E+38
double 8 -1.79769313486231570E+308
to
+1.79769313486231570E+308
float a = 200;
float b = 200.5f; // 200.5F;
double c = 300; 14
double d = 300.7d; // 300.7D;
15. CHARACTER TYPE
Mr.Warawut
Khangkhan
Data Type Size Range
(Byte)
Chapter 2 Basic Programming Concepts
char 2 0 to 65,535
char c = ‘A’;
char cInt = 65;
15
16. Mr.Warawut
Chapter 2 Basic Programming Concepts
Khangkhan
16
EX. PROGRAMMING
CHARACTER TYPE
17. BOOLEAN TYPE
Mr.Warawut
Khangkhan
F F
F2 F F ˈ F
Chapter 2 Basic Programming Concepts
true false
boolean b1 = false;
boolean b2 = true;
17
18. STRING TYPE
Mr.Warawut
Khangkhan
Java F F
F F
Chapter 2 Basic Programming Concepts
(class)
String name = “Warawut”;
String langProg = “Java”;
18
24. (OPERATOR)
Mr.Warawut
Khangkhan
F (Expression) F
F F F , , ˆ F
Chapter 2 Basic Programming Concepts
F F (Operator) F
F
num1 + num2
a = (b - c) * d
24
25. (OPERATOR)
Mr.Warawut
Khangkhan
F (Assignment
Operators)
Chapter 2 Basic Programming Concepts
F (Arithmetic
Operators)
(Comparison
Operators)
F (Logical Operators)
(Unary Operators)
(Bitwise Operators) 25
26. ASSIGNMENT
OPERATORS
Mr.Warawut
Khangkhan
Operator Description
= F
Chapter 2 Basic Programming Concepts
+= F
-= F
*= F
/= F
%= F
26
27. ASSIGNMENT
OPERATORS
Mr.Warawut
Khangkhan
F F F F F
a = 30 a = 30 30
Chapter 2 Basic Programming Concepts
a += 5 a = a + 5 35
a -= 10 a = a - 10 25
a *= 2 a = a * 2 50
a /= 5 a = a / 5 10
a %= 5 a = a % 5 0
27
28. ARITHMETIC OPERATORS
Mr.Warawut
Khangkhan
Operator Description Example
+ a + b
Chapter 2 Basic Programming Concepts
- a - b
* a * b
/ a / b
% a % b
28
29. COMPARISON
OPERATORS
Mr.Warawut
Khangkhan
Operator Description Example
== F a == b
Chapter 2 Basic Programming Concepts
!= F F a != b
> F a > b
>= F F a >= b
< F F a < b
<= F F F a <= b
29
30. LOGICAL OPERATORS
Mr.Warawut
Khangkhan
Operator Description Example
&& and a && b
Chapter 2 Basic Programming Concepts
|| or a || b
! not !a
30
31. LOGICAL OPERATORS
Mr.Warawut
Khangkhan
a b a && b a || b !a !b
T T T T F F
Chapter 2 Basic Programming Concepts
T F F T F T
F T F T T F
F F F F T T
T – True
F – False
31
32. UNARY OPERATORS
Mr.Warawut
Khangkhan
Operator Description Format Example Execute
++ F Postfix a = b++ a=b
b=b+1
Chapter 2 Basic Programming Concepts
Prefix a = ++ b b=b+1
a=b
F Postfix a = b-- a=b
-- b=b-1
Prefix a = --b b=b–1
a=b
32
34. BITWISE OPERATORS
Mr.Warawut
Khangkhan
Operator Description
<< left shift F
Chapter 2 Basic Programming Concepts
>> right shift
& bitwise AND
| bitwise OR
^ bitwise XOR
ˈ F
34
(Operand) F Integer Character
35. BITWISE OPERATORS
Mr.Warawut
Khangkhan
a b a & b a | b a ^ b
1 1 1 1 0
Chapter 2 Basic Programming Concepts
1 0 0 1 1
0 1 0 1 1
0 0 0 0 0
1 – Open (True)
0 – Close (False)
35
36. BITWISE OPERATORS
Mr.Warawut
Khangkhan
F a = 01000001, b = 00100001
a&b
Chapter 2 Basic Programming Concepts
a 0 1 0 0 0 0 0 1
&
b 0 0 1 0 0 0 0 1
0 0 0 0 0 0 0 1 36
37. BITWISE OPERATORS
Mr.Warawut
Khangkhan
F a = 01000001, b = 00100001
a|b
Chapter 2 Basic Programming Concepts
a 0 1 0 0 0 0 0 1
|
b 0 0 1 0 0 0 0 1
0 1 1 0 0 0 0 1 37
38. BITWISE OPERATORS
Mr.Warawut
Khangkhan
F a = 01000001, b = 00100001
a^b
Chapter 2 Basic Programming Concepts
a 0 1 0 0 0 0 0 1
^
b 0 0 1 0 0 0 0 1
0 1 1 0 0 0 0 0 38
39. BITWISE OPERATORS
Mr.Warawut
Khangkhan
a << 2
Chapter 2 Basic Programming Concepts
a 0 0 1 0 0 0 0 1
1 0 0 0 0 1 0 0
If x = 8 then y = x << 2 is that
y = 8 * 22 = 32
39
40. BITWISE OPERATORS
Mr.Warawut
Khangkhan
b >> 3
Chapter 2 Basic Programming Concepts
b 0 1 0 0 0 0 0 1
0 0 0 0 1 0 0 0
If x = 32 then y = x >> 2 is that
y = 32 / 22 = 8
40
42. Mr.Warawut
Chapter 2 Basic Programming Concepts
Khangkhan
42
F
F
F
F
F
F
(Operand)
F
F
ˈ
F
43. 1 ( ), [ ] L R
2 ++, --, !, ~ R L
Mr.Warawut
Khangkhan
3 *, /, % L R
4 +, - L R
5 <<, >>, >>> L R
Chapter 2 Basic Programming Concepts
6 <, <=, >, >= L R
7 ==, != L R
8 & L R
9 ^ L R
10 | L R
11 && L R
12 || L R
13 ?: L R
14 =, +=, -=, *=, /=, %=, <<=, >>=, >>>=, &=, ^=, R L
!= 43
44. Mr.Warawut
Chapter 2 Basic Programming Concepts
Khangkhan
44
25
25
a = 5, b = 2, c = 10
70
70
(a + b) * c = ?
a+b*c=?
45. a = 5, b = 2, c = 10, d = 3
Mr.Warawut
Khangkhan
a+b*c+d=? 73 28
Chapter 2 Basic Programming Concepts
(a + b) * c + d = ? 73 28
(a + b) * (c + d) = ? 73
a + (b * c) + d = ? 28
45
46. Mr.Warawut Khangkhan Chapter 2 Basic Programming Concepts
Chapter 2 Basic Programming Concepts
CONVERSION
DATA TYPE
46
47. F
Mr.Warawut
Khangkhan
Implicit Type Conversion - F F
Chapter 2 Basic Programming Concepts
Explicit Type Conversion - F
F
47
48. Mr.Warawut
Chapter 2 Basic Programming Concepts
Khangkhan
48
F F
ˈ
IMPLICIT TYPE
F
CONVERSION
ˈ
F
F
F
49. EXPLICIT TYPE
CONVERSION
Mr.Warawut
Khangkhan
F F ˈ F
F ˈ F F
Chapter 2 Basic Programming Concepts
valNameResult = (dataType) valName
valNameResult = F F
dataType = F
valName = F F
49
51. EXPLICIT TYPE
CONVERSION
Mr.Warawut
Khangkhan
F F F
F ˈ F Java
Chapter 2 Basic Programming Concepts
F F F
(Character) F ˈ F F
F ˈ F (Method)
(Class) Wrapper ˈ class F F
(Primitive Data Type)
51
52. F METHOD F CLASS
WRAPPER
Mr.Warawut
Khangkhan
Class Primitive Method F F ˈ Method F F ˈ
Wrapper Data Type
Chapter 2 Basic Programming Concepts
Integer int Integer.parseInt( ) Integer.toString( )
Float float Float.parseFloat( ) Float.toString( )
Double double Double.parseDouble( ) Double.toString( )
52
55. F F
PRINTLN
Mr.Warawut
Khangkhan
Method println( ) print( ) F
F Object out Class System
Chapter 2 Basic Programming Concepts
System.out.println(arg1 + arg2 + … + arg_n);
or
System.out.print(arg1 + arg2 + .. + arg_n);
arg1, arg2, arg_n = ˈ F F
55
56. F
ARGUMENT
Mr.Warawut
Khangkhan
b cursor 1
Chapter 2 Basic Programming Concepts
f F F
n F
r cursor F
t
’ ‘
” ”
xxx Ascii F 065 A 56
uxxx Unicode F u0008 F
58. F F PRINTF
Mr.Warawut
Khangkhan
Method printf( ) F
F Object out Class System
Chapter 2 Basic Programming Concepts
System.out.printf(Control_String, arg1 + arg2
+ … + arg_n);
Control_String = F ,
F
arg1, arg2, arg_n = ˈ F F 58
59. Mr.Warawut
Chapter 2 Basic Programming Concepts
Khangkhan
59
METHOD
exponential
F
F
F
F
PRINTF
%d
%u
%c
%e
%s
%f
60. Mr.Warawut
Chapter 2 Basic Programming Concepts
Khangkhan
60
F
F
F F F
F
F F
METHOD PRINTF
F
F
F
F
F
F
F
F
F ˈ
F
F
)
+
-
.(
F
F
62. F F
JOPTIONPANE
Mr.Warawut
Khangkhan
Class JOptionPane F
Graphic Mode Popup
Chapter 2 Basic Programming Concepts
Window F Dialogbox
MessageDialog F method
showMessageDialog( ) F F
F Class JOptionPane F F Object
Class JOptionPane F F import class
package javax.swing F project
62
63. F F
JOPTIONPANE
Mr.Warawut
Khangkhan
JOptionPane.showMessageDialog(
Parent_Window, Message, Title, Type);
Chapter 2 Basic Programming Concepts
Parent_Window = ˈ F F F
F ˈ
null Dialog box
F
Message = ˈ F Dialog box
Title = ˈ F F Title bar 63
64. F F
JOPTIONPANE
Mr.Warawut
Khangkhan
Type = ˈ Dialog box
F F
Chapter 2 Basic Programming Concepts
• ERROR_MESSAGE - F
• INFORMATION_MESSAGE -
F
• PLAIN_MESSAGE - F
F F
• QUESTION_MESSAGE -
• WARNING_MESSAGE - F 64
66. F
DECIMALFORMAT
Mr.Warawut
Khangkhan
ˈ
F F F F
Chapter 2 Basic Programming Concepts
Class DecimalFormat
F
import package java.text F
Project
66
67. F
DECIMALFORMAT
Mr.Warawut
Khangkhan
DecmialFormat df = new DecimalFormat(arg);
String str = df.format(payment);
Chapter 2 Basic Programming Concepts
arg = ˈ F F
• 0 0 F
• “#” F F0 ˈ 0
F
• “,” ˈ
df = ˈ object class F 67
str = ˈ F F F
69. F F
INPUTSTREAMREADER
Mr.Warawut
Khangkhan
Class InputStreamReader ˈ
F Class BufferedReader
Chapter 2 Basic Programming Concepts
F 1 F Method readLine( )
ˈ F F (String)
F import package java.io F
69
70. F
INPUTSTREAMREADER
Mr.Warawut
Khangkhan
InputStreamReader rd = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(rd);
Chapter 2 Basic Programming Concepts
or
BufferedReader stdin = new InputStreamReader(
new InputStreamReader(System.in));
reader = ˈ object class InputStreamReader
stdin = ˈ object class BufferedReader
70
72. F F SCANNER
Mr.Warawut
Khangkhan
F F ˈ F
F
Chapter 2 Basic Programming Concepts
F Scanner F F object class
Scanner F
import package java.util
72
73. SCANNER
Mr.Warawut
Khangkhan
Scanner sn = new Scanner(System.in);
Chapter 2 Basic Programming Concepts
sn = ˈ object F class Scanner
System.in = ˈ F F F
F F ˂ F
Method
•nextInt( ) F Integer
•nextFloat( ) F Float
•nextDouble( ) F Double 73
•nextLine( ) F String
75. F F
JOPTIONPANE
Mr.Warawut
Khangkhan
F Method showInputDialog( ) F
ˈ F F (String)
Chapter 2 Basic Programming Concepts
F JOptionPane F F object
class JOptionPane F
import package javax.swing
75
76. JOPTIONPANE
Mr.Warawut
Khangkhan
JOptionPane.showInputDialog(
Parent_Window, Message, Title, Type);
Chapter 2 Basic Programming Concepts
Parent_Window = ˈ F F F
F ˈ
null Dialog box
F
Message = ˈ F Dialog box
Title = ˈ F F Title bar 76
77. JOPTIONPANE
Mr.Warawut
Khangkhan
Type = ˈ Dialog box
F F
Chapter 2 Basic Programming Concepts
• ERROR_MESSAGE - F
• INFORMATION_MESSAGE -
F
• PLAIN_MESSAGE - F
F F
• QUESTION_MESSAGE -
• WARNING_MESSAGE - F 77