It's not a secret anymore!
The moose likes Beginning Java and the fly likes post_Increment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "post_Increment" Watch "post_Increment" New topic


Anil Kumardvg

Joined: May 21, 2007
Posts: 15
I am studying head first java in that he has given (page:105)
numOfHits++ is the same as saying numOfHits = numOfHitts + 1, except slightly more efficient.

I am not getting how its more efficient ? both are same know

dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
yes!!! both are same ,see the value numOfHits++ what it means is
we add or increment numOfHits by 1(ie to the previous or original value of numOfHits 1 is added),it is more efficient in use.
Now in the second statement numOfHits=numOfHits+1;
Here also to the previous value of numOfHits 1 is added but it is stored in it.
Anil Kumardvg

Joined: May 21, 2007
Posts: 15
thanks for your reply
that means numOfHitts = numOfHitts + 1 takes more operations first it adds the 1 to numOfHitts and then stores it in numOfHitts

Is it correct
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
yes i think it is correct.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

Yes both are same. But to the compiler its very efficient and easy.

Thats why they are called as shorthand operators

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
At least with Java 6, the compiler actually compiles all of these three statements

to the same bytecode:

IINC i 1

So it seems as if the statement from the book is at least outdated, if not plain wrong.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
At least with Java 6, the compiler actually compiles ... to the same bytecode

Hooray! It's about time somebody woke up and did the compiler-writer's job. A language that exposes things about hardware and compiler implementation is offensive to me. The ++ operator originally corresponded to a particular instruction on a particular machine. I want my legions of business coders to think about insurance and customer service, not machine instructions.

Another silly example was a mainframe system that used signed dates because unsigned fields generated one more single-cycle machine instruction to force them to positive. Back then the mainframe ran 33mhz and we were charged $0.10 a CPU second, so every 33 million date assignments saved a dime. And explaining to every person who touched the data why they really didn't have to worry about negative dates cost how much?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: post_Increment
jQuery in Action, 3rd edition