ADVERTISEMENT
ADVERTISEMENT

Java find output programs (Autoboxing & Unboxing) | set 2

Find the output of Java programs | Autoboxing & Unboxing | Set 2: Enhance the knowledge of Java Autoboxing & Unboxing concepts by solving and finding the output of some Java programs.
Submitted by Nidhi, on February 04, 2021

Question 1:

public class BoxingEx {
  public static void main(String[] args) {
    char charVar = 'A';

    Char Obj = charVar;

    System.out.println(Obj);
  }
}

Output:

/BoxingEx.java:5: error: cannot find symbol
    Char Obj = charVar;
    ^
  symbol:   class Char
  location: class BoxingEx
1 error

Explanation:

The above program will generate a syntax error because we used wrapper class Char instead of Character. The Char class is not available in Java. The correct code is given below,

public class BoxingEx
{
     public static void main(String []args)
     {
        char charVar = 'A';
        
        Character  Obj = charVar;

        System.out.println(Obj);
     }
}

Question 2:

public class BoxingEx {
  public static void main(String[] args) {
    char charVar = 'A';

    Character Obj = charVar;
    Obj++;

    System.out.println(Obj);
  }
}

Output:

B

Explanation:

In the above program, we created a local variable charVar initialized with 'A'.

Character  Obj = charVar;
Obj++;

In the above statement we auto-box the variable charVar into wrapper class Character, and then increase the value of Object Obj using post-increment operator. Then the value of the object Obj will 'B' and that will be printed on the console screen.

Question 3:

public class BoxingEx {
  public static void main(String[] args) {
    long value = 'A';

    Long Obj = value;

    long value1 = Obj.longValue();

    System.out.println(value1);
  }
}

Output:

65

Explanation:

In the above program, we created a local variable value initialized with 'A'. Then auto-box value into object Obj. After that un-box the value of variable value and assigned to the variable value1 and then print variable value1 that will print 65, which is the ASCII value of character 'A'.



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.