What are the differences between Java's primitive data types and reference types?
Java's primitive data types store actual values and are predefined by the language, with a fixed size. Reference types, such as objects and arrays, store references (or memory addresses) to the actual data. Primitive types have faster access and are stored in the stack, while reference types are stored in the heap. Additionally, primitive types cannot be null, whereas reference types can be.
What are the eight primitive data types in Java and their default values?
The eight primitive data types in Java are: byte (0), short (0), int (0), long (0L), float (0.0f), double (0.0d), char ('\\u0000'), and boolean (false).
How do Java's primitive data types affect memory allocation and performance?
Java's primitive data types are stored directly in memory, offering consistent memory size and alignment, leading to efficient memory allocation. They generally provide faster performance compared to objects due to their fixed size and lack of overhead associated with object creation, garbage collection, and object references.
Can Java primitive data types be null?
No, Java primitive data types cannot be null. They are predefined by Java and hold simple values like int, float, etc. Only their corresponding wrapper classes, like Integer or Float, can be null.
How do you convert between Java's primitive data types?
In Java, you can convert between primitive data types using casting for compatible types (e.g., `(int) doubleValue`) or using wrapper classes for more complex conversions (e.g., `Integer.toString(intValue)` for int to String conversion). For automatic conversions, Java also provides implicit type casting (widening conversion).