Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Be the JVM: what is the output

 
Eddie Davis
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked at an earlier post on this same problem but still didn't understand. How do I iterate through this to get the output 13 15 x =6
Sorry I know this is really simple. Also what is the difference between ++y and Y++. Are they both meaning y + 1

Earlier post http://www.coderanch.com/t/412112/Beginning-Java/java/What-output#1815575

Thank you to anyone who can explain this to me!

 
Jeff Storey
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eddie,

Which part are you confused about? I think the answer to your second question about ++y and y++ will help you out here. ++y means to pre-increment Y (before it's used in the statement), and y++ means to post-increment it (after it's used in the statement).

So,



Jeff
 
Campbell Ritchie
Sheriff
Pie
Posts: 49451
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The increments are not done "before" and "after" the statements for ++i and i++. In both cases the increment is done before anything else, since those operators have high precedences.

The problem with i++ is that there are two values involved. There is the value of i and the value of i++. If you start with int i = 3; then the value of i is increased by ++i and i++. So as soon as you reach either of those constructs, the value of i is 4. But the value of ++i and i++ in their locations in the statements are different. In the case of ++i, the value of the expression "++i" is the same as the new value of i, viz 4. This is quite easy to understand. The case of i++, however, causes no end of confusion, and lots of people ask that sort of question here.

In its location in the statement, the value of the expression "i++" is the old value of i, so when you do arithmetic involving i++ it counts as 3.

This is useful behaviour when you are dealing with arrays. You can have an array you are adding elements to, and you have a variable count which tells you how many elements have already been added.When you add anything to this array, it goes into the array at the position of "count", remembering that the index of an array is one less than the number of elements so far. So using count++ puts the element in the correct position in the array, and also increases the count. If you used ++count, it would put the element in the wrong location.

By the way, Jeff Storey, are you sure you had "++x = " + x++ the right way round?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is another source of wisdom.
 
Eddie Davis
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for the help. I see stepping through the code how we get 13 and 15 now that I understand ==i and i++ but I fail to see where the 6 is coming from in the output.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49451
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

For your next trick, my glamorous assistant will produce a pencil and paper. Now write down the stages that method goes through to get the output. And rememberwill print 3.
 
Eddie Davis
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand the code snippet you just provided but still don't see how we end up with 6, providing the final output of 13 15 x = 6
When the code above becomes true x (by my count) would = 8 and not 6

Sorry.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eddie:

x will equal 6 at the end. This table should help illustrate what's going on:


Since the value of x is printed when the value of y>14, the only time it can print is when x = 6.

John.
 
Eddie Davis
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How did the value of y jump from 11 to 13? Thanks
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eddie:

Well, y gets incremented on every pass through the loop, but when x = 5, the condition at line 18 is triggered, so line 20 is run, which increments y and prints the value. So y jumps from 11 to 13. The same thing happens on the next pass through the loop since x = 6, so y jumps from 13 to 15. Since y > 14, the condition at line 22 is triggered, which runs lines 24 (printing out the value of x, which is 6), and 25 (breaking out of the loop).

John.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49451
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to get your paper and pencil out again. Follow all the iterations of the loop, particularly now you know the answers. Also look at the older thread.
 
Eddie Davis
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah! I see it , I finally see it! Thank you so much for sticking with me, each of you. All of your instructions, tips and code snippets helped me. It's really appreciated!
 
Eddie Davis
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just one more question. (promise) I noticed that I can compile the code but when I attempt to run it I see these error's. I understand that this is just a lesson in reading a loop but mistakenly thought that since it compiled it might run and display the output.

Thanks

Errors seen in Terminal.

Exception in thread "main" java.lang.NoClassDefFoundError: output (wrong name: OutPut)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:700)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:254)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:399)
william-daviss-macbook:Java topoftiger$







 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eddie:

Did you save your class file as output.class? That might be the problem.

John.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the name of your class file you are trying to run ? Check whether that's available in the classpath.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49451
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John de Michele wrote:Did you save your class file as output.class?
Agree it looks as if there is a naming difference somewhere, but surely it's in the .java file, not the .class file? Or writing the wrong name in "java OutPut", maybe. You have to stick with the unusual spelling of output as OutPut if that's what the code says.
 
Eddie Davis
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ugh, I didn't notice that I named the Java file "output.java" but the class is actually OutPut. It's working now! Thanks all of you, for helping me out. Now on to the next chapter.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree it looks as if there is a naming difference somewhere, but surely it's in the .java file, not the .class file? Or writing the wrong name in "java OutPut", maybe. You have to stick with the unusual spelling of output as OutPut if that's what the code says.


D'oh! Yes, that's what I meant .

John.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic