Wrapper class

- The class wraps primitive type. - When handle 8 types of primitive type as object. =>why? OOP is Object-Oriented Programming. It needs to be as Object. 90% is Object and remains are primitive type because of performance.

public final class Integer extends Number implements Comparable {
    ...
    private int value;//wraps int type.
}

primitive type

wrapper class

constructor

example

boolean

Boolean

Boolean(boolean value)

Boolean(String s)

Boolean b = new Boolean(true);

Boolean b2 = new Boolean("true");

char

Character

Character(char value)

Character c = new Character('a');

byte

Byte

Byte(byte value)

Byte(String s)

Byte b = new Byte(10);

Byte b2 = newByte("10");

short

Short

Short(short value)

Short(String s)

Short s = new Short(10);

Short s2 = new Short("10");

int

Integer

Integer(int value)

Integer(String s)

Integer i = new Integer(100);

Integer i2 = new Integer("100");

long

Long

Long(long value)

Long(String s)

Long l = new Long(100);

Long l2 = new Long("100");

float

Float

Float(double value)

Float(float value)

Float(String s)

Float f = new Float(1.0); Float f = new Float(1.0f); Float f = new Float("1.0f");

double

Double

Double(double value)

Double(String s)

Double d = new Double(1.0);

Double d = new Double("1.0");

Integer i = new Integer(100);//address : 0x100
Integer i2 = new Integer(100);//address : 0x200

System.out.prinln("i==i2?" + (i==i2));//false.0x100!=0x200
System.out.prinln("i.equals(i2)?" + i.equals(i2));//true.means overriding.
//오른쪽 값이 더 작으면 양수,오른쪽 값이 더 크면 음수, 같으면 0
System.out.prinln("i.compareTo(i2) ="+i.compareTo(i2)); 
System.out.prinln("i.toString()="+i.toString());

Last updated