0% found this document useful (0 votes)
4 views11 pages

WrapperClasses & Generic in Java

The document explains mutable and immutable objects in Java, detailing wrapper classes that allow primitive data types to be treated as objects. It covers boxing and unboxing processes, as well as auto-boxing and auto-unboxing, with examples demonstrating their usage. Additionally, it introduces generic programming components, including generic variables, methods, classes, and interfaces, with a user-defined generic class example.

Uploaded by

Pravalika Nasu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

WrapperClasses & Generic in Java

The document explains mutable and immutable objects in Java, detailing wrapper classes that allow primitive data types to be treated as objects. It covers boxing and unboxing processes, as well as auto-boxing and auto-unboxing, with examples demonstrating their usage. Additionally, it introduces generic programming components, including generic variables, methods, classes, and interfaces, with a user-defined generic class example.

Uploaded by

Pravalika Nasu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Mutable Objects:
=>The Objects once created can be modified are known as
Mutable Objects.

[Link] Objects:
=>The Objects once created cannot be modified are known as
Immutable Objects, which are also known as Secured Object or
Constant Object.
-----------------------------------------------------------------------------
Wrapper Classes in Java:
=>The pre-defined classes from [Link] package which are used to
make primitive datatypes available in the form of Objects(Wrapper
Class-Objects) are known as Wrapper Classes.
=>Every Primitive datatype will have its own Wrapper Class and
there are 8 Wrapper Classes.
=>The following are the list of Wrapper Classes:
datatype WrapperClass
byte Byte
short Short
int Integer
long Long
float Float
double Double
Character Character
Boolean Boolean

faq:
define Boxing process?
=>The process of binding primitive datatypes into WrapperClass
Objects is known as Boxing
process.
=>The following are the list of constructors from each
WrapperClass:
WrapperClass List-of-Constructors
Byte Byte(byte),Byte(String)
Short Short(short),Short(String)
Integer Integer(int),Integer(String)
Long Long(long),Long(String)
Float Float(float),Float(String),Float(double)
Long Long(long),Long(String)
Character Character(char)
Boolean Boolean(boolean),Boolean(String)

faq:
define UnBoxing process?
=>The process of taking Primitive datatypes Out of WrapperClass
Objects is known as UnBoxing process.
=>We use the following methods to perform UnBoxing process:
public byte byteValue();
public short shortValue();
public int intValue();
public long longValue();
public float floatValue();
public double doubleValue();
public char charValue();
public boolean booleanValue()
----------------------------------------------------------------------------
Diagrams:

Ex:
Program : [Link]
package maccess;
public class DemoWrapperClass1
{
@SuppressWarnings("removal")
public static void main(String[] args)
{
//Boxing process
Integer ob1 = new Integer(12);//Con_call
Integer ob2 = new Integer("23");//Con_call
Float ob3 = new Float(12.34F);
Float ob4 = new Float("23.12F");
Character ob5 = new Character('A');
Boolean ob6 = new Boolean(true);
Boolean ob7 = new Boolean("false");
//UnBoxing process
int i1 = [Link]();
int i2 = [Link]();
float f1 = [Link]();
float f2 = [Link]();
char ch = [Link]();
boolean b1 = [Link]();
boolean b2 = [Link]();
[Link]("------data values-----");
[Link]("int-value from ob1:"+i1);
[Link]("int-value from ob2:"+i2);
[Link]("float-value from ob3:"+f1);
[Link]("float-value from ob4:"+f2);
[Link]("char-value from ob5:"+ch);
[Link]("boolean-value from ob6:"+b1);
[Link]("boolean-value from ob7:"+b2);
}
}

o/p:
------data values-----
int-value from ob1:12
int-value from ob2:23
float-value from ob3:12.34
float-value from ob4:23.12
char-value from ob5:A
boolean-value from ob6:true
boolean-value from ob7:false
-------------------------------------------------------------------
faq:
define AutoBoxing process?
=>The Boxing process which is performed automatically is known
as AutoBoxing process.
=>In AutoBoxing process the NonPrimitive datatype variables are
assigned with primitive
datatype values.
Ex:
Integer ob1 = 12;
Float ob2 = 12.34F;
Character ob3 = 'A';
Boolean ob4 = true;

