...........................Back to home................................

What is Autoboxing (boxing) and unboxing in Java?

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object.
Wrapper classes are those whose objects wraps a primitive data type within them. In the java.lang package java provides a separate class for each of the primitive data type namely Byte, Character, Double, Integer, Float, Long, Short.

Converting primitive datatype to object is called boxing.
Character obj = 'a';
Integer obj = new Integer ("2526");
Integer a3=5;

Whereas, converting an object into corresponding primitive datatype is known as unboxing.
public class Sample {
public static void main (String args[]){
Integer obj = new Integer("2526");
int i = obj.intValue();

Scanner Class in Java

Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double etc. and strings. It is the easiest way to read input in a Java program, though not very efficient if you want an input method for scenarios where time is a constraint like in competitive programming.
To create an object of Scanner class, we usually pass the predefined object System.in, which represents the standard input stream. We may pass an object of class File if we want to read input from a file.
To read numerical values of a certain data type XYZ, the function to use is nextXYZ(). For example, to read a value of type short, we can use nextShort()
To read strings, we use nextLine().
To read a single character, we use next().charAt(0). next() function returns the next token/word in the input as a string and charAt(0) funtion returns the first character in that string.

Method Inputs
nextInt() Integer
nextFloat() Float
nextDouble() Double
nextLong() Long
nextShort() Short
next() Single word
nextLine() Line of Strings
nextBoolean() Boolean

Java Data Type Casting Type Conversion

Summary: By the end of this tutorial "Java Data Type Casting Type Conversion", you will be comfortable with converting one data type to another either implicitly or explicitly.

Java supports two types of castings – primitive data type casting and reference type casting. Reference type casting is nothing but assigning one Java object to another object. It comes with very strict rules and is explained clearly in Object Casting. Now let us go for data type casting.

Java data type casting comes with 3 flavors.

  1. Implicit casting
  2. Explicit casting
  3. Boolean casting.

1. Implicit casting (widening conversion)

A data type of lower size (occupying less memory) is assigned to a data type of higher size. This is done implicitly by the JVM. The lower size is widened to higher size. This is also named as automatic type conversion.


        int x = 10;                    // occupies 4 bytes
        double y = x;                  // occupies 8 bytes
        System.out.println(y);         // prints 10.0

In the above code 4 bytes integer value is assigned to 8 bytes double value.

2. Explicit casting (narrowing conversion)

A data type of higher size (occupying more memory) cannot be assigned to a data type of lower size. This is not done implicitly by the JVM and requires explicit casting; a casting operation to be performed by the programmer. The higher size is narrowed to lower size.

       double x = 10.5;             // 8 bytes
       int y = x;                   // 4 bytes ;  raises compilation error

In the above code, 8 bytes double value is narrowed to 4 bytes int value. It raises error. Let us explicitly type cast it.

       double x = 10.5;                  
       int y = (int) x;

The double x is explicitly converted to int y. The thumb rule is, on both sides, the same data type should exist.

3. Boolean casting

A boolean value cannot be assigned to any other data type. Except boolean, all the remaining 7 data types can be assigned to one another either implicitly or explicitly; but boolean cannot. We say, boolean is incompatible for conversion. Maximum we can assign a boolean value to another boolean.

Following raises error.

       boolean x = true;
       int y = x;                    // error
       boolean x = true;
       int y = (int) x;           // error

byte –> short –> int –> long –> float –> double

In the above statement, left to right can be assigned implicitly and right to left requires explicit casting. That is, byte can be assigned to short implicitly but short to byte requires explicit casting.

Following char operations are possible
public class Demo
  public static void main(String args[])
    char ch1 = 'A';
    double d1 = ch1;
    System.out.println(d1);              // prints 65.0
    System.out.println(ch1 * ch1);       // prints 4225 , 65 * 65
    double d2 = 66.0;
    char ch2 = (char) d2;
    System.out.println(ch2);             // prints B




 The operators of the Java programming language, it may be helpful for you to know ahead of time which operators have the highest precedence. The operators in the following table are listed according to precedence order. The closer to the top of the table an operator appears, the higher its precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. Operators on the same line have equal precedence. When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.

Operator Precedence




expr++ expr--


++expr --expr +expr -expr ~ !


* / %


+ -


<< >> >>>


< > <= >= instanceof


== !=

bitwise AND


bitwise exclusive OR


bitwise inclusive OR


logical AND


logical OR



? :


= += -= *= /= %= &= ^= |= <<= >>= >>>=

In general-purpose programming, certain operators tend to appear more frequently than others; for example, the assignment operator "=" is far more common than the unsigned right shift operator ">>>". With that in mind, the following discussion focuses first on the operators that you're most likely to use on a regular basis, and ends focusing on those that are less common. Each discussion is accompanied by sample code that you can compile and run. Studying its output will help reinforce what you've just learned.