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, $
_ _
• Java is a case-sensitive language
• Identifiers must be different to keywords
Primitive data type
A primitive data type is a
type that is retained from
the C language (Java's
original language).
There are 8 primitive data
types
For example
– int a = 8;
– double b;
The law of type coercion
For primitive types, casting automatically occurs in the
direction of the
char arrow
int long float double
byte short
For example
– int a = 5;
– double b = 9.4;
– b = a; // automatic style casting
– a = (int)b; //explicitly casts the decimal part will be dropped
Convert string to primitive type
Consider the expression 1
String a = “3”;
String b = “4”;
“34”
String c = a + b;
=> c là ?
Consider the expression 1
int a = Integer.parseInt(“3”);
int b = 7Integer.parseInt(“4”);
int c = a + b;
Operators & expressions
Operators
NUMERALS COMPARE LOGIC assign
+, -, *, /, %, + >, <, >=, <=, =, +=, -=,
&&, ||, !
+, -- ==, != *=, /=, %=
Exam
Write a program that inputs a number and prints out whether the
number is even or odd
if(a%2==1)
System.out.println(“%d is odd”,a);
else. else
System.out.println(“%d is even”,a);
Exam
Lesson 3: Write a program to swap the values of two
variables as follows:
Enter 02 integers to store in two variables a and b.
Write a command to swap the values of these two
variables.
Print to the screen the value of variables a and b
after swap.
05/29/2025 9/11
Primitive Data Types - Variables
• A primitive is a Type Byte Minimu Maximu
s m m
simple non-object char 2 \u0000 \uFFFF
data type that byte 1 -27 27 - 1
represents a single short 2 -215 215 – 1
value. int 4 -231 231 – 1
• Java’s primitive long 8 -263 263 - 1
data types are: float 4
double 8
boolea true/false
n
Type var [=Initial value] ;
Reference Data Types -
Variables
• A reference data type for example:
String s=new String("Hello");
contains reference/address
of dynamically created
objects.
• reference types in Java:
Class types.
Array types
Interface types
• Default value of any
reference variable is null
One Dimensional Arrays (1)
• An array is a container object that holds a
fixed number of values of a single type.
• The length of an array is established when
the array is created.
• Each item in an array is called an element,
and each element is accessed by its
numerical index.
One Dimensional Arrays (2)
• Declaring a Variable to Refer to an Array
int[] anArray;
or float anArrayOfFloats[];
• Creating, Initializing, and Accessing an
Array
anArray = new int[10];
• Copying Arrays
• Use arraycopy method from System class.
One Dimensional Arrays (3)
int[] ar;
ar= new int[3];
3
ar[0]=1; ar[1]=2; ar[2]=3;
Heap 2
int a2[]; 10000 1
int[] a3 = {1,2,3,4,5};
int a4[] = {1,2,3,4,5};
Stack ar 10000
Array is a reference variable
int n=10;
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
int m[][]= { {1,2,3,4}, {91,92}, {2001,2002}};
int[] replacement = {5,6,7,8,9,10}; m[i][j]
m[1]= replacement; int[][] m; // declare a matrix
int r=10, c=5; // number of rows, columns
m= new int[r][c]; // memory allocate
Exam
Lesson 1: Enter the list of n real numbers
ArrayList<Double>.
Calculate the total and output to the screen
Lesson 2: Enter the list of names of 5 Students
output to the screen
05/29/2025 16/11
Operators
Category Operators
(Descending Precedence)
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)
-1 1111 1111 1111 1111
-1 >>1 1111 1111 1111 1111
-1 1111 1111 1111 1111
3 0000 0000 0000 0011 -1 >>>1 0111 1111 1111 1111 (2147483647)
4 0000 0000 0000 0100
3|4 0000 0000 0000 0111 (7)
3 0000 0000 0000 0011
4 0000 0000 0000 0100
3&4 0000 0000 0000 0000 (0)
3 0000 0000 0000 0011
4 0000 0000 0000 0100
3^4 0000 0000 0000 0111 (7 ): XOR BIT
Literals and Value Variables
Character: ‘a’ Value variable
String: String S=“Hello”;
Escape sequences: see the page
10
Stack
Integral literals:
28, 0x1c, 0X1A ( default: int).
123l, 123L (long)
Floating point:
1.234 (default: double) n 10
1.3f 1.3F
1.3E+21
int n=10;
1.3d 1.3D
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)
Evaluating Expressions and
Operator Precedence
• The compiler generally evaluates such
expressions from the innermost to outermost
parentheses, left to right.
int x = 1; int y = 2; int z = 3;
int answer = ((8 * (y + z)) + y) * x;
would be evaluated piece by piece as follows:
((8 * (y + z) ) + y) * x
((8 * 5) + y) * x
(40 + y) * x
42 * x
42
Operator Precedence- Evaluation Order
Order:
(1) [ ] a[b] a[1]
(2) = ( from the right) b=0 return 0
a[1] = 0
Basic Constructs
They are taken from C-language
Selection
if, if … else
switch (char/int exp)… case … default…
Loops
for
do… while
while
Basic Logic Constructs
They are the same with those in C-statements
An enhanced for loop
a 1 2 3 4 5
x 1
Exercise 1: Write a program to find the largest number
of 3 integers a, b, c.
Exercise 2: Write a program to input a student's
grades. Print out the student's academic rating.
(Gradement. If the score is >= 9, Excellent. If the
score is from 8 to close to 9, Good. If the score is from
7 to close to 8, Good. If the score is from 6 to close to
7, Medium, If the score is from 5 to close to 6. ,
Average, the rest are Weak) . The rest is incorrect
score entry.
Lesson 3: Write a program to solve quadratic
equations
Lesson 1: Enter array of integer type, Print
array, sort array, find smallest value Min in
array.
Lesson 2: Input the full name and points
information. Output array descending by points
The String type
• A String represents a sequence of zero or
more Unicode characters.
• String name = "Steve";
• String s = “”;
• String s = null;
• String concatenation.
• String x = "foo“ + "bar“ + "!";
• Java is a case-sensitive language.
Type Conversions and Explicit Casting
* 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
• Proper Use of Indentation
• Statements within a block of code should be indented
relative to the starting/ending line of the enclosing
block.
• Use Comments Wisely
• Placement of Braces
• Opening brace at the end of the line of code that
starts a given block. Each closing brace goes on its
own line, aligned with the first character of the line
con.
• Descriptive Variable Names
Pass Arguments to the method main
Pass
Arguments to
the method
main
Summary
• The traditional features of the language,
including variables, data types, operators,
and control flow.
• There are two categories : primitive types
and reference types
• the scope of a declaration is that part of
the program throughout which the
declaration is visible