Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How does Java read code?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Does Java read line by line starting from the top of the code? I.e.



It will read "while (x<5)" first then "y=y+x", then println's, then "x=x+1"? It confuses me? Or does it read it all in one go?

Thank you,

Zukky.
 
Saloon Keeper
Posts: 12251
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "reading", or "read it all in one go"?

The java compiler translates code into byte code, which pretty much is a very simple set of instructions like "move data from memory address 'a' to memory address 'b'" and "jump to the instruction at line 'c' if the data in register 'd' equals 0". This code is then executed instruction by instruction by the Virtual Machine, which may do a number of fancy optimizations such as reordering lines, pipelining or even predicting instructions, etc. It's really quite complex.

Note that a line such as x++; may actually be translated into multiple instructions.
 
Ranch Hand
Posts: 71
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

In a while loop, the program goes from the top of the loop to the bottom until X < 5 is no longer true and is false. After the condition is false the program jumps out of the loop and continues on to the bottom of the program. Every time it goes to the top of the loop it asks the question is X < 5 if it is true it will do one more iteration.

the X= X+ 1; statement in our program is where X is incremented by one.

When working with loops be careful that you do not get into a infinite loop by leaving this this statement out X= X+ 1; this statement advances the value of X.

Because then the loop will be always true and you will be in the loop until you stop the program on your compiler. I have done this many of times


Hope this helps!

Bill
 
Stephan van Hulst
Saloon Keeper
Posts: 12251
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for loops, they're usually translated to conditional jumps. The first piece of Java gets converted to the second piece of bytecode:

Instruction 2 puts the value of a on the stack, and instruction 3 puts 5 on the stack. Instruction 4 takes two values off the stack (a and 5) and compares them, and if 5 is greater or equal to a, then it jumps to instruction 13, which returns from the method. otherwise it continues to instruction 7, which increases the value of a, after which the code jumps back to instruction 2.
 
Zukky Bee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:What do you mean by "reading", or "read it all in one go"?

The java compiler translates code into byte code, which pretty much is a very simple set of instructions like "move data from memory address 'a' to memory address 'b'" and "jump to the instruction at line 'c' if the data in register 'd' equals 0". This code is then executed instruction by instruction by the Virtual Machine, which may do a number of fancy optimizations such as reordering lines, pipelining or even predicting instructions, etc. It's really quite complex.

Note that a line such as x++; may actually be translated into multiple instructions.



I don't think you guys got what I was trying to say, maybe i'm confused but basically what I am trying to say is:

- What goes first - 1) y=y+x or 2) x=x+1? The reason why I ask this:

- The output of the code snippet posted earlier is:
00 11 23 36 410

How can the value of y be "3" in "23"? y=y+x = which is 1 + 1 (from previous value), which equates to 2. So surely the answer should be 2 (i.e. 22)? However, it seems like x=x+1 is firing first, then the y=y+x is firing second. Thus making x=2 and y=1 equating to 2 + 1 = 3 (i.e. 23). Do you understand? It seems to have an order of code execution. This is what I am trying to understand?

 
author
Posts: 23882
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Zukky Bee wrote:
How can the value of y be "3" in "23"? y=y+x = which is 1 + 1 (from previous value), which equates to 2. So surely the answer should be 2 (i.e. 22)? However, it seems like x=x+1 is firing first, then the y=y+x is firing second. Thus making x=2 and y=1 equating to 2 + 1 = 3 (i.e. 23). Do you understand? It seems to have an order of code execution. This is what I am trying to understand?



If your point of reference is between the print statements, then yes, "x = x + 1" is firing first... think about it, your code prints output, then, it does "x = x + 1", loops around to the top of the loop, and then "y = y + x".

Henry
 
Zukky Bee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

Zukky Bee wrote:
How can the value of y be "3" in "23"? y=y+x = which is 1 + 1 (from previous value), which equates to 2. So surely the answer should be 2 (i.e. 22)? However, it seems like x=x+1 is firing first, then the y=y+x is firing second. Thus making x=2 and y=1 equating to 2 + 1 = 3 (i.e. 23). Do you understand? It seems to have an order of code execution. This is what I am trying to understand?



If your point of reference is between the print statements, then yes, "x = x + 1" is firing first... think about it, your code prints output, then, it does "x = x + 1", loops around to the top of the loop, and then "y = y + x".

Henry





