Java OCA OCP Practice Question 205

Question

Suppose myShort is a short and wrapped is a Short.

Which of the following are legal Java statements?

Choose all correct options.

  • A. myShort = wrapped;
  • B. wrapped = myShort;
  • C. myShort = new Short((short)9);
  • D. myShort = 9;


A, B, C, D.

Note

All four statements are legal.

A and C are examples of unboxing, which allows assignment from a wrapper to a primitive.

B is an example of boxing, which allows assignment from a primitive to a wrapper.

D is assignment of a literal int to a short, which is legal because the right-hand side of the assignment is a literal rather than a variable.




PreviousNext

Related