• 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

Operator precedence?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I've written a class and when I run it, it does not run as expected. Not sure why, please can you explain?

Code as below:


My expectation is it should work like this :
(a) create new Wheel(wheelId, wheelMake)
(b) allocate the reference of the new wheel at wheels[numberOfWheels]
(c) increment numberOfWheels

The above works as expected with lines 2 & 3. With Line 1, step(c) seem to happen even before step(a)!

I can post the whole code if that is useful.

Am I missing something?

Thanks.
[ May 03, 2006: Message edited by: InvalidNameException ]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Hope u know that "Java guarantees that expressions will be evaluated left to right".

So numberOfWheels is incremented first before creating a reference to wheel. numberOfWheels gets higehr prevedence as it inside brackets.

Sathya
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java doesn't do expressions blindly left to right. It has a set of operator precedence rules, which are applied first. Only when operators have equal precedence does the left to right rule come in.

In the original posting, the expected behaviour sounds right to me. Therefore, I guess that something in the rest of the code is not right.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, didn't notice you are new. Welcome.

Hello. Please get your name into the right format before somebody notices.

I have tried your code or something similar, and I did NOT get the result you described.
You would have thought that when you start with wheel 0 that the numberOfWheels++ puts your details into wheels[1]. But it doesn't. It puts the details where they belong, in wheels[0].

Try it: Have 4 wheels. Set your first wheel to "Dunlop 14," the second to "Pirelli 15," the third to "Goodyear 16," and the fourth to "Continental 17."
Print them out using an old-fashioned for loop, not a for-each loop:-
It will print
Number: 0, Make: Dunlop, Size, 14
Number: 1, Make: Pirelli, Size, 15
Number: 2, Make: Goodyear, Size, 16
Number: 3, Make: Continental, Size, 17

Why?

If you look here on the java tutorial, you will find an operator precedence chart. The very top row contains the two unary postfix operators, something++ and something--, so they have the very top precedence, but you will find on this page that the postfix operator "evaluates to the value of op before it was [incremented/decremented]."

What that means, is that the something++ changes your something, but the "before" value is used.
Another way to look at it (not accurate): Imagine that although your something++ operator has a high precedence, it has a delay in its execution, until after the statement has been finished with.
Or another way: keep your something++ on a line all on its own if you get confused.

CR
[ May 03, 2006: Message edited by: Campbell Ritchie ]
 
Rajib G
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your replies so far. I should really have posted the whole code in the first post. Here it is now.

say you now compile & run the code in 2 versions - one as it is (runs fine with no exception) and another version by uncommenting Line1 and commenting out lines 2 & 3, that's when it throws IllegalArgumentException (as per my code at Line EX1) - and that's when my grey cells fail to grasp why it prints numberOfWheels = 1.

Does it mean that
array[index++] = new MyArrayElementClass();
is not same as
array[index] = new MyArrayElementClass();
index++;

Hmmm...

Confused.com

 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
...Please get your name into the right format before somebody notices...


I noticed...

"InvalidNameException,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your name here.

Thank you for your prompt attention, and enjoy the ranch!

-Marc
 
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's some very simple test code:



Based on the results of running it, Sathya is correct. The counter increments after it is used as an index, but before the constructor executes.

Does it mean that
array[index++] = new MyArrayElementClass();
is not same as
array[index] = new MyArrayElementClass();
index++;



Seems to be that way, if the constructor depends on index. An equivalent to lines 2/3 should be:


[ May 03, 2006: Message edited by: Yuriy Zilbergleyt ]
 
Rajib G
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Satya & Yuriy. I wrote another piece of (simpler) code to come to the same conclusion as you have explained. And I was trying to understand the theory behind it.
In the expression, array[index++] = new MyClass(); we have got 3 operators : [] postfix++ = , and in that order of operator precedence. So [] must be evaluated first. But postfix++ is really a part of []. So execution of [] starts, and within it, postfix++ gets executed, then execution of [] ends. Next = is to be executed. Its execution starts but the constructor really needs to be exeuted first as a part of assignment - so constructor gets executed first, and then the assignment operation can complete.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Rajib G,"

Thank you for revising your display name, but I'm going to have to ask you to make another revision. If you check the policy (linked to above), you will see that last names are required. The first name can be an initial, but the last cannot.

Thanks,
Marc
 
Rajib G
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry mate, I don't wanna write my full name here, and don't wanna write a make-believe surname. So if "Rajib G" - which is my true name (albeit not full) - is not good enough for the rules of this forums, then please suggest how I can close my account, or if you are a authorised person to do so, please close my account.
Thanks.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic