Monday, November 5, 2012

Primitive Variable / Reference Variable

Primitive Variable
Primitive TypeSizeMinimum ValueMaximum ValueWrapper Type
char  16-bit    Unicode 0  Unicode 216-1  Character
byte  8-bit    -128  +127  Byte
short  16-bit    -215
(-32,768)
  +215-1
(32,767)
  Short
int  32-bit    -231
(-2,147,483,648)
  +231-1
(2,147,483,647)
  Integer
long  64-bit    -263
(-9,223,372,036,854,775,808)
  +263-1
(9,223,372,036,854,775,807)
  Long
float  32-bit   单精度 float ff = 1.2344355f;  Float
double  64-bit   双精度 double dd = 1.2344355;  Double
boolean  1-bit    true or false  Boolean
void  -----    -----    -----    Void
Use a byte if either you specifically need to manipulate a signed byte (rare!), or when you need to move around a block of bytes;
整数:
万:short
万亿:int
巨大的整数用long  

浮点数:
单精度:float
双精度:double

Reference Variable - "remote control"
" It is important to understand that it is the type of reference variable - not the type of object that it refers to - that determines what members can be accessed. "

suppose you have:
Object x = "hello";
The type of the variable is Object, but the type of the object it refers to is String. it's the variable type which determines what you can do though - so you can't call
// Invalid
String y = x.toUpperCase();
The compiler only knows that you're calling a method on Object, which doesn't includetoUpperCase. Similarly, overloaded methods are only resolved against the ones you know about:
public class Superclass
{
    public void foo(Object x) {}
}

public class Subclass extends Superclass
{
    public void foo(String y) {}
}
...
Subclass x = new Subclass();
Superclass y = x;

x.foo("hello"); // Calls Subclass.foo(String)
y.foo("hello"); // Calls Superclass.foo(Object)


No comments:

Post a Comment