Professional Documents
Culture Documents
CHAPTER
02
0
0
0
0L
0.0F
0.0D
true false
1.2 (Reference Data Types) 1 ( )
1) (String)
0
String
null
15
16
1.3 ()
byte
short
int
long
float
double
2.
1) ( )
(1) '$' (Dollar Sign)
'_' (Underscore) num, $_$, _hey, $1, xxx3, test1_$
(2) 3girl1man, 2you, 5hundredMile
(3) 1
(Reserved Words / Keywords) 53
(4)
abstract
catch
do
final
implements
native
public
switch
true
assert
char
double
finally
import
new
return
synchronized
try
boolean
class
else
float
instanceof
null
short
this
void
break
const *
enum
for
int
package
static
throw
volatile
byte
continue
extends
goto *
interface
private
strictfp
throws
while
case
default
false
if
long
protected
super
transient
(5)
(
)
(6) ( )
(7) (Case-Sensitive) var Var
2) ( )
(1) () /
(2) ( Editor )
(3)
stdID, firstName, birthDate
(4)
hostName, numberOfCourse, currentTime, firstDayOfMonth
PI, MONTH_OF_YEAR
(5)
'$' '_'
(6)
3.
1)
< > <>;
int x;
double num;
String lastName;
boolean check;
2553 ( 7 2/2553) ()
2)
(Constant)
true;
a = b;
float y = 2.9f;
Comma
4.
1)
byte short
byte x = 12; short y = 199;
int ()
int dec = 26;
int ()
0 () int oct = 032;
int ()
0x 0X int hex = 0x1A;
long
l L long n = 26L;
2)
float
double
double
double
f F float f = 1.3F;
( ) double d = 1.3;
()
d D double d = 1.3D;
( a x 10b) a e
E b ()
8
double s = 93.478e8; 93.478 x 10
5
double t = 631E-5; 631 x 10
200
double u = 2.0e+200; 2.0 x 10
3)
char (
)
Single Quote char c =
'a';
2553 ( 7 2/2553) ()
17
18
char ( Unicode)
(15 )
var1 = 014;
1.
var2 = 014f;
2.
var3 = 'C';
3. int
2553 ( 7 2/2553) ()
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
var4 = 0E-0;
double
var5 = 0x1D;
var6 = 1.0e2.0;
var7 = .1000;
var8 = 08;
var9 = null;
var10 = 100E012;
var11 = 1f + "";
var12 = 1e1 + "";
var13 = 0d;
var14 = 3.f;
var15 = 0xbeef;
Scanner
1) Scanner Scanner Scanner
import java.util.Scanner;
1
2
3
4
import java.util.Scanner;
public class Test {
...
}
2)
Scanner <> = new Scanner(System.in);
1
2
3
4
5
6
7
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
...
}
kb
}
System.in
(keyboard)
2553 ( 7 2/2553) ()
19
20
3)
1) int nextInt() nextInt
( )
nextInt()
kb s1)
6) String ( \t \n)
next()
String <> = <>.next();
String s2 = kb.next();
( String kb s2)
3 [ ]
(15 )
Scanner kb = new Scanner(System.in);
1.
2.
var1 = kb.nextInt();
087
var2 = kb.nextLong();
-0
2553 ( 7 2/2553) ()
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
int
7.0
var4 = kb.nextFloat();
22.50000
long
46L
float
7.2F
double
-0.1
double
-13
double
var10 = kb.nextInt();
double
var11 = kb.nextInt();
1.0
int
var12 = kb.nextDouble();
1.0
var13 = kb.nextLine();
22.50000
var14 = kb.nextLine();
Hi java CU
var15 = kb.next();
Hi java CU
var16 = kb.nextInt();
191
String
System.out
2) println()
System.out.println(...);
1) System.out.print(num);
2) System.out.print(13);
3) System.out.print(x * y + (10 z));
4) System.out.print("Hello Java");
5) 1-4 System.out.print("Money = " + (x *
+
2553 ( 7 2/2553) ()
21
22
3.
1 import java.util.Scanner;
Scanner
2 public class Test {
3
public static void main(String[] args) {
4
Scanner kb = new Scanner(System.in);
5
System.out.print("Please enter number: ");
6
int num = kb.nextInt();
7
8
...
9
print()
10
System.out.println("Result is " + num);
11
}
12 }
println()
Please enter number:
15
Enter
15 num
num
4 [ ]
-
id, name,
age gpa
(10 )
1
2553 ( 7 2/2553) ()
5 []
a b
a b b
a (10 )
1
Enter
Enter
Value
Value
a:
b:
of
of
13
21
a: 21
b: 13
x + y a
x - y a
*
x * y a
/
x / y a
%
(Modulo)
x % y a
+ b + c
- b - c
* b * c
/ b / c
% b % c
x++
x 1 x = 2, y = 1 x++
++x
1 x x = 2, y = 1 ++x
x- x 1 x = 2, y = 1 x---x
1 x x = 2, y = 1 --x
+ y = 3
+ y = 4
+ y = 3
+ y = 2
2553 ( 7 2/2553) ()
23
24
3) (Comparison/Relational Operators)
==
x == y ( )
!=
x != y ( )
<
x < y ( )
>
x > y ( )
<=
x <= y ( )
>=
x >= y ( )
4) (Logical Operators)
(x >= y) && (x != 0)
&&
(AND)
(x == y) || (x == z)
||
(OR)
!(x <= y)
!
(NOT)
5) (Assignment Operators)
x
+=
x += y
x = x + y
x
-=
x -= y
x = x - y
x
*=
x *= y
x = x * y
x
/=
x /= y
x = x / y
x
= y y x
+= 14
x = x + 14
-= 1
x=x-1
*= y--
x = x * y-/= 5.8
x = x / 5.8
6 [ ] (5 )
a = 21, b = 0;
1. int
System.out.println("a" + a - b);
2.
3.
4.
int x = 6;
System.out.println(x-- + ++x - --x - x-- + x++ + x);
5.
2. ( )
/
()
1. (Parentheses)
x (2 + y) (2 + y) x
2. (Method Call) () Math.sqrt(a + 2) Math.sqrt()
2553 ( 7 2/2553) ()
/
+
(Unary Plus)
2 * +a +a 2
3.
(Unary Minus)
a / 3 a 3
*
(Multiplication)
x * 3 + 2 x * 3 2
4. (Division)
/
3 5 / x 5 / x 3
%
(Modulo)
y % 2 + 1 y % 2 1
+
(Addition)
x + 17 > 19 x + 17 >
5.
(Subtraction)
x 9.5 = 42.5 x 9.5 =
6. : (<), (<=), (>), (>=)
7. : (==), (!=)
&&
8. (Logical AND)
a || b && c && ||
||
9. (Logical OR)
x = d > 3 || d <= 15 || =
=
10. (Assignment)
7 [ ]
(5 )
1)
a * (b + -(c / d) / e) + (f g % h)
2)
3)
a * b + (-c) / d / e * f - g % h
4)
cal = (1 x % 2.5 + y * z) + 3 * k -7 / w
5)
3.
20
1
2
+ - * / %
1.
()
= += -= *= /=
2.
[1] (
1)
== != < > <= >=
3.
boolean
+ - * / % = += -= *= /=
char
4.
2553 ( 7 2/2553) ()
25
26
1
5.
6.
char
7.
char
8.
char
9.
char
10.
char
11.
char
12.
char
13.
14.
String
15.
16.
String
17.
String
18.
19. boolean
20.
-
++ -++ -+ - * / %
char
( int)
= += -= *= /=
char
char
char
boolean
++ --
[2]
-
++ --
char
char
String
String
String
String
String
== !=
String
boolean
= && ||
boolean
boolean
boolean
boolean
+ - * / %
+= -= *= /=
=
char
char
char
( )
1 2
[2]
2 int
[1]
8 [ ]
(10 )
1. (5 + 4) + ((3 + 2L) 1)
2. (5 2 / 1) + 0.000 + '\u0000'
3. 5.0F * 6 ('D' - 'A')
*4. 2 % (0.4 + 0.3F)
*5. !("Java" == "Jaba");
6. (13 / 5) + (2 / 3)
7. '0' - '1' + "5" + false
8. 4E1 / 2
9. !false || true && false || !true
10. 0.0 / (10 + 'q' 'Y') != 0L
2553 ( 7 2/2553) ()
4. (Type Conversions)
1)
19
double a = 34 + 1.0F;
float b = 4L / 3;
//a = 35.0
//b = 1.0F
int c = 'A';
char d = 65;
//c = 65
//d = 'A'
2) (Casting)
(< >) < />
(/ )
byte b = 50;
b = (byte)(b * 2);
//b = 100
char c = '1';
int num = (int) c;
double x = 5.99;
long y = (long) x;
//y = 5
//num = 49
//Error
//Error
9 [ ]
(10 )
1. (byte)(3 + 5.4) / 7
2. (3 + 5.4) / (float) 7
3. (float)((3 + 2) / 7)
4. char c = (double) 'A';
5. char c = (int) 'b'; c += 1.7;
6. char c = 67 + (int) 3.91
7. (int) 19.8E-1
8. (short)((double) 1 / 2) == 00L + 0
9. (byte) 011 + (short) 1.9991
10. (byte) '0' '6' + (char) 1.5
5. (Mathematic Class)
double
1. Math.toRadians(degree)
double
2. Math.toDegrees(radian)
double
3. Math.sin(radian)
(Sine)
double
4. Math.cos(radian)
(Cosine)
2553 ( 7 2/2553) ()
27
28
5.
6.
7.
8.
9.
10.
11.
Math.sqrt(x)
double
Math.pow(x, y)
double, double
xy
Math.abs(x)
double
(Absolute)
Math.max(x, y)
double, double
x y
Math.min(x, y)
double, double
x y
Math.log10(x)
double
Math.PI
PI 3.141592653589793
Math.random()
[0.0, 1.0)
12.
(int)(Math.random() * (b a + 1) + a)
a b
double ( 12 int)
10 [ ] (Assignment Statement)
(6 )
1.
2r 2
b 2 4ac
2.
mn
3.
3x 3 + 4 y 4
+ xy
( x + y)( x y)
p2 q2
2mn
p+q
11 [ ]
n
(6 )
1. 0 9
2. 1 10
3. 0 10
4. 0 987
5. 1 500
6. 2 600
2553 ( 7 2/2553) ()
12 []
4
(10 )
Enter number: 1234
1
13 []
6 2 2
(10 )
1
2553 ( 7 2/2553) ()
29
30
14 [] 1
(Money Machine)
1000 500 100 50 20
10 5 2 1
(15 )
Money : 2897
1
1000 Baht
500 Baht
100 Baht
50 Baht
20 Baht
10 Baht
5 Baht
2 Baht
1 Baht
:
:
:
:
:
:
:
:
:
2
1
3
1
2
0
1
1
0
Do you know?
1 / 0
1.0 / 0.0
-1.0 / 0.0
1.0 / (1.0 / 0)
0.0 / 0.0
=
=
=
=
=
Error
Infinity
-Infinity
0.0
NaN
2553 ( 7 2/2553) ()