Operators
Operators
operator is a Symsmi
Logical manipulations. a;
3. Logical Operators. 1
4. Assignment Operators.
Operators. '
6. Conditional Operators.
8- Special Operators.
-, *, /, 0/0.
Ex : a + b
a-b
a*b
ta/b_
a%b
Ex : a < b
a<=b
a>b
/a>=b
a==b
al=b
Ex: (a>b)&&(c<d).
:,-=,*=', | :_
Ex: ++m(or)m++:m+1.
follows.
Exp1?Exp2:Exp3;
x=a;
else
x=b;
7. Bit Wise Operators : Bit Wise Operators are used for testing
the bits (or) shifting them to the left (or) right. These Operators
v' V Separator
_ ; terminates statements
g ~ Selects a field or method from an object; Separates package names from sub_
Example :
int i=5;
Example:
Comments*/
/* text */ g
, The compiler ignores this kind of comment, .just like it ignores comments that use /**
l l Example: '_ 4 v
and join() are two different methods to check whether a thread has finished its execution
The isAlive() method return true if the thread upon which it iS Called lS Stlll running
The join() method waits until the thread on which it is called terminates.
Using join() method, we tell our thread to wait until the specified thread completes its
execution. There are overloaded version of join method, which allows us to specify
time for which you want to wait for the specified thread to terminate.
Example program: `
import java.|ang.*;
Thread t = Thread.currentThread();
System.out.print(t.getName());
t.start();
Suspend() method is used to suspend any thread from critical section on specified
time.
The suspended thread will be in blocked state until resume() method is called on it.
These methods are deprecated, as when not used with precautions, the thread locks, if
.H i v i \ 1 “UuimiviiquU
ANSWER KEY
PART - A
Ans: Java compiler generates an architecture-neutral object file format Which makes the
compiled code to be executable on many processors, with the presence of Java runtime
system.
command line. The command line arguments are stored as string array passed to main(). For
example the following program displays all ofthe command line arguments.
System.out.println ( “args[“+i+”]:”+args[i]);
}_
Output :
agrs [1]: is
Ans: There may be a situation when we need to execute a block of code several number of
Java has very flexible three looping mechanisms. You can use one of the following three
loops:
l while Loop
I do...while Loop
I for Loop
while loop:
A while loop is a control structure that allows you to repeat a task a certain number of times.
Syntax:
While (Boolean_expression)
//Statements -
},
When executing, ifthe boolean_expression result is true, then the actions inside the loop will
Here, key point ofthe while loop is that the loop might not ever run. When the expression is
tested and the result is false, the loop body will be skipped and the first statement after the
do...while loop:
A do...while loop is similar to a while loop, except that a do...while loop iS guaranteed t0
Syntax:
do
//Statements
}while (Boolean_expression);
Notice that the Boolean expression appears at the end of the loop, so the statements in the
loop execute once before the Boolean is tested. If the Boolean expression is true, the flow of
control jumps back up to do, and the statements in the loop execute again. This process
for loop:
A for loop is a repetition control structure that allows you to efficiently write a loop that
needs to execute a specific number oftimes. A for loop is useful When you know how many
Syntax:
for(initializationgBoolean_expression; update)
//Statements
Ans: The break statement terminates the labelled statement; it does not transfer the flow of
Control to the label. Control flow is transferred to the statement immediately following the
This means you can only break loops that are currently being executed.
first:
second:
class MultithreadingDemo implements Runnable{
tobj.start();
Output:
o This is the second way of creating a thread. Here we need to create a new class that
° The class should override the run() method which is the entry point for the new thread.
Example :
}
public static void main(String args[]){
obj.start();
Output:
Ans: Interface: An interface is basically a kind of class like classes, interfaces contains
methods and variables but with a major difference. The difference is that interfaces define
only abstract methods and final fields .this means that interfaces do not specify the any code
if? _
b. adding variablesgData is encapsulated in a'class by placing data fields inside the body of
the class definition. These variables are called`instance variables. Because they are created
when ever an object ofthe class is instantiate§ We can declare the instance variables exactly
{»__, ___! V
int length;
int width;
}
The class Rectangle contains two integer type instance variablesDIt is allowed to declare them
in one line as
Adding methodsQA class with only data fields has no life. The objects created by such a
class cannot respond to any messages) We must there fore add methods that are necessary for
manipulating the data contained in the class. Methods are declared inside the body of the
class but immediately after the declaration of instance variables. The general form of a
method body;
creating objects is also referred to as instantiating an object. Objects in java are created using
the new operator. The new operator creates an object of the specified class and returns a
Example:
Rectangle rectl;
The first statement declares a variable to hold the object reference and the second one
actually assigns the object reference to the variable. The variable rectl is now an object of the
rectangle class.
7;;[0w to create multiple threads in java? Explain with an example java program.
W!!
° One way of creating a thread is to create a class that implements the Runnable
° This run method is the entry point for the thread and thread will be alive till run
° Once the thread is created it will start running when start() method gets called.
Output:
o This is the second way of creating a thread. Here we need to create a new class that
° The class should override the run() method which is the entry point for the new thread
as described above.
Example :
obj.start(); v
Output:
Ans: Interface: An interface is basically a kind of class like classes, interfaces contains
methods and variables but with a major difference. The difference is that interfaces define
only abstract methods and final fields .this means that interfaces do not specify the any code
lt is the responsibility ofthe class that implements an interface to define the code for
interface lnterfacename
Variable declaration;
Methods declarations;
}i
Here interface is the keyword and interface name is any valid java variable.
Example:
interface Area
{
}_
return (x*y);
{t
return (pi*x*X);
class InterfaceTest
Area area;
area = rect;
area =cir;
1. Byte Streams.
2. Character Streams.
I.
l SequencelnputStream
, ByteArrayInputStream
m,
..
FIG 5.3 :
Hierarchies forFileOutputStream
l FileOutputStream `
ByteArrayOutputStream
l DataOutputStream '
l PipedOutputStream
ObjectOutputStream
l l FilterOutputStream
I BufferedOutputStream
output as characters. There are two types of abstract classes Reader and Writer_
Reader Hierarchy:
,
¥
.o
FIG 5.5 : _ `
Writer Hierarchy: ` y 5 a
0>
*m
*w
'5