Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The != (inequality) operator is a binary relational operator that evaluates whether two operands are not equal. It returns a boolean value: true if the operands differ in value or reference, and false if they are identical. A notable exception to this rule involves floating-point NaN (Not-a-Number) values; in accordance with IEEE 754 standards, Float.NaN != Float.NaN and Double.NaN != Double.NaN evaluate to true.
operand1 != operand2

Primitive Type Evaluation

When applied to primitive data types (byte, short, int, long, float, double, char, boolean), the != operator compares the actual literal values stored in memory. If the operands are of different numeric types, Java applies binary numeric promotion before comparison. The narrower type is implicitly widened to match the broader type.
int a = 5;
double b = 5.0;
boolean result = (a != b); // Evaluates to false. 'a' is promoted to 5.0 before comparison.

char c = 'A';
int d = 65;
boolean charResult = (c != d); // Evaluates to false. 'A' is promoted to its ASCII integer value 65.

Reference Type Evaluation

When applied to reference types (objects, arrays, strings), the != operator evaluates reference inequality. It checks whether the two operands point to different memory addresses (object identities) on the heap. It does not evaluate the internal state or content of the objects.
String str1 = new String("Java");
String str2 = new String("Java");
boolean result = (str1 != str2); // Evaluates to true. They hold the same string, but occupy different memory addresses.

Null Evaluation

When comparing reference types, the != operator is null-safe. It can evaluate whether a reference variable points to a memory location other than the null literal without throwing an exception.
Object obj = null;
boolean result = (obj != null); // Evaluates to false.

Autounboxing Mechanics

When evaluating inequality between a primitive type and its corresponding wrapper class (e.g., int and Integer), Java performs autounboxing. The wrapper object is converted to its primitive value before the comparison occurs. However, this operation is not null-safe. If the wrapper object is null and is compared to a primitive type using !=, Java’s attempt to unbox the null reference will throw a NullPointerException. If both operands are wrapper objects, no unboxing occurs. The operator defaults back to reference inequality.
Integer wrapper1 = 1000; // Autoboxed value outside the -128 to 127 cache
int primitive = 1000;
boolean result1 = (wrapper1 != primitive); // Evaluates to false (value comparison via autounboxing).

Integer wrapper2 = 1000;
boolean result2 = (wrapper1 != wrapper2); // Evaluates to true (reference comparison, no unboxing).

Integer nullWrapper = null;
// boolean result3 = (nullWrapper != primitive); // Throws NullPointerException during autounboxing.

Operator Precedence

The != operator shares the same precedence level as the equality operator (==). It has lower precedence than relational magnitude operators (<, >, <=, >=) but higher precedence than logical operators (&&, ||). It evaluates from left to right.
Master Java with Deep Grasping Methodology!Learn More