• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

uneasy code for beginners

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


please explain this code ...... line 2 and 3 ....wat kind of for Loop is this ??? i never created Object class either ...how this code execute ...looks uneasy...
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information.

Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Everything" in Java is an Object--so any object created can be treated as an Object.

It's a new-style "for" loop, some people call it a "for each" loop. Here you're creating a String[], treating it as an Object, then immediately casting it back into what it really is.

But the code as it stands now won't compile.
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well it compiles and run i got the output as one.two.three.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, after you edited it and added the brace.
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you mean the code is difficult to understand. Uneasy implies an emotional state, such as nervousness or fear. As for the code being difficult to understand, as David said, everything in Java is a class (except for primitive types), and they are all descended from the class Object. All you're doing is running a foreach loop and doing a cast.
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code seems weirdly formatted....



And even if you make the formatting better -- it seems to have some an unnecessary block.

Henry
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i Don't see a conditional statement to enter the loop or incrementer to increment ...
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arun Giridharan wrote:i Don't see a conditional statement to enter the loop or incrementer to increment ...



As already mentioned, this isn't the standard "for" loop.

Henry
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This kind of looping is introduced since java 1.5 . Its called "for each" loop ...The loop construct is inspired from C# language

it general sytax is

for(Type var:Iterator){
//print var or modify var

}

here var is local variable of the loop construct and Type is the Type of the variable ...it can be any Class.
Iterator can be any subclass of iterator. In java any array or collection is iterator so you can place any array at that place.
In your example an Object is downcasted to String[]..thus each element of String[] array is assigned to local variable of the loop which will be used inside the block and in next iteration next variable of String[] will be assigned to local variable and it will be again used inside the block...and so on till all the elements of the String[] array are used.

 
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
More information here: The Java 5 For-Each Loop
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

arun r mehta wrote:The loop construct is inspired from C# language


Not really; it's inspired by *every* language that has rational collection/sequence iterators.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I must confess I've never seen an array declaration without a set of square brackets on the left side... I went and compiled it to remove my doubt.

Are there any subtle differences between the following?

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Krep Lock wrote:
Are there any subtle differences between the following?



The key to understanding this is to remember that every array is an Object; i.e., you can say

Object intArray = new int[] { 1, 2, 3 };

and similarly, you can also say

Object stringArray = new String[] { "a", "b", "c" };

in both cases, you can't treat the arrays as arrays without casting them. So in the second case, you can say

String[] usableStringArray = (String[]) stringArray;

and then access the Strings in the array. But also note that for every reference type X that is a subtype of Y, X[] is a subclass of Y[] -- i.e., String[] is a subclass of Object[], so an Object[] variable can refer to a String[], and you can access the members of the array that way:

Object[] semiUsableStringArray = (Object[]) stringArray;

So although those original two lines of code look similar, they're rather different in meaning and intent.
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.....i'm not in a good sound of this statement.....still something bothers me.......type cast conversion is done right ....from Object to String[] ....... is this is a condition .... until all elements are sealed to string[] ....... it should be like do this until this condition is true........
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your "." is stuck; please fix it.

What's to understand? It's going to iterate over each element in the array.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object myObj = new String[] {"one", "two", "three"};

from the above statement , here we can hold child class object with parent reference, it means here String[] values are Object type. we already know that Object class is parent class of String class and above statement is anonymous array initialization.


for (String s : (String[])myObj)


from the above statement, it is a enhanced for loop. This loop is introduced in java 1.5 version to retieve elements from An array or from a Collection object
exactly it means if an array int[] a={10,20,30}
to print above array elements we write code like this
for(int i=0;i<a.length;i++)
System.out.println(a[i]);

instead of above for loop we can use enhanced for loop like thi way
for(int i:a)
System.out.println(i);


and one more thing from this statement ... for (String s : (String[])myObj)
here we are retrieving String values, but myObjis object type ind it stored String[]. so we need to type cast that Object type to String[] type for this (String[])myObj is written in above code..>
 
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use the code button rather than coloured text, which some people find difficult to read.
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you! Guys
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic