Câu hỏi phỏng vấn Java
Câu hỏi

What are the Data Types supported by Java? What is Autoboxing and Unboxing?

Câu trả lời

In Java, there are two main categories of data types: primitive data types and reference data types.

Primitive Data Types

Java supports eight primitive data types:

  1. byte: An 8-bit signed integer with a minimum value of -128 and a maximum value of 127.
  2. short: A 16-bit signed integer with a minimum value of -32,768 and a maximum value of 32,767.
  3. int: A 32-bit signed integer with a minimum value of -2^31 and a maximum value of 2^31-1.
  4. long: A 64-bit signed integer with a minimum value of -2^63 and a maximum value of 2^63-1.
  5. float: A single-precision 32-bit IEEE 754 floating-point.
  6. double: A double-precision 64-bit IEEE 754 floating-point.
  7. boolean: A data type that can only hold two possible values: true or false.
  8. char: A single 16-bit Unicode character.

Reference Data Types

Reference data types in Java include classes, interfaces, and arrays. These types refer to objects and hence they are called reference types. The value of a reference type is a reference to an object in memory, not the object itself.

Autoboxing and Unboxing

Autoboxing and unboxing are processes that automatically convert between a primitive type and its corresponding wrapper class in Java. Wrapper classes are part of the java.lang package and provide a way to use primitive data types as objects.

Autoboxing

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If you assign a primitive value to a variable of the corresponding wrapper class, the compiler automatically converts the primitive value to an object of that class.

Unboxing

Unboxing is the reverse process where the object of a wrapper class is converted back to a primitive type. This happens when an object of a wrapper class is used in a context where a primitive value is required, such as in an arithmetic operation or a method argument that expects a primitive value.

Here is an example of autoboxing and unboxing in Java:...

junior

junior

Gợi ý câu hỏi phỏng vấn

middle

What is the difference between an Applet and a Servlet?

middle

What’s the difference between Enumeration and Iterator interfaces?

middle

What is difference between Array and ArrayList ? When will you use Array over ArrayList ?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào