Beruflich Dokumente
Kultur Dokumente
The
eight primitive data types byte, short, int, long, float, double, char and boolean are not objects.
A Wrapper class is a class whose object wraps or contains a primitive data types.. In other words, we
can wrap a primitive value into a wrapper class object.
1. They convert primitive data types into objects. Objects are needed if we wish to
modify the arguments passed into a method (because primitive types are passed by
value).
2. The classes in java.util package handles only objects and hence wrapper classes help
in this case also.
3. Data structures in the Collection framework, such as ArrayList and Vector, store only
objects (reference types) and not primitive types.
4. An object is needed to support synchronization in multithreading.
5.
Example:
Output:
Values of Wrapper objects (printing as objects)
Byte object byteobj: 1
Integer object intobj: 10
Float object floatobj: 18.6
Double object doubleobj: 250.5
Character object charobj: a
Unwrapped values (printing as data types)
byte value, bv: 1
int value, iv: 10
float value, fv: 18.6
double value, dv: 250.5
char value, cv: a
Integer class
Constructors :
Parameters
arg : integer argument whose Binary representation we want
Return
Binary representation of the argument.
2. bitcount() : java.lang.Integer.bitCount()
method converts the integer value of argument to Binary string and then returns
the no. of 1’s present in it.
Syntax
Output:
Hexadecimal string of 16 : f
Hexadecimal string of 100 : 80
Octal string of 16 : 17
Octal string of 100 : 200
34
13
54.0
Character class
The above statement creates a Character object which contain ‘a’ of type char. There
is only one constructor in Character class which expect an argument of char data type.
If we pass a primitive char into a method that expects an object, the compiler
automatically converts the char to a Character class object. This feature is called
Autoboxing and Unboxing.
Note : The Character class is immutable like String class i.e once it’s object is
created, it cannot be changed.
Syntax :
boolean isLetter(char ch)
Parameters :
ch - a primitive character
Returns :
returns true if ch is a alphabet, otherwise return false
Example:
Output:
true
false
Syntax :
boolean isDigit(char ch)
Parameters :
ch - a primitive character
Returns :
returns true if ch is a digit, otherwise return false
Example:
false
true
Syntax :
boolean isWhitespace(char ch)
Parameters :
ch - a primitive character
Returns :
returns true if ch is a whitespace.
otherwise return false
Example:
Output:
false
true
true
true
true
false
Syntax :
boolean isUpperCase(char ch)
Example:
Output:
true
false
true
Syntax :
boolean isLowerCase(char ch)
Example:
Output:
false
true
true
Syntax :
char toUpperCase(char ch)
Parameters :
ch - a primitive character
Returns :
returns the uppercase form of the specified char value.
Example:
Output:
A
65
48
Syntax :
char toLowerCase(char ch)
Parameters :
ch - a primitive character
Returns :
returns the lowercase form of the specified char value.
Example:
Output:
a
97
48
Syntax :
String toString(char ch)
Parameters :
ch - a primitive character
Returns :
returns a String object.
Example:
Output:
x
y