unit1-part2
unit1-part2
UNIT-I
OPERATORS:
Arithmetic Operators
Relational Operators
Bitwise Operators
Logical Operators
Assignment Operators
Misc Operators
A = 0011 1100
B = 0000 1101
-----------------
~A = 1100 0011
(A & B) will
Binary AND Operator copies a bit to the
& give 12 which
result if it exists in both operands.
is 0000 1100
(A | B) will
Binary OR Operator copies a bit if it exists
| give 61 which
in either operand.
is 0011 1101
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
(A ^ B) will
Binary XOR Operator copies the bit if it is
^ give 49 which
set in one operand but not both.
is 0011 0001
C <<= 2 is
<<= Left shift AND assignment operator same as C = C
<< 2
C >>= 2 is
>>= Right shift AND assignment operator same as C = C
>> 2
C &= 2 is same
&= Bitwise AND assignment operator
as C = C & 2
Misc Operators:
Conditional Operator ( ? : ):
Value of b is : 30
Value of b is : 20
instanceof Operator:
true
This operator will still return true if the object being compared is
the assignment compatible with the type on the right. Following is
one more example:
class Vehicle
{
public class Car extends Vehicle {
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
true
Precedence of Java Operators:
if Statement:
Syntax: if (condition)
statement1;
else
statement2;
class BiggestNo
{ int a=5,b=7,c=6;
else if ( b > c)
else
Output:
D:/kumar>javac BiggestNo.java
D:/kumar>java BiggestNo
b is big
Switch Statement:
break;
break;
………….…..
break;
executed.
class ColorDemo
switch (color)
}
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
Output:
D:/kumar>javac ColorDemo.java
D:/kumar>java ColorDemo
red
while Loop:
statements;
class Natural
{ int i=1;
{ System.out.print (i + ―\t‖);
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
i++;
Output:
D:/kumar>javac Natural.java
D:/kumar>java Natural
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
do-while Loop:
Syntax: do
statements;
} while (condition);
class Natural1
{ int i=1;
do
{ System.out.print (i + ―\t‖);
i++;
Output:
D:/kumar>javac Natural1.java
D:/kumar>java Natural1
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
for Loop:
{ statements;
class Natural2
{ int i;
System.out.print (i + ―\t‖);
}
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
Output:
D:/kumar>javac Natural2.java
D:/kumar>java Natural2
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
D:/kumar>
break:
Ø break can be used inside the switch block to come out of the
switch block.
class BreakDemo
{ boolean t = true;
first:
second:
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
third:
Output:
D:/kumar>javac BreakDemo.java
D:/kumar>java BreakDemo
D:/kumar>
continue:
Syntax: continue;
class Natural
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
{ int i=1;
while (true)
{ System.out.print (i + ―\t‖);
i++;
if (i <= 20 )
continue;
else
break;
Output:
D:/kumar>javac Natural.java
D:/kumar>java Natural
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
D:/kumar>
return statement:
//Demonstrate return
class ReturnDemo
{ boolean t = true;
if (t)
return;
Output:
D:/kumar>javac ReternDemo.java
D:/kumar>java ReturnDemo
Introducing Classes:
Class Fundamentals:
When you define a class, you declare its exact form and nature.
You do this by specifying the data that it contains and the code
that operates on that data. While very simple classes may contain
only code or only data, most real-world classes contain both. As
you will see, a class‘ code defines the interface to its data.
class classname {
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list) {
// body of method
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
type methodname2(parameter-list) {
// body of method
// ...
type methodnameN(parameter-list) {
// body of method
Notice that the general form of a class does not specify a main(
)method. Java classes do not need to have a main( ) method. You
only specify one if that class is the starting point for your program.
Further, applets don‘t require a main( ) method at all.
The Bicycle class uses the following lines of code to define its
fields:
The fields of Bicycle are named cadence, gear, and speed and are
all of data type integer (int). The public keyword identifies these
fields as public members, accessible by any object that can access
the class.
A Simple Class:
Let‘s begin our study of the class with a simple example. Here is a
class called Box that defines
class Box {
double width;
double height;
double depth;
}
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
class BoxDemo2 {
double vol;
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
instance variables */
mybox2.width = 3;
mybox2.height = 6;
mybox2.depth = 9;
Volume is 3000.0
Volume is 162.0
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
To actually create a Box object, you will use a statement like the
following:
For example, to assign the width variable of mybox the value 100,
you would use the following statement:
mybox.width = 100;
This statement tells the compiler to assign the copy of width that is
contained within the mybox object the value of 100.
following output:
Volume is 3000.0
Volume is 162.0
This means that if you have two Box objects, each has its own
copy of depth, width, and height. It is important to understand
that changes to the instance variables of one object have no effect
on the instance variables of another.
Declaring Objects
Box b2 = b1;
Although b1 and b2 both refer to the same object, they are not
linked in any other way.
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
Box b2 = b1;
// ...
b1 = null;
Here, b1 has been set to null, but b2 still points to the original
object.
Methods :
Naming a Method
run
runFast
getBackground
getFinalData
compareTo
setX
isEmpty
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
Any method declared void doesn't return a value. It does not need
to contain a return statement, but it may do so. In such a case, a
return statement can be used to branch out of a control flow block
and exit the method and is simply used like this:
return;
If you try to return a value from a method that is declared void,
you will get a compiler error.
returnValue;
The data type of the return value must match the method's
declared return type; you can't return an integer value from a
method declared to return a boolean.
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
Parameters:
Parameter Types
Parameter Names
Constructors:
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
You can rework the Box example so that the dimensions of a box
are automatically initialized when an object is constructed. To do
so, replace setDim( ) with a constructor.
class Box {
double width;
double height;
double depth;
Box() {
System.out.println("Constructing Box");
width = 10;
height = 10;
depth = 10;
double volume() {
class BoxDemo6 {
double vol;
vol = mybox1.volume();
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
vol = mybox2.volume();
Constructing Box
Constructing Box
Volume is 1000.0
Volume is 1000.0
As you can see, both mybox1 and mybox2 were initialized by the
Box( ) constructor when they were created. Since the constructor
gives all boxes the same dimensions, 10 by 10 by 10, both
mybox1 and mybox2 will have the same volume. The println( )
statement inside Box( ) is for the sake of illustration only.
Parameterized Constructors
As you can probably guess, this makes them much more useful.
For example, the following version of Box defines a parameterized
constructor that sets the dimensions of a box as specified by those
parameters. Pay special attention to how Box objects are created.
class Box {
double width;
double height;
double depth;
width = w;
height = h;
depth = d;
double volume() {
}
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
class BoxDemo7 {
double vol;
vol = mybox1.volume();
vol = mybox2.volume();
Volume is 3000.0
Volume is 162.0
the values 10, 20, and 15 are passed to the Box( ) constructor
when new creates the object. Thus, mybox1‘s copy of width,
height, and depth will contain the values 10, 20, and 15,
respectively.
Overloading Constructors:
What if you simply wanted a box and did not care (or know) what
its initial dimensions were? Or, what if you want to be able to
initialize a cube by specifying only one value that would be used
for all three dimensions?
As the Box class is currently written, these other options are not
available to you.
*/
class Box {
double width;
double height;
double depth;
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
width = w;
height = h;
depth = d;
Box() {
Box(double len) {
double volume() {
class OverloadCons {
double vol;
vol = mybox1.volume();
vol = mybox2.volume();
vol = mycube.volume();
this.depth = d;
}
This version of Box( ) operates exactly like the earlier version. The
use of this is redundant, but perfectly correct. Inside Box( ), this
will always refer to the invoking object.
Finalize () method:
The finalize() method is invoked each time before the object is
garbage collected. This method can be used to perform cleanup
processing. This method is defined in System class as:
A Stack Class
As we have seen, the class is the mechanism by which
encapsulation is achieved in Java. By creating a class, you are
creating a new data type that defines both the nature of the data
being manipulated and the routines used to manipulate it. Further,
the methods define a consistent and controlled interface to the
class‘ data. Thus, you can use the class through its methods
without having to worry about the details of its implementation or
how the data is actually managed within the class. In a sense, a
class is like a ―data engine.‖ No knowledge of what goes on inside
the engine is required to use the engine through its controls. In
fact, since the details are hidden, its inner workings can be
changed as needed. As long as your code uses the class through
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
18
17
16
15
14
13
12
11
10
As you can see, the contents of each stack are separate.
One last point about the Stack class. As it is currently
implemented, it is possible for the array that holds the stack, stck,
to be altered by code outside of the Stack class. This leaves
Stack open to misuse or mischief. In the next chapter, you will see
how to remedy this situation.
Overloading Methods
Suppose that you have a class that can use calligraphy to draw
various types of data (strings, integers, and so on) and that
contains a method for drawing each data type. It is cumbersome to
use a new name for each method—for example, drawString,
drawInteger, drawFloat, and so on. In the Java programming
language, you can use the same name for all the drawing methods
but pass a different argument list to each method. Thus, the data
drawing class might declare four methods named draw, each of
which has a different parameter list.
You cannot declare more than one method with the same name
and the same number and type of arguments, because the
compiler cannot tell them apart.
parameter passing:
As you will see, Java uses both approaches, depending upon what
is passed.
class Test {
i *= 2;
j /= 2;
class CallByValue {
ob.meth(a, b);
As you can see, the operations that occur inside meth( ) have no
effect on the values of a and b used in the call; their values here
did not change to 30 and 10.
Keep in mind that when you create a variable of a class type, you
are only creating a reference to an object. Thus, when you pass
this reference to a method, the parameter that receives it will refer
to the same object as that referred to by the argument. This
effectively means that objects are passed to methods by use of
call-by-reference. Changes to the object inside the method do
affect the object used as an argument.
class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
// pass an object
void meth(Test o) {
o.a *= 2;
o.b /= 2;
class CallByRef {
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
ob.meth(ob);
System.out.println("ob.a and ob.b after call: " +ob.a + " " + ob.b);
As you can see, in this case, the actions inside meth( ) have
affected the object used as an argument.
Recursion:
class Factorial {
int fact(int n) {
int result;
if(n==1) return 1;
result = fact(n-1) * n;
return result;
class Recursion {
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
Access Modifiers:
1. private
2. default
3. protected
4. public
1.private:
2.default:
3.protected:
4.public:
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
UNDERSTANDING STATIC:
class Student9{
int rollno;
String name;
static String college = "ITS";
s1.display();
s2.display();
s3.display();
}
}
Test it Now
Output:111 Karan BBDIT
222 Aryan BBDIT
333 Sonoo BBDIT
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
class OuterClass {
...
class NestedClass {
...
}
}
Nested classes are divided into two categories: static and
non-static. Nested classes that are declared static are called static
nested classes. Non-static nested classes are called inner classes.
class OuterClass {
...
static class StaticNestedClass {
SREE RAMA ENGINEERING COLLEGE OOP’S PROGRAMMING THROUGH JAVA CSE
...
}
class InnerClass {
...
}
}
Non static nested class is also known as inner class. It has access
to all variables and methods of outer class and may refer to them
directly. But the reverse is not true, that is outer class cannot
directly access members of inner class. One more thing is an inner
class must created and instantiated within the scope of outer class
only.
args[5]: -1
for(int x : v)
System.out.print(x + " ");
System.out.println();
}
public static void main(String args[])
{
// Notice how an array must be created to
// hold the arguments.
int n1[] = { 10 };
int n2[] = { 1, 2, 3 };
int n3[] = { };
vaTest(n1); // 1 arg
vaTest(n2); // 3 args
vaTest(n3); // no args
}
}