An int is not null, it may be 0 if not initialized.
If you want an integer to be able to be null, you need to use Integer instead of int.
Integer id;
String name;
public Integer getId() { return id; }
Besides, the statement if(person.equals(null)) can't be true because if person is null, then a NullPointerException will be thrown. So the correct expression is if (person == null)
An int is not null, it may be 0 if not initialized.
If you want an integer to be able to be null, you need to use Integer instead of int.
Integer id;
String name;
public Integer getId() { return id; }
Besides, the statement if(person.equals(null)) can't be true because if person is null, then a NullPointerException will be thrown. So the correct expression is if (person == null)
primitives dont have null value. default have for an int is 0.
if(person.getId()==0){}
Default values for primitives in java:
Data Type Default Value (for fields)
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char '\u0000'
boolean false
Objects have null as default value.
String (or any object)--->null
1.) I need to check if the object is not null; Is the following expression correct;
if (person == null){
}
the above piece of code checks if person is null. you need to do
if (person != null){ // checks if person is not null
}
and
if(person.equals(null))
The above code would throw NullPointerException when person is null.
Hi! So this is my first time posting on here so sorry if this formating is wrong and it doesn't make much sense. I am working with Java and we have to create 2 Integer variables that store the value null in the beginning. These same variables then need to be used later to get user input and set that as the new value. We were provided with a sample run of what should be outputted. We were also provided with objectives that I put in at the bottom of this post. The issue is that I cannot set Integer a and b to two values. Meaning I cannot have Integer equal null and the user input. However, the parameters set to show me that to get this problem correct it needs to be that way. integer a must equal null and user input. Again I am really sorry if this is the wrong formatting, this is my first time using Reddit! Thanks for helping or attempting too!
Sample run:
null nullEnter values:>7>12Average of 7 and 12 is 9.5
----
MY CODE:
import java.util.Scanner;
public class U2_L7_Activity_Two{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
Integer a = null;
Integer b = null;
System.out.println(a + " " + b);
System.out.println("Enter values:");
Integer a = scan.nextInt();
Integer b = scan.nextInt();
double avg = (a+b)/2.0;
System.out.println("Average of " + a + " and " + b + " is " + avg);
}
}
Objectives:
Debug the code provided in the starter file so it does the following:
creates two Integer objects a and b, and initializes them as null
prints the values of a and b (should result in the output "null null")
sets a and b to inputs entered by the user
finds the average of the two values and stores this in a Double value
prints a sentence as shown in the sample run with the values of a, b and the average
parseInt() is just going to throw an exception if the parsing can't complete successfully. You can instead use Integers, the corresponding object type, which makes things a little bit cleaner. So you probably want something closer to:
Integer s = null;
try {
s = Integer.valueOf(startField.getText());
}
catch (NumberFormatException e) {
// ...
}
if (s != null) { ... }
Beware if you do decide to use parseInt()! parseInt() doesn't support good internationalization, so you have to jump through even more hoops:
try {
NumberFormat nf = NumberFormat.getIntegerInstance(locale);
nf.setParseIntegerOnly(true);
nf.setMaximumIntegerDigits(9); // Or whatever you'd like to max out at.
// Start parsing from the beginning.
ParsePosition p = new ParsePosition(0);
int val = format.parse(str, p).intValue();
if (p.getIndex() != str.length()) {
// There's some stuff after all the digits are done being processed.
}
// Work with the processed value here.
} catch (java.text.ParseFormatException exc) {
// Something blew up in the parsing.
}
Try this:
Integer startIn = null;
try {
startIn = Integer.valueOf(startField.getText());
} catch (NumberFormatException e) {
.
.
.
}
if (startIn == null) {
// Prompt for value...
}
With Java 8:
if (Optional.ofNullable(myInteger).orElse(0) != 0) {
...
}
Note that Optional may help you to completely avoid the if condition at all, depending on your use case...
Since StringUtils class is mentioned in the question, I assume that Apache Commons lib is already used in the project.
Then you can use the following:
if (0 != ObjectUtils.defaultIfNull(myInteger, 0)) { ... }
Or using static import:
if (0 != defaultIfNull(myInteger, 0)) { ... }