File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

post_Increment

 
Anil Kumardvg
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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

thanks
regards
Anil
 
dhwani mathur
Ranch Hand
Posts: 621
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 621
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i think it is correct.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes both are same. But to the compiler its very efficient and easy.

Thats why they are called as shorthand operators
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic