Wednesday, January 17, 2007

SCJP 5.0 Mock Questions - Enum

SCJP 5.0 Mock Questions - Enum

SCJP 5.0 Mock Questions - Enum


1) Autoboxing/Unboxing is used for converting the primitive types to wraper vise versa without explicit cast.
2) When we use the boxing conversions, the casting is done behined the scenes.
3) Primtive type methods takes the higher priority when one method's parameters primitive type and another has the wrapper type.
eg:
void method(int i); // will be called
void method(Integer i);
4)Same case above, autoboxing takes the higher priority then varargs.
eg:
void method(Integer i); // will be called
void method(Integer i ...);
5)When wrapper type value is null, boxing conversion will throw Null pointer exception.
eg:
Integer i = null;
int k = i; // throws null pointer exception

6)Don't use boxing conversion inside forloop, it will affect the performance. Internally, JVM does the
casting for each iteration.

7)Boxing conversions is included in the Java 5.0(Tiger) to save the development time and reduce the code.
It is not replacement for the primitive types.

6)Consider the following code fragment:

   Integer i1 = 100;
Integer i2 = 100;
Integer i3 = 1000;
Integer i4 = 1000;
System.out.println(i1==i2);
System.out.println(i3==i4);

Can you guess what will be printed on the screen? If your answer is false--well, you're wrong.

In this case, J2SE 5.0 works differently. Certain ranges of values are stored as immutable objects by the Java Virtual Machine. So, in this case, the output is:

true
false

Normally, when the primitive types are boxed into the wrapper types, the JVM allocates memory and creates a new object. But for some special cases, the JVM reuses the same object.

The following is the list of primitives stored as immutable objects:

  • boolean values true and false
  • All byte values
  • short values between -128 and 127
  • int values between -128 and 127
  • char in the range u0000 to u007F
7)

Boxing applies only to the java.lang.Boolean wrappers in evaluating expressions involving logical operators -- AND (&&), OR(||) NOT (!). So, the following code would be perfectly alright in Tiger and prints The condition evaluates to false.
Boolean aBoolean = true;
Boolean anotherBoolean = false;
if (aBoolean && anotherBoolean){
out.println("The condition evaluates to true");
} else {
out.println("The condition evaluates to false");
}
8)
Primitive widening uses the "smallest" method argument possible.
Used individually, boxing and var-args are compatible with overloading.
You CANNOT widen from one wrapper type to another. (IS-A fails.)
You CANNOT widen and then box. (An int can't become a Long.)
You can box and then widen. (An int can become an Object, via Integer.)
You can combine var-args with either widening or boxing.

Source : www.javabeat.net
Author : Krishna Srinivasan(krishnas@javabeat.net)



No comments: