• 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

doubt in the advance for loop in java6

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Can anyone please tell me what will the output of the following code... i cannot try this at my PC since i don't have java 6.0 on it.
int [] p = new int [] {1,2,3,4};
for(int a : p){
System.out.println(p+" " );
}
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, please put your code between the code tags.

The enhanced for loop is a quick way of iterating over a Collection or Array for you.

As if you coded a regular for loop and within the loop retrieve the current value using the current index of the loop, the enhanced loop does that for you by assigning the current value to what is declared before the colon.

This means your example is wrong.

Instead of



it needs to be

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Erum Momin wrote:Can anyone please tell me what will the output of the following code... i cannot try this at my PC since i don't have java 6.0 on it.


You can have different version of Java on the one machine. Just make sure to update the paths before compilation/run so that the version you expect is used.
I'd recommend creating a couple of batch files/bash scripts to do this.

Was this from a book or mock exam? If so, please quote the source.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For all the details of the new for-each loop, see: The For-Each Loop
 
Erum Momin
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sebastian Janisch wrote:First, please put your code between the code tags.

The enhanced for loop is a quick way of iterating over a Collection or Array for you.

As if you coded a regular for loop and within the loop retrieve the current value using the current index of the loop, the enhanced loop does that for you by assigning the current value to what is declared before the colon.

This means your example is wrong.

Instead of



it needs to be


Yeah, i know how does teh enhanced for loop works but i just wanted to try this since in many mock tests i've found this.
 
Erum Momin
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Irwin wrote:

Erum Momin wrote:Can anyone please tell me what will the output of the following code... i cannot try this at my PC since i don't have java 6.0 on it.


You can have different version of Java on the one machine. Just make sure to update the paths before compilation/run so that the version you expect is used.
I'd recommend creating a couple of batch files/bash scripts to do this.

Was this from a book or mock exam? If so, please quote the source.


No this is not from the book... this was from examLab. But the question was asked with some other compilation error along with this type of for loop... and because of that the for loop part is not explained anywhere. I was just wondering what if this type of question comes in exam.... then would it cause a compilation error or anything else would happen?
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as i see it there is no compilation error in this. The answer will be the (name of the object)@hashcode of the reference of the array object(4 times) that's it. Its the same as printing a reference of an object.

 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output will be as Nitish says. '@' followed by some hex 4 times.

Yes, you will need to know how this kind of loop works and what happens when you invoke "toString()" on an object that does not override it.

If you are studying for SCJP6, then you really need to get JDK 6 installed and practice.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess the toString() method which was inherited from Object class is called here.

public String toString() method returns getClass().getName + '@' + Integer.toHexString(hashCode())


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic