Module3_Chapter4_Core Java
Module3_Chapter4_Core Java
MANUAL V8.3
MODULE CODE:
DL.A.01.01
ANUDIP FOUNDATION
Trainer
Manual Core Java
1
Trainer
Manual Core Java
Chapter 4
2
Trainer
Manual Core Java
Chapter 4
In Java, a wrapper class is a class that has objects containing or wrapping primitive data types. An object created
within a wrapper class has a field capable of storing primitive data types. A wrapper class object can be used to wrap
a primitive value. A wrapper class can be utilized to convert any type of data into an object.
1. Wrapper classes convert primitive data types into objects. Objects are required for modifying arguments
2. Wrapper classes are also necessary as java.util packages can only operate with objects.
4. Collection framework data structures store only objects, but cannot store primitive data types.
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
3
Trainer
Manual Core Java
int c=18;
Integer j=Integer.valueOf(c);
Integer k=c;
}}
Output: 18 18 18
* Integer
The Java integer class (java.lang.Integer) is a wrapper class for the primitive data type ‘int’. It contains methods for
converting ‘int’ values into strings and strings into ‘int’ values. An Integer wrapper class object can hold only
* Integer(int c): Used for creating an integer object with the given value
* Integer(String s): Used for creating an integer object with given string-represented int value
4
Trainer
Manual Core Java
System.out.println(intObj1);
System.out.println(intObj2);
}
}
Output:
15
15
* Float
The Java float class (java.lang.Float) is a wrapper class for the primitive data type ‘float’. It contains methods for
converting ‘float’ values into strings and strings into ‘float’ values. A float wrapper class object can hold only
* Float(float c): Used for creating a float object with given value
* Float(String s): Used for creating a float object with given string-represented parsed float value
5
Trainer
Manual Core Java
import java.util.*;
float f = 12.2f;
}
}
}
Output:
* Double
The Java double class (java.lang.Double) is a wrapper class for the primitive data type ‘double’. It contains
methods for converting ‘double’ values into strings and strings into ‘double’ values. A double wrapper class can
* Double(double c): Used for creating a double object with the given value
* Float(String s): Used for creating a double object with given string-represented parsed double value
6
Trainer
Manual Core Java
double c = 20.20;
System.out.println(cObj1);
System.out.println(cObj3);
}
}
Output:
20.2
45.48
7
Trainer
Manual Core Java
9. int compareTo(int i) - Compares object numerical value to integer i’s value. It returns 0 for equal values, positive
if invoking object has higher value, and negative if invoking object value is lower.
10.static int compare(int num1, int num2) - Compares num1 and num2 values. It returns 0 for equal values,
negative value if num 1 < num 2, and positive value if num 1 > num 2.
11. boolean equals(Object intObj) - Returns a true output if integer object and intObj are equal. Returns false if
10. isNaN(float) - Returns true output if a number is equivalent to the Not-a-Number (NaN) value.
8
Trainer
Manual Core Java
10. isNaN() - Returns a true output if a Double value is Not-a-Number (NaN) value.
14. valueOf(String) - Returns new value for Double initialized to the string-specified value representation
See the example programme for Java Wrapper Class below. Write the same programme for the class WrapperDemo1
with three integers a, b and c, where a, b and c are equal. Show the resulting output.
int c=18;
Integer j=Integer.valueOf(c);
Integer k=c;
9
Trainer
Manual Core Java
}}
10
Trainer
Manual Core Java
Instructions: The progress of students will be assessed with the exercises mentioned below.
a) primitive
b) non-primitive
c) integer
a) arrays
b) objects
c) arguments
a) multithreading
b) single threading
c) core threading
11
Trainer
Manual Core Java
a) Arrays
b) threads
c) strings
5. An Integer wrapper class _________ is capable of holding only one int value.
a) object
b) program
c) structure
a) a clone
b) a decimal
c) an integer
a) int
b) float
c) object
12
Trainer
Manual Core Java
a) float string
b) float thread
c) float object
a) single, doubles
b) double, singles
c) double, strings
a) Value
b) type
c) object
13