• Post Reply Bookmark Topic Watch Topic
  • New Topic

uneasy code for beginners  RSS feed

 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • 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...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
OK
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
well it compiles and run i got the output as one.two.three.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, after you edited it and added the brace.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
i Don't see a conditional statement to enter the loop or incrementer to increment ...
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
arun r mehta
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • 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.

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More information here: The Java 5 For-Each Loop
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
Krep Lock
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • 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?

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
satish varma
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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..>
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you! Guys
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!