Session02-Learning The Java Language
Session02-Learning The Java Language
(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
simple non- char 2 \u0000 \uFFFF
object data byte 1 -27 27 - 1
type that short 2 -215 215 – 1
represents a int 4 -231 231 – 1
single value.
Java’s long 8 -263 263 - 1
primitive data float 4
types are: double 8
boolean true/false
Unary ++ -- + - ! ~ (type)
Arithmetic * / %
+ -
Shift << >> >>>
Comparison < <= > >= instanceof
== !=
Bitwise & ^ |
They are the same with
Short-circuit && ||
those in C language
Conditional ?:
Assignment = op=
Session 02 - Learning the Java Language
Using Operators Demonstration
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)
1.3f 1.3F
1.3E+21 int n=10;
1.3d 1.3D
8/40
Java Expressions
• Java is an expression-oriented language. A
simple expression in Java is either:
• A constant: 7, false
• A char - literal enclosed in single quotes: 'A', '3‘
• A String - literal enclosed in double quotes: "foo“
• The name of any properly declared variables: x
• Any two|one of the preceding types of
expression that are combined with one of the
Java binary operators: i++, x + 2, (x + 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
1000 m
replacement 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
}