int can't be null, but Integer can. You need to be careful when unboxing null Integers since this can cause a lot of confusion and head scratching!
e.g. this:
int a = object.getA(); // getA returns a null Integer
will give you a NullPointerException, despite object not being null!
To follow up on your question, if you want to indicate the absence of a value, I would investigate java.util.Optional<Integer> or java.util.OptionalInt
int can't be null, but Integer can. You need to be careful when unboxing null Integers since this can cause a lot of confusion and head scratching!
e.g. this:
int a = object.getA(); // getA returns a null Integer
will give you a NullPointerException, despite object not being null!
To follow up on your question, if you want to indicate the absence of a value, I would investigate java.util.Optional<Integer> or java.util.OptionalInt
No. Only object references can be null, not primitives.
Help on Java Program regarding Integer and Null
Making an int null - Post.Byes
checking null value in an integer - Software & Applications - Spiceworks Community
Friendly reminder about Integer, int, nulls and autoboxing/unboxing
I ignore autoboxing warnings, until the other day..
ResultSet RS = doSomeDatabaseQuery();
Double foo = RS.getDouble("someDoubleField");Silly me, thinking "getDouble()", which returns a field from a database which can quite plausibly be NULL, would return a Double. No, it returns a double. If the value in the db was NULL, it returns a 0.
What you actually have to do is:
ResultSet RS = doSomeDatabaseQuery();
double foo = RS.getDouble("someDoubleField");
Double bar = RS.wasNull() ? null : Double.valueOf(foo);Because I had autoboxing warnings turned off, we didn't notice this bug. Let this be a lesson! Don't ignore warnings!
More on reddit.comInteger a = null; int b = a;
Line 2 will throw a null pointer exception.
I ignore autoboxing warnings, until the other day..
ResultSet RS = doSomeDatabaseQuery();
Double foo = RS.getDouble("someDoubleField");
Silly me, thinking "getDouble()", which returns a field from a database which can quite plausibly be NULL, would return a Double. No, it returns a double. If the value in the db was NULL, it returns a 0.
What you actually have to do is:
ResultSet RS = doSomeDatabaseQuery();
double foo = RS.getDouble("someDoubleField");
Double bar = RS.wasNull() ? null : Double.valueOf(foo);
Because I had autoboxing warnings turned off, we didn't notice this bug. Let this be a lesson! Don't ignore warnings!
My favorite autoboxing gotcha:
Object o = true ? new Integer(1) : new Double(2.0);
System.out.println(o);
What do you think it prints?
EDIT: A little clearer:
Integer i = new Integer(1);
Double d = new Double(2.0);
Object o = true ? i : d;
System.out.println("i=" + i);
System.out.println("d=" + d);
System.out.println("o=" + o);
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