faq:
define AutoUnBoxing process?
=>The UnBoxing process which is performed automatically is
known as AutoUnBoxing process.
=>In AutoUnBoxing process the primitive datatype variables are
assigned with NonPrimitive datatype variables.
Ex:
int i = ob1;
float f = ob2;
char ch = ob3;
boolean b = ob4;

Ex:
Program : [Link]
package maccess;
public class DemoWrapperClass2 {
public static void main(String[] args) {
//AutoBoxing process
Integer ob1 = 12;
Float ob2 = 12.34F;
Character ob3 = 'A';
Boolean ob4 = true;
//AutonBoxing process
int i = ob1;
float f = ob2;
char ch = ob3;
boolean b = ob4;
[Link]("=====Data values====");
[Link]("int value from ob1 : "+i);
[Link]("float value from ob2 : "+f);
[Link]("char value from ob3 : "+ch);
[Link]("boolean value from ob4 : "+b);
}
}

o/p:
=====Data values====
int value from ob1 : 12
float value from ob2 : 12.34
char value from ob3 : A
boolean value from ob4 : true

=================================================
=============================
Note:
=>All WrapperClass Objects are automatically Immutable Objects
---------------------------------------------------------------------------
faq:
why we have to make Primitive datatypes available in the form of
Objects?
=>Java-Frameworks and Java-Tools will work with Objects,
because of this reason we have to make primitive datatypes
available in the form of Objects.

=================================================
=============================

define Generic Programming Components?


=>The components which are ready to accept any type are known
as Generic Programming Components.
=>The following are some important Generic Programming
Components:
[Link] Variables
[Link] Methods
[Link] Classes
[Link] Interfaces
[Link] Variables:
=>The variables which are ready to accept any type of data are
known as Generic Variables.
=>we use the generic types to declare generic variables:
T - Types
E - Element
K - Key
V - Value
R - Result

[Link] Methods:
=>The methods which are ready to accept any type of data as
parameters are known as
Generic methods.
structure of Generic Methods:
<T>return_type method_name(T)
{
//method_body
}

[Link] Classes:
=>The Class Objects which are ready to hold any type of data are
known as Generic Classes.
Structure of GenericClass:
class Class_name<T>
{
//Class_body
}

[Link] Interfaces:
=>The Interfaces which are implemented to Generic Classes are
known as Generic Interfaces.
Structure of Generic Interfaces:
interface Interface_name<T>
{
//Interface_body
}
------------------------------------------------------------------
Ex:(Demonstrating User defined Generic Class)
ProjectName : App_Generic_Types
test : [Link]
package test;
public class Display<T>
{
public T ob;

public final T getOb() {


return ob;
}

public final void setOb(T ob) {


[Link] = ob;
}

}
maccess : [Link](MainClass)
package maccess;
import [Link];
public class DemoGeneric {
@SuppressWarnings("removal")
public static void main(String[] args) {
Display<Integer> ob1 = new Display<Integer>();
Display<Float> ob2 = new Display<Float>();
Display<Character> ob3 = new Display<Character>();
Display<Boolean> ob4 = new Display<Boolean>();
Display<String> ob5 = new Display<String>();

[Link](new Integer(12));
[Link](new Float(12.34F));
[Link](new Character('A'));
[Link](new Boolean(true));
[Link](new String("NIT"));

[Link]("======Details======");
[Link]("data from ob1 : "+[Link]());
[Link]("data from ob2 : "+[Link]());
[Link]("data from ob3 : "+[Link]());
[Link]("data from ob4 : "+[Link]());
[Link]("data from ob5 : "+[Link]());

}
}

o/p:
======Details======
data from ob1 : 12
data from ob2 : 12.34
data from ob3 : A
data from ob4 : true
data from ob5 : NIT

=================================================
===========

You might also like