Lecture 2
Lecture 2
JAVA PROGRAM
Java Source
Code
Java Compiler
Java Enabled
Browser Java Interpreter
Output
Output
SIMPLE JAVA PROGRAM-EXAMPLE 1
/*This is a simple java program*/
class Example
{
public static void main (String args[])
{
System.out.println (“This is a simple Java program”);
}
}
SIMPLE JAVA PROGRAM-SOME
IMPORTANT POINTS
public: Access specifier. main() must be made public,
since it must be called by code defined outside it’s class.
Static: It is required because main() is called without
creating an object of it’s class
String args[]: An array of objects of type String class.
Each object of type string contains a character string. It
is used to manipulate command line argument.
Java is case sensitive.
System predefined class that refers to system.
out It is static data member of System class
println() It is a member of out object
JRE AND JDK
JRE: Java Runtime Environment
provides
libraries,
float 32 bits
double 64 bits
CHAR
It uses unicode to represent character.
The char type is unsigned 16 bit values ranging from 0 to 65536.
ASCII still ranges from 0 to 127.
Example:
class test
{
public static void main (String args[])
{
char ch1, ch2;
ch1=88;
ch2=‘Y’;
System.out.println (“ch1 and ch2: “ + ch1+” “+ch2);
}
}
Output: ch1 and ch2: X Y
BOOLEANS
Size is 1 bit – two value: true and false.
This type is returned by all relational operators.
Example:
boolean b;
b= true;
1. System.out.println(“b is “+b);
2. System.out.println(“10>9 is “ +(10>9));
Output:
b is true
10>9 is true
VARIABLES
Variable is a name for a location in memory.
Declaring a variable:
type identifier [=value][,identifier [=value]….];
The initialization expression must result in a value of the
same or compatible type as that specified for the
variable.
Dynamic Initialization: initialization expression may use
any element valid at the time of initialization, including
calls to methods, other variables, or literals.
When a variable is not initialized, the value of that
variable is undefined.
SCOPE AND LIFETIME OF A
VARIABLE
A block begins with an opening curly brace and ends by
a closing curly brace.
A block determines scope, that defines which objects are
visible to other parts of your program.
Variables declared within a block localize themselves.
In case of nested block, the outer block encloses the
inner block. The variables declared in the outer block is
visible to the inner block but the reverse is not true.
A variable will not hold it’s value once it has gone out of
it’s scope.
In an inner block, it is not possible to declare a variable
of the same name as in outer block.
SCOPE AND LIFETIME OF A
VARIABLE
Example:
public static void main( String args[])
{
int x =10;
if ( x == 10)
{
int y =20;
System.out.println(“x and y: “ +x +” “+y);
x= y * 2;
}
y= 100; //Error
System.out.println (“x is “+x);
}
AUTOMATIC TYPE
CONVERSION
When one type of data is assigned to another type of
variable, an automatic type conversion will take place if:
1. Two types are compatible.
2. The destination type is larger than the source type.
It is known as widening conversion.
The numeric types, including integer and floating point
types are compatible.
Numeric types are not compatible with char or boolean.
Char and boolean are not compatible with each other.
CASTING TYPE
Problem: assigning int value to a byte type variable.
This type of conversion is known as narrowing conversion.
A cast is simply an explicit type conversion : (target_type) value
Example:
int a;
byte b;
….
b = (byte) a;
Iteration statement
Jump statement
1. if statement
2. switch statement
if (condition)
statement;
Statement x;
statement3; false
else true
conditon3 statement3
statement4; evaluated
false
statement4
THE SWITCH STATEMENT
The switch statement provides another means to decide which
statement to execute next
The switch statement evaluates an expression, then attempts to
match the result to one of several possible cases
The expression of a switch statement must result in an integral
type (byte, short, int, char etc)
switch (expression) {
case value1:
statement-list1
switch break;
and case value2:
case statement-list2
are break;
reserved case value3:
words statement-list3 If expression
break; matches value2,
case default: control jumps
statement-list4 from here
}
THE SWITCH STATEMENT
A break statement causes control to transfer to the end of the
switch statement
If a break statement is not used, the flow of control will
continue into the next case
Sometimes this can be appropriate, but usually we want to
execute only the statements associated with one case
THE SWITCH STATEMENT
A switch statement can have an optional default case
The default case has no associated value and simply uses
the reserved word default
If the default case is present, control will transfer to it if
no other case value matches
If there is no default case, and no other value matches,
control falls through to the statement after the switch
SWITCH EXAMPLE
char letter = 'b'; char letter = 'b';
B B
C
ITERATION STATEMENTS
Iteration statements allow us to execute a statement multiple times until a
termination condition is met.
Often they are referred to as loops
Java has three kinds of iteration statements:
the while loop
the do-while loop
the for loop
THE WHILE STATEMENT
The while statement has the following syntax:
while (condition)
while is a
statement;
reserved word
condition
evaluated
true false
statement
do and do{
while are statement;
reserved } while (condition);
words
statement
condition
evaluated
true
initialization;
while (condition) {
statement;
increment;
}
LOGIC OF A FOR LOOP
initialization
condition
evaluated
true false
statement
increment
FOR EXAMPLE
int LIMIT = 5;
for (int count = 1; count <= LIMIT; count++) {
System.out.println(count);
}
Output:
1
2
3
4
5
THE FOR STATEMENT
Each expression in the header of a for loop is optional