0% found this document useful (0 votes)
9 views

Wrapper classes in Java

Uploaded by

Sayan Adhikary
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Wrapper classes in Java

Uploaded by

Sayan Adhikary
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Wrapper classes in Java :- The wrapper class in Java provides the mechanism to convert

primitive into object and object into primitive.


Since J2SE 5.0, autoboxing and unboxing feature convert primitives into objects and objects
into primitives automatically. The automatic conversion of primitive into an object is known as
autoboxing and vice-versa unboxing.
Use of Wrapper classes in Java Java is an object-oriented programming language, so we
need to deal with objects many times like in Collections, Serialization, Synchronization, etc.
Let us see the different scenarios, where we need to use the wrapper classes.
Change the value in Method: Java supports only call by value. So, if we pass a primitive
value, it will not change the original value. But, if we convert the primitive value in an object,
it will change the original value.Serialization: We need to convert the objects into streams to
perform the serialization. If we have a primitive value, we can convert it in objects through
the wrapper classes. Synchronization: Java synchronization works with objects in
Multithreading. java.util package: The java.util package provides the utility classes to deal
with objects.Collection Framework: Java collection framework works with objects only. All
classes of the collection framework (ArrayList, LinkedList, Vector, HashSet, LinkedHashSet,
TreeSet, PriorityQueue, ArrayDeque, etc.) deal with objects only. The eight classes of
the java.lang package are known as wrapper classes in Java. The list of eight wrapper classes
are given below:
Primitive Type//Wrapper class:-
Boolean//Boolean;char//Character;byte//Byte;short//Short;int//Integer;long//Long;float//
Float;double//Double
Autoboxing :- The automatic conversion of primitive data type into its corresponding
wrapper class is known as autoboxing, for example, byte to Byte, char to Character, int to
Integer, long to Long, float to Float, boolean to Boolean, double to Double, and short to Short.
Since Java 5, we do not need to use the valueOf() method of wrapper classes to convert the
primitive into objects.(EX)
Unboxing :- The automatic conversion of wrapper type into its corresponding primitive type
is known as unboxing. It is the reverse process of autoboxing. Since Java 5, we do not need to
use the intValue() method of wrapper classes to convert the wrapper type into primitives.(EX)
Custom Wrapper class in Java :- Java Wrapper classes wrap the primitive data types, that
is why it is known as wrapper classes. We can also create a class which wraps a primitive data
type. So, we can create a custom wrapper class in Java.(EX)
Call by Value and Call by Reference in Java :- There is only call by value in java, not call
by reference. If we call a method passing a value, it is known as call by value. The changes
being done in the called method, is not affected in the calling method.(EX)
Another Example of call by value in java:-In case of call by reference original value is
changed if we made changes in the called method. If we pass object in place of any primitive
value, original value will be changed.(EX)
BorderLayout (LayoutManagers)

Java LayoutManagers
The LayoutManagers are used to arrange components in a particular manner. The Java
LayoutManagers facilitates us to control the positioning and size of the components in GUI
forms. LayoutManager is an interface that is implemented by all the classes of layout
managers. There are the following classes that represent the layout managers:
java.awt.BorderLayout java.awt.FlowLayout java.awt.GridLayout java.awt.CardLayout
java.awt.GridBagLayout javax.swing.BoxLayout javax.swing.GroupLayout
javax.swing.ScrollPaneLayout javax.swing.SpringLayout etc.
Java BorderLayout :- The BorderLayout is used to arrange the components in five regions:
north, south, east, west, and center. Each region (area) may contain one component only. It is
the default layout of a frame or window. The BorderLayout provides five constants for each
region:
public static final int NORTH public static final int SOUTH public static final int
EAST public static final int WEST public static final int CENTER
Constructors of BorderLayout class: BorderLayout(): creates a border layout but with no
gaps between the components. BorderLayout(int hgap, int vgap): creates a border layout
with the given horizontal and vertical gaps between the components.
Example of BorderLayout class: Using BorderLayout() constructor
FileName: Border.java
Java GridLayout
The Java GridLayout class is used to arrange the components in a rectangular grid. One
component is displayed in each rectangle.
Constructors of GridLayout class
GridLayout(): creates a grid layout with one column per component in a row.
GridLayout(int rows, int columns): creates a grid layout with the given rows and columns
but no gaps between the components.
GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout with the
given rows and columns along with given horizontal and vertical gaps.
Example of GridLayout class: Using GridLayout() Constructor
The GridLayout() constructor creates only one row. The following example shows the usage of
the parameterless constructor.
Java FlowLayout:- The Java FlowLayout class is used to arrange the components in a line,
one after another (in a flow). It is the default layout of the applet or panel.
Fields of FlowLayout class :- public static final int LEFT , public static final int
RIGHT, public static final int CENTER, public static final int LEADING, public static
final int TRAILING, Constructors of FlowLayout class
FlowLayout(): creates a flow layout with centered alignment and a default 5 unit horizontal
and vertical gap.
FlowLayout(int align): creates a flow layout with the given alignment and a default 5 unit
horizontal and vertical gap.
FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given alignment
and the given horizontal and vertical gap.
Java BoxLayout Class
public static final int X_AXIS: Alignment of the components are horizontal from left to
right.
public static final int Y_AXIS: Alignment of the components are vertical from top to bottom.
public static final int LINE_AXIS: Alignment of the components is similar to the way words
are aligned in a line, which is based on the ComponentOrientation property of the container. If
the ComponentOrientation property of the container is horizontal, then the components are
aligned horizontally; otherwise, the components are aligned vertically. For horizontal
orientations, we have two cases: left to right, and right to left. If the value
ComponentOrientation property of the container is from left to right, then the components are
rendered from left to right, and for right to left, the rendering of components is also from right
to left. In the case of vertical orientations, the components are always rendered from top to
bottom.
public static final int PAGE_AXIS: Alignment of the components is similar to the way text
lines are put on a page, which is based on the ComponentOrientation property of the
container. If the ComponentOrientation property of the container is horizontal, then
components are aligned vertically; otherwise, the components are aligned horizontally. For
horizontal orientations, we have two cases: left to right, and right to left. If the value
ComponentOrientation property of the container is also from left to right, then the
components are rendered from left to right, and for right to left, the rendering of components
is from right to left. In the case of vertical orientations, the components are always rendered
from top to bottom.
public static final int PAGE_AXIS: Alignment of the components is similar to the way text
lines are put on a page, which is based on the ComponentOrientation property of the
container. If the ComponentOrientation property of the container is horizontal, then
components are aligned.

You might also like