Java 2
Java 2
JAVA 2 Page :1
Operators :
Arithmetic Operators :
+ , - , * , / , % (Modulo Division)
For Modulo division , the sign of the result is always the sign of the first operand (the dividend).
It can be applied to the integer and floating point data as well.
Ex. –14 % 3 = -2 , 14 % -3 = 2
14 / 4 = 3 , 15/10.0 = 1.5 , 15/10 = 1
Relational Operators :
< , <= , > , >=
= = (equal to ) , != (Not equal to)
Logical Operators :
&& Logical AND
|| Logical OR
! Logical NOT
An expression which combines two or more relational expressions is termed as a logical
expression or a compound relational expression.
It produces a value of true or false.
Assignment Operators :
=
+= , -= , *= , /= , %=
Increment and Decrement Operators :
++ , --
Conditional Operator:
The character pair ? : is a ternary operator.
Syntax : exp1 ? exp2 : exp3
Exp1 is evaluated first. If it is true (nonzero) , then the expression exp2 is evaluated and
becomes the value of the conditional expression. If exp1 is false, exp3 is evaluated and its
value becomes the value of the conditional expression.
Bitwise Operators :
They are used for manipulation of data at values of bit level. These operators are used for testing
the bits, or shifting them to the right or left.
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ One’s complement
<< Shift left
>> Shift right
The Bitwise NOT: Also called the bitwise complement, the unary NOT operator, ~, inverts all
of the bits of its operand.
The Bitwise AND : It produces a 1 bit if both operand are also 1. A zero is produced in all other
cases.
The Bitwise OR : It combines bits such that if either of the bits in the operands is a 1, then the
resultant bit is a 1.
The Bitwise XOR : It combines bits such that if exactly one operand is 1, then the result is 1.
otherwise, the result is zero.
The Left Shift: It shifts all of the bits in a value to the left a specified number of times.
Syntax : value << num.
For each shift left, the high-order bit is shifted out(lost) and a zero is brought in on the right.
The Right Shift : It shifts all of the bits in a value to the right a specified number of times.
Syntax: : value >> num
Krunal Mahajan
JAVA 2 Page :2
Each time a value is right shifted, the low-order bits are lost.
Each time you shift a value to the right, it divides that value by two- and discards any remainder.
You can take advantage of this for high-performance division by 2.
short-circuit operators : The || and && opearators. These are called short-circuit
operators.
To evaluate X && Y, first evaluate X. If X is false then stop: the whole expression is false.
Otherwise, evaluate Y then AND the two values. This idea is called short-circuit evaluation.
The || OR operator is also a short-circuit operator. Since OR evaluates to true when one or both
of its operands are true, short-circuit evaluation stops with the first true. To evaluate X||Y, first
evaluate X. If X is true then stop: the whole expression is true. Otherwise evaluate Y and OR the
two values.
1. The bitwise AND operator ( & )
(boolean expression1) & (boolean expression2)
To evaluate the above expression, Java first evaluates both boolean
expression1 and boolean expression2. Hence only if both boolean expression1
and Boolean expression2 evaluate to true, the whole expression evaluates
to true.
2. The conditional AND operator ( && )
( boolean expression1 ) && ( boolean expression2 )
Here Java first evaluates boolean expression1, only if it evaluates to true,
boolean expression2 is evaluated. Hence boolean expression2 is not evaluated
if boolean expression1 evaluates to false.
The conditional AND operator, sometimes called the short-circuit operator is
more efficient that the bitwise AND operator. As it saves the processing of
expression2 by first evaluating expression1 and ascertaining that the final
result will be false.
Garbage Collection:
Java handles de allocation of objects automatically. The technique that accomplishes this is
called garbage collection.
It works like this: when no references to an object exist, that object is assumed to be no longer
needed, and the memory occupied by the object can be reclaimed.
Garbage collection occurs sporadically (if at all) during the execution of your program. it will not
occur simply because one or more objects exist that are no longer used.
The finalize() Method:
Sometimes an object will need to perform some action when it is destroyed. For ex. if an object
is holding some non-Java resource such as a file handle or window character font, then you
might want to make sure these resources are freed before an object is destroyed. To handle
such situations, Java provides a mechanism called finalization.
By using finalization, you can define specific actions that will occur when an object is just about
to be reclaimed by the garbage collector.
Inside the finalize() method you will specify those actions that must be performed before an
object is destroyed.
Syntax : protected void finalize()
{
.. }
Krunal Mahajan
JAVA 2 Page :8
Here, the keyword protected is a specifier that prevents access to finalize() by code defined
outside its class.
finalize() is only called just prior to garbage collection. It is not called when an object goes out-
of-scope. This means that you cannot know when –or even if – finalize() will be executed.
Therefore, your program should provide other means of releasing system resources etc, used
by the object. it must not rely on finalize() for normal program operation. The finalize() method
only approximates the function of a destructor.
this keyword : Sometimes a method will need to refer to the object that invoked it. To
allow this, Java defines the this keyword.
this can be used inside any method to refer to the current object. that is , this is always
a reference to the object on which the method was invoked.
If instance variable names and formal parameter names are same then use this to
access the instance variable. Use this to overcome the instance variable hiding.
Ex. Box(double w, double h, double d)
{ this.w=w; this.h=h; this.d=d; }
Instance variable Hiding :
When a local variable has the same name as an instance variable, the local variable hides the
instance variable. Use this to overcome the instance variable hiding.
Static Members:
If we want to define a member that is common to all the objects and accessed without using
a particular object. ie, the member belongs to the class as a whole rather than the objects
created from class. Such members can be defined as follows.
static int count;
static int max(int x,int y);
The members that are declared static are called static members. Since these members are
associated with the class itself rather than individual objects, the static variables and static
methods are often referred to as class variables and class methods in order to distinguish
them from their counterparts, instance variables and instance methods.
Static variables are used when we want to have a variable common to all instances of a
class.
Krunal Mahajan
JAVA 2 Page :9
Like static variables , static methods can be called without using the objects. They are also
available for use by other classes.
The static methods are called using class names. In fact, there is no need to create objects.
Static methods have several restrictions.
1. They can only call other static methods.
2. They can only access static data.
3. They cannot refer to this or super in any way.
Static Block :
It is used to initialize static variables. It is executed only once when the class is loaded.
Syntax : static
{ }
Inheritance :
Java provides the facility of reusability of the class through Inheritance.
It is the process by which objects of one class acquire the properties of objects of another class.
Once a class has been created, new class can be created by reusing the properties of the existing
one. The mechanism of deriving a new class from an old one is called inheritance. The old class
is referred to as the base class or super class and the new one is called the derived class or
subclass. A class that is inherited is called superclass. The class that does inheriting is called a
subclass. Subclass inherits all of the instance variables and methods defined by the superclass
and add its own, unique elements. A subclass cannot access those members of the superclass that
have been declared as private.
To inherit a class, the keyword extends is used.
The general form of a class declaration that inherits a superclass is
Class subclass_name extends superclass_name
{
}
when subclass become superclass for another subclass then it is called multilevel inheritance.
You can build hierarchies that contain as many layers of inheritance as you like.
No class can be a superclass of itself.
You can only specify one superclass for any subclass that you create. Java does not support the
multiple inheritance means a class can not have more than one superclass.
A reference variable of a superclass can be assigned a reference to any subclass derived from that
superclass. Reference is the type of the reference variable – not the type of the object that it
refers to. It determines what members can be accessed. That is, when a reference to a subclass
object is assigned to a superclass reference variable, you will have access only to those parts of
the object defined by the superclass. (because superclass has no knowledge of what a subclass
adds to it.)
Super Keyword: It has two general forms.
The first calls the superclass’s constructor. Syntax : super(parameter-list);
When a subclass needs to refer to the superclass immediately above the calling class , it can do
so by use of the keyword super. Super() always refers to the superclass immediately above the
calling class. This is true even in a multilevel hierarchy.
Super() must always be the first statement executed inside a subclass’s constructor. Since
constructors can be overloaded, super() can be called using any form defined by the superclass.
The constructor executed will be the one that matches the arguments.
The second use of super keyword is to access a member of the superclass that has been hidden by
a member of a subclass by the same name in the superclass. Syntax : [Link].
Here , member can be either a method or an instance variable.
Krunal Mahajan
JAVA 2 Page :10
In a class hierarchy , constructors are called in order of derivation , from superclass to subclass.
Further super() must be the first statement executed in a subclass’s constructor, this order is same
whether or not super( ) is used. If super( ) is not used, then the default constructor of each
superclass will be executed.
Method Overriding : In a class hierarchy , when a method in a subclass has the same name and
type signature as a method in its superclass, then the method in the subclass is said to override
the method in the superclass. When an overridden method is called from within a subclass, it will
always refer to the version of that method defined by the subclass.
The version of the method defined by the superclass will be hidden. If you wish to access the
superclass version of an overridden method , you can do so by using super.
Method overriding occurs only when the names and the type signatures of the two methods are
identical. If they are not, then the two methods are simply overloaded.
Dynamic Method Dispatch : It is the mechanism by which a call to an overridden method is
resolved at run time rather than compile time. Java implements run-time polymorphism using
dynamic method dispatch.
A superclass reference variable can refer to a subclass object. Java uses this fact to resolve calls
to overridden methods at run time.
When an overridden method is called through a superclass reference, Java determines which
version of that method to execute based upon the type of the object being referred to at the time
the call occurs. Thus, this determination is made at run time. When different types of objects are
referred to , different versions of an overridden method will be called. In other words, it is the
type of objects being referred to (not the type of reference variable) that determines which
version of an overridden by a subclass, then when different types of objects are referred to
through a superclass reference variable, different versions of the method are executed.
Interface :
Java does not support Multiple Inheritance. That is, classes in Java cannot have more than one
superclass. Java provides an alternative approach known as interfaces to support the concept of
multiple inheritance.
Although a class cannot be a subclass of more than one superclass, it can implement more than
one interface.
Defining Interface:
An interface is basically a kind of class. Interfaces contain 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 any code to implement these methods and data fields
contain only constants.
o The scope of variables and methods in an interface is public by default.
o We can not create an instance of an interface.
The syntax for defining an interface is very similar to that for defining class.
interface <name>
{
variable declaration;
method declaration;
}
Here , interface is the keyword. All the variables are treated as constants and static although the
keyword final and static are not present.
Implementing Interface : Interfaces are used as superclasses whose properties are inherited by
classes. It is therefore necessary to create a class that inherits the given interface.
The implements keyword is used to inherit the interface. This is done as follows.
class class_name implements interface_name
Krunal Mahajan
JAVA 2 Page :11
{
body of class
}
This shows that a class can extend another class while implementing interfaces.
When a class implements more than one interface, they are separated by a comma.
The class that implements interface must define the code for the methods.
Extending Interface:
Like classes, interfaces can also be extended. That is , an interface can be subinterfaced from
other interfaces. The new subinterface will inherit all the members of the superinterface in the
manner similar to subclasses.
This is achieved using the keyword extends.
Inner Classes
As with instance methods and variables, an inner class is associated with an instance of its
enclosing class and has direct access to that object's methods and fields. Also, because an inner
class is associated with an instance, it cannot define any static members itself.
Krunal Mahajan
JAVA 2 Page :12
Objects that are instances of an inner class exist within an instance of the outer class. Consider
the following classes:
class OuterClass {
...
class InnerClass {
...
}
}
An instance of InnerClass can exist only within an instance of OuterClass and has direct
access to the methods and fields of its enclosing instance. To instantiate an inner class, you must
first instantiate the outer class. Then, create the inner object within the outer object.
Wrapper Class: : Wrapper class is a wrapper around a primitive data type. It represents
primitive data types in their corresponding class instances e.g. a boolean data type can be
represented as a Boolean class instance. All of the primitive wrapper classes in Java are
immutable i.e. once assigned a value to a wrapper class instance cannot be changed
further. Wrapper Classes are used broadly with Collection classes in the [Link]
package. Following table lists the primitive types and the corresponding wrapper classes:
Primitive Wrapper
boolean [Link]
byte [Link]
char [Link]
double [Link]
float [Link]
int [Link]
long [Link]
short [Link]
void [Link]
Features Of the Wrapper Classes
All the methods of the wrapper classes are static.
The Wrapper class does not contain constructors.
Once a value is assigned to a wrapper class instance it can not be changed, anymore.
Wrapper Classes : Methods
There are some of the methods of the Wrapper class which are used to manipulate the data.
1. add(int, Object): To insert an element at the specified position.
2. add(Object): To insert an object at the end of a list.
3. addAll(ArrayList): To insert an array list of objects to another list.
4. get(): To retrieve the elements contained with in an ArrayList object.
5. size(): To get the dynamic capacity of a list.
7. remove(): To remove an element from a particular position specified by a index value.
8. set(int, Object): To replace an element at the position specified by a index value.