型にはプリミティブ型とオブジェクト型(下記ラッパークラス含む)があり、
型宣言の方法によって挙動が異なる。
例えば、
class Sample {
private int sample;
Sample() {}
public void setValue(int k) {
sample = k;
}
public int getValue() {
return sample;
}
}
public class Test {
public static void test(int x,Sample y) {
x = 20;
y.setValue(20);
}
public static void main(String args[]) {
int a = 50;
Sample b = new Sample();
test(a,b);
System.out.println("a:"+a);
System.out.println("b:"+b.getValue());
}
}
のように記述すると、プリミティブ型aは値をそのまま買えているだけなので、a:50と表示されるが、
bに関してはオブジェクトを通じて値を変えているので、b:20と表示される。
他にもプリミティブ型はVector型では使用できないなどの違いがある。
プリミティブ型 |
ラッパークラス |
boolean |
Boolean |
char |
Character |
byte |
Byte |
short |
Short |
int |
Integer |
long |
Long |
float |
Float |
double |
Double |
最終更新:2011年04月18日 22:19