Java / Data types
System.out.println(1.0/0);
1. What are the primitive data types in Java ?
There are eight primitive data types. byte. short. int. long. float. double. boolean. char.
2. What is Autoboxing and Unboxing?
Autoboxing refers to the automatic conversion of primitive types to its corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double etc. This automatic conversion is accomplished by the compiler. Character ch = 'c';// char autoboxed to its wrapper clas...
3. Is Java primitive data type stored on stack or heap?
Primitive types declared locally will be on the stack while primitive types that are defined as part of an object instance are stored on the heap. Local variables are stored on stack while instance and static variables are stored on the heap.
4. What is the default value of local variables in Java?
There is no default value for local variables.
5. Can you compare a boolean with an int variable in Java?
No. you will get compilation error.
6. Difference between double and float variables in Java.
In java, float takes 4 bytes in memory while Double takes 8 bytes in memory. Float is single precision floating point decimal number while Double is double precision decimal number.
7. What is the default value of char data type in Java?
The default value of a char primitive type is '\u0000'(null character) as stated in the Java Language Specification. The shortcut for 'u0000' is '\0', So the null can be represented either by 'u0000' or '\0'. The below Java program validates null representations using instance char field 'c'. pub...
8. When does autoboxing and unboxing occur in Java?
Autoboxing and unboxing can happen where an object is expected and primitive type is provided, for example, In a method invocation where an object argument is expected but primitive values are provided, Java automatically converts primitive into its equal value Object. lassic use of autoboxing is...
9. What is the output?
System.out.println(1.0/0);
System.out.println(1.0/0);
Most of us may expect ArithmeticException, however, in this case, there will be no exception instead it prints Infinity . 1.0 is a double literal and double datatype supports infinity.
10. How primitive variables passed to methods - by value or by reference?
In Java, primitive variables are passed to methods by value. If the passed value changes in the method, it does not change the original value.