Two Laptop Bag*
The moose likes Beginning Java and the fly likes Testing for an uninitialized value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Testing for an uninitialized value" Watch "Testing for an uninitialized value" New topic
Author

Testing for an uninitialized value

Sasha Hernandez
Greenhorn

Joined: Oct 14, 2003
Posts: 21
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

Joined: Nov 21, 2003
Posts: 20
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

Joined: Aug 24, 2001
Posts: 348
I think you mean this
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

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

Joined: Nov 21, 2003
Posts: 20
I think you mean this
code:
if (x == 0 ) doThis(); else doThat();

Yes, yes. Thank you, chi. My bad.
Sasha Hernandez
Greenhorn

Joined: Oct 14, 2003
Posts: 21
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

Joined: Dec 10, 2001
Posts: 7023
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 ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Testing for an uninitialized value