Java Fundamentals: Enums, Precision, and Operator Concerns
Exploring Enum Behavior and Object References
Enums in Java behave as reference types with singleton-like guarantees. The following example checks identity and type characteristics.
enum Dimension { SMALL, MEDIUM, LARGE }
public class EnumAnalysis {
public static void main(String[] args) {
Dimension a = Dimension.SMALL;
Dimension b = Dimension.LARGE;
System.out.println(a == b);
System.out.println(a.getClass().isPrimitive());
Dimension c = Dimension.valueOf("SMALL");
System.out.println(a == c);
for (Dimension val : Dimension.values()) {
System.out.println(val);
}
}
}
Enums are not primitive types; they are reference-based. Each constant maps to a single instance, making == and equals() equivalent when comparing enum values. The values() method returns all defined constents, while valueOf() reconstructs a constant from its name.
User Input and Type Conversion
A simple dialog-based program demonstrates string-to-integer parsing and basic arithmetic.
import javax.swing.JOptionPane;
public class SimpleCalculator {
public static void main(String[] args) {
String rawFirst = JOptionPane.showInputDialog("First integer:");
String rawSecond = JOptionPane.showInputDialog("Second integer:");
int num1 = Integer.parseInt(rawFirst);
int num2 = Integer.parseInt(rawSecond);
int result = num1 + num2;
JOptionPane.showMessageDialog(
null,
"The sum is " + result,
"Result",
JOptionPane.PLAIN_MESSAGE
);
System.exit(0);
}
}
Local Variables vs. Static Fields
When a local variable shares a name with a static field, the local declaration takes precedence inside the method scope.
public class ShadowingDemo {
private static int counter = 1;
public static void main(String[] args) {
int counter = 2;
System.out.println(counter);
}
}
The output is 2 because the local variable counter shadows the static field within main.
Floating-Point Arithmetic Limitations
Double-precision floating-point numbers cannot represent all decimal fractions exact, leading to subtle errors.
public class ImpreciseDouble {
public static void main(String[] args) {
System.out.println("0.05 + 0.01 = " + (0.05 + 0.01));
System.out.println("1.0 - 0.42 = " + (1.0 - 0.42));
System.out.println("4.015 * 100 = " + (4.015 * 100));
System.out.println("123.3 / 100 = " + (123.3 / 100));
}
}
Results may include extra decimal places because double stores fractions as binary approximations. Only numbers that are powers of two in fractional form finish exactly.
Accurate Arithmetic with BigDecimal
For precise decimal calculations, BigDecimal should be constructed from a String rather than a double.
import java.math.BigDecimal;
public class AccurateDecimalDemo {
public static void main(String[] args) {
BigDecimal fromStr1 = new BigDecimal("0.05");
BigDecimal fromStr2 = BigDecimal.valueOf(0.01);
BigDecimal fromDbl = new BigDecimal(0.05);
System.out.println("String-based constructor:");
System.out.println("0.05 + 0.01 = " + fromStr1.add(fromStr2));
System.out.println("0.05 - 0.01 = " + fromStr1.subtract(fromStr2));
System.out.println("0.05 * 0.01 = " + fromStr1.multiply(fromStr2));
System.out.println("0.05 / 0.01 = " + fromStr1.divide(fromStr2));
System.out.println("\ndouble-based constructor:");
System.out.println("0.05 + 0.01 = " + fromDbl.add(fromStr2));
System.out.println("0.05 - 0.01 = " + fromDbl.subtract(fromStr2));
System.out.println("0.05 * 0.01 = " + fromDbl.multiply(fromStr2));
System.out.println("0.05 / 0.01 = " + fromDbl.divide(fromStr2));
}
}
Operations must use instance methods (add, subtract, multiply, divide) instead of arithmetic operators. A BigDecimal built from a double retains the binary approximation errors of the original value.
Concatenation and Operator Precedence
The + operator evaluates left-to-right. Mixing strings and integers changes the interpretation.
public class ConcatenationTrap {
public static void main(String[] args) {
int m = 100;
int n = 200;
System.out.println("x+y=" + m + n);
System.out.println(m + n + "=x+y");
}
}
The first line prints x+y=100200 because a string triggers concatenation for subsequent values. The second line evaluates m + n before the string, producign 300=x+y.