Professional Documents
Culture Documents
Cs Unt 1 Half
Cs Unt 1 Half
15
After the file is compiled successfully, byte code(.class file) is generated by the
Compiler. When byte code(.class file) is executed, the following steps are performed
at runtime -
Class loader which is is subsystem of JVM (Java Virtual machine) loads the
java class.
Byte Code verifier checks the code fragments for illegal codes that can violate
access right to the object.
Interpreter reads the byte code stream and then executes the instructions,
step by step.
1.1.5 Data Types
Data types classify the different values to be stored in the variable. In java, there
are two types of data types:
Integral
Character
Floating-point
Integer
d i
This process is Automatic and non-explicit, it is known Conversion.
as
The
second case is discussed in next topic.
int i = 100;
Output:
Int value 100
Long value 100
Float value 100.0
UNIT ONE 1.17
1.1.7 Casting
Case (2) Variable of larger capacity is be assigned to another variable of smaller
capacity.
double d =10;
int i;
i (int) d; // Type casting
In such cases, you have to explicitly specify the type cast operator. This process is
known as Type Casting.
In case, you do not specify a type cast operator; the compiler gives an error. Since
this rule is enforced by the compiler, it makes the programmer aware that the
conversion he is about to do may cause some loss in data and prevents accidental
losses.
When you are assigning a larger type value to a variable of smaller type, then
you need to perform explicit type casting
double d = 100.04;
Output:
Double value 100.04
Long value 100
Int value 100
want to execute the code only if the user entered some text; if he hasn't, don
execute the code because it will just lead to a runtime error.
if (Statement)
/ / do something here.
This statement can also be extended to do something else if the condition is false:
if (statement)
/7 do something here...
else
1/ do sonmething else..
For example, if you are determining whether someone is old enough to drive
you might have a statement that says "if your age is 18 or older, you can drive; else
you cannot drive."
switch statement.
if (Statement)
// do something here...
This statement can also be extended to do something else if the condition is false:
if (statement)
// do something here...
else
1/ do something else.
For example, if you are determining whether someone is old enough to drive
you might have a statement that says "if your age is 18 or older, you can drive; else
you cannot drive."
if(age= 18)
else
System.out.pri
println("You are not old enough to drive ");
Output:
You can drive.
Conditional Operators:
In the example above, we used a single operator: >=ie. "great than or equal to.
More than:>
Greater than or equal to: >=
conditional statements:
AND:&&
NOT:!
OR: 11
be from 18 to age 85, in which
For example, Let driving age is considered to age
case we could use the AND operator:
else if ( age = 18 && age <= 85)
This will return true only if both conditions are met. The operators NOT and OR
can be used similarly
else
Output:
You can drive.
inti=5;
switch(i)
case 1:
System.out.println (i is 1");
break;
case 2:
break;
case 3:
System.out.println("i is 4'");
break;
case 5:
System.out.println(i is 5");
break;
default:
System.out.println("Invalid value");
Output:
i is 5
1.1.9 Looops
of code whern some
is used in programming
to repeat a specific block
Loop
conditions become true. There
are three types of loops in java.
//code to be executed
System.out.println(i);
Output:
for-each Loop:
The for-each loop is used to traverse array or collection in
java. It is easier to use
than simple for loop because we don't need to increment value and use
notation. It works on elements basis not index. It returns element one
subscript
by one in the
defined variable.
Syntax:
for(lype var:array)
//code to be executed
System.out.printlni);
Output:
10
12
25
30
56
only.
Syntax:
labelname:
for(initialization; condition; incr/decr)
//code to be executed
aa:
bb:
for(int j=1; j<=3; j++)
UNIT ON
if(i-28&c-2)
break aa;
System.out.println(i+""+j);
Output:
11
12
13
21
If you break bb;, it will break inner
use
if(i--2&&j=2)
break bb;
System.out.printlni+""+i);
UNIT ONE 1.25
Output:
11
12
13
21
31
32
33
While Loop:
The Java while loop is used to iterate a part of the program several times. If the
number of iteration is not fixed, it is recommended to use while loop.
Syntax:
while(condition)
//code to be executed
int i=1;
while(i<=5)
System.out.println(i)
i++
Output:
4
5
1.26
UNIT ON
do-while Loop:
The Java do-while loop is used to iterate a
part of the program several times
the number of iteration is not fixed and you must have to execute the nes.
loop l
at
once, it is recommended to use do-while loop. The Java do-while loop is
least once because condition is checked after
executecd
loop body.
Syntax:
//code to be executed
whilefcondition);
Program to show usage of do-while loop
public dass DoWhileExample
int i=1;
do
System.out.printini);
i+t
whileic-5;
Output:
1.1.10 Classes
It is a
template or blueprint from which
that determines objects are
how an object
will behave and what created. A class is an entity
contains fields and methods. A the object will contain. A class
method is like function
object. used to expose behavior of a
UNIT ONNE 1.27
Syntax:
class <class_name>
field;
method;
1.1.11 Objects
An object is nothing but a component which consists of methods
self-contained
and properties to make a particular type of data useful. Object determines the
behavior of the class.
A class is a blueprint or prototype that defines the variables and the methods
(functions) common to all objects of a certain kind.
An object is a instance/specimen of a class. Software objects are often used to
// instance variable
private boolean isOn
1/ method
public void turnOn()
isOn= true;
1/ method
public void turnOff()
isOn= false;
1.28
UNIT ON
Here, defined a class named Lamp. The class has one instance
we
variak
(variable defined inside class) isOn and two methods turnOn) and turnOff). The iable
variables and methods defined within a class are called members of the class These
The two keywords, private and public in the above program are
modifiers. The private keyword makes instance variables and methods
acce
which can be accessed only from inside the same class. The public
priva
keyword make-
instance variables and methods public which can be accessed from outside
class.
of the
In the above program, isOn variable is
private whereas turnOn0 and turnOff
methods are public. If you try to access
private members from outside of the class
compiler throws error.
boolean isOn;
void turnOn)
isOn = true;
void turnOff()
isOn= false;
class ClassObjectsExample
the"."operator.
Example-
1.turnOn0
This statement calls turnOn()
method inside Lamp class for 11 object.
within the
the above statement, all statements
When you call the method using back to
the control of program jumps
bodyof turnOn) method are executed. Then,
the statement following 11.turnOn);
class Lamp
void turnOn() (
ison true;
class ClassobjectsExample
main(String[] args) {
public static void
11.turnon();-
as:
can be accessed
instance variable
Similarly, the
inside
12isOn= false;
members c a n be accessed only from
that, the private (outside of the
It is important to
note
lies within the main) method
12.isOn = false;
code
the class. If the will show
error.
boolean isOn;
void turnOn)
isOn= true;
1.30
UNIT
void turnOff)
isOn- false;
void displayLightStatus()
class ClassObjectsExample
Lamp 11 =
new
Lamp(), 12 =new Lamp();
11.turnOn0;
12.turnOff(0
11.displayLightStatus0
12.displayLightStatus0;
Output:
Light on? true
Light on? false
Let us
analyse step by step the above program. Here in the program -
true because
Finally, 11.displayLightStatus();
statement displays Light on?
true for 11 object.
isOn variable holds
because isOn
And, 12.displayLightStatus();
statement displays Light on? false
variable holds false for 12 object
within a class are called instance variable for a reason.
Example
void func0 f.
void func(int a) . .
floatfunc(double a)
{...
floatfunc(int a, float b) have same name
but accept
overloaded. These methods
method is
Here, func()
different arguments. Overloaded methods
same.
methods are not
these
Notice that, the return type of must differ in parameters they
different return type, but they
or not have
may may
accept.
Why method overloading? be
numbers but there can
display(1)
display(1, 4);
Output
Arguments: 1
Arguments: 1 and 4
In above overloaded method
program, first method accepts one argume
whereas other method accepts two arguments.
Overloading by changing datatype of
arguments
Program to show method overloading by changing
arguments
datatype of
class MethodOverloading
display(1);
display('Hello");
Output:
Got Integer data.
that is invoked
a method (but not actually a method)
A constructor is similar to
instantiated.
automatically when
an object is and
and a constructor by its name
class Test
Test0
// constructor body