• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 972
  • Last Modified:

Check if int is intialized

Is there a way to check if a int value is intialized like strings
when i try this i get an error. Is there any other way to check if an int value is intialized?
int a;
if (a==null)
{
System.out.println("a not intialized");
}

Open in new window

0
angelblade27
Asked:
angelblade27
  • 4
  • 3
1 Solution
 
contactkarthiCommented:
in your case the program will not compile saying a is not initialised if you try using it any where. if a==null also will throw error saying a is not initialised
0
 
angelblade27Author Commented:
yes i realise that is there any check that i can do to see if it is intialized?
0
 
contactkarthiCommented:
I dont think so...

but generally when the compiler throws an error it points out the exact line where you have the problem
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
angelblade27Author Commented:
is there a java practice for determining if a value is set or not?
The issue is that i need to do one statement if this variable has not been set but i don't want to have a default value to check for
0
 
contactkarthiCommented:
can you rephrase this
>> The issue is that i need to do one statement if this variable has not been set but i don't want to have a default value to check for

im not able to get your question
0
 
angelblade27Author Commented:
I have a class which has variables
public class first{
int a;
int b;
}

public class second{
public void trial (first cool){
if (cool.a==??){
System.out.println("This is not intialized");
}
}

}

the ?? is what should check for
0
 
contactkarthiCommented:
in this case a will be automatically initialized to zero
0
 
m00003643Commented:
In Java all primitives (int, bool, char, etc.) have an initial value.  Only Objects (Integer, Boolean, etc...) can be null.  As contactkarthi stated, the initial value for an int is 0.  If you really want to have uninitialized values, try this:

public class first{
Integer a;
Integer b;
}

Since Integer (as opposed to int) is an actual Object, it will initially be null.  You can then check if (null == a) from your Second class.

Bonus tip:  it is actually faster to compare (null == ??) than to compare (?? == null).  
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now