Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes for loop problem 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 "for loop problem" Watch "for loop problem" New topic

for loop problem

geet kaur
Ranch Hand

Joined: Sep 03, 2008
Posts: 79

aren't both the for loops initialisng the array elements?
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
the jdk1.5 for loop is used for read only purpose and not the changes the data or values.

My Techie Blog - Java Questions
geet kaur
Ranch Hand

Joined: Sep 03, 2008
Posts: 79
hey thanks !!!
got it
Manju Kavi
Ranch Hand

Joined: Sep 25, 2008
Posts: 33
for each loop is used for iterating purpose, an advanced version of old for loop..
suresh pilakal babu
Ranch Hand

Joined: Jul 01, 2008
Posts: 31
Hi Geet,

In below for loop

for(Dog a og){
a=new Dog();
}//but if i use this one then the output is null

you are not initialising dog array, but you are assign the Dog object to the variable 'a'. thats why displaying 'null'.

if you want to initialse dog array you have modify like this

int i = 0;
for(Dog a og){
a=new Dog();
dog[i++] = a;
I agree. Here's the link:
subject: for loop problem
It's not a secret anymore!