Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Q1) i) Primitive Datatypes in Java

ii) Table of Primitive Datatypes


datatype size Range Corresponding Default Value
Wrapper Class
byte 1 byte 0
char 2 byte 0
short 2 byte 0
int 4 byte 0
long 8 byte 0L or 0l
float 4 byte 0.0f or 0.0F
double 8 byte 0.0
char 2 byte '
boolean NA false

Q2)i) Literals in Java


ii) Integral literals in Java
Q3)i) Wrapper classes in Java
1) Wrapper classes wrap primitives into object form so that we can handle primitives just like
objects
2) To define several utility methods for the primitives

1) Float class contains 3 Float f1 = new Float(10.5f);


constructors Float f2 = new Float(10.5);
2) It takes int,float,double,String Float f3 = new Float("10.5f");
values as arguments Float f4 = new Float("10.5f");
3) If string does not represent Float f5 = new Float(10);
any int,float,double value Float f6 = new Float("10");
We get
NumberFormatException

1) Character class contains only Character c1 = new Character('a');// Correct


one constructor Character c2 = new Character("a");// Error
2) It takes char primitive as Character c3 = new Character(97);// Error
argument
1) Boolean class has 2
constructors
2) 1st one takes primitive…2nd
one takes String as argument
3) If we are passing primitive as
argument…then pass only true or
false
If we pass anything else..we
will get C.E
4) If String represents any other
literal other than "true"(Case
Insensitive)
The content becomes true
5) Otherwise the content
becomes false
1) In every Wrapper Class…toString() method is overridden to return content of the object
directly
2) In every Wrapper Class…. .equals() method is overridden for content comparision of Wrapper
Objects

ii) .xxxValue() utility method of Wrapper Class Objects


1) We can use .xxxValue() method to convert Wrapper Class Objects to primitives
2) Every Number type wrapper class contains the following .xxxValue() methods
 public byte byteValue() [Returns a byte value for the corresponding Number type
object]
 public short/int/long short/int/longValue() [Returns a short/int/long primitive value
for the corresponding Number type object]
 public float/double float/doubleValue() [Returns a float/double primitive value for
the corresponding Number type object]

3) In total there are total of 38 ((6*6)+2) .xxxValue() methods

Double d =
System.out
System.out
System.out
System.out
System.out
System.out

1) Character class contains the following method to convert Character object to char primitve Character 
2) public char charValue() System.out

1) Boolean class contains the following method to convert Boolean object to boolean primitve Boolean b1
2) public boolean booleanValue() Boolean b2
Boolean b3
System.out
System.out
System.out

iii) .parseXxx() method [ String  primitive ]


1) public static primitive parseXxx(String s)
2) Every Wrapper class (except Character class) contains this method to convert String to corresponding
primitive type
3) Xxx = Byte,Short,Int,Long,Float,Double,Boolean
4) We get NumberFormatException if the string doesn’t represent a primitive value for the required
Wrapper class

1) public static primitive parseXxx(String s , int radix)


2) Every Integral type wrapper class (Byte,Short,Integer,Long,Float,Double) contains this method
3) Xxx = Byte,Short,Int,Long,Float,Double,Boolean
4) This method converts the specified radix String to the corresonding primitive value
5) We get NumberFormatException if the string doesn’t represent a primitive value for the required
Wrapper class

iv) toString() method of Wrapper Classes [Wrapper Object / primitive


- String ]
1) public String toString()
2) Every Wrapper Class contains the above method to convert Wrapper object to String type
3) It is the overridding method of the Object class toString() method

1) public static String toString(primitive p)


2) Every Wrapper Class contains the above static method to convert primitive to String form
3) It is the overridding method of the Object class toString() method

1) public static String toString(primitive p , int radix)


2) only Integr/Long classes contain the above static method to convert primitive to specified radix
String form
3) It is the overridding method of the Object class toString() method

1) public static String toBinaryString (primitive p)


2) public static String toOctalString (primitive p)
3) public static String toHexString (primitive p)

v) Some more important methods of Wrapper Classes


vi) Partial Hierarchy of Object class

j
b
O
m
u
N
o
B
i
g
n
r
t
S
a
h
C
n
i
r
t
S
t
r
e
n
a
e
ff
u
d
l
i
t
r

1) String,StringBuffer,StringBuilder,all Wrapper Classes are final classes


2) Wrapper classes = children of Number class + Boolean class + Character class
3) Wrapper classes which are not children of Number class are Character and Boolean classes
4) String,Wrapper classes are immutable classes….so String and Wrapper objects are immutable
objects
5) Object class is the root of all Java classes
6) The Object class is present in the java.lang package….The java.lang package is imported by
default…We don’t have to import it explicitly

vii) Concept of Autoboxing and Unboxing


1) Automatic Conversion of primitive value - Wrapper Object by compiler is called
Autoboxing
2) Automatic conversion of Wrapper Object - primitive type by the compiler is called Auto-
unboxing
3) Bcoz of Autoboxing,Autounboxing concepts…after Java 1.5 version we can use wrapper
objects and primitive types interchangeably

1) Automatic Conversion of primitive value -


Wrapper Object by compiler is called Autoboxing
2) Integer I = 10  Integer I = Integer.valueOf (10)
….After compilation line1 is changed to line2 by the
compiler
3) i.e internally Autoboxing concept is implemented by
compiler using valueOf() method

1) Automatic conversion of Wrapper Object - Integer I = new Integer(10)


primitive type by the compiler is called Auto-unboxing int i = I
2) After compilation line2 is changed by compiler as System.out.println(i);//10
follows int i =10  int i = Integer.valueOf(10)
3) i.e internally Auto-unboxing concept is
implemented using valueOf() method

viii) Some Illustrations


public class Test public class Test
{ {
    static Integer I = 0;     static Integer I;
    public static void main()     public static void main()
    {     {
        int m = I; //line 1         int m = I; //line 1
        System.out.println(m);//0         System.out.println(m);//R.E:-NullPointerException
    }     }
} }

1) I is a Wrapper Object whose value 1) I is a Wrapper Object…whose default value is null (I = null)
Is 0 (I=0) 2) after compilation line1 becomes int m = I.intValue()
2) after compilation line1 becomes 3) So we get R.E…NullPointerException
int m = I.intValue()
3) So it works fine

You might also like