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

Answer from Brian Agnew on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com › can-an-int-be-null-in-java
Can an int be null in Java?
The default value for an int data type is 0 and it cannot not be null. The different primitive data types have their different default values, but the objects in Java can be null.
🌐
W3Docs
w3docs.com › java
Can an int be null in Java?
No, an int data type in Java cannot be null. In Java, the int data type is a primitive type that represents a 32-bit signed integer.
🌐
W3Docs
w3docs.com › java
How to check if an int is a null
In Java, an int is a primitive data type and cannot be null.
🌐
Medium
medium.com › @roscoe.kerby › choosing-between-int-and-integer-in-java-when-performance-meets-nullability-d3b232652d9f
Choosing Between int and Integer in Java: When Performance Meets Nullability | by Roscoe Kerby [ROSCODE] | Medium
October 23, 2023 - It incurs a slight performance overhead due to object creation and garbage collection. One of the primary distinctions between int and Integer is nullability. In Java, primitive data types like int cannot hold a null value, making it challenging ...
Find elsewhere
🌐
Blogger
javahungry.blogspot.com › 2021 › 12 › check-if-int-is-null-in-java.html
Check if an int is a null in Java | Java Hungry
Primitive data type int can not be checked for a null value. On the other hand, Integer is an object and it can have a null value as shown below in the example. Integer wrapper class is just like any other class in Java. Using variables of the Integer type, we store references to Integer objects.
🌐
Oracle
docs.oracle.com › cd › A97335_01 › apps.102 › a83723 › keyprog4.htm
Wrapper Classes for Null-Handling
Java primitive types (such as int, double, or float) cannot have null values, which you must consider in choosing your result expression and host expression types. SQLJ consistently enforces retrieving SQL nulls as Java nulls, in contrast to JDBC, which retrieves nulls as 0 or false for certain ...
🌐
LabEx
labex.io › tutorials › java-how-to-handle-integer-null-comparison-437111
How to handle Integer null comparison | LabEx
When an Integer is null, it means no object is referenced in memory. This is different from a primitive int which always has a default value of 0. ... At LabEx, we recommend developers understand these nuanced behaviors to write more defensive ...
🌐
Post.Byes
post.bytes.com › home › forum › topic › java
Making an int null - Post.Byes
Is it even possible to put an integer back to NULL after it has been initialized? If asking for user input, is there a way for Java to leave the value NULL if the user simply hits "enter?" Thank you in advance for your help. ... int can't be null In java int is a primitive type so you can't ...
🌐
Oracle
docs.oracle.com › cd › A97335_02 › apps.102 › a83723 › keyprog4.htm
Null-Handling
Java primitive types (such as int, double, or float) cannot have null values, which you must consider in choosing your result expression and host expression types. SQLJ consistently enforces retrieving SQL nulls as Java nulls, in contrast to JDBC, which retrieves nulls as 0 or false for certain ...
🌐
Reddit
reddit.com › r/learnjava › help on java program regarding integer and null
r/learnjava on Reddit: Help on Java Program regarding Integer and Null
September 28, 2020 -

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

🌐
Bukkit
bukkit.org › threads › check-if-an-integer-is-null.459624
Check if an integer is null | Bukkit Forums
Hello, I have a command where I use arguments to convert them to integer but I want to check whether or not they are null. When one is null, an error...
🌐
Delft Stack
delftstack.com › home › howto › java › check if int is null java
How to Check if Int Is Null in Java | Delft Stack
February 12, 2024 - In Java, the primitive data type int cannot be assigned a value of null because it is a value type, not an object.
🌐
Quora
quora.com › In-Java-if-I-have-an-empty-int-variable-as-my-input-how-do-I-print-an-error-message-for-it-as-output
In Java, if I have an empty int variable as my input, how do I print an error message for it as output? - Quora
Answer (1 of 3): An int variable can not be ‘empty’, it can at best (or worst) only be uninitialized with some predefined value. In other words, the variable can contain whatever crap there happens to be in that memory location. The trick it to initialize your variable.
🌐
GeeksforGeeks
geeksforgeeks.org › java › interesting-facts-about-null-in-java
Interesting facts about null in Java - GeeksforGeeks
September 3, 2024 - It is not recommended to use null as a value for a variable or object that is intended to hold a value of a primitive data type, such as int or double, because it will cause a compile-time error.
🌐
Qlik Community
community.qlik.com › t5 › Talend-Studio › How-can-I-test-if-a-value-of-INT-type-Not-INTEGER-TYPE-I-m › td-p › 2357049
How can I test if a value of INT-type (Not INTEGER TYPE, I'm talking about INT type) is null?
November 15, 2024 - If it truly needs to be of the int data type, is there a value that could be considered equivalent to null, such as 0 (zero)? If so, you could use something like this: row3.code2 == 0 ? null : row3.code2.toString() ... Ditto - same here! ... The int data type is a primitive data type that is not instantiated from a Java class, unlike Integer.