Professional Documents
Culture Documents
(http://docs.oracle.com/javase/tutorial/java/index.html)
Letter Letters
$ Digits, $
_ _
3/40
Primitive Data Types - Variables
Type Bytes Minimum Maximum
• A primitive is a
char 2 \u0000 \uFFFF
simple non-
object data byte 1 -27 27 - 1
type that short 2 -215 215 – 1
represents a int 4 -231 231 – 1
single value.
long 8 -263 263 - 1
Java’s
primitive data float 4
types are: double 8
boolean true/false
Unary ++ -- + - ! ~ (type)
Arithmetic * / %
+ -
Shift << >> >>>
Comparison < <= > >= instanceof
== !=
Bitwise & ^ |
Short-circuit && || They are the same with
Conditional ?: those in C language
Assignment = op=
6/40
Using Operators Demonstration
Use 2 bytes to store value
1: 0000 0000 0000 0001
1111 1111 1111 1110 ( 1-complement)
-1 1111 1111 1111 1111 ( 2-complement)
-1 <<1 1111 1111 1111 1110 (-2)
int[] a3 = {1,2,3,4,5};
int a4[] = {1,2,3,4,5};
Stack ar 10000
12/40
Multiple Dimensional Arrays
10 2002
9 500
2001
92
500
8 91
200 200
7
100 4
6 8000
5 3
1000
8000 2
replacement 1000 m
1
100
Order:
(1) [ ] a[b] a[1]
(2) = ( from the right) b=0 return 0
a[1] = 0
15/40
Basic Constructs
a 1 2 3 4 5
x 1
17/40
The String type
* Widening Conversion: OK
• Narrowing conversion: Not
allowed. We must use
explicit casting.
• A boolean can not be
converted to any other
type.
• A non-boolean can be
converted to another non-
boolean type.
0000 0001
0000 0000
y n
19/34
Scope of a Variable
Scope of the
variable y
Scope of the
variable i
Class java.lang.System
Class java.util.Scanner
Refer to Java documentation:
java.lang.String class,
- the format method,
- format string
for more details
n= sc.nextInt();
Session 02 - Java Fundamentals
Elements of Java Style
class MountainBike
extends Bicycle {
// new fields and methods defining
// a mountain bike would go here
}