File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Reference Variable Casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reference Variable Casting" Watch "Reference Variable Casting" New topic

Reference Variable Casting

Nelo Angelo
Ranch Hand

Joined: Jul 25, 2011
Posts: 44

This block of code is given in SCJP Sun Certified Programmer for Java 6 Exam - Mc Graw Hill.

I am not able to understand what some of the lines actually does.

I love java but she hates me... :'(
Zachary Pepin

Joined: Jul 29, 2011
Posts: 5
As for your first question, I'm not sure why they created two Animal objects.

As for the loop, basically, it is meant to be a simpler syntax for iterating over a set of objects. You can think of the colon as being like the word "in", so it is saying something like "for each Animal in a, do this". For an array like this, it is shorter than writing something like the following.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15101

See: The For-Each Loop. This was a new feature of Java 5.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Nelo Angelo
Ranch Hand

Joined: Jul 25, 2011
Posts: 44

@Zachary n @Jesper
Thanks a lot

Very nicely illustrated.
I agree. Here's the link:
subject: Reference Variable Casting
It's not a secret anymore!