wood burning stoves
The moose likes Junior/Associate Certification (SCJA,OCAJ 5/6/8) and the fly likes SAMPLE QUESTION Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Junior/Associate Certification (SCJA,OCAJ 5/6/8)


Atah Tabotnjap

Joined: Mar 17, 2008
Posts: 24
Hi all

Following is a sample question from Ralf Peters book and according to the book a, b and d are correct. Can someone please help me clarify how option 'b' can posibly be correct ?? is 'Integer' refering to a class or did they mean to say 'int'? Will appreciate clarification. Thanks

Question 3-8

Which of the given code fragments will compile without errors?
Select all correct answers:

a. Integer i1 = new Integer(10);
b. Integer i2 = 10;
c. Int i3 = new Integer(10);
d. int i4 = 10;
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Hi Ojong Tabot,

Java 1.5 introduced the autoboxing of primitive and wrapper classes. Autoboxing means you can do things like:

Boolean myBoolean = true;
char myChar = new Character('c');

Before java 1.5 you needed to do like this:

Boolean myBoolean = new Boolean(true); // or new Boolean("true");
char myChar = new Character('c').charValue();

Here is a relation of primitive types and yours wrapper classes:

byte --> Byte
short --> Short
int --> Integer
long --> Long
float --> Float
double --> Double
char --> Character
boolean --> Boolean

only the wrapper class Character has one constructor that has a char as parameter. All other have two constructors, one of your primitive type and other using a String.

Let's see what you have posted:

Originally posted by Ojong Tabot:

a. Integer i1 = new Integer(10);
b. Integer i2 = 10;
c. Int i3 = new Integer(10);
d. int i4 = 10;

a. Here is ok. It's used a constructor that has your primitive type as parameter.

b. It's ok. Here we see a boxing case, what happens here is:
Integer i2 = new Integer(10);
But remember, is done in a automatic way. (only after 1.5)

c. Wrong. There isnt a Int type.

d. Ok too, normal use of a int.

if you still continue with your doubt, feel free to ask again and i'll try to make it more clear.


Raphael Rabadan
[ July 05, 2008: Message edited by: Raphael Rabadan ]

SCJP Java 6 (98%) - Story, SCJA (88%) - Story
wood burning stoves
It's not a secret anymore!