Wrapper classes
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
Every primitive has a wrapper class: int → Integer, double → Double, char → Character,
boolean → Boolean, long → Long… They're needed where an object is required: in collections, in generics, as null.
Integer a = 5; // autoboxing: int → Integer
int b = a; // unboxing
Integer.parseInt("42"), Double.parseDouble("2.5")
Integer.MAX_VALUE, Integer.MIN_VALUE
Integer.toBinaryString(10) // "1010"
Integer.toHexString(255) // "ff"
Integer.parseInt("ff", 16) // 255 — from another number base
Character.isDigit('7'), Character.isLetter('é'), Character.toUpperCase('a')
Two traps:
1. Integer x = null; int y = x; — a NullPointerException on unboxing.
2. For wrappers, == compares references. Java caches Integers from −128 to 127, so
Integer.valueOf(127) == Integer.valueOf(127) is true, but for 128 it's false! Compare with equals.
Task. For each number from the input, print a line: the number, its binary form, its hexadecimal form and
the number of ones in the binary form (Integer.bitCount). On the last line, print the sum of the digit characters in the whole input
(with Character.isDigit and Character.getNumericValue).
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.