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
Discussions

Help on Java Program regarding Integer and Null
You should probably actually ask a question if you want anyone to help. Are you running into errors? Are you not getting the right outputs? What's the problem? More on reddit.com
🌐 r/learnjava
7
1
September 28, 2020
Making an int null - Post.Byes
Hello, I am pretty new to Java programming. I am having trouble with trying to convert an integer of value 0 to NULL. I have tried the following and it does not work: if (a==0) { a=NULL; } Any suggestions? Is it even possible to put an integer back to NULL after it has been initialized? More on post.bytes.com
🌐 post.bytes.com
April 24, 2006
checking null value in an integer - Software & Applications - Spiceworks Community
Hi, I have a question on how to check for NULL value in an integer variable before using this variable to set value in an OTD. For string variable we can do the following and it works if (ls_abc != null) { } But for integer: int li_ID; li_ID != null (is not a valid statement as one cannot check ... More on community.spiceworks.com
🌐 community.spiceworks.com
0
March 7, 2008
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.com
🌐 r/java
19
14
November 22, 2012
🌐
AmbitionBox
ambitionbox.com › interviews › inceptive-technologies-question › how-to-give-null-value-to-int-variable-KbqruDSE
How can you assign a null value to an int variable?
Prepare for your next job interview with AmbitionBox. Read 12 Lakh+ interview questions & answers shared by real candidates across 1 Lakh+ companies in India.
🌐
Java Vogue
javavogue.com › home › check if int is null in java
Check If Int is Null In Java - Java Vogue
October 11, 2022 - Int is primitive data type in java and Such data types in java stores data in binary form in memory by default. That means they can’t be null in java .So we can’t check int for a null value . On the other hand, Integer is an object and it can have a null value in java .
🌐
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 - 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 to represent the absence of a value.
🌐
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.
Find elsewhere
🌐
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

🌐
Codemia
codemia.io › knowledge-hub › path › can_an_int_be_null_in_java
Can an int be null in Java?
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
TutorialsPoint
tutorialspoint.com › can-an-int-be-null-in-java
Can an int be null in Java?
September 19, 2024 - 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.
🌐
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
Read Also: How to convert int to Integer in Java · int stores the values in the memory in the form of binary by default. It means they can't be null. Primitive data type int can not be checked for a null value.
🌐
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.
🌐
Post.Byes
post.bytes.com › home › forum › topic › java
Making an int null - Post.Byes
April 24, 2006 - 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 set it to null. try setting it to -1 or 0 instead.
🌐
Coderanch
coderanch.com › t › 479855 › java › int-null
int[] = null; help (I/O and Streams forum at Coderanch)
January 22, 2010 - Nowhere are you calling "exp = ... just that and that's why that example is working and this one isn't. The easiest solution is adding "exp = new int[5];" just before the loop....
🌐
Upwork
upwork.com › resources › articles › {name}
Null in Java: Understanding the Basics - Upwork
August 5, 2024 - In Java, null is a literal, a special constant you can point to whenever you wish to point to the absence of a value. It is neither an object nor a type (a common misconception some newcomers to the Java language grapple with). The concept of null was introduced in programming languages to represent the absence of a value or a non-existent object.
🌐
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.
🌐
Spiceworks
community.spiceworks.com › software & applications
checking null value in an integer - Software & Applications - Spiceworks Community
March 7, 2008 - Hi, I have a question on how to check for NULL value in an integer variable before using this variable to set value in an OTD. For string variable we can do the following and it works if (ls_abc != null) { } But for integer: int li_ID; li_ID != null (is not a valid statement as one cannot check ...
🌐
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 - 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.
🌐
Coderanch
coderanch.com › t › 526742 › java › AutoBoxing-Integer-int-conversion
AutoBoxing Integer to int conversion (Beginning Java forum at Coderanch)
February 9, 2011 - Hi and welcome to the Javaranch. What happens is that you have an Integer reference with you want to convert into an int value. This happens by calling the intValue() method on the Integer reference. But because the Integer reference is null it results in a NullPointerException.