02.1 - Learning The Java Language
02.1 - 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
• Concept: Package.
Keywords and Identifiers
Keywords: Almost of them are similar to
those in C language
Naming Convention:
Letter Letters
$ Digits, $
_ _
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)
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
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 of a Variable
Scope of the
variable y
Scope of the
variable i
Input/Output Data (1)
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();
Input/Output Data (2)
PACKAG
import java.util.Scanner;
E
//Functionality provided
SYNTAX
scVar.nextInt();
scVar.nextDouble();
......
Input/Output Data (3)
import java.util.Scanner; // or import java.util.*;
TemperatureInteractive.java
23
Input/Output Data (4)
The statement
Scanner sc = new Scanner(System.in);
– Declares a variable “sc” of Scanner type
– The initialization “new Scanner(System.in)”
• Constructs a Scanner object
– We will discuss more about object later
• Attaches it to the standard input “System.in”
(which is the keyboard)
– This Scanner object sc will receive input from this source
• Scanner can attach to a variety of input sources;
this is just a typical usage
24
Input/Output Data (5)
//Functionality provided
System.out.print( output_string );
SYNTAX
System.out.println( output_string );
Output:
System.out.print("ABC"); ABCDEF
System.out.println("DEF"); GHI
System.out.println("GHI"); Very C-like 3.142
27
Input/Output Data (8)
Java introduces printf() in Java 1.5
– Very similar to the C version
The format string contains normal characters and a number
of specifiers
– Specifier starts with a percent sign (%)
– Value of the appropriate type must be supplied for each specifier
Common specifiers and modifiers:
%d for integer value
%f for double floating-point value %[-][W].[P]type
SYNTAX
%s for string
for boolean value
-: For left alignment
%b
W: For width
%c for character value
P: For precision
28
Elements of Java Style