Primitive Data & Operation
-
1
Variable Identifier Naming Convention Constant Primitive Data Arithmetic Expression Type Conversion String Scanner class Library & Package in Java
Watcharin Puangplia : 16/ 16/11/ 11/53
$%&'() (Variable) ďœ‹ ďœ‹ ďœŠ ďœŠ ! "#$% & ďœŠ ďœŠ ' ďœ‹ (ďœ‹ ) '
" ďœ‹*' %! *ďœ‹ " ! &" + (Declared) 5( " &$6 *' %! % & ( ďœ‹ 7 *ďœ‹ " ďœ‹ int total = 200; //Declaring & Initializing int total; //Declaring total = 200; //Initializing Initializing " ! &" +*' %! E *' !ďœ’ ( ďœ‹ 7 ( "' (ďœ‹(ďœ‹ " ! &" + ďœŠ "' int a = 10, b = 20, c = 30,‌, z = 100; int a, b, c,‌,z; a = 10, b = 20, c = 30,‌, z = 100;
2
$%&'() (Variable) *' %! java %$ďœŠ " !ďœ’ 3 " UďœŠ Instance Variable (Non-Static Field) !ďœ’ *' %! 7" ďœ‹ " Class Variable (Static Field) !ďœ’ *' %! 7"" ( ďœ‹ ) !ďœ’ ďœŠ ! &" $ R *' %! Instance & ! ďœ‹ (ďœ‹ Local Variable !ďœ’ *' %! ' ! # 7ďœŠ (W (*ďœŠ E
*6 & ' "* ďœŠ Class Variable & ďœŠ static ďœ‹
3
! &" +*' %! ďœŠ *' %! Instance Variable & ďœŠ ďœŠ static ďœ‹ *' %! ! &" +*' %!
*' %! carModel % & color ( Class Variable : !ďœ’ *' %! MyCar)
$%&*+ďœŠ-. : $%&'() (Variable) public class MyCar { Static String carModel = “Jazzâ€?; String color = “Blackâ€?;
W ( main ďœ‹ *' %! door (Local Variable : *' %! 7ďœŠ) W ()
public static void main(String[] args) { int door = 3; ďœ‹ *' %! Honda " Mycar MyCar Honda = new Mycar(); Honda.color = “Whiteâ€?;
4
}
}
(Instance Variable : *' %! 7" ďœ‹ " class)
System.out.print (“Color of my new car is� + Honda.color); System.out.print(“, it has� + door + “ doors.�);
/-)()0/-1$%&'() (Variable Declaration) < ( ď&#x153;&#x2039; 7 > < *' %! > = < ď&#x153;&#x160; *' %! > ; $%&*+ď&#x153;&#x160;-.
5
Position= ď&#x153;&#x2039; ď&#x153;&#x2039; $ " 7" ď&#x153;&#x2039;
Hobby= (7 ' , tď&#x153;? , ď&#x153;&#x160; " *ď&#x153;&#x2039; 7 = 170 R . '"= 65 "".
public class VarDeclare { public static void main(String[] args) { String position, hobby; int height = 170, weight = 65; position = â&#x20AC;&#x153; ď&#x153;&#x2039; ď&#x153;&#x2039; $ " 7" ď&#x153;&#x2039; â&#x20AC;?; hobby = â&#x20AC;&#x153;(7 ' , tď&#x153;? , ď&#x153;&#x160; " * ď&#x153;&#x17D;â&#x20AC;?; System.out.println(â&#x20AC;&#x153;Position= â&#x20AC;? + position); System.out.println(â&#x20AC;&#x153;Hobby= â&#x20AC;? + hobby); System.out.println(â&#x20AC;&#x153; 7 = â&#x20AC;? + height + â&#x20AC;&#x153; R .â&#x20AC;?); System.out.println(â&#x20AC;&#x153; '"= â&#x20AC;? + weight + â&#x20AC;&#x153; "".â&#x20AC;?); } }
Identifiers w7ď&#x153;&#x2039; 5! %" *' ď&#x153;&#x2039; 5! %" ď&#x153;&#x160; Class, *' %! , Method !ď&#x153;&#x2019; *ď&#x153;&#x2039; *' ď&#x153;&#x160; Identifier int total; double salary; Variable Name char grade; String personName; public void setName (String name) Method Name Public String getName ()
6
/-)34ď&#x153;&#x2039; Identifier 36 Java
7
Identifier E ! &" $ (ď&#x153;&#x2039; *' '"x *' $ (Dollar-sign) _ (Underscore) ď&#x153;&#x2039; *ď&#x153;&#x2039; (ď&#x153;&#x2039; *' 'U '"x|ď&#x153;&#x17D; ( ď&#x153;&#x2039; Reserved words ď&#x153;&#x2039; ď&#x153;&#x2039; ď&#x153;&#x2039; ď&#x153;&#x160; true true, false false, null ď&#x153;&#x2039; ď&#x153;&#x2039; ď&#x153;&#x160; ď&#x153;&#x160; | Case sensitive (*' ď&#x153;&#x17D; #"*' ď&#x153;&#x17D; Uď&#x153;&#x160; %*"*ď&#x153;&#x160; "' ) *' (ď&#x153;&#x2039; ď&#x153;&#x160; "'(
ThisIsOk, x15, _myName, $java this-is-not-ok, 10person, a b c, my/name
$%&*+-./-)34 Identifier 36 Java indentifier
8
MyVariable _MyVariable $data Sum_Score …
indentifier
My Variable 9Pi @net System
Naming Convention
9
8ď&#x153;&#x2039;*$/9.36/-)$%:.4;<* Class Class !ď&#x153;&#x2019; *' % " ď&#x153;&#x2039; *ď&#x153;&#x2039; (ď&#x153;&#x2039; '"x *' Uď&#x153;&#x160; " ' * (ď&#x153;&#x2039; '"x *' #" ď&#x153;&#x160; Student, Employee, Person, Sample ! &" $ " ď&#x153;&#x2039; ď&#x153;&#x2039; '"x *' Uď&#x153;&#x160; !ď&#x153;&#x2019; *' *ď&#x153;&#x2039; %*ď&#x153;&#x160; & ď&#x153;&#x160; StudentScore, PersonJob Class "'$ t ď&#x153;&#x17D; *ď&#x153;&#x2039; " $' "*ď&#x153;&#x2039; !ď&#x153;&#x2019; ( "'
Naming Convention
10
8ď&#x153;&#x2039;*$/9.36/-)$%:.4;<* Variable & Instance Variable & Instance (Object) !ď&#x153;&#x2019; *' % " ď&#x153;&#x2039; *ď&#x153;&#x2039; (ď&#x153;&#x2039; '"x *' #" ď&#x153;&#x160; student, employee, person ! &" $ " ď&#x153;&#x2039; ď&#x153;&#x2039; '"x *' Uď&#x153;&#x160; !ď&#x153;&#x2019; *' *ď&#x153;&#x2039; %*ď&#x153;&#x160; & ( " ď&#x153;&#x2039; % ") ď&#x153;&#x160; studentScore, personJob
Naming Convention
8ď&#x153;&#x2039;*$/9.36/-)$%:.4;<* Method Method !ď&#x153;&#x2019; " *' % " ď&#x153;&#x2039; *ď&#x153;&#x2039; (ď&#x153;&#x2039; '"x *' #" ! &" $ " ď&#x153;&#x2039; ď&#x153;&#x2039; '"x *' Uď&#x153;&#x160; !ď&#x153;&#x2019; *' *ď&#x153;&#x2039; %*ď&#x153;&#x160; & ( " ď&#x153;&#x2039; % ")
*' ď&#x153;&#x160; " *' Method
11
setName (), calSalary (), printList (), getName (), toString (), showDetails
=ď&#x153;&#x160;-=.>?< (Constant)
12
!ď&#x153;&#x2019; Identifier ď&#x153;&#x2039; "'$*' %! ' E ! *ď&#x153;&#x160; "' * ď&#x153;&#x160; " ( ď&#x153;&#x2039; ' & !ď&#x153;&#x2019; ď&#x153;&#x160; ď&#x153;&#x160; ! %! (ď&#x153;&#x2039; " 5! %" ' ď&#x153;&#x2039; ! %! ď&#x153;&#x160; & ď&#x153;&#x160; " Compile 5! %" & " ( Error (Compile-time error) ď&#x153;&#x2039; Modifier â&#x20AC;&#x2DC;finalâ&#x20AC;&#x2122; " ! &" + ď&#x153;&#x2039; ď&#x153;&#x160; !ď&#x153;&#x2019; ď&#x153;&#x160; (Constant) ď&#x153;&#x2039; ď&#x153;&#x2039; '"x *' Uď&#x153;&#x160; ' (% & ď&#x153;&#x2039; _ (Underscore) !ď&#x153;&#x2019; *' ' & ď&#x153;&#x160; ď&#x153;&#x160; - final double START_SAL = 10000; - final int MAX_SCORE = 100;
Keyword @);* =A->?B < .&6 (Reserved Word) Keyword +x compiler ) x & ď&#x153;&#x2039; % & ' &*ď&#x153;&#x2039; ( " ' $ keyword %*ď&#x153;&#x160; &*'
13
B%E9%/FG'+/=A- (separator) B%E9%/FG'+/=A-
14
@6->?<
; () , .
JK;<*)0LM/-)BN:6BMO8*.=A-B%<.$-.P Q-+36Q-F-R-&-
{}
JK;<*)0LML9T*/=A-B%<.8*.Q-F-R-&- =A-B%<.$-.P R0*+U 36L9T*/ J46 =A-B%<.Q-+36 class, method @);* 4MO=A-B%<.W=).B)-.=&L=MX$-.P J46 if, while, for J(6$6
BA-@)%L$*>-+ method @);*=A-B%<.*;<6P 36Q-F-R-&BA-@)%L'+/$%&'()@);*=A-B%<.36Q-F-R-&JK;<*3436/-))0LM attribute @);* method 8*. object @);*3436/-))0LM package
4ď&#x153;&#x160;*.&ď&#x153;&#x160;-. (whitespace) 5! %" ) x & ď&#x153;&#x160; ď&#x153;&#x160; % " ! &5 ' *ď&#x153;&#x160; E ) 5! %" (ď&#x153;&#x2039; 5( ď&#x153;&#x160; ď&#x153;&#x160; & ( ď&#x153;&#x160; "# (ď&#x153;&#x2039; ' compiler ) x & ď&#x153;&#x160; ď&#x153;&#x160; ď&#x153;&#x160; " ď&#x153;&#x2039; "'$ ( 5! %" bytecode ď&#x153;&#x160; ď&#x153;&#x160; & ď&#x153;&#x160; ď&#x153;&#x2039; 7!%$$ sourcecode (7 ď&#x153;&#x160; 7!%$$ ď&#x153;&#x160; ď&#x153;&#x160;
! &" $(ď&#x153;&#x2039; â&#x20AC;&#x201C; ď&#x153;&#x160; ď&#x153;&#x160;
â&#x20AC;&#x201C; % #! â&#x20AC;&#x201C; " $ '( ď&#x153;&#x160;
15
46NO8ď&#x153;&#x2039;*XU9K;6 : Z-6 (Primitive Data) ď&#x153;&#x2039; 7 Â&#x2021; ) x Java ' ( 8 ( ('
16
*# (Integer) : 4 ( byte, short, int, long + (Floating Point Number) : 2 ( float, double ď&#x153;&#x160; * "& (Boolean) : 1 ( true, false ď&#x153;&#x160; '" & ( (Character) : 1 ( char
46NO8ď&#x153;&#x2039;*XU9K;6 : Z-6 (Primitive Data) 8 2
6 4
17
2
46NO8*XU9K;6 : Z-6 (Primitive Data)
18
46NO 8*XU9
RA-6&6LN$
=Default
byte
8
0
-27 (-128) ef. 27-1 (127)
short
16
0
-215 (-32768) ef. 215-1 (32767)
int
32
0
-231 (-2147483468) ef. 231-1 (2147483467)
long
64
0
-263 (J989LJ+*0X-/) ef. 263-1 (J98L&/J+*0X-/)
float
32
0.0
J/TLJ98>16N+X'90J98+//A-9%.
double
64
0.0
J/TLJ98>16N+X'90J98+//A-9%._O90J*?+O/&- float
boolean
2
false
char
16
\u0000
4&.8*.=->?<J/TL_O
true @);* false 34J/TL*%/8)0>?<X?)@%B$%:.'$ 0 ef. 65535
46NO8*XU9K;6 : Z-6 : Numeric
%*"* & 7 %* & ( ( 7 " " ( 7 * " & "'$
7 %* & ( ( 7 ! int "#$ ( 7 6( = 2,147,483,647 * " "#$ $'* ! & *' ! & R 13 * %
* " ( 7 ! long " "#$ ( 7 6( = 9,223,372,036,854,775,807 7 *# ! ( long & *' '"x l L * 3l, 089L, 0xBCL
19
46NO8ď&#x153;&#x2039;*XU9K;6 : Z-6 : Character
!ď&#x153;&#x2019; *' %! "#$ ď&#x153;&#x160; '" & " Unicode Character Set 5( '"x Unicode ' " 0 â&#x20AC;&#x201C; 127 !ď&#x153;&#x2019; '"x 6( ( "'$*' '"x ASCII char ď&#x153;&#x160; ď&#x153;&#x160; String (*' %! '"x|& !ď&#x153;&#x2019; '" &) %*ď&#x153;&#x160; "' 7ď&#x153;&#x160; String (ď&#x153;&#x2039; ď&#x153;&#x160; char *ď&#x153;&#x2039; 7ď&#x153;&#x160; Single quotes (â&#x20AC;&#x2DC;)
char a = 33 ---> â&#x20AC;&#x2DC;!â&#x20AC;&#x2122;, char b = 65 ---> â&#x20AC;&#x2DC;Aâ&#x20AC;&#x2122; char : â&#x20AC;&#x2DC;câ&#x20AC;&#x2122; â&#x20AC;&#x2DC;aâ&#x20AC;&#x2122; â&#x20AC;&#x2DC;râ&#x20AC;&#x2122; String : â&#x20AC;&#x153;carâ&#x20AC;? ---> â&#x20AC;&#x2DC;câ&#x20AC;&#x2122; + â&#x20AC;&#x2DC;aâ&#x20AC;&#x2122; + â&#x20AC;&#x2DC;râ&#x20AC;&#x2122; int x = 1 % & char y = â&#x20AC;&#x2DC;1â&#x20AC;&#x2122; ---> x % & y ď&#x153;&#x160; %*"*ď&#x153;&#x160; "'
20
46NO8ď&#x153;&#x2039;*XU9K;6 : Z-6 : Character Character Java ' '" &*ď&#x153;&#x160; E ď&#x153;&#x160; (ď&#x153;&#x2039;
21
46NO8ď&#x153;&#x2039;*XU9K;6 : Z-6 : Character ASCII Character Set !ď&#x153;&#x2019; subset *' '"x Unicode ' " \u0000 (0) to \u007f (127)
22
46NO8ď&#x153;&#x2039;*XU9K;6 : Z-6 : Character
23
!
46NO8ď&#x153;&#x2039;*XU9K;6 : Z-6 : Boolean
24
Java ď&#x153;&#x160; ď&#x153;&#x2039; *# % ď&#x153;&#x160; boolean (%*"*ď&#x153;&#x160; ") x C % & C++) boolean ď&#x153;&#x160; (ď&#x153;&#x2039; 2 ď&#x153;&#x160; ď&#x153;&#x160; ' literal ( ď&#x153;&#x2039; % ( ď&#x153;&#x160; ) !ď&#x153;&#x2019; true % & false boolean x = false; //OK boolean y = 1; //compile-time error boolean check = true;
Arithmetic Expression /-)=A-G&6>-.=GN$1-B$)ď&#x153;&#x17D;
25
Expression ! &" $(ď&#x153;&#x2039; (Operator) % &*' 7"" & (Operand) (Operator) ! &" $(ď&#x153;&#x2039; + , - , * , / , % $ ) x ď&#x153;&#x160; C % & C++ ' ď&#x153;&#x2039; % (ď&#x153;&#x2039; Â&#x2018; & "'$ *# ď&#x153;&#x160; ' ( ď&#x153;&#x2039; 7 w ' Wď&#x153;&#x17D; (ď&#x153;&#x2039; "" | & "'$ ( ď&#x153;&#x2039; 7 Operand (' *# & *# Result *# + & + Result + *# & + Result +
Arithmetic Expression /-)=A-G&6>-.=GN$1-B$)ď&#x153;&#x17D; $%&OA-J6N6/-) + * / % -++
26
=&-X@X-+ " $ " $ " " 7| " " & Â&#x2018; & +x ď&#x153;&#x2039; " ( ď&#x153;&#x160; ' & 1 " ď&#x153;&#x160; ' &1
$%&*+ď&#x153;&#x160;-. Xâ&#x20AC;&#x201C;Y X+Y X*Y X/Y 11%3 = 3 +x 2 (' ' 2 !ď&#x153;&#x2019; w ' Wď&#x153;&#x17D; X- - - -X "'$ X=X-1 X+ + + +X "'$ X=X+1
27
class IntAndDouble { public static void main (String args[]) { int i = 10; double x = 2.5; double k; /-)=A-G&6>-.=GN$1-B$)ď&#x153;&#x17D; System.out.println("i is " + i); System.out.println("x 8 is " + x); k = i + x; 8.0 System.out.println("i + 15x is " + k); k = i * x; System.out.println("i *15.0 x is " + k); i is 10 k = i - x; 0 x is 2.5 System.out.println("i -0.6 x is " + k); i + x is 12.5 k = x - i; i * x is 25 System.out.println("x 0.6 - i is " + k); i - x is 7.5 k = i / x; 1 x - i is -7.5 System.out.println("i /1.5 x is " + k); i / x is 4 k = x / i; x / i is 0.25 System.out.println("x 3 / i is " + k); } }
Arithmetic Expression /-)=A-G&6>-.=GN$1-B$)ď&#x153;&#x17D;
28
3+5 3 + 5.0 3*5 3 * 5.0 3/5 3 / 5.0 3.0 / 5 10 % 3 10.5 % 3 10 % 3.5
= = = = = = = = = =
8 8 15 15 0.6 0.6 0.6 1 1 1
1/2*3.5 = 0.00 0.0 3.5 * 1 / 2 = 1.75 1.0 /2 * 3.5 = 1.75
Shorthand Assignment Operators
Java Shortcut Assignment Operators 5 *' ‘+=’ , ‘-=’ , ‘*=’ , ‘/=’ , ‘%=’ i i i i i
29
+= 8 -= 8.0 *= 8 /= 8 %= 8
---> ---> ---> ---> --->
i i i i i
= = = = =
i i i i i
+8 - 8.0 *8 /8 %8
Increment and Decrement Operators
Java Increment and Decrement Operators 4 %$$ â&#x20AC;&#x2DC;++varâ&#x20AC;&#x2122; , â&#x20AC;&#x2DC;var++â&#x20AC;&#x2122; , â&#x20AC;&#x2DC;--varâ&#x20AC;&#x2122; , â&#x20AC;&#x2DC;var--â&#x20AC;&#x2122; ++var, --var / ( ď&#x153;&#x160; *' %! "ď&#x153;&#x160; " ' ď&#x153;&#x160; assisgn ď&#x153;&#x160; ď&#x153;&#x160; ď&#x153;&#x160; ď&#x153;&#x2039; var++, var-assign ď&#x153;&#x160; ď&#x153;&#x160; ď&#x153;&#x160; 6( *' %! ď&#x153;&#x2039;"ď&#x153;&#x160; " '
ď&#x153;&#x160; / ( ď&#x153;&#x160; *' %!
30
Increment and Decrement Operators
int a = 0, b = 0, x = 1, y = 1;
System.out.print(a System.out.print(a System.out.print(a a = x++; System.out.print(a b = ++y; System.out.print(a
31
+ “:” + b + “:” + x++ + “:” + ++y); + “:” + b + “:” + ++x + “:” + y); + “:” + b + “:” + x + “:” + y++); + “:” + b + “:” + x + “:” + y); + “:” + b + “:” + x + “:” + y)
int i = 10; int new_i = 5 * (i++); 1. new_i = 5 * i; 2. i = i + 1;
int i = 10; int new_i = 5 * (++i) 1. i = i + 1; 2. new_i = 5 * i;
Operator Precedence 9A-O%L/-)>A-.-68*.J=);* < .@X-+$ď&#x153;&#x160;-.P 36 Expression 1. 2. 3. 4. 5.
() ++ , -*, /, % +, =
'$ 7ď&#x153;&#x160; ('$ ( "' & | " "Rď&#x153;&#x2039; !
32
Operator Precedence 9A-O%L/-)>A-.-68*.J=);* < .@X-+$ď&#x153;&#x160;-.P 36 Expression
int x = 2;
33
8
/ 2
4
* 3
x++ 1
8
+ 4
10
/ 2
2
% 3
48
/ 5
(6
* 4
(7
+ 3
(8
2
x++ ) * 1
x 3
4 2
5
(--x) 1
((x++) 1 2
5)))
8 2 18
Assignment Operator (=)
" 7ď&#x153;&#x160; ('$ ď&#x153;&#x2039; 6( $"'$ E
x
= 4
sum / 4 + min * total 1
3 =A-G&6
/A-@6O=ď&#x153;&#x160;-3@ď&#x153;&#x2039;$%&'()
34
2
Type Conversion /-)/A-@6O=-)0@&-.$%&'()>?<X8 ? *XU9$-.46NO/%6
35
Automatic Type Conversions Casting
Type Conversion 1. Automatic Type Conversions double bigType; byte smallType = 120; bigType = smallType; Automatic Type Conversions Data type *' %! ' 2 &*ď&#x153;&#x2039; ď&#x153;&#x2039; "' (ď&#x153;&#x2039; (Compatible) 5( " 6ď&#x153;&#x160; ď&#x153;&#x2039; 7 ( ď&#x153;&#x2039; "' (ď&#x153;&#x2039; ( ď&#x153;&#x2039; 7 *' [byte, short, int, long, float, double] Data type *' %! Â&#x201D;ď&#x153;?ď&#x153;&#x201C; Rď&#x153;&#x2039; (left hand-side) &*ď&#x153;&#x2039; ď&#x153;&#x160; ( $ *) "" ď&#x153;&#x160; Data type *' %! Â&#x201D;ď&#x153;?ď&#x153;&#x201C;
(right hand-side) " Type Conversion '"x|& ď&#x153;&#x160; widening conversion R & ď&#x153;&#x160; ď&#x153;&#x2039; 7 7U & ď&#x153;&#x160; " ! %! ď&#x153;&#x160;
36
Type Conversion
37
1. Automatic Type Conversions char smallType = ‘A’; int bigType; bigType = smallType; Result ---> bigType = 65 int smallType = 10000; double bigType; bigType = (double) smallType; bigType = smallType; Result ---> bigType = 10000.0
X?=-J>-/%6
Type Conversion 2. Casting
38
!ď&#x153;&#x2019; " ! ď&#x153;&#x160; Data type ! !ď&#x153;&#x2019; ď&#x153;&#x160; Data type * ď&#x153;&#x2039; " ď&#x153;&#x160; ď&#x153;&#x160; Data type ' & Uď&#x153;&#x160; #"" ď&#x153;&#x160; " ď&#x153;&#x2039; Data type : boolean ď&#x153;&#x160; ! %! !ď&#x153;&#x2019; Data type (ď&#x153;&#x2039; " Type Conversion '"x|& ď&#x153;&#x160; narrow conversion " '"x|& ď&#x153;&#x2039; 7 $ ď&#x153;&#x160; 7U & ď&#x153;&#x160; " ! %! ď&#x153;&#x160; R " ( (ď&#x153;&#x2039; 2 " | - ! " + !ď&#x153;&#x2019; *# & ď&#x153;&#x2039; 6( + ! - ! " *# Uď&#x153;&#x160;" ď&#x153;&#x160; !ď&#x153;&#x2019; *# #"" ď&#x153;&#x160; & (ď&#x153;&#x2039;w ' Wď&#x153;&#x17D; !ď&#x153;&#x2019; ď&#x153;&#x160; 7" modulo (ď&#x153;&#x2039; ( ď&#x153;&#x2039; 7 '$ ď&#x153;&#x160;
Type Conversion 2. Casting
39
Syntax : (<type>) <term> byte smallType; int bigType = 257; smallType = bigType ; //compile-time error : Data type int U" byte smallType = (byte) bigType ; //OK : narrow conversion Result ---> smallType = 1 ( Data type : byte 8 $ * (256 ) Result ---> 257 % 256 = 1
8ď&#x153;&#x2039;*=&))0&%.J/?<+&/%L/-) cast 2. Casting
40
" cast ď&#x153;&#x2039; 7 " + !ď&#x153;&#x2019; *# (*' ' 6( + & !) " cast ď&#x153;&#x2039; 7 " ď&#x153;&#x2039; 7 ( Uď&#x153;&#x160; ( "#$ Uď&#x153;&#x160;" ď&#x153;&#x160; ) ! !ď&#x153;&#x2019; ď&#x153;&#x2039; 7 #"" ď&#x153;&#x160; ( "#$ #"" ď&#x153;&#x160; ) % ď&#x153;&#x2039; ď&#x153;&#x160; & (ď&#x153;&#x2039;%*ď&#x153;&#x160; & ď&#x153;&#x2039;w ' Wď&#x153;&#x17D; w ( !
$%&*+-. : cast
41
(int) (fX + fY) = 4 (int) fX + (int) fY = 3 (int) (b1 + b2) = 128 (b1 + b2) = -1
public class castSample{ public static void main(String[] args){ float fX = 1.3F, fY = 2.7F; 127 int iX; 1 iX = (int) (fX + fY); System.out.println(“(int) (fX + fY) = ”+iX); -1 iX = (int) fX + (int) fY; System.out.println(“(int) fX + (int) fY = ”+iX); byte b1 = 127, b2 =1; iX = (int) (b1 + b2); System.out.println(“(int) (b1 + b2) = ”+iX); b2 = (byte) (b1 + 1); System.out.println(“(b1 + b2) = ”+(b1+b2)); } }
1.3 + 2.7 1+2 127 + 1
01111111 00000001 10000000
String
42
Char type ď&#x153;&#x2039;% *' '"x (ď&#x153;&#x2039; *' ( ď&#x153;&#x160; ' ď&#x153;&#x160; â&#x20AC;&#x2DC;Hâ&#x20AC;&#x2122; ď&#x153;&#x2039; *ď&#x153;&#x2039; " ď&#x153;&#x2039; '" & ď&#x153;&#x2039; ) x Java & ď&#x153;&#x2039; Data type " ď&#x153;&#x160; String ď&#x153;&#x160; String msg = â&#x20AC;&#x153;Helloâ&#x20AC;?; String !ď&#x153;&#x2019; class " ( ď&#x153;&#x2039;5( Java Library ď&#x153;&#x160; ( "'$ class E ď&#x153;&#x160; Integer, System, Random !ď&#x153;&#x2019; *ď&#x153;&#x2039; String ď&#x153;&#x160; ď&#x153;&#x160; ď&#x153;&#x2039; 7 ( Â&#x2021; (Primitive data type) ) x Java %*ď&#x153;&#x160; & " ď&#x153;&#x160; !ď&#x153;&#x2019; Reference type (Object) ď&#x153;&#x2039; & ď&#x153;&#x2039; !ď&#x153;&#x2019; String *ď&#x153;&#x2039; 7ď&#x153;&#x160; Double quotes â&#x20AC;&#x153;â&#x20AC;? " ď&#x153;&#x2039; !ď&#x153;&#x2019; String "'$ ď&#x153;&#x2039; 7 ( E (ď&#x153;&#x2039;5( ď&#x153;&#x2039; â&#x20AC;&#x2DC;+â&#x20AC;&#x2122; ď&#x153;&#x160; String + String Result String String + Number Result String
String
43
System.out.println (“ABC” + “xyz”); System.out.println ("ABC" + 222); System.out.println (111 + "xyz"); System.out.println (111 + 222); System.out.println ("111" + "222"); System.out.println ("111 plus 222 is " + 111 plus 222 is 111222 System.out.println ("111 plus 222 is " + 111 plus 222 is 333
ABCxyz ABC222 111xyz 333 111222 111 + 222); (111 + 222));
Getting input from the console With Scanner class
) x Java & ď&#x153;&#x2039; System.out !ď&#x153;&#x2019; Standard output ď&#x153;&#x160; Standard input ' & ď&#x153;&#x2039; System.in & ď&#x153;&#x2039; class Scanner ď&#x153;&#x2039; Object '$ ď&#x153;&#x160; ď&#x153;&#x160; input " System.in ( !ď&#x153;&#x2019; ď&#x153;&#x160; * (*ď&#x153;&#x160; "'$w7ď&#x153;&#x2039; ď&#x153;&#x2039;wď&#x153;&#x160; console) *' ď&#x153;&#x160; " ď&#x153;&#x2039; Object " class Scanner Scanner <reference name> = new Scanner (System.in); Scanner in = new Scanner (System.in);
44
Scannerâ&#x20AC;&#x2122;s method BA-@)%L*ď&#x153;&#x160;-6=ď&#x153;&#x160;-8*.8ď&#x153;&#x2039;*XU946NO$ď&#x153;&#x160;-.P pď&#x153;&#x160;-6>-. console
45
next(), nextLine() nextByte() nextShort() nextInt() nextLong() nextFloat() nextDouble()
'$ ď&#x153;&#x160; '$ ď&#x153;&#x160; '$ ď&#x153;&#x160; '$ ď&#x153;&#x160; '$ ď&#x153;&#x160; '$ ď&#x153;&#x160; '$ ď&#x153;&#x160;
input input input input input input input
!ď&#x153;&#x2019; !ď&#x153;&#x2019; !ď&#x153;&#x2019; !ď&#x153;&#x2019; !ď&#x153;&#x2019; !ď&#x153;&#x2019; !ď&#x153;&#x2019;
String byte short int long float double
Scanner’s method $%&*+-./-)*-6=-8*.8*XU946NO String
System.out.print(“Enter your name: ”); Scanner in = new Scanner(System.in); String name = in.nextLine(); System.out.print(“Your name is : ” + name); 1
2
3
46
Class Libraries in Java
47
Class Library !ď&#x153;&#x2019; " 6 ď&#x153;&#x160; Classes *ď&#x153;&#x160; E Java '( * ď&#x153;&#x2039; ď&#x153;&#x2039; ď&#x153;&#x2039; " 'Â&#x2013; 5! %" Java standard class library ď&#x153;&#x160; !ď&#x153;&#x2019; ď&#x153;&#x160; Java development environment
Class Libraries in Java Library
Packages
Classes
48
Packages in Java *' ď&#x153;&#x160; Package Standard class library
java.lang java.applet java.awt
javax.swing
49
java.net java.util javax.xml.parsers
General support Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities and components Network communication Utilities XML document processing
Packages in Java The import declaration
50
Class 6" class 7ď&#x153;&#x160; java.lang Package 7" import ď&#x153;&#x2039; 5! %" 5( '*5 '* 7ď&#x153;&#x160;% ď&#x153;&#x2039; (' ' ď&#x153;&#x160;*ď&#x153;&#x2039; ! &" + " import " ď&#x153;&#x160; System, String " import class ď&#x153;&#x2039; ď&#x153;&#x2039; 5! %" &*ď&#x153;&#x2039; 7ď&#x153;&#x160; ' " Package ( ď&#x153;&#x2039; ) % & 7ď&#x153;&#x160;"ď&#x153;&#x160; ! &5 E 5! %" " ď&#x153;&#x2039; Comment
Packages in Java The import declaration
51
*ď&#x153;&#x2039; " ď&#x153;&#x2039; class *ď&#x153;&#x160; E Package (E & W " ď&#x153;&#x2039;
2 7!%$$ (' ! &" + ď&#x153;&#x2039;5( ď&#x153;&#x160; *# ď&#x153;&#x160; java.util.Scanner in = new java.util.Scanner(System.in); import class ď&#x153;&#x2039; ď&#x153;&#x2039; 5! %" " ' ! &" + ď&#x153;&#x2039;5( ď&#x153;&#x160; Â&#x2018; & Class ď&#x153;&#x160; import java.util.Scanner; Scanner in = new Scanner(System.in); " import class E class Package ( "' (ď&#x153;&#x2039;5( ď&#x153;&#x2039;*' '"x wildcard (*) ď&#x153;&#x160; import java.util.*;
Lab Assignment Problem 1 : PersonRecord.java 5! %" *ď&#x153;&#x2039; "#$% & ď&#x153;&#x160; ď&#x153;&#x160; ď&#x153;&#x2039; 7 *ď&#x153;&#x160; E (ď&#x153;&#x2039; (' - ď&#x153;&#x2039; - - "6 ' "5! %" # *ď&#x153;&#x2039; % ( & ( ď&#x153;&#x2039; 7 (ď&#x153;&#x2039; 1.
User "#$
Title name First name Last name
: Mr. : Chavanit : Wongjunya
Person name
: Mr.Chavanit Wongjunya
Lab Assignment Problem 2 : CircleCal.java 5! %" *ď&#x153;&#x2039; | % & ď&#x153;&#x2039; $ " (ď&#x153;&#x2039; 5( | " ď&#x153;&#x160; '+ (Radius) " w7ď&#x153;&#x2039; ď&#x153;&#x2039;" ( 2. ' "5! %" | # *ď&#x153;&#x2039; % ( w ' Wď&#x153;&#x17D;
% & ď&#x153;&#x2039; $ | (ď&#x153;&#x2039; 4. Âś = 3.14 ď&#x153;&#x2039; ď&#x153;&#x160; " Class Math (ď&#x153;&#x2039;(' Math.PI 5. 7* " | = Âś r2 6. 7* " | ď&#x153;&#x2039; $ = 2 Âś r * r '+ " 1.
Lab Assignment CircleCal.java : Ex User "#$
Radius : 54 Area of circle with radius 54 : 9160.88 Perimeter of circle with radius 54 : 339.29
Lab Assignment Problem 3 : SpeedCar.java 5! %" *ď&#x153;&#x2039; | # *ď&#x153;&#x17D; (ď&#x153;&#x2039; 5( | " & & % & w 7ď&#x153;&#x2039; ď&#x153;&#x2039;" ( 5( - & & ď&#x153;&#x160; !ď&#x153;&#x2019; " . - ď&#x153;&#x160; !ď&#x153;&#x2019; 3. ' "5! %" | # *ď&#x153;&#x2039; % ( w # | (ď&#x153;&#x2039; 5( # (' " ď&#x153;&#x160; ď&#x153;&#x160; !ď&#x153;&#x2019; " ./ . 1.
4.
# (" ./ .) = & & (" .) ( .)
Lab Assignment SpeedCar.java : Ex User "#$
Distance (Km.) : 24 Time (Minutes) : 20 Speed of car with distance 24 Km and time 20 minutes : 72 Km./Hour
Lab Assignment Problem 4 : SalaryPlan.java 5! %" *ď&#x153;&#x2039; | ( ' " '" ď&#x153;&#x160; *ď&#x153;&#x160; E * w7ď&#x153;&#x2039; ď&#x153;&#x2039;" ( (ď&#x153;&#x2039; 5( - ( ď&#x153;&#x160; !ď&#x153;&#x2019; $ / ( - '( ď&#x153;&#x160; ď&#x153;&#x160; !ď&#x153;&#x2019; ď&#x153;&#x2039; & (%) ( 2. ď&#x153;&#x160; *ď&#x153;&#x160; E '" (' - ! &"' ' : 7% ( - $" 6 : 5% ( - ' ď&#x153;&#x17D; : 20% ( ' "5! %" | # *ď&#x153;&#x2039; % ( & ( ( % & '"%*ď&#x153;&#x160; &! & ) (ď&#x153;&#x2039; 1.
Lab Assignment SalaryPlan.java : Ex User "#$
Salary (Baht) Social security (%) Providence fund (%) Saving rate (%)
: : : :
10000 7 5 40
Total salary Total of expenses Social security 7% Providence fund 5% Saving 40% Balance of salary
: : : : : :
10000 Baht 5200 Baht 700 Baht 500 Baht 4000 Baht 4800 Baht
Lab Assignment Problem 5 : Cashier.java 5! %" *ď&#x153;&#x2039; % " ď&#x153;&#x160; " w7ď&#x153;&#x2039; ď&#x153;&#x2039; " ( 5( % " " !ď&#x153;&#x2019; - W $'* 1000 $ , 500 $ , 100 $ , 50 $ , 20 $ - U 10 $ , 5 $ , 1 $ 1.
' "5! %" | # *ď&#x153;&#x2039; % ( & ( W $'* / U (ď&#x153;&#x2039; "" % " ď&#x153;&#x160; (ď&#x153;&#x2039;
Lab Assignment Cashier.java : Ex User "#$
Cash (Baht)
: 4887
Total cash Bank 1000 Bank 500 Bank 100 Bank 50 Bank 20 Coin 10 Coin 5 Coin 1
: : : : : : : : :
4887 4 Unit 1 Unit 3 Unit 1 Unit 1 Unit 1 Unit 1 Unit 2 Unit