WHY is it firing "x=x+1" first is my question? y=y+x is at the top? Surely it should do that first, then x=x+1? Does Java execute from top line and then down (i'm guessing not because of the way the code is being executed?)
 
lowercase baba
Posts: 12893
63
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This code fires in order, over and over. so you get here with both x and y set to 0.
Since x is less than 5, we enter the loop.
y gets set to the sum of y + x, or 0 + 0, or 0.
We print "00"
we print "x = 0 y = 0 |"
we set x to whatever x + 1 equals, or 0 + 1 or just 1, so x is now 1.
we loop back to the top.

Since x is less than 5, we enter the loop.
y gets set to the sum of y + x, or 0 + 1, or 1.
We print "11"
we print "x = 1 y = 1 |"
we set x to whatever x + 1 equals, or 1 + 1 or just 2, so x is now 2.
we loop back to the top.

Since x is less than 5, we enter the loop.
y gets set to the sum of y + x, or 1 + 2, or 3.
We print "23"
we print "x = 2 y = 3 |"
we set x to whatever x + 1 equals, or 2 + 1 or just 3, so x is now 3.
we loop back to the top.

Since x is less than 5, we enter the loop.
y gets set to the sum of y + x, or 3 + 3, or 6.
We print "36"
we print "x = 3 y = 6 |"
we set x to whatever x + 1 equals, or 3 + 1 or just 4, so x is now 4.
we loop back to the top.

Since x is less than 5, we enter the loop.
y gets set to the sum of y + x, or 6 + 4, or 10.
We print "410"
we print "x = 4 y = 10 |"
we set x to whatever x + 1 equals, or 4 + 1 or just 5, so x is now 5.
we loop back to the top.

Since x is NOT less than 5, we break out of the loop.
 
Zukky Bee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote:
This code fires in order, over and over. so you get here with both x and y set to 0.
Since x is less than 5, we enter the loop.
y gets set to the sum of y + x, or 0 + 0, or 0.
We print "00"
we print "x = 0 y = 0 |"
we set x to whatever x + 1 equals, or 0 + 1 or just 1, so x is now 1.
we loop back to the top.

Since x is less than 5, we enter the loop.
y gets set to the sum of y + x, or 0 + 1, or 1.
We print "11"
we print "x = 1 y = 1 |"
we set x to whatever x + 1 equals, or 1 + 1 or just 2, so x is now 2.
we loop back to the top.

Since x is less than 5, we enter the loop.
y gets set to the sum of y + x, or 1 + 2, or 3.
We print "23"
we print "x = 2 y = 3 |"
we set x to whatever x + 1 equals, or 2 + 1 or just 3, so x is now 3.
we loop back to the top.

Since x is less than 5, we enter the loop.
y gets set to the sum of y + x, or 3 + 3, or 6.
We print "36"
we print "x = 3 y = 6 |"
we set x to whatever x + 1 equals, or 3 + 1 or just 4, so x is now 4.
we loop back to the top.


Since x is less than 5, we enter the loop.
y gets set to the sum of y + x, or 6 + 4, or 10.
We print "410"
we print "x = 4 y = 10 |"
we set x to whatever x + 1 equals, or 4 + 1 or just 5, so x is now 5.
we loop back to the top.

Since x is NOT less than 5, we break out of the loop.



Thank you very much for the pseudocode. It made sense. So it does fire the code in a top down order. You missed out "36", so I just added it in, if you can amend your answer so people that are reading this don't get confused, that would be awesome.

Question here is - does Java read all of its code from top-down?
 
Henry Wong
author
Posts: 23882
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Zukky Bee wrote:
WHY is it firing "x=x+1" first is my question? y=y+x is at the top? Surely it should do that first, then x=x+1? Does Java execute from top line and then down (i'm guessing not because of the way the code is being executed?)




It has already done the "y=y+x" at the top *before* the print statement. After the print statement, it must finish the "x=x+1" first, before it can go and execute next loop, and then do "y=y+x" for the next iteration.

Henry
 
Stephan van Hulst
Saloon Keeper
Posts: 12251
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Zukky Bee wrote:Question here is - does Java read all of its code from top-down?



Java executes method bodies from top to bottom, and may "jump" back up on finishing a loop body.
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Question here is - does Java read all of its code from top-down?



There isn't really a concept of java 'reading' code. If by 'reading' you mean 'is the order of execution of statements always the same as the order they appear in the file?' then the answer is no.

Take the following example:



So as you'll see by executing that, the print statements are not executed in the order they appear in the file. In my example the first line to be executed is inside the main() method, near the bottom of the file.

Java's order of execution will generally go from top to bottom of a block (such as a method body), but there are ways that some statements can be run multiple times (such as using a while loop as your example does). There are also ways that statements can be skipped over without being run at all, such as by using an if statement.

One of the skills in learning to program is to be able to look at the code and know what will be executed and in what order. That's something that will come with practice.
 
Marshal
Posts: 70211
280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a “beginning” topic: moving discussion.
 
On top of spaghetti all covered in cheese, there was this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic