• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java program's baffling output?  RSS feed

 
A. M. Anisul Huq
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, please look at the code:



Well, this program should show error (as b is assigned 420). Instead, it runs correctly and gives the following output:

420 111

Why?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A. M. Anisul Huq wrote:Well, this program should show error (as b is assigned 420).

Yes, but it's only assigned 420 after the statement is complete.

In order to work out what cell 'a[b]' actually refers to, the compiler must make some decision, and it makes sense for it to evaluate it first, don't you think?

If not, feel free to explain why, and perhaps we can help you further; but it's a general truth: the expression on the left-hand side of an assignment is always evaluated first.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!