Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Contents
1.
2.
3.
4.
5.
6.
Lecture 1 Review
Modifying The First Java Program
Primitive Data Types
Logical Operators and Assignments
Casting
Sample Codes
Program Processing
Compilation
javac HelloWorld.java
Execution
java HelloWorld
Range
Storage Size
byte
8-bit signed
short
16-bit signed
int
long
263 to 2631
(i.e., -9223372036854775808
to 9223372036854775807)
64-bit signed
float
Negative range:
-3.4028235E+38 to -1.4E-45
Positive range:
1.4E-45 to 3.4028235E+38
double
Negative range:
-1.7976931348623157E+308 to
-4.9E-324
Positive range:
4.9E-324 to 1.7976931348623157E+308
Identifiers
An identifier is a sequence of characters that consist
of letters, digits, underscores (_), and dollar signs
($).
An identifier must start with a letter, an underscore
(_), or a dollar sign ($).
It cannot start with a digit.
Should not have space in between.
An identifier cannot be a reserved word.
An identifier cannot be true, false, or null.
An identifier can be of any length.
ALL RIGHTS RESERVED
No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide
Identifiers
VALID identifiers
a1
num_1
$num
INVALID identifiers
1a
num 1
public
ALL RIGHTS RESERVED
No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide
Numeric Operators
Name
Meaning
Example
Result
Addition
34 + 1
35
Subtraction
34.0 0.1
33.9
Multiplication
300 * 30
9000
Division
1.0 / 2.0
0.5
Remainder
20 % 3
Variables
type variableName =
e.g.
10
int
initialValue;
x = 1;
Declaring Variables
int x;
// Declare x to be an
// integer variable;
11
// Declare m to be a
// character variable;
//Assign 1 to x;
double d = 1.4;
//Assign 1.4 to d;
char a = A;
//Assign A to a;
12
//Assign 12345 to s2
Constants
Constants are similar to variables except that they
hold a fixed value. They are also called READ only
variables.
final datatype CONSTANTNAME = VALUE;
Constants are
word final.
declared
with
the
reserved
14
an
action
on
variables,
Arithmetic Expression
y = mx + c ;
is translated to
y = m * x + c ;
16
Arithmetic Expression
x = a(y * m);
3
is translated to
17
Arithmetic Expression
x = a(y * m);
3
is translated to
x = a*(y*y*y * m);
18
Arithmetic Expression
a = y(ax3 + c);
is translated to
Answer:
19
Arithmetic Expression
3 4x 10(y 5)(a b c)
4 9 x
9(
)
5
x
x
y
is translated to
Answer:
20
1.( ) - parentheses
2.*, / , %
3.+ , If the expressions occurs at the same time solve it
from left to right!
21
53
(3) multiplication
(4) addition
(5) addition
(6) subtraction
24
Operator
Example
Equivalent
+=
i += 8
i = i + 8
-=
f -= 8.0
f = f - 8.0
*=
i *= 8
i = i * 8
/=
i /= 8
i = i / 8
%=
i %= 8
i = i % 8
e) y/ = 5 + 2 * 3;
b)y* = 2;
f) y*= 2*y;
c)y- = 12 y;
d)y% = 16 / y ;
25
g) y+ = 20/5 + 8 * 2;
h) y% = 80 / y - 1;
int i = 10;
int newNum = 10 * i++;
int i = 10;
int newNum = 10 * (++i);
27
Same effect as
Same effect as
int newNum = 10 * i;
i = i + 1;
i = i + 1;
int newNum = 10 * i;
Casting
Example:
float money = 8.75f;
int dollars;
dollars = (int) money;
Type Casting
range increases
byte, short, int, long, float, double
Implicit casting
int e = 3;
double d = (double)e;
means d = 3.0 (type widening)
Explicit casting
float
int i
means
int j
means
x
=
i
=
j
= 3.0, y = 3.9;
(int)x;
= 3 (type narrowing)
(int)y;
= 3 (fraction part is truncated)
35
Casting Continued
Casting char variables
char y = 'A';
int c;
c = (int) y;
char y is cast as an int c is 65
int x = 97;
char m = (char) x;
int x is cast as char m = a
ALL RIGHTS RESERVED
No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide
short
char
int
long*
float
float, double
double
To
byte
char
short
byte, char
char
byte, short
int
long
float
double
39
40
41
42
Input
There are two ways of obtaining input from the user.
1. Using the Scanner class (console input)
43
44
48
49
TypesofMessages:
a)ERROR_MESSAGEfordisplayinganerrormessage
b)INFORMATION_MESSAGEfordisplayingan
informationalmessage
c)QUESTION_MESSAGEfordisplayingaquery
message
d)WARNING_MESSAGEfordisplayingawarning
message
e)PLAIN_MESSAGEfordisplayinganyothertypeof
message
JAVA
BPROG120
50
51
52
54
55
56
57
END OF CHAPTER 2
58