4Learning the Java Language1
4Learning the Java Language1
(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, $
_ _
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
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
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
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)
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
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
* 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