You are on page 1of 9

Java data types:

P1: Data types specify the different sizes and values that can be stored in the variable.
There are two types of data types in java-
1)Primitive data types.
2)Non Primitive data types.

P2: Primitive data types : The primitive data types include : int, float, char etc.
Basically these are the data types that can be found in earlier programming languages
like C/C++ and java too has support for them.

Non primitive data types are limited to java only; these are the data types that have
been made specifically for java; these include - class, array and string etc.

P3: About those primitive data types :


Int stands for integer and it ranges from -231 to 231.
Float data type is used for decimal points but it is not very precise.
Double data type is similar to float and is very precise.
Char is a 2 byte data type and holds one character.

P1: why is char having space of 2 bytes? In c++ it is of one byte right?
P3: This is because java uses the UNICODE system and UNICODE defines char as 2
bytes.

P2: These primitive data types are only capable of pass by value and there aren’t many
methods you can apply to them.
So that is why java has introduced some new types of data types called wrapper class
objects.

Wrapper class:
P4: For every primitive type in Java, there is a built-in object type called a wrapper
class. The wrapper class for int is called Integer, and for double it is called Double.
When we create an object to a wrapper class, it contains a field and in this field, we can
store primitive data types. In other words, we can wrap a primitive value into a wrapper
class object.

P5: Primitive wrapper classes are used to create an Object that needs to represent
primitive types in Collection classes (i.e., in the Java API), in the java.util package and
in the java.lang.reflect reflection package. Collection classes are Java API-defined
classes that can store objects in a manner similar to how data structures like arrays
store primitive data types like int, double, long or char, etc.,

P4: arrays store primitive data types while collections actually store objects.Primitive
wrapper classes are not the same thing as primitive types. Whereas variables, for
example, can be declared in Java as data types double, short, int, etc.,

P5: the primitive wrapper classes create instantiated objects and methods that inherit
but hide the primitive data types, not like variables that are assigned the data type
values

P6: so that means, the term Primitive wrapper class does not mean that wrapper
classes are primitive types. It should be understood to be a class that wraps primitive
types. Wrapper classes can be used to store the same value as of a primitive type
variable but the instances/objects of wrapper classes themselves are Non-Primitive.

P1: The Byte, Short, Integer, Long, Float, and Double wrapper classes are all subclasses
of the Number class.The wrapper classes BigDecimal and BigInteger are not one of the
primitive wrapper classes but are immutable.

P6: Yes, We cannot say that Wrapper classes themselves are Primitive types. They just
wrap the primitive types.

Types of Wrapper class:


P7: The Character class of the java.lang package wraps a value of the primitive data
type char. It offers a number of useful class (i.e., static) methods for manipulating
characters. You can create a Character object with the Character constructor.

P8: The Byte class wraps a value of primitive type byte in an object. An object of type
Byte contains a single field whose type is byte.

P7: Short wrapper class is used to create an object version of a primitive short
value.Constructor of Short wrapper class takes a primitive short value and can also take
string equivalent of primitive short value

P9: The Integer class wraps a value of the primitive type int in an object. An object of
type Integer contains a single field whose type is int.In addition, this class provides
several methods for converting an int to a String and a String to an int, as well as other
constants and methods useful when dealing with an int.
P8: The Long class wraps a value of the primitive type long in an object. An object of
type Long contains a single field whose type is long.In addition, this class provides
several methods for converting a long to a String and a String to a long, as well as
other constants and methods useful when dealing with a long.

P9: The Float class wraps a value of primitive type float in an object. An object of type
Float contains a single field whose type is float.In addition, this class provides several
methods for converting a float to a String and a String to a float, as well as other
constants and methods useful when dealing with a float.

P7: Double class is a wrapper class for the primitive type double which contains several
methods to effectively deal with a double value like converting it to a string
representation, and vice-versa. An object of Double class can hold a single double
value.

P8: The Boolean class wraps a value of the primitive type boolean in an object. An
object of type Boolean contains a single field whose type is boolean.

P9:In addition, this class provides many methods for converting a boolean to a String
and a String to a boolean, as well as other constants and methods useful when dealing
with a boolean.

Need of Wrapper class:


P10: Java is an object-oriented programming language, so we need to deal with objects
many times like in Collections, Serialization, Synchronization, etc. Let us talk about the
different scenarios, where we need to use the wrapper classes.

P1: 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.

P10: 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.

P1: Synchronization: Java synchronization works with objects in Multithreading.


java.util package: The java.util package provides the utility classes to deal with objects.
P10: 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

