0% found this document useful (0 votes)
5 views12 pages

Java 2

The document provides an overview of Java operators, including arithmetic, relational, logical, and bitwise operators, along with their functionalities and examples. It also covers variable declarations, data types, type conversion, arrays, and class structures in Java, emphasizing the importance of data types and variable scope. Additionally, it explains the creation of objects and methods within classes, highlighting the encapsulation of data.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views12 pages

Java 2

The document provides an overview of Java operators, including arithmetic, relational, logical, and bitwise operators, along with their functionalities and examples. It also covers variable declarations, data types, type conversion, arrays, and class structures in Java, emphasizing the importance of data types and variable scope. Additionally, it explains the creation of objects and methods within classes, highlighting the encapsulation of data.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Krunal Mahajan

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.

Operator Precedence and Associativity:


()
++ , -- (Right to Left)
! Logical NOT (Right to Left)
* , / , %
+ , -
< , <= , > , >= , == , !=
&& , ||
?: (Right to Left)
= (Right to Left)
op= (Right to Left)
Variables :
 A variable is an identifier that denotes a storage location used to store a data value.
 A variable may take different values at different times during the execution of the program.
 A variable name can be chosen by the programmer of his choice.
 It should be short and in a meaningful way so as to reflect what it represents in the program.
Ex. average , height , amt
Data Types :
 Every variable in Java has a data type.
 Java is a strongly typed language. Every variable has a type, every expression has a type,
and every type is strictly defined. All assignments, whether explicitly or via parameter
passing in method calls, are checked for type compatibility. The Java compiler checks all
expressions and parameters to ensure that the types are compatible. Any type mismatches are
errors that must be corrected. You cannot assign a floating-point value to an integer. It will
help reduce the possibility of errors in your code.
Krunal Mahajan
JAVA 2 Page :3
 Data type specify the size and type of values that can be stored.
Data Types

Primitive (Intrinsic/Built -in) Non-primitive(Derived)

Numeric Non-Numeric Arrays classes Interface

Integer Floating-point Character Boolean

byte short int long float double


Integer types : Java supports four types of integers. They are byte , short , int and long.
 Java does not support the concept of unsigned types and therefore all Java values are signed
meaning they can be positive or negative.
Byte : The smallest integer type is byte.
This is a signed 8-bit (1 byte) type that has a range from –128 to 127.
Variables of type byte are especially useful when you’re working with a stream of data from a
network or file. They are also useful when you are working with raw binary data.
Short : Short is a signed 16-bit (2 byte) type.
It has a range from –32768 to 32767.
int : The most commonly used integer type is int.
It is a signed 32-bit(4 byte) that has a range from –2,147,483,648 to 2,147,483,647.
An integer expression involving bytes,shorts,ints and literal numbers, the entire expression is
prompted to int before the calculation is done.
It can hold whole numbers such as 123 , -96.
Long : Long is a signed 64-bit(8 byte) type and is useful when an int type is not large enough
to hold the desired value. The range of a long is quite large. This makes it useful when big,
whole numbers are needed.
Floating Point Types : They can hold numbers containing fractional parts such as 27.59 and –
1.564. There are two types of float. They are float and double.
float: The float type values are single-precision numbers that uses 32-bits(4 byte) of storage.
double: The double types represent double precision numbers that uses 64-bits(8 byte) of
storage.
 Floating point numbers are treated as double-precision quantities. To force them to be in
single-precision mode , we must append f or F to the numbers. Ex. 1.23F
 All math functions, such as sin(),cos() and sqrt() , return double values.
 When you need to maintain accuracy or manipulate large valued numbers, double is the best
choice.
Character Type :
 It can hold only a single character.
 Java uses Unicode to represent characters. It requires 16 bits.(2 byte).
 The range of a char is 0 to 65536. There are no negative chars.
 You can operate character as integers. This allows you to add two characters , or to increment
the value of a character variable.
Boolean Type:
 It is used when we want to test a particular condition during the execution of the program.
 There are only two values that a Boolean type can take : true or false.
 All comparision operators return Boolean type values.
 Boolean values are often used in selection and iteration statements.
Krunal Mahajan
JAVA 2 Page :4

Type Size(bytes) [Link] [Link]


byte 1 -128 127
short 2 -32768 32767
int 4
long 8
float 4 3.4e-038 3.4e+038
double 8 1.7e-308 1.7e+308
char 2 0 65536
boolean 1
Declaration of Variables :
 Variables are the names of storage locations.
 A Variable must be declared to the compiler before it is used in the program.
 Declaration does three things.
1. It tells the compiler what the variable name is.
2. It specifies what type of data the variable will hold.
3. The place of declaration in the program decides the scope of the variable.
Syntax :
type variable1, variable2, ……. VariableN;
Scope of Variables :
 The area of the program where the variable is accessible is called its scope.
 Java variables are actually classified into three kinds.
