Tuesday, October 1, 2013

What is autoboxing and unboxing?

Autoboxing is the automatic conversion of Primitive Data Types into their respective Wrapper classes. The primitive type is boxed into a wrapper class. Unboxing is the reverse conversion where the primitive type is unboxed from the Wrapper Class. This conversion is done by Java compiler. This feature was introduced in Java 5.

Autoboxing Example:
int number1 = 10;
Integer number2 = number1;

The variable number1 of primitive type is autoboxed into a Wrapper class of type Integer for the assignment.

Unboxing Example:
Integer number3 = 5;
int number4 = number3;

The variable number3 of Integer Wrapper class type is unboxed into a primitive type for the assignment.

Monday, September 30, 2013

What are the differences between Primitive Data Type and Wrapper class? When to use what?

  1. Primitive Types are special data types built into the language. They are not objects created from a class.
  2. Wrapper Classes wrap the primitive data type into an Object of its Class.
  3. An instance of a primitive class holds the actual value of the instance, but instance of a wrapper class holds a reference to the object.
  4. Wrapper class can be null but Primitive Types can't be null. Ex: Integer can be null, int can't.
  5. Wrapper Classes have utility functions such as conversion from one type to another, which are not available in Primitive Data Types.
  6. Wrapper classes other than Boolean have constants MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type. 
When to use Primitive Type?
  • You should use primitive types unless you need an object. 
  • Wrapper classes are less efficient than primitive data types  because the value is wrapped inside an object. For example, an ArrayList<Integer> is far less efficient than an int[] array.
When to use Wrappers?
  • When a function expects Object as a parameter, only Wrapper class objects can be passed.
  • Collections cannot be created of primitive data types. The type parameter inside the angle brackets cannot be a primitive type. User Wrapper Classes.

Sunday, September 29, 2013

What are Wrapper Classes?

Wrapper Classes wrap the primitive data type into an Object of its Class. The following table lists the wrapper classes corresponding to each primitive type:

Primitive Data TypeWrapper Class
booleanBoolean
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
True to their name, wrapper classes, contain a member variable corresponding to the primitive data type. The value in this member variable can be extracted using a method. The name of the method is of the format <primitive_data_type_name>Value(). For example, the class Boolean contains a method booleanValue() which returns the value stored in the object. This has become simpler since the introduction of autoboxing feature in Java 5.

Monday, September 23, 2013

What are the Primitive Data Types?

There are eight primitive data types in Java. They are:
  1. boolean: This supports only two values true and false. This type is used for true/false conditions.
  2. byte: Represents 8 bit integer. It is useful while handling large arrays of numbers, as the memory usage is minimal.
  3. short: Represents 16 bit integer.
  4. int: Represents 32 bit integer.
  5. long: Represents 64 bit integer.
  6. float: Represents 32 bit single precision floating point.
  7. double: Represents 64 bit double precision floating point
  8. char: Represents a single Unicode character. Its size is 16 bits.
Data TypeDefault Value (for fields)Size (bits)Range
booleanfalseNot definedtrue/false
byte08-128 to 127
short016-32,768 to 32,767
int032-2,147,483,648 to 2,147,483,647 
long0L64-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float0.0f32Details coming up in a separate post.
double0.0d64Details coming up in a separate post.
char'\u0000'16'\u0000' to '\uffff'
Of these types, char and boolean are not numeric. The rest are numeric in nature.

Primitive data types are not dependent on any external libraries for usage. This is because they are built into the language itself.


Sunday, September 22, 2013

What is a Data Type?

Data is, perhaps, the most essential part of programming. All Data in Java has a data type. Consequentially, all variables also have data types. Java, being a statically typed language, requires that variables must always be declared. The declaration of a variable is the way to notify the compiler and the JVM about
  1. the types of values that can be stored,
  2. the range of values that can be stored, and
  3. the capabilities provided by the variable.
This helps determine how much memory should be allocated for the variable and if a certain operation can be performed on a variable. Thus, the compiler can detect potential bugs in the code during the compile time itself. For example, what would the result of dividing a string by a number be? 

In Java, data types are classified as
  1. Primitive Data Types
  2. Reference Data Types