- Wrappper classes :-
· Wrapper classes are immutable.
· given,
Float f = new Float(“Str”);
Str can take values | Str can’t take values |
123 or +123 i.e. positive integer(with ‘+’ sign) | 1l or 1L i.e. suffixing ‘L’ will cause error. |
-2343 i.e. negative integer | 0x10 i.e. we cannot give hexadecimal numbers as input. |
±1.0 or ±.0d i.e. double number | |
±.0f i.e. float number | |
±1.1e5f i.e. we can have ‘e’ in the input string | |
±1.1e5d i.e. we can have ‘e’ in the input string | |
· given ,
Long l = new Long(“Str”);
Str can take values | Str can’t take values |
123 i.e. positive integer without ‘+’ sign | +123 i.e. positive number with ‘+’ sign |
-234 i.e. negative integer | 0x133 i.e. hexadecimal number |
| ±1.0f i.e. floating point number |
| ±1.0 i.e. double precision number |
| ±.1e1f or ±.1e2d i.e. ‘e’ is not allowed |
| 1l or 1L i.e. suffixing ‘L’ is not allowed |
· Float constructor is overloaded,
Ø takes a primitive “double number”
Ø takes a string
Ø takes a primitive “float number”
· The equals method when used with wrapper classes gives output as true if
Ø both input objects are of the same class
Ø both have the same value
→ Remember if two objects of different class are input to equals method then the output is false and not compiler or runtime error.
· there isn’t a no argument constructor in any of the wrapper classes.
· given,
Byte b = new Byte(1);//causes compiler error because 1 → is an integer and Byte constructor takes only byte .similarly for short.
· given,
Long lg1 = new Long(1);
Long lg2 = new Long(lg1);//causes compiler error because Long constructor doesn’t take in Long object
· <wrapperClassName>.toString(); is overloaded
Ø takes a no argument constructor and returns string of the given wrpper object
Ø (static)takes argument corresponding to the wrapper class i.e.
Integer.toString() takes int number and Float.toString() takes float number only unlike its constructor
· wrapper classes ( excluding Character and Boolean) extends Number class and has 6 methods and they are
Ø byteValue
Ø shortValue
Ø intValue
Ø longValue
Ø floatValue
Ø doubleValue
→ there isn’t a “charValue” int the wrapper class(excluding Character).
· Boolean.valueOf() returns a reference to either Boolean.TRUE or Boolean.FALSE
Boolean b1 = Boolean.valueOf(true);
Boolean b2 = Boolean.valueOf(“TrUe”);
“b1==b2“ is true.
· Remember NaN is not equal to Nan.
· Boolean has two constructors,
Ø takes boolean values i.e. true or false.
Ø takes String values i.e given,
Boolean b = new Boolean(“TrUe”);//input string if passes the equalsIgnoreCase method with the string “true” then the new Boolean object will contain value - true.
Ø given,
Boolean b = new Boolean(<<AnyStringLiteral>>);
for e.g. Boolean b = new Boolean(“santosh”);
Boolean b1 = new Boolean(null);
then , b.equals(b1) will be true because b and b1 contain – false .
· given,
Double a = new Double(Double.NaN);
Double b = new Double(Double.NaN);
then a.equals(b) will be true.
· Given,
Double c = new Double(0.0);
Double d = new Double(-0.0);
c.equals(d) → gives the output of false.
· Remember “0.0 == -0.0” is true.