1) Instance Variables 2) Class Variables and 3) Local Variables
 Instance and class variables are declared inside a class.
Instance Variables : Instance variables are created when the objects are instantiated and
therefore they are associated with the objects. They take different values for each objects.
Class Variables : Class variables are global to a class and belong to the entire set of objects that
class creates. Only one memory location is created for each class variable.
Local variables: Variables declared and used inside methods are called local variables. They
are called so because they are not available for use outside the method definition.
 Each block can contain its own set of local variable declarations. We cannot, however,
declare a variable to have the same name as one in an outer block.
Symbolic Constant :
We face two problems in the programs. They are
1. Problem in modification of the program.
2. Problem in Understanding the program.
 Assignment of a symbolic name to such constants frees us from these problems. For ex. we
may use the name TOTAL to denote the number of students and MARKS to denote the
pass_marks.
 Constant values are assigned to these names at the beginning of the program. Subsequent use
of the symbolic names will be substituted with the defined value at the appropriate points.
Syntax : final type symbolic_name = value;
Ex. final int TOTAL = 100;
final float PI = 3.14;
 After declaration of symbolic constants, they should not be assigned any other value within
the program by using an assignment statement.
Type Conversion And Casting:
It the two types are compatible, then Java will perform the conversion automatically.
Krunal Mahajan
JAVA 2 Page :5
To obtain a conversion between incompatible types, you must use a cast, which performs an
explicit conversion.
Automatic Conversions:
When one type of data is assigned to another type of variable, an automatic type conversion will
take place if the following two conditions are met.
1) The two type are compatible.
2) The destination type is larger than the source type.
When these two conditions are met, a widening conversion takes place.
(Numeric types , including integer and floating-point types are compatible with each other.
However , the numeric types are not compatible with char or Boolean. Also , char and Boolean
are not compatible with each other.
Java also performs an automatic type conversion when storing a literal integer constant into
variables of type byte,short or long.)
Casting Incompatible Types:
To assign an int value to a byte variable, you must use a cast. This is called narrowing
conversion. Syntax : (target type) value
When a floating-point value is assigned to an integer type, the fractional component is lost.
Type Promotion Rules:
1) First , all byte and short values are prompted to int.
2) Then, if one operand is a long, the whole expression is prompted to long.
3) If one operand is a float operand, the entire expression is prompted to float.
4) If any of the operands is double, the result is double.
Arrays:
An array is a group of like-typed variables that are referred to by a common name. Arrays of any
type can be created and may have one or more dimensions. A specific element in an array is
accessed by its index. Array index start at zero.
One-Dimensional Arrays:
 To create an array, you first must create an array variable of the desired type.
Syntax : type var-name[]; Ex. int a[];
 The array declaration does create actual array. In fact, the value of array is set to null, which
represents an array with no value. To link array with an actual physical array of integers, you
must allocate one using new and assign it to array. new is a special operator that allocates
memory. Thus, in Java all arrays are dynamically allocated.
Array-var = new type[size]; Ex. a=new int[12];
Here, type specifies the type of data being allocated, size specifies the number of elements in the
array.
 The elements in the array allocated by new will automatically be initialized to zero.
 It is possible to combine the declaration of the array variable with the allocation of the array
itself. Ex. int a[]=new int[12]; or int[] a=new int[12];
Arrays can be initialized when they are declared. There is no need to use new.
Ex. int a[]={ 1,2,3,4,5};
The Java run-time system will check to be sure that all array indexes are in the correct range.
Multidimensional Arrays:
They are actually arrays of arrays. Syntax : type array-var[][]=new type[Rows][Cols];
When you allocate memory for a multidimensional array, you need only specify the memory for
the first dimension. You can allocate the remaining dimensions separately. Since
multidimensional arrays are actually arrays of arrays, the length of each array is under your
control. Ex. int twod[][]=new int[3][];
It is possible to initialize multidimensional arrays. To do so, simply enclose each dimension’s
initializer within its own set of curly braces.
Krunal Mahajan
JAVA 2 Page :6
Class:
 Classes create objects and objects use methods to communicate between them.
 Classes provide a convenient method for packing together a group of logically related data
items (fields) and functions (methods) that work on them.
 A class is essentially a description of how to make an object that contains fields and
methods. It provides a sort of template for an object and behaves like a basic data type such
as int. A class is a user-defined data type with a template that serves to define its properties.
Once the class type has been defined, we can create “variables” of that type using
declarations that are similar to the basic type declarations. In Java, these variables are
termed as instances of classes, which are the actual objects.
Syntax :
Class classname
{
[ variable declarations;]
[ methods declarations;] }
Adding Variables:
Data 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 whenever an object of
the class is instantiated. We can declare the instance variables exactly the same way as we
declare local variables.
Adding Methods :
 A class with only data fields has no life. We must therefore add methods that are necessary
for manipulating the data contained in the class.
 Instance variables and methods in classes are accessible by all the methods in the class.
Creating Objects:
 An object in Java is essentially a block of memory that contains space to store all the
instance variables. Creating an object is also referred to as instantiating an object.
 Objects in Java are created using new operator. The new operator creates an object of the
specified class and returns a reference to that object.
 The new allocates memory for an object during run time.
 Syntax : class-var= new classname();
The classname() specifies the constructor for the class.
Ex. Rectangle rect1; // (Rectangle is class ) declare
rect1=new Rectangle( ); // instantiate
rect1 is a reference to Rectangle object.
 Both statements can be combined into one as shown below.
Rectangle rect1 = new Rectangle( );
 The method Rectangle( ) is the default constructor of the class. We can create any number
of objects of Rectangle.
 Each object has its own copy of the instance variables of its class.
 It is also possible to create two or more references to the same object.
Rectangle R1=new Rectangle( );
Rectangle R2=R1;
Both R1 and R2 refer to the same object.
Accessing Class Members:
Syntax : [Link];
[Link](parameter-list);
Access Modifiers : Access modifiers are used to specify the visibility and accessibility of a
class, member variables and methods. Java provides some access modifiers like: public, private
etc.. These can also be used with the member variables and methods to specify their
accessibility.
1. public keyword specifies that the public class, the public fields and the public methods
can be accessed from anywhere.
Krunal Mahajan
JAVA 2 Page :7
2. private: This keyword provides the accessibility only within a class i.e. private fields
and methods can be accessed only within the same class.
3. protected: This modifier makes a member of the class available to all classes in the same
package and all sub classes of the class.
4. default : Its not a keyword. When we don't write any access modifier then default is
considered. It allows the class, fields and methods accessible within the package only.
Parameterized Method:
A parameter is a variable defined by a method that receives a value when the method is called.
An argument is a value that is passed to a method when it is invoked. Parameterized method is
used to initialize different objects with different data values.
Methods Overloading :
 In Java , it is possible to create methods that have the same name, but different parameter
lists and different definitions. This is called method overloading.
 Method overloading is used when objects are required to perform similar tasks but using
different input parameters. When we call a method, Java matches up the method name first
and then the number and type of parameters to decide which one of the definitions to
execute. Java will employ its automatic type conversions only if no exactmatch is found.
 This process is known as polymorphism because it is one way that Java implements the
“one interface, multiple methods”.
The method’s return type does not play any role in this.
Constructors :
 All objects that are created must be given initial values.
 Java supports a special type of method, called a constructor, that enables an objects to
initialize itself when it is created.
 Constructors have the same name as the class itself.
 They do not specify a return type not even void. This is because they return the instance of
the class itself.
 Each class has its own default constructor. It initialize instance variable with zero.
 Each class also has its own copy constructor. Ie. You can assign one object to another
object of the same class type directly.
Parameterized Constructors:
Default constructor initialize all objects with the same values. Parameterized constructor is used
to set different values for different objects when the objects are created.

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.

Using objects as Parameters:


We can pass objects to methods. One of the most common uses of object parameters involves
constructors. You will want to construct a new object so that it is initially the same as some
existing object. to do this, you must define a constructor that takes an object of its class as a
parameter.
There are two ways to pass an argument to a subroutine.
The first way is call-by-value. This method copies the value of an argument into the formal
parameter of the subroutine. Therefore, changes made to the parameter of the subroutine have
no effect on the argument used to call it.
The second way an argument can be passed is call-by-reference. In this method, a reference to
an argument is passed to the parameter. Inside the subroutine, this reference is used to access
the actual argument specified in the call. This means that changes made to the parameter will
affect the argument used to call the subroutine.
In Java , when you pass a simple type to a method, it is passed by value.
When you pass an object to a method, it is passed by reference.
Returning Objects:
A method can return any type of data, including class types that you create.
Recursion :
Recursion is the attribute that allows a method to call itself. A method that calls itself is said to
be recursive. Ex. the computation of the factorial of a number.

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.

Explain Nested Class , Innerclass, static nested class.


Nested Classes
The Java programming language allows you to define a class within another class. Such a
class is called a nested class.
class OuterClass {
...
class NestedClass {
...
}
}
Nested classes are divided into two categories: static and non-static. Nested classes that
are declared static are simply called static nested classes. Non-static nested classes are
called inner classes.
class OuterClass {
...
static class StaticNestedClass {
...
}
class InnerClass {
...
}
}
A nested class is a member of its enclosing class. Non-static nested classes (inner classes)
have access to other members of the enclosing class, even if they are declared private. Static
nested classes do not have access to other members of the enclosing class.

Static Nested Classes


As with class methods and variables, a static nested class is associated with its outer class. And
like static class methods, a static nested class cannot refer directly to instance variables or
methods defined in its enclosing class — it can use them only through an object reference.
Static nested classes are accessed using the enclosing class name:
[Link]
For example, to create an object for the static nested class, use this syntax:
[Link] nestedObject = new
[Link]();

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.

You might also like