Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Testing for an uninitialized value

 
Sasha Hernandez
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say you have the variable x of type int. How would you test the variable to see if it is uninitialized, undefined, or null?
 
Mike Minner
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A variable of type int would not uninitialized, undefined, or null. It will have a default value of 0 if not given a value upon declaration. So you could try:

This code will test to see if x has a value of 0. If it does not, it will doThat().
[ November 23, 2003: Message edited by: Mike Minner ]
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you mean this
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mike Minner
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you mean this
code:
if (x == 0 ) doThis(); else doThat();

Yes, yes. Thank you, chi. My bad.
 
Sasha Hernandez
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic