• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does the code flow execute in the below order  RSS feed

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does the code flow execute in the below order, and what impact does the additional angular brackets have?

I do not understand why ac are output first? Any ideas? Thanks




 
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always tell us where such questions come from, to avoid copyright problems and to allow us to see the source. I presume the bad formatting was in the source; some cert book writers like to try and mislead their readers with bad formatting.
I presume you have compiled and executed that code. In which case you will have a corresponding .class file. Inspect the bytecode with javap -c _05_Question_ That will show you the order in which code is imputed as part of the constructor.

If that doesn't explain it, I shall come back later and give you some more hints.
 
Mark S.Raphael
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, sure. On this occasion, the source code has come from a mock exam for OCA which I acquired from a book that I have purchased.

Exam 1Z0-808

http://javarevisited.blogspot.ch/2015/11/5-free-ocajp8-ocpjp8-mock-exams-and-practice-questions.html#ixzz5EOf2MIzR



 
Campbell Ritchie
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for telling us the source.

Have you worked out why you got acbd yet?
 
Mark S.Raphael
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, not yet.
I will try what you have suggested shortly.
 
Campbell Ritchie
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark S.Raphael wrote:. . . I will try what you have suggested shortly.

Good, but I probably can't reply until tomorrow, so you might have a wait.
 
Mark S.Raphael
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,

The response from using the javap -c commands are as follows:

  Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>")V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: iconst_1
       1: istore_1
       2: iload_1
       3: aload_0
       4: arraylength
       5: if_icmpgt     23
       8: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
      11: aload_0
      12: iload_1
      13: aaload
      14: invokevirtual #3                  // Method java/io/PrintStream.printlnLjava/lang/String;)V
      17: iinc          1, 1
      20: goto          2
      23: return
 
Mark S.Raphael
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does the output in the previous post help me?
 
Campbell Ritchie
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's something missing. I got different results when I tried it; I had more code for the constructor. Let's try again. Which version of Java® are you using?Tick the disable smilies box
Note that in bytecode the constructors are replaced by pseudo‑methods called <init>
I hope that printout will be more helpful to you.
 
Campbell Ritchie
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had been using Java10; I got this result with Java5, whihc i sthe oldest version I appear to have installed:-That doesn't appear any different from the Java10 version, at least when you correct for the poor formatting.
 
Mark S.Raphael
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay thanks for that. I was using Java 10
 
Campbell Ritchie
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure but has it helped answer your question?
 
Mark S.Raphael
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I can see how to use this utility to assist with the analysis
 
Campbell Ritchie
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you worked it out yet?
 
Mark S.Raphael
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am clear on the code execution sequence. thanks; and thanks again for showing me the utility to debug the code
 
Campbell Ritchie
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!