JAVA- Unit 1
Prepared by
Dr. D. Suganthi,
AP/MCA
Java Tutorial
Write Once, Run Anywhere
Java – Introduction
Java - General
Java Development Kit
Java - General
How it works…!
Compile-time Environment
Compile-time Environment
Java
Bytecodes
move locally
or through
network
Java
Source
(.java)
Java
Compiler
Java
Bytecode
(.class )
Java
Interpreter
Just in Time
Compiler
Runtime System
Class Loader
Bytecode
Verifier
Java Class
Libraries
Operating System
Hardware
Java
Virtual
machine
How it works…!
Object-Oriented
Java Advantages
Basic Java Syntax
Primitive Types and Variables
float s;
char a;
Byte c;
int a, index = 2;
double g = 1.2, brightness
boolean v = false;
Initialisation
Declarations
int index = 1.2; // compiler error
boolean retOk = 1; // compiler error
double fiveFourths = 5 / 4; // no error!
float ratio = 5.8f; // correct
double fiveFourths = 5.0 / 4.0; // correct
Assignment
int a = 1, b = 2, c = 5
a = b = c
System.out.print(
“a= “ + a + “b= “ + b + “c= “ + c)
System.out.println("a= " + a + "; b= " + b + "; c= " + c);
Basic Mathematical Operators
double myVal = a + b % d – c * d / b;
double myVal = (a + (b % d)) –
((c * d) / b);
Flow of Control
Alternation: if, if else, switch
Looping: for, while, do while
Escapes: break, continue, return
If – The Conditional Statement
if ( x < 10 ) x = 10;
if ( x < 10 )
x = 10;
if ( x < 10 ) { x = 10; }
Relational Operators
== Equal (careful)
!= Not equal
>= Greater than or equal
<= Less than or equal
> Greater than
< Less than
If… else
if (x != oldx) {
System.out.print(“x was changed”);
}
else {
System.out.print(“x is unchanged”);
}
Nested if … else
if ( myVal > 100 ) {
if ( remainderOn == true) {
myVal = mVal % 100;
}
else {
myVal = myVal / 100.0;
}
}
else
{
System.out.print(“myVal is in range”);
}
else if
if ( n == 1 ) {
// execute code block #1
}
else if ( j == 2 ) {
// execute code block #2
}
else {
// if all previous tests have failed, execute code block #3
}
A Warning…
WRONG!
if( i == j )
if ( j == k )
System.out.print(
“i equals k”);
else
System.out.print(
“i is not equal to j”);
CORRECT!
if( i == j ) {
if ( j == k )
System.out.print(
“i equals k”);
}
else
System.out.print(“i is not equal to j”); // Correct!
The switch Statement
switch ( n ) {
case 1:
// execute code block #1
break;
case 2:
// execute code block #2
break;
default:
// if all previous tests fail then //execute code block #4
break;
}
The for loop
for ( i = 0; i < n; n++ ) {
// this code body will execute n times
// ifrom 0 to n-1
}
for ( j = 0; j < 10; j++ ) {
for ( i = 0; i < 20; i++ ){
// this code body will execute 200 times
}
}
while loops
while(response == 1) {
System.out.print( “ID =” + userID[n]);
n++;
response = readInt( “Enter “);
}
What is the minimum number of times the loop is executed?
What is the maximum number of times?
do {… } while loops
do {
System.out.print( “ID =” + userID[n] );
n++;
response = readInt( “Enter ” );
}while (response == 1);
What is the minimum number of times the loop is executed?
What is the maximum number of times?
Arrays
myArray has room for 8 elements
3
6
3
1
6
3
4
1
myArray =
0
1
2
3
4
5
6
7
Declaring Arrays
int myArray[];
declares myArray to be an array of integers
myArray = new int[8];
sets up 8 integer-sized spaces in memory, labelled myArray[0] to myArray[7]
int myArray[] = new int[8];
combines the two statements in one line
Assigning Values
myArray[0] = 3;
myArray[1] = 6;
myArray[2] = 3; ...
int myArray[] = {3, 6, 3, 1, 6, 3, 4, 1};
Iterating Through Arrays
for (int i = 0; i < myArray.length; i++) {
myArray[i] = getsomevalue();
}
Declaring the Array
1. Declare the array
private Student studentList[];
2 .Create the array
studentList = new Student[10];
3. Create Student objects and add them to the array: studentList[0] = new Student("Cathy", "Computing");