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
🌐
Post.Byes
post.bytes.com › home › forum › topic › java
Making an int null - Post.Byes
Also using a=NULL; won't work because java is case sensitive, (all keywords are lower case) you need to use a=null; instead. Regards, Alex ... If you are using 1.5 or above (you should be) then read about autoboxing. With autoboxing you can do sill things like [code=java] Integer i = 20; i = null; [/code]
🌐
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

🌐
W3Docs
w3docs.com › java
Can an int be null in Java?
Here is an example of using an Integer variable to store a null value: Integer x = null; if (x == null) { System.out.println("x is null"); } else { System.out.println("x is not null"); } ... This code declares an Integer variable called x and ...
🌐
Baeldung
baeldung.com › home › persistence › inserting null into an integer column using jdbc
Inserting Null Into an Integer Column Using JDBC | Baeldung
January 8, 2024 - To create a database table that reflects this Java class, we’ll use the following SQL query: CREATE TABLE Person (id INTEGER not null, name VARCHAR(50), lastName VARCHAR(50), age INTEGER, PRIMARY KEY (id)); With all that out of the way, now ...
🌐
Blogger
javarevisited.blogspot.com › 2014 › 12 › 9-things-about-null-in-java.html
9 Things about null keyword and reference in Java
It's just a special value, which can be assigned to any reference type and you can typecast null to any type, as shown below : String str = null; // null can be assigned to String Integer itr = null; // you can assign null to Integer also Double dbl = null; // null can also be assigned to Double ...
🌐
Qlik Community
community.qlik.com › t5 › Talend-Studio › tMap-Assign-null-values-to-an-Integer › td-p › 2427640
Solved: tMap - Assign null values to an Integer - Qlik Community - 2427640
March 7, 2024 - Define a context variable with Integer type, let the value as empty. Make sure the Integer column is nullable, use the context variable in the place where you need to assign null value.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › can-an-int-be-null-in-java
Can an int be null in Java?
The following example can be used to store null to an integer class object: public class Main { public static void main(String[] args) { // Declare an object of Integer class Integer int_var = null; // Checking whether int_var is null or not if (int_var == null) { System.out.println("int_var is null"); } else { System.out.println("int_var value: " + int_var); } } }
🌐
Oracle
docs.oracle.com › cd › A97335_01 › apps.102 › a83723 › keyprog4.htm
Wrapper Classes for Null-Handling
To avoid the possibility of null values being assigned to Java primitives, use the following wrapper classes instead of primitive types: ... In case you must convert back to a primitive value, each of these wrapper classes has an xxxValue() method. For example, intValue() returns an int value from an Integer object and floatValue() returns a float value from a Float object.
🌐
The Eclipse Foundation
eclipse.org › forums › index.php › t › 1091102
Eclipse Community Forums: Papyrus » How to put Integer default value to null | The Eclipse Foundation
The Eclipse Foundation - home to a global community, the Eclipse IDE, Jakarta EE and over 350 open source projects, including runtimes, tools and frameworks.
🌐
Upwork
upwork.com › resources › articles › {name}
Null in Java: Understanding the Basics - Upwork
August 5, 2024 - When programming in Java, it's important to write null in lowercase. Both Null and NULL will throw compile-time errors. This strict case sensitivity helps avoid mistakes and maintains consistency in code. Just as there is a default value for primitive types (e.g., 0 for integers, false for Booleans), null is the default value for reference types.
🌐
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 - The choice between int and Integer depends on the specific needs of your code: ... Performance is Critical: If your application requires high-performance data processing, especially for numerical operations, using int is generally preferred. Nullability is Not Needed: If you don’t need to represent the absence of a value and can rely on default values (e.g., 0 for integers), int is the natural choice.
🌐
Javatpoint
javatpoint.com › facts-about-null-in-java
Facts about null in Java - Javatpoint
Java String valueOf() Java Integer getInteger() Method · Package class · Java abstract Keyword · Java boolean Keyword · Java byte keyword · Java case keyword · Java char keyword · Java class keyword · Java double keyword · Java float keyword · Java int keyword · Java new Keyword · Java null reserved word ·