• 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

post_Increment

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i think it is correct.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
reply
    Bookmark Topic Watch Topic
  • New Topic