MainMenu

Home Java Overview Maven Tutorials

Thursday, 17 October 2019

Wrapper class in Java

Wrapper in java



Wrapper Class In java


Wrapper Class In Java
before this , there is a question
Question :- Java is 100% Object Oriented Programing Language or not?
Answer :- Java is 99.9 % object Oriented Programming Language
It is not 100% Object Oriented Programing language , due to its primitive data Types :-
int, float , char, double etc..
These are derived from C and these are not objects
and 100% object oriented means that every thing should be in form of object.
and primitive data types are not object
for example :-
int data;
here data is not an object, it's a variable.

So in java , we have classes for every data types, we call the as wrapper classes,

Table for primitive data type and wrapper classes

>
Primitive TypeWrapper Class
booleanBOOLEAN
charCharacter
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
for example for int, we have class Integer
primitive int variable
int x = 20;
not if i want to convert this varibale into an object, then i have to write
Integer i = new Interger(x); OR Integer i = new Interger(20);
now i is 20;
This way of converting primitive into object is called as AutoBoxing

Now, how to take out value from Integer Object
Interger i = new Integer(20);
direct value can't be assigned to object so we need to use a method datatypeValue
int j = i.intValue();
now j = 10;
So getting the priitive value from object is called as Unboxing
public class Wrappractice
{
public static void main(String args[])
{
int i = 20;
Interger mydata = new Integer(i); //Autoboxing
System.out.println("value of i is " + i);
int j;
j = mydata.intValue();
System.out.println( "value od j is " +j); //Unboxing
}
}

1 comment:

  1. It would be great if you can explain a scenario where these are actually will be implemented. To change over straightforward information types into objects, that is, to give object structure to an information type; To make a website like naijaloaded and to convert strings into information types (known as parsing operations).This is the thing that you need to explain.If you clarify the contrast between crude information type and information in object structure regarding a java program for use in List,Map and so forth.

    ReplyDelete