There is no way to check it for undefined, because technically during runtime primitive data type can not be uninitialized. The reasons: 1. When the primitive data type is an attribute of an object, it will be initialized automatically by the system 2. When the primitive data type is not member of attribute, the compiler will now allow your code to compile if there is any chance that it might not be initialized yet before using. [ November 23, 2003: Message edited by: Wirianto Djunaidi ]
Joined: Nov 21, 2003
I think you mean this code: if (x == 0 ) doThis(); else doThat();
Yes, yes. Thank you, chi. My bad.
Joined: Oct 14, 2003
Suppose you had a program that accepted input as follows: (x = 5) + x; (x = 5) + y; If you were to enter the first expression, (x = 5) + x;, x is defined with the value of 5. There will be no exceptions raised. The program will terminate normally with the final value of 10. If you were to enter the second expression, (x = 5) + y;, x would have the value of 5, but y is not defined. Since y is not defined, the program will terminate with a null pointer exception. If you were to enter the second expression, how would you test the value of y to see if it were defined or not? x and y are not primitive data types.
Sasha, Your examples don't compile in Java. If the program terminates with a NullPointerException, then something was the null reference, and it was asked to do something. If you wanted to test whether an object reference refers to null, then if (myRef == null) would be an appropriate strategy. As previously mentioned, the compiler will catch uninitialized variables. [ November 27, 2003: Message edited by: Dirk Schreckmann ]