Java numeric assignment rules are important because they affect how values are stored, converted, promoted, boxed, unboxed, and cast. In Java SE 25, the core rules for primitive numeric types, reference assignment, compound assignment, and variable scope remain consistent with the long-standing Java language model, but they should be explained using current terminology and modern examples.
byte type is an 8-bit signed integer. Its range is
-128 through 127. The leftmost bit participates in
representing the sign when the value is interpreted using two's complement
representation.
byte min = -128;
byte max = 127;
System.out.println(min);
System.out.println(max);
+= provide?
b += a behaves like b = (byte)(b + a) when
b is a byte. This can be convenient, but it can also
hide narrowing conversion and overflow behavior.
byte b = 10;
int a = 20;
b += a; // Compiles: equivalent to b = (byte)(b + a)
// b = b + a; // Does not compile without an explicit cast
System.out.println(b);
+=, -=, *=, and /= work?
package com.java.operators;
public class CompoundOperators {
public static void main(String[] args) {
int a = 10;
int b = a;
b += a;
System.out.println(b);
a = b = 10;
System.out.println(a);
b /= a;
System.out.println(b);
}
}
Output:
20
10
1
Number n = Integer.valueOf(25); // Valid: Integer is a subclass of Number
// Integer i = n; // Does not compile without a cast
Integer i = (Integer) n; // Valid at runtime because n refers to an Integer
System.out.println(i);
java.lang.ClassCastException?
ClassCastException is a runtime exception thrown when code attempts
to cast an object to a type that is not compatible with the object's actual
runtime class.
Object value = Integer.valueOf(0);
// This compiles because value is declared as Object,
// but it fails at runtime because the object is an Integer, not a String.
String text = (String) value;
System.out.println(text);
static.if (true) {
int count = 25;
System.out.println(count);
}
// System.out.println(count); // Does not compile: count is out of scope
Integer, Long, Float, and Double?
'\u004E' is a char. It can be converted to a numeric
primitive value, and then boxing can create the corresponding wrapper object.
For clarity, use an explicit cast when assigning to wrapper types other than
Character.
package com.java.assignments;
public class UnicodeAssign {
public static void main(String[] args) {
char letterN = '\u004E';
Integer i = (int) letterN;
Long l = (long) letterN;
Float f = (float) letterN;
Double d = (double) letterN;
System.out.println(letterN);
System.out.println(i);
System.out.println(l);
System.out.println(f);
System.out.println(d);
}
}
Output:
N
78
78
78.0
78.0
public class LocalVariableDemo {
public static void main(String[] args) {
int total = 25; // local variable
System.out.println(total);
}
}
static.
public class Account {
private int balance; // instance variable
public Account(int openingBalance) {
this.balance = openingBalance;
}
public int getBalance() {
return balance;
}
}
These rules are still essential for Java SE 25 programming because they explain why some assignments compile, why some casts fail at runtime, and why variable placement inside a class, method, or block changes visibility.