• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SAMPLE QUESTION

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Your's,

Raphael Rabadan
[ July 05, 2008: Message edited by: Raphael Rabadan ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic