Java OCA OCP Practice Question 233

Question

What is the output of the following?

public class Main {
   public static void main(String... args) {
      Integer integer = new Integer(4); 
      System.out.print(integer.byteValue()); 
          // w w w .j a v  a2  s. com
      System.out.print("-"); 
           
      int i = new Integer(4); 
      System.out.print(i.byteValue()); 
   }

}
  • A. 4-0
  • B. 4-4
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


C.

Note

Java does not allow calling a method on a primitive.

While autoboxing does allow the assignment of an Integer to an int, it does not allow calling an instance method on a primitive. Therefore, the last line does not compile.




PreviousNext

Related