• 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

a = a++ and the like

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,
I am preparing for SCJP, one funda i cant get my mind around is lets say
int a = 1;
int b = 1;
a = a+++b;
will give a = 2. Alright, i understand the postfix thingy here but what is the sure shot funda to get this right, also what about:
a=a+++b++

Can eneyone please help. Also, which mock exams are the closest to the real SCJP.

Thanks,
Rocky
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a=a+++b++

This will also return you a=2.
and there is no sure shot funda for this.Just understand it clearly.

Regarding mocks I find morcus green test easy.and most people recommend that only.But if you want to score good marks then you have to try some of heardest mocks.
Please go thru

http://www.javaranch.com/maha/_Mock_Exams/_mock_exams.html
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember this concept :
Whenever Postfix operator is used, it assigns the value first and then will increment it, and in prefix its vice-versa.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

First, this is horrible code that should be refactored immediately and the original programmer slapped with a dead mackerel.

Second, what happens with that code is this (at least in theory - a smart compiler should optimize this out):

The value contained in the 'i' variable is incremented. However, the expression 'i++' evaluates to the pre-increment (original) value of 'i'. The '=' assignment operator has the least precedence, so it happens last: the pre-increment (original) value of 'i' is assigned back to 'i'.

The moral? Don't write silly code like this.

And you can bet that you won't see a question like that on the SCJP exam.
 
Rocky Jaiswal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone,
actually i understood the concept but wanted to throughly undersatnd the funda, it was because i found 3 questions like this in a mock exam. But if you say that such questions wont ever come then i am happy. I gave the marcus green exam and got around 75%, a few silly mistakes, i think i am close to giving the actual exam but still lack some confidence, specially around initializers.

Thanks a lot everyone,
great to be on javaranch
Rocky
 
reply
    Bookmark Topic Watch Topic
  • New Topic