Java Puzzle – Variable Declaration



In Java, how will you declare a variable x such that the following two conditions are satisfied?
  1. 1.    x = ++x  is valid
  2. 2.    x = x + 1 is invalid
    
    Leave your code in Comments ... 

Comments

  1. Disappointing .... :(
    No one can solve this or no one has time to look at ....

    Anyway here is the answer

    byte x = 100;
    // or short x = 100; or char x = 100;
    x = ++x; // valid
    x = x + 1; // not valid

    ReplyDelete
  2. If I say I knew it, but read the question wrong, will you believe me?

    ReplyDelete
  3. Yes i have to , as you have proof (similar but previous dated post) :)

    Otherwise ... :)

    ReplyDelete
  4. Good one dude. ++ automatically involves casting while "+" does not , remembered this concept while I prepared for SCJP , some how didn't recall it quickly :)

    Thanks
    Javin
    How classpath works in Java

    ReplyDelete
  5. do you syntactically valid or both the statements should give same value of x ??

    ReplyDelete

Post a Comment

Popular posts from this blog

Composite Design Pattern by example

State Design Pattern by Example

Eclipse command framework core expression: Property tester