Why java is asking to initialize the variables whe

2019-01-28 02:11发布

Please see the code below. The method printTest() is printing the default value of the uninitialized variables but when it's comes to main method java is asking for variable initialization. Can anybody explain why?

   public class Test1 {

    public static void main(String[] args) {   
      int j;
      String t;

      System.out.println(j);
      System.out.println(t);
    }
  }


  public class Test2 {

   int i;
   String test;

  public static void main(String[] args)   {   
    new Test().printTest();
  }

   void printTest()   {
     System.out.println(i);
     System.out.println(test);
  }

  }

7条回答
手持菜刀,她持情操
2楼-- · 2019-01-28 02:39

Java documentation for primitives

Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.

查看更多
来,给爷笑一个
3楼-- · 2019-01-28 02:42

Local variables are used mostly for intermediate calculations whereas instance variables are supposed to carry data for calculations for future and intermediate as well. Java doesnt forces to initialize instance variable and allows default value but for local variables its the developers call to adssign the value. So to avoid mistakes you need to initialize local variables.

查看更多
孤傲高冷的网名
4楼-- · 2019-01-28 02:43

Your global variable was not initialised anywhere. You are trying to print var i and test with values not seen / existing. It is not null or 0 nor blank.

Your case is a bit similar to this (from answer: Uninitialized int vs Integer)

i == 0; Uninitialized
i == null; Undefined
test == 0; java.lang.NullPointerException
test == null; Uninitialized

but when it's comes to main method java is asking for variable initialization.

Your local variable which are i and t is similar case to the global variables.

Variables needs to be initialised. I personally think that any computing with variables needs to have an initial value or it will not exist.

Computers are currently based on physical use of computation / mathematics, so whatever rules are in Math it will also be applied to computers unless that we have entered a new stage of computing quantum or something further.

In mathematics, a variable is a symbol (commonly an alphabetic character) designating a value that may change within the scope of a given problem or set of operations.

(from: http://en.wikipedia.org/wiki/Variable_(mathematics))

So given with that rule, a variable without any value for me is non existing empty = x is empty and no known way to compute emptiness because no one can see it.

查看更多
三岁会撩人
5楼-- · 2019-01-28 02:47

You have to initialize local variables in java. Simple as that. If you however don't have a value, you could set the value to null. Note: int is a simple data type and can not have the value null, therefore I changed the class to "Integer", which is the class that would wrap around int anyhow.

public class Test {

    int i;
    String test;

    public static void main(String[] args) {
        Integer j = null;
        String t = null;
        //other options for initialization value:
        t = new String();
        t = "";

        System.out.println(j);
        System.out.println(t);

        new Test().printTest();
    }

    void printTest() {
        System.out.println(i);
        System.out.println(test);
    }
}
查看更多
啃猪蹄的小仙女
6楼-- · 2019-01-28 02:55

Here in your program, your variable needs to carry some value as you used the variable in if-else loop and as i can see if the program go to else loop there is nothing to display as 'sizeDisplay' that's why your sizeDisplay needs to be initialized as "null".

查看更多
Rolldiameter
7楼-- · 2019-01-28 03:01

This is a problem with what the compiler can know about.

In the case of the Main Method, the compiler knows for sure that the variables have not be initialized.

But in the case of the printTest method, the compiler does know that the could be some other method (or same package class) which initializes the class variables.

查看更多
登录 后发表回答