Primitive data types and their wrapper classes
1. The primitive data types are not objects ie: they do not belong to any class, they are defined in the language itself.
2. Wrapper class wraps (encloses) around a data type and gives it an object appearance so wherever, the data type is required as an object, this object can be used.
eg:-
int k = 100; //declaring primitive int
Integer it1 = new Integer(k); //converting primitive int into Integer class
2. Wrapper class wraps (encloses) around a data type and gives it an object appearance so wherever, the data type is required as an object, this object can be used.
eg:-
int k = 100; //declaring primitive int
Integer it1 = new Integer(k); //converting primitive int into Integer class
3. Wrapper classes include methods to unwrap the object and give back the data type.
eg:- int m = it1.intValue(); //returns back to primitive int from Integer object.
Importance of Wrapper classes
—————————
1) To convert simple data types into objects, that is, to give object form to a data type; here constructors are used.
2) To convert strings into data types (known as parsing operations), here methods of type parseXXX() are used.
eg:-
int m =Integer.parseInt(“24”); //converts string numeric value into primitive int
int m =Integer.parseInt(“24”); //converts string numeric value into primitive int
Autoboxing and unboxing
————————-
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on.
If the conversion goes the other way, this is called unboxing.
eg:-
List listNumbers = new ArrayList();
listNumbers.add(7); //In Arraylist primitive data type is automatically converted into Integer class object.
————————-
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on.
If the conversion goes the other way, this is called unboxing.
eg:-
List listNumbers = new ArrayList();
listNumbers.add(7); //In Arraylist primitive data type is automatically converted into Integer class object.
Lets take one more example to understand this:
public class WrapperClass {
public static void main(String[] args) {
int x = 20;
//-----------------------------------------------------
//All 6 number classes extends from abstract class Number
Integer bigX = new Integer(990);
Byte bigByy = new Byte((byte)100);
Short bigSh = new Short((short)11000);
short sh = 4500;
Short bigSh2 = new Short(sh);
Long bigLl = new Long(100000);
Double bigDd = new Double(2345.55);
Integer bigX2 = new Integer("5555");
//------------------------------------------------------
Character ch = new Character('d');
Boolean bigBb = new Boolean(false);
//-------------------------------------------------------
// conversion into class object with valueOf method
Integer bigInt = Integer.valueOf(23);
Double bigDou = Double.valueOf(75.55f);
Long bigLong = Long.valueOf(40000);
//--------------------------------------------------------
Integer bigInt2 = Integer.valueOf("77");
// Integer bigInt3 = Integer.valueOf("abc"); this line will throw NumberFormatException
Double bigDou3 = Double.valueOf("777.77f");
//--------------------------------------------------------------
Integer bigInt5 = Integer.parseInt("777");
// Integer bigInt6 = Integer.parseInt("abcd"); this line will throw NumberFormatException
System.out.println(bigInt5); //answer will be in primitive
//---------------------------------------------------------
//Automatic conversion from small primitive to class object. (auto-boxing)
Integer bigI5 = 900; //new Integer(900);
int smI = bigInt2; //unboxing(class object to primitive)
// any operation occurs in primitive
Integer bigSum = Integer.valueOf(bigI5.intValue() +90);
Integer bigSum2 = bigI5 + 90; //(first unboxing and then boxing)
System.out.println(bigX==bigSum); // checks object equality (false)
System.out.println(bigX.equals(bigSum)); //content equality (true)
}
}
***************************************************************************************
QACult Best Software Testing Courses in Chandigarh tricity.- We love to enhance your knowledge.
QACult is the premier institute catering to the requirements of experienced and fresh pass-out that gives leaders like you a new way to experience Quality engineering—while you work and giving you the flexibility to both advance your career.
Our faculty have 12+ years of industrial experience and have developed many automation testing frameworks in java using TestNG or BDD (cucumber) methodology. We expertise in developing automation testing frameworks in java, python, javascript, php, ruby(WATIR-webdriver & Capybara) and Appium.
please subscribe our channel for more such updates:
Our faculty have 12+ years of industrial experience and have developed many automation testing frameworks in java using TestNG or BDD (cucumber) methodology. We expertise in developing automation testing frameworks in java, python, javascript, php, ruby(WATIR-webdriver & Capybara) and Appium.
please subscribe our channel for more such updates:
And visit for website: www.qacult.com for various blogs and Upcoming Events.
Comments
Post a Comment