0

What is the value of a primitive type prior to an assignment?

What I'd like to do is have a variable that, if set, returns whatever its set to and if not returns null. I recognise that the below is a stupid way of doing it, and I've already solved the problem. However, in thinking about it I realised that I didn't know how primitive types acted prior to assignment and a quick Google didn't show anything. So this is more about answering that question than about finding a better way of solving the problem.

for example:

public class Something{
  int value;
  public Something(){
    //irrelevant stuff
  }

  public int getValue(){
    return value;
  }
}

public class SomeOtherClass{
  public SomeOtherClass(){
    Something s = new Something();
    System.out.println(s.getValue);
  }
}

What would be shown?

4

2 回答 2

4

一个整数类成员默认初始化为0,如果你没有自己显式地初始化它。

请参阅语言规范中的http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5

于 2013-05-25T10:25:59.223 回答
0

在代码中它是s.getValue()一种方法。

于 2013-05-25T10:48:40.627 回答