• 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

For Loop

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


Mex is a non abstract class having no-arg constructor.

Anyone please explain this to me. I thought the for loop runs sevaral times as it is creating new Mex[] array every time.

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


Means



and

new Mex(){} is anonymous class that is extending Mex class. And Mex class has default constructor added by compiler.>
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm afraid punit that there is a big difference in the actual code generated by the compiler and what you gave. It might look small but it might result in confusion with another concept. The actual code will look like this



That's why this code compiles fine



Also look carefully, there is no anonymous inner class in his code...
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I did not decompile this, I just imagined it should have this way, thanks for reminding me that I have decompiler also
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit I think that depends on decompiler also as:



for this cavaj decompiler is showing this code:


and jd-gui-0.2.6 decompiler is showing this code:



When I was writing above explanation than I though declaring ouside will be more efficient.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But buddy did you pay attention to the final example that I gave. In that case the outside declaration will fail as the final field will be changed on each iteration...
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just try out Ankit on your pc, here I am not getting output what you are expecting.

See I made the field final, I did not get any difference.

Actual code:



Decompiled in cavaj decompiler:



Decompiled in jd decompiler:
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is ridiculous. cavaj decompiler is showing me the same thing as showing to you. But this is weird that different decompilers show different things
 
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

Ankit Garg wrote:But this is weird that different decompilers show different things


The transformation from Java source code to Java byte code that a Java compiler does, is not exactly one to one. Therefore, it's not possible to get the exact original source code back from the byte code. Different decompilers might interpret the byte code a little differently, and show different decompiled source code.

You can use the javap tool included with the JDK to see what the byte code looks like. Try something like:

javap -c ClassName
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good news! Last version of JD-GUI (0.2.8) can decompiled for-each loops.

Original source code:

Decompiled source code:
reply
    Bookmark Topic Watch Topic
  • New Topic