• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

SCJP 5.0 Objective 1.3 - two questions

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In less than a month I'm gonna take SCJP 5.0 Exam. I'm studying but every day a lots of doubts appear. Two questions ABOUT Section 1: Declarations, Initialization and Scoping > Objective 1.3, to begin with:

DOUBT ONE:

int big = Integer.getInteger("7");

The Sun E Practice says that the above code is correct because autoboxing. I understand the concept of boxing (primitive to object) and unboxing (Object to primtive). But What does it do? It compiles but throws a java.lang.NullPointerException???

DOUBT TWO:

class CopyArray {
public static void main(String [] args) {
int [] x = {1, 2 ,3};
int [] y = x;
for(int i : x){ //1
System.out.println(y[i]);
}}}
Once again: I don't understand this version of the enhanced for loop. The output is
2
3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at CopyArray.main(CopyArray.java:6)

Many thanks in advance for your help!

(Spanish is my mother tongue!).
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, pal

I recommend you to start reading the API documentation about the Integer.getInteger method.

I guess you assume getInteger transforms a String into an Integer and that's a wrong asumption.

What you mean is



The second doubt is also kind of obvious.

In the first iteration of the for loop you assign to i the first value of x which is 1, then you print y[i] which is 2 then, in the next loop i assumes 2 and in the next one 3, as there is not shuch thing as y[3] in your array your program fails throwing an ArrayIndexOutOfBoundsException.

You seem to assume that i holds the current index of the array. That's wrong assumption, the variable i is actually holding, in every iteration, a value of the array.

Try this, you will see that i actually holds the values of the array in every iteration and not the current index


Keep working, pal. But I do not recommend you to rush to take the exam, you seem to need more time than a month.
[ January 12, 2006: Message edited by: Edwin Dalorzo ]
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, first, I know for a fact that Edwin is trying to help you here and is not trying to be mean. So please don't be offended by his comment.

With a lot, and I mean a lot of studying there is a possibility to pass the exam in a month. I highly suggest getting Kathy and Bert's SCJP 5.0 exam book, and reading up on a lot of the APIs like Edwin suggests. I see that you are already running code and testing it which is also very important to do.

Mark
[ January 12, 2006: Message edited by: Mark Spritzler ]
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark, I realized that I might have been kind of rude up there, so I changed my words a bit.

I am sorry Enrique, I did not mean to offend!

Keep working Enrique, I just recommend you to take you time to get ready to do the exam if you realy want to pass. If you rush into it, you might come back dissapointed. The exam is not difficult actually, but it requires you to know the java.lang API very, very well.

Try Whizlabs simulator, and as Mark recommends, get yourself a couple of books, I could share some with you if you want.

Whizlabs
[ January 12, 2006: Message edited by: Edwin Dalorzo ]
 
Enrique Villamizar
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both for your help and advice. I know I have to work really hard. I'll do it my best. These days l'll ask you many questions!
 
Mark Spritzler
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"I could share some with you if you want"

But not the copyrighted ones. Right?

Mark
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I don't know, Mark?

I guess this is copyrighted, but freely distribuited by Sun at their site.

Java Language Specification Third Edition

Java Virtual Machine Specification

I studied for my Certification Exam using those books.
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic