Professional Documents
Culture Documents
2.12.2.learning The Java Language
2.12.2.learning The Java Language
(http://docs.oracle.com/javase/tutorial/java/index.html)
Objectives
• Study some fundamentals of Java
languages: Data types, variables, arrays,
operators, logic constructs.
• Pass arguments to the main method
• Input/output variables
Keywords and Identifiers
Keywords: Almost of them are similar to
those in C language
Naming Convention:
Letter Letters
$ Digits, $
_ _
Heap 2
int a2[]; 10000 1
int[] a3 = {1,2,3,4,5};
int a4[] = {1,2,3,4,5};
Stack ar 10000
9 500
2001
92
500
8 91
200 200
7
100 4
6 8000
5 3
1000
8000 2
replacement 1000 m
1
100
Unary ++ -- + - ! ~ (type)
Arithmetic * / %
+ -
Shift << >> >>>
Comparison < <= > >= instanceof
== !=
Bitwise & ^ |
Short-circuit && || They are the same with
Conditional ?: those in C language
Assignment = op=
Using Operators Demonstration
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)
Order:
(1) [ ] a[b] a[1]
(2) = ( from the right) b=0 return 0
a[1] = 0
Basic Constructs
a 1 2 3 4 5
x 1
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
Scope
The scope of a declaration is the portion of a program
over which that declaration is visible. Scopes include
global scope
file scope
function scope
Class scope
block scope
The scope of a non-global declaration begins at the
declaration and ends at the closing brace for that
declaration.
A non-global declaration is called a local declaration.
Scope of a Variable
Scope of the
variable y
Scope of the
variable i
Input/Output Data
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();
Elements of Java Style