• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Testing for an uninitialized value

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you mean this
 
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic