File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in assertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in assertion " Watch "Doubt in assertion " New topic

Doubt in assertion

sangeetha raj

Joined: Feb 23, 2012
Posts: 16
Am new to this forum..I am preparing for scjp 6..I have a doubt in the following code..

I found that there is an error in line 6,because we can't input an array into an int..But they have given its correct in the scjp 6 (kathy sierra) book , self test section question number 10..Please clear my doubt..correct me if am wrong..Thanks in advance..

(In the book)They have given this as answer : 1 1 3 3 9 9

public class Circles {
4. public static void main(String[] args) {
5. int[] ia = {1,3,5,7,9};
6. for(int x : ia) {
7. for(int j = 0; j < 3; j++) {
8. if(x > 4 && x < 8) continue;
9. System.out.print(" " + x);
10. if(j == 1) break;
11. continue;
12. }
13. continue;
14. }
15. }
16. }

Zeeshan Sheikh
Ranch Hand

Joined: Nov 20, 2011
Posts: 144

I am not sure why you think there is an error on line 6. Did you try to compile it yourself. All its doing passing reference of ia to int x which in turn you will get value at specific index. Try this

do after first loop and see the values.

Hope this helps.

MySQL Blog
Anayonkar Shivalkar

Joined: Dec 08, 2010
Posts: 1545

Hi sangeetha raj,

Welcome to CodeRanch!

I see that title of your post is 'Doubt in assertion', however, I do not see a single assertion in code provided by you?

Are you sure you have doubt in assertion? and that you have posted correct code?

Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Zeeshan Sheikh
Ranch Hand

Joined: Nov 20, 2011
Posts: 144

.I have a doubt in the following code...

I was going by above line
Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

Hi Sangeetha. Welcome to The Ranch!

Are you saying that you think that for(int x : ia) { ... } is assigning the array ia to x? That would cause an error, but that's not what's happening. This is the enhanced for loop that was introduced in Java 5, and it's assigning each element of ia in turn to x.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
For the purpose of the exam, enhanced loop is for array or any object that extends the Iterable interface such as list.
Here are the valid statement for enhanced for loop:
for (int x : ia) where ia is int [] ;
for (int[] x: ia) where ia is int[][];
for (Object x: list) where list is linked list or arraylist.
for (Object x : myHashSet) where myHashSet is a HashSet.

However, in the exam, you may have this:
for (Object x: myHashMap) where myHashMap is a HashMap type.
It seems to work. But it won't compile because HashMap does not implement Iterable interface.
For more detail, please refer to Mughal and Rasmussen's study guide.
Correct me if I am wrong.

I agree. Here's the link:
subject: Doubt in assertion
It's not a secret anymore!