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.
Videos
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