Primitive data types/ primary data types
Use of Wrapper classes in Java
Java is an object-oriented programming language, so we need to deal
with objects many times like in Collections, Serialization,
Synchronization, etc. Let us see the different scenarios, where we
need to use the wrapper classes.
o 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.
o 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.
o Synchronization: Java synchronization works with objects in
Multithreading.
o java.util package: The java.util package provides the utility
classes to deal with objects.
o 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.
56.67
56
F=(int)56.57
Autoboxing
o The automatic conversion of primitive data type into its
corresponding wrapper class is known as autoboxing,
o for example, byte to Byte, char to Character, int to Integer, long
to Long, float to Float, boolean to Boolean, double to Double,
and short to Short.
o Since Java 5, we do not need to use the valueOf() method of
wrapper classes to convert the primitive into objects.
Unboxing
The automatic conversion of wrapper type into its corresponding
primitive type is known as unboxing.
It is the reverse process of autoboxing. Since Java 5, we do not need
to use the intValue() method of wrapper classes to convert the
wrapper type into primitives.
intValue(), byteValue(), shortValue(), longValue(), floatValue(),
doubleValue(), charValue(), booleanValue()
Java Inner Classes (Nested Classes)
1. Java Inner classes
2. Advantage of Inner class
3. Difference between nested class and inner class
4. Types of Nested classes
Java inner class or nested class is a class that is declared inside the
class or interface.
We use inner classes to logically group classes and interfaces in one
place to be more readable and maintainable.
Additionally, it can access all the members of the outer class,
including private data members and methods.
Java Member Inner class
A non-static class that is created inside a class but outside a method is
called member inner class. It is also known as a regular inner class.
It can be declared with access modifiers like public, default, private,
and protected.
Syntax:
1. class Outer{
2. //code
3. class Inner{
4. //code
5. }
6. }
Java Anonymous inner class
Java anonymous inner class is an inner class without a name and for
which only a single object is created.
Java Local inner class
Inner class is define in the member function of outer class.