Java Questions 41 - 50  «Prev  Next»

Primitive Values and References in Java SE 25

These eleven questions cover booleans, UTF-16 char values, strings, reference initialization, and integer conversions.

  1. Which values can a boolean have?

    Answer: The primitive boolean type has exactly two values: true and false. Java does not implicitly convert integers to booleans, so 0 and 1 are not substitutes in assignments or conditions.

    A Boolean wrapper reference can also be null, but null is not a third primitive boolean value. Unboxing a null Boolean throws NullPointerException.

  2. How are character literals written?

    Answer: Use single quotes, such as char letter = 'a';. An escape can represent a code unit, for example char letterN = '\u004E'; for N.

    Unicode escapes are processed early in source translation. A char literal represents one UTF-16 code unit, so a supplementary Unicode character generally cannot be represented as one char literal. Use code-point APIs or a String as appropriate.

  3. Can a hexadecimal integer literal initialize a char?

    Answer: Yes, when the assignment rules permit constant narrowing and the value fits 0 through 65,535. For example, char letter = 0x004E; stores the same code unit as 'N'.

    The literal is an integer literal, not a distinct "hex char" type. A nonconstant int expression generally requires an explicit narrowing cast, which can discard information if the value is outside the char range.



  4. How should char be understood in modern Unicode text?

    Answer: Char is an unsigned 16-bit integral type commonly used for a UTF-16 code unit. It is not always a complete Unicode code point or a complete user-perceived character.

    A supplementary code point uses two surrogate code units in UTF-16. A displayed character can also involve multiple code points. Choose code-point or text-boundary APIs when a task is about characters rather than individual code units.

  5. Can a numeric literal be assigned to a char if its value fits?

    Answer: A suitable integer constant expression can be narrowed during assignment when representable. For example, char letter = 65; stores 'A'. The maximum char value is 65,535, not 65,545.

    A long literal such as 65L does not qualify for the same implicit narrowing rule. A general int variable also needs a cast unless it is an eligible constant expression. Range alone does not make every source-type conversion implicit.

  6. Can Strings be written as literals?

    Answer: Yes. String greeting = "Hello"; assigns a reference to a String represented by a literal. Java also supports text blocks for suitable multiline source text. Strings are immutable.

    String literals and string-valued constant expressions are interned. That does not mean every String operation searches the intern pool or every result is newly allocated. Use equals for content comparison; == compares references.



  7. What is a reference variable?

    Answer: It is a variable of reference type, holding a reference value or null. A reference value can identify a class instance or an array.

    It does not hold another variable by reference. Assigning one reference variable to another copies the reference value, producing aliases when the value is non-null.

  8. What value does a reference variable store?

    Answer: It stores a reference, not the entire object. Multiple variables can refer to the same object, and assigning null to one variable does not change another alias.

    References are abstract language values. Java does not expose them as fixed-width physical addresses or permit pointer arithmetic. Runtime representations can differ between JVMs.


  9. Is an unassigned reference variable always null?

    Answer: No. Reference fields and newly allocated reference-array elements receive the default value null. A local reference variable must be definitely assigned before it is read.

    Even a null comparison reads the variable. String text; if (text == null) { } therefore fails to compile. Parameters receive their values from the invocation rather than from a default initialization rule.

  10. Can an int value be stored in a byte?

    Answer: Yes, if the conversion is legal. An int constant expression within -128 through 127 can be assigned directly, as in byte value = 100;. A general int expression requires an explicit cast or another permitted conversion context.

    Casting does not validate the range. For example, (byte) 128 yields -128 by retaining the low eight bits. Validate first when silent wraparound would be incorrect.

  11. How are int and char used for unsigned values and Unicode?

    Answer: Int remains a signed 32-bit primitive. Integer's unsigned utility methods interpret its bit pattern as an unsigned value for selected operations; they do not change the language type. Char stores an unsigned 16-bit value, while int can represent a full Unicode code point.

    public class UnicodeAndUnsigned {
        public static void main(String[] args) {
            String face = new String(Character.toChars(0x1F600));
            System.out.println(face.length());
            System.out.println(face.codePointCount(0, face.length()));
            System.out.println(Integer.toUnsignedString(-1));
            char letter = 65;
            System.out.println(letter);
        }
    }

    The output is 2, 1, 4294967295, and A. String's public indexing model uses UTF-16 code units; its internal storage need not be a char array.

References: JLS: character and string literals, JLS: assignment conversions, and String API.


SEMrush Software