Common methods of Wrapper class:


P2: parseInt(int i) − One thing you guys should know is that Primitive wrapper classes
are immutable. In parsing integer the function returns an integer, given a string
representation of decimal, binary, octal, or hexadecimal (radix equals 10, 2, 8, or 16
respectively) numbers as input.

P3: So basically the thing is...Parsing is to read the value of one object to convert it to
another type. For example you may have a string with a value of "10". Internally that
string contains the Unicode characters '1' and '0' not the actual number 10. The method
Integer. parseInt takes that string value and returns a real number.

P2: yes, This method is used to get the primitive data type of a certain String.
parseInt() is a static method and can have one argument or two.

P3: Okay, so whatever we just discussed was for integer parsing but what about other
types wouldn’t the method change?

P4: Glad you asked that question. I was just about to say that too.
Top-down parser is the parser which generates parse for the given input string with the
help of grammar productions by expanding the non-terminals

P5: i.e. it starts from the start symbol and ends on the terminals. It uses the leftmost
derivation.

P4: Yes, you are right and


Bottom-up Parser is the parser which generates the parse tree for the given input string
with the help of grammar productions by compressing the non-terminals.

P5: it starts from non-terminals and ends on the start symbol. It uses the reverse of
the right most derivation.

P4: Yes, you are right again.

P5: I believe we should now talk about the utility methods. But I would like to ask why
are wrapper classes final?
P10: these classes get special treatment in the language itself - unlike any normal
classes, these are recognized by the compiler to implement auto(un)boxing.
P1: yes and Simply allowing subclasses would already create pitfalls (unwrapping a
subclass, performing arithmetic and wrapping back would change the type).

P5: The objective of Wrapper class is to define several utility methods which are
required for the primitive types. There are 4 utility methods for primitive type which is
defined by Wrapper class:

P4: valueof(): We can use valueOf() method to create Wrapper object for a given
primitive or String. There are 3 types of valueOf() methods:

P5: 1)Wrapper valueOf(String s) : Every wrapper class except Character class contains
a static valueOf() method to create a Wrapper class object for given String.

P4: Wrapper valueOf(String s, int radix) : Every Integral Wrapper class Byte, Short,
Integer, Long) contains the following valueOf() method to create a Wrapper object for
the given String with specified radix. The range of the radix is 2 to 36

P5: Wrapper valueOf(primitive p) : Every Wrapper class including Character class


contains the following method to create a Wrapper object for the given primitive type.

P6: value(): This method is used to convert the value of the current Number object to
the primitive data type specified.Here prefix will represent primitive number data types
(byte, short, int, long, float, double). Each of the primitive data types we mentioned
above contains 6 methods to get the Primitive for the given Wrapper object.
byteValue()
shortValue()
intValue()
longValue()
floatValue()
doubleValue()

P7: The toString() method returns the string representation of the object.
If you print any object, java compiler internally invokes the toString() method on the
object. So by overriding the toString() method, we can get the desired output,i.e.
Values of the object.

P8: Every wrapper class contains the following toString() method to convert Wrapper
Object to String type.

P7: yes and, We can use parse() methods to convert String to primitive. There are two
types of parse() methods:
primitive parse(String s) : Every Wrapper class except character class contains the
following parse() method to find primitive for the given String object. public static
primitive parse(String s);

P8: and the other one is :


parseXxx(String s, int radix) :Every Integral type Wrapper class (Byte, Short, Integer,
Long) contains the following parseXxx() method to convert specified radix String to
primitive.public static primitive parseXxx(String s, int radix);
It takes String as an argument and converts it into primitive type.

Boxing of Wrapper class:


P9: Boxing-converting from primitive data type to corresponding object class object
manually or with some help of a statement that is called normal boxing
if we take a case of boxing like if we take Int x=90, now I want to convert X into
object...we will use a statement “Integer y (ie. any object variable) =
Integer.valueOf(x)”

P10: valueOf is a dedicated method where if you pass a integer or a stream it is going
to return to the integer class object
And if I print this integer y it will be called as boxing as I have converted a normal
variable into an object but with the help of this additional method

P9: 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

P10: When autoboxing happens - when a primitive value is passed as a parameter to a


method that expects an object of the corresponding wrapper class.
And when the primitive value is assigned to a variable of the corresponding wrapper
class.
For example - conversion of int to integer, long to long,double to double

P1: Long back when (prior to Java 5, almost 15 years back) there was no autoboxing
and we, for example, couldn't simply call add(5) on a collection of Integers.
At that time, those primitive values needed to be manually converted to corresponding
wrapper classes and stored in collections.
Today, with autoboxing, we can easily do ArrayList.add(101) but internally Java
converts the primitive value to an Integer before storing it in the ArrayList using the
valueOf() method*.*
P2: Converting an object of a wrapper type to its corresponding primitive value is
called unboxing. For example conversion of Integer to int.

P3: The Java compiler applies unboxing when an object of a wrapper class is:
Passed as a parameter to a method that expects a value of the corresponding primitive
type.
Assigned to a variable of the corresponding primitive type.

P2: In unboxing, the Java compiler automatically converts wrapper class objects into
their corresponding primitive types. For example,Like autoboxing, unboxing can also be
used with Java collections.
When does autoboxing and unboxing happen in Java?

P3: Let's talk about cases with examples, where autoboxing happens.
Case 1: When a method is expecting a wrapper class object but the value that is passed
as parameter is a primitive type. For example in the below code, the method
myMethod() is expecting an object of Integer wrapper class, however we passed a
primitive int type. The program ran fine as compiler does the autoboxing (conversion of
int to Integer)

P1: When at some point of time, you are assigning a primitive type value to an object
of its wrapper class. For example: The below statements are valid because the compiler
does the autoboxing at runtime.

P2: and also when dealing with collection framework classes.

P4: I would like to add:


Unboxing
The Java compiler applies unboxing when an object of a wrapper class is:
Passed as a parameter to a method that expects a value of the corresponding primitive
type.
Assigned to a variable of the corresponding primitive type.

Creating instances of Wrapper class:


P4: Wrapper class a class whose object wraps or contains primitive data types. When
we create an object to a wrapper class, it contains a field and in this field, we can store
primitive data types. In other words, we can wrap a primitive value into a wrapper class
object.
P5: We can use two ways to construct the instance of the Wrapper Classes
Using the constructor of the wrapper class
Using the valueOf() method provided by the Wrapper classes

P6: Can you elaborate the differences between these two methods?

P5: Note that, the instances of the classes point to the memory locations assigned in
the heap and themselves do not hold the value. While we are creating objects with the
help of the constructor of the wrapper class, each time a new memory is allocated in
the heap and the objects point to the different memory locations.

P6:Also it is not so in the case of the valueOf() method as the valueOf() method checks
if any memory is allocated to the same value for that class in the heap. If it finds the
value, then it provides the location of the previously allocated memory to the new
instance and both start pointing to the same memory location in the heap. Hence, on
the comparison, it returns true.

P7: There is actually another method:


Using the concept of AutoBoxing
AutoBoxing is to reduce the efforts to write the valueOf() method each time we are
creating instances, AutoBoxing is implemented. The automatic conversion of primitive
types to the object of their corresponding wrapper classes is known as AutoBoxing.

P8: We were creating wrapper classes until now by using valueOf() method, but it
seems quite lengthy when we can use AutoBoxing. In AutoBoxing, our work is done by
the compiler, i.e. Java compiler in the background would perform the valueOf()
operation and create the instance of it.

P9: The primary advantage of Wrapper Classes is that we need Wrapper objects to
function with collections which is only possible with the help of Wrapper classes.

P10: As the wrapper classes have objects we can store null as a value. We could not
store null in variables of primitive data type. Another crucial point to note is that
primitive data types are more efficient than wrapper class objects and it is essential to
know when to use which

P9: With Java 5.0, additional wrapper classes were introduced in the
java.util.concurrent.atomic package. These classes are mutable and cannot be used as
a replacement for the regular wrapper classes. Instead, they provide atomic operations
for addition, increment and assignment.
P10: The atomic classes are mutable, but have strong memory consistency guarantees
with regard to modifications. So they serve a different purpose from the immutable
wrapper classes.
The real advantage of the Atomic* classes is that they expose an atomic
compare-and-swap method, which can be very useful for implementing lock-free
algorithms.

Conclusion:
P1: 1 - Java classes are convenience classes and they should be treated as such.
2 - If we are to compare the performance benefits obtained between wrapper class and
primitive types, then it is obvious that primitive type must always be used.

P2: Also another important point to keep in mind is that if we are to pass a primitive
value by reference to a function we must use an object reference (wrapper class)
because Java passes primitive types by value to methods.

P3: To summarize all this, wrapper classes form the base of all method invocations. It
also allows Java to manipulate primitive data types without changing their actual
values. A strong concept of all these discussed topics would surely benefit you a lot in
your development journey.

You might also like