• Sat. Nov 23rd, 2024

Wrapper Class in Java 

Byskkumar199650

May 15, 2022
wrapper_classes

Java is 99% OOP   ,Java is oop language and as said everything in java is an object.

(But What about the primitives?)

They are sort of left out in the world of objects ,that is ,they cannot paticipate in the object activites

Wrapper Classes

As a solution to this problem ,Java allows you to include the primitives in the family of objects by using What are called wrapper classes.

There is a wrapper class for every primitive data type in java.

This class encapsulates a single value for the primitive data type

For instance the wrapper class for int is Integer,for float is Float,and so on.

Primitive type->Wrapper Class

boolean->Boolean

byte->Byte

char->Character

short->Short

int->Integer

long->Long

float->Float

double->Double

Useful methods of wrapper class

1.valueOf()

 static method

Return the Object reference of relative wrapper class

2.parseXxx()

static method

Xxx can be replaced by any primitve type

It returns xxx type value

3.xxxValue()

Instance method of wrapper class

Xxx can be replaced by any primitive type

Returns corresponding primitive type

Example:

public class Example

{

public static void main(String[] args)

{

Integer i1=Integer.valueOf(“123”);

Integer i1=Integer.valueOf(“1010101”,2);

Double d1=Double.valueOf(“3.14”);

int a=Integer.parseInt(“123”);

double b=Double.parseDouble(“13.45”);

int c=i1.intValue();

}

}

                        Static member in java

Static Variables:

Static variables are declared in the class usingStatic keywordStatic variables are by default initialized to its default valueStatic variables has a single copy for the whole class and does not depend on the objects

Static Function

Static functions defined inside the class are qualified with the keyword staticStatic functions can only access static members of the same classStatic function can be invoked using class name and dot operator

Static Class:

We have a class inside a class Which is Known as inner ClassInner class can be qualified with the keyword static.

Example:

public class Example

{

int x://instance member variable

static int x;// static member variable

public void fun1(){}//instance member 

public static void fun2(){y=4}// static member function}

static class Test

{

public static String country=”SKLEARNING”;

}

public static void main(String []args)

{

Example ex1=new Example();

Example ex2=new Example();

Example.y=5;//we also write y=5,because it is in same class

Example.Fun2();

Example.Test.country;

}

}

By skkumar199650

Highly skilled and dedicated Software Developer with over 5 years of experience in developing and debugging applications. Proficient in a wide range of technologies, including .NET Technology, Core Java, MS SQL Server, AWS, Liferay, Angular, Ionic, and Azure, Power BI,SSRS,SSIS. I am passionate about delivering robust and scalable software solutions that meet the highest standards. I thrive in collaborative environments, leveraging my strong analytical and problem-solving skills to drive successful outcomes.