Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Interviewquestions Corejava Core Java Interview Question, Interview Question

 
 

Share on Google+Share on Google+

Core Java Interview Question, Interview Question

Advertisement
This page discusses - Core Java Interview Question, Interview Question

Core Java Interview Question Page 28

     

Conversion Casting and Promotion

Question: What are wrapped classes

Answer: Wrapped classes are classes that allow primitive types to be accessed as objects.

Question: What are the four general cases for Conversion and Casting

Answer: 

Conversion of primitives
Casting of primitives
Conversion of object references
Casting of object references

Question: When can conversion happen

Answer: 

It can happen during

Assignment
Method call
Arithmetic promotion

Question: What are the rules for primitive assignment and method call conversion

Answer: 

A boolean can not be converted to any other type
A non Boolean can be converted to another non boolean type, if the conversion is widening conversion
A non Boolean cannot be converted to another non boolean type, if the conversion is narrowing conversion
See figure below for simplicity
assignment
Question: What are the rules for primitive arithmetic promotion conversion

Answer: 

For Unary operators :

If operant is byte, short or a char it is converted to an int
If it is any other type it is not converted

For binary operands :

If one of the operands is double, the other operand is converted to double
Else If one of the operands is float, the other operand is converted to float
Else If one of the operands is long, the other operand is converted to long
Else both the operands are converted to int

Question: What are the rules for casting primitive types

Answer: 

You can cast any non Boolean type to any other non boolean type
You cannot cast a boolean to any other type; you cannot cast any other type to a boolean

Question: What are the rules for object reference assignment and method call conversion

Answer: An interface type can only be converted to an interface type or to object. If the new type is an interface, it must be a superinterface of the old type. A class type can be converted to a class type or to an interface type. If converting to a class type the new type should be superclass of the old type. If converting to an interface type new type the old class must implement the interface. An array maybe converted to class object, to the interface cloneable, or to an array. Only an array of object references types may be converted to an array, and the old element type must be convertible to the new element.

Question: What are the rules for Object reference casting

Answer: Casting from Old types to Newtypes
Compile time rules

  • When both Oldtypes and Newtypes are classes, one should be subclass of the other
  • When both Oldtype ad Newtype are arrays, both arrays must contain reference types (not primitive), and it must be legal to cast an element of Oldtype to an element of Newtype
  • You can always cast between an interface and a non-final object

Runtime rules

  • If Newtype is a class. The class of the expression being converted must be Newtype or must inherit from Newtype
  • If NewType is an interface, the class of the expression being converted must implement Newtype
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 18, 2011

Related Tutorials

Discuss: Core Java Interview Question, Interview Question  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com