Separate variables from objects and review decimal, hexadecimal, binary, octal, and floating-point literals.
Answer: The JVM model includes more than a stack and heap. Class instances and arrays are allocated from the heap; method invocations use frames with local-variable storage and operand stacks. Class and method information belongs to the method area in the specification.
Primitive values can occur in object fields and arrays as well as locals. References can also appear in all those places. Actual JVM implementations may optimize storage, eliminate allocations, or keep values in registers while preserving required behavior.
Answer: Distinguish instance fields, static fields, local variables, parameters, and array elements. Also distinguish a variable holding a value from an object to which a reference points.
These categories are not themselves three Java data types. Reference types include class types, interface types, type variables, and array types. Primitive types form a separate category.
Answer: Instance fields are part of each object's state, whether they hold primitive or reference values. Local variables belong to an invocation's execution context in the JVM's frame model.
A local reference can point to a heap object; its locality does not make the object a local variable. Avoid promising an exact physical address or allocation strategy, because runtime optimization can change representation.
Answer: Java supports decimal, hexadecimal with 0x or 0X, binary with 0b or 0B, and octal with a leading zero. Underscores can separate digits in permitted positions.
public class LiteralBases {
public static void main(String[] args) {
int decimal = 122;
int hex = 0x7A;
int binary = 0b1111010;
int octal = 0172;
System.out.println(decimal + " " + hex + " " + binary + " " + octal);
}
}
The output is 122 122 122 122. The base affects source notation, not the stored variable's type or a permanent display format.
Answer: int length = 10; declares an int using a decimal literal. long distance = 10L; uses a long literal. Prefer uppercase L because lowercase l resembles the digit 1.
int length = 10L; does not compile without a suitable explicit conversion. A leading zero such as 0172 instead denotes octal, so it is not an example of decimal notation.
Answer: Use a leading zero followed by octal digits, 0 through 7. For example, 0172 means decimal 122: one times 64, seven times 8, plus two.
A token such as 08 is not a valid integer literal. Leading zeros in source are therefore significant rather than decorative. Ordinary decimal output does not retain the literal's original octal spelling.
Answer: No. A through F and a through f both represent 10 through 15. The prefix can be 0x or 0X. Thus 0x1c and 0X1C both represent decimal 28.
The complete letter mapping is A=10, B=11, C=12, D=13, E=14, F=15. Hexadecimal notation is useful for bit patterns, but does not turn int into an unsigned primitive type.
Answer: Without an L or l suffix, an integer literal has type int, subject to the literal's permitted range. The suffix makes it long. This applies to binary literals as well as decimal, octal, and hexadecimal forms.
For example, long value = 3_000_000_000L; needs the suffix; assigning an oversized unsuffixed token to a long variable does not fix the token. Nondecimal literals can denote signed bit patterns such as 0xffffffff, which is int -1.
Answer: Decimal forms include 0.1, 1e3, and 2.0f; a decimal point is not always required. Hexadecimal floating-point literals use a binary exponent marked by p or P, such as 0x1.0p3 for 8.0.
An unsuffixed floating-point literal is double. Use f or F for float; d or D explicitly selects double. Binary floating-point does not represent every decimal fraction exactly.
Answer: Their formats use 32 and 64 bits respectively, so double uses twice the format width. Float has 24 bits of significand precision, while double has 53; double also has a larger exponent range.
float value = 498.47F; is a complete primitive declaration. Using Float instead would introduce a wrapper reference through boxing. Format width should not be confused with the total memory overhead of a wrapper object.
References: JVMS: runtime data areas, JLS: literals, and JLS: types and values.