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?
No, In Java int cannot be null. There are some primitive data types and int is the one of the primitive data types in Java programming. The default value for an int data type is 0 and it cannot not be null. The different primitive data types have the
🌐
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

🌐
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.
🌐
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
public class CheckIfIntIsNull { public static void main(String args[]) { // Integer primtive data type int empId = 23; // Integer wrapper class Integer empId2 = new Integer(34); System.out.println("Primitive int empId is: "+ empId); System.out.println("Wrapper class Integer object is: "+ empId2); /* Can not check for null property for int, We can check for null property for Integer */ if (empId2 == null) { System.out.println("Integer wrapper class object is NULL"); } else { System.out.println("Integer wrapper class object is NOT NULL"); } } } Output: Primitive integer empId is: 23 Wrapper clas
🌐
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
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.
Find elsewhere
🌐
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.
🌐
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.
🌐
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.
🌐
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 - Such data types store data in binary form in memory by default. That means they can’t be null. In Java, the primitive data type int cannot be assigned a value of null because it is a value type, not an object.
🌐
Blogger
javarevisited.blogspot.com › 2014 › 12 › 9-things-about-null-in-java.html
9 Things about null keyword and reference in Java
Just like every primitive has default value e.g. int has 0, boolean has false, null is the default value of any reference type, loosely spoken to all object as well. Just like if you create a boolean variable, it got default value as false, ...
🌐
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.
🌐
LabEx
labex.io › tutorials › java-how-to-check-if-an-integer-object-is-null-in-java-560008
How to Check If an Integer Object Is Null in Java | LabEx
We declare an Integer variable myInteger and explicitly set it to null. We use an if statement to check if myInteger is equal to null using the == operator. This is the standard way to check if an object reference is null in Java.
🌐
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...