Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By wsaryada
  • 1 Post By pbrockway2

Thread: Increament Operator Error

  1. #1
    himanshu124 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Increament Operator Error

    i started learning JAVA a few days back.
    i thought of writing my first program on arithmetic operators.
    but i m getting compilation error with the "++" operator.
    below is my code.
    Java Code:
    public class IntArithmeticOperations {
           public static void main(String args[]){
    	 System.out.println("Arithmetic Operations Sample");
    	 
    	 int i=10;
    	 
    	 int increamentResult=++i;//This is ok
    	 int increamentResult1=++(i);//This is also ok
    	 int increamentResult2=doit(++i);  //This is also ok
    	 int increamentResult3=++(++i);   //This gives a compilation error
    	 int increamentResult4=++(i=i++); //This gives a compilation error
    	 int increamentResult5=++(i+1); //This gives a compilation error
    	 
    	 System.out.println("IncreamentResult is"+increamentResult);
    	 System.out.println("IncreamentResult1 is"+increamentResult1);
    	 System.out.println("IncreamentResult2 is"+increamentResult2);
    	 System.out.println("IncreamentResult3 is"+increamentResult3);
    	 System.out.println("IncreamentResult4 is"+increamentResult4);
    	 System.out.println("IncreamentResult5 is"+increamentResult5);
        }
     public static int doit(int i){
    	 return i;
     }
    }
    Please help me understand how the "++" operator works.
    Last edited by himanshu124; 08-18-2012 at 12:45 PM.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Increament Operator Error

    The ++ operators works with a variable and doesn't not with a value. For example the ++(i) works because i is a variable. But ++(++i) doesn't work because (++i) is a value and not a variable. The compiler error message might already tell you that it "required a variable but found a value instead".

  3. #3
    himanshu124 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: Increament Operator Error

    Thanks a lot this was bugging me since morning.

    But still have a silly question isn't the expression "i++" evaluated by the compiler as "i=i+1".
    so the in the instruction int increamentResult=++(i++) the expression in () be evaluated as i=i+1 and the instruction be changed to int increamentResult=++(i) with the "i" in () having the new value?

    sorry for my English.

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Increament Operator Error

    Maybe the following example makes it clearer regarding variable or value.

    1. variable means something like this:

    Java Code:
    int i = 10;
    int j = ++i; // this works
    2. value means something like this:

    Java Code:
    int j = ++10; // this will not work
    The value of expression ++i and i = i + 1 is equals. but still ++i return the value of i incremented by 1. it is not the variable i.
    Zyril likes this.

  5. #5
    himanshu124 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: Increament Operator Error

    Thanks this cleared all my doubts about ++ operator.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Increament Operator Error

    i started learning JAVA a few days back.
    Java Code:
    int increamentResult4=++(i=i++);
    Just my 2c, but this is not good Java to be learning.

    Not because the line fails to compile, but, rather, because by some mischance it might compile: and mean g0d knows what.

    [edit] Reading this again, this I should add... Welcome to the forums! And there's nothing wrong with asking about code like this.

    In fact examining such monstrosities is a good way of getting understanding about the darker corners of the language. It's just that I don't think it a good place to start and that even if it was good (valid) Java, it would be bad (unclear) Java.
    Last edited by pbrockway2; 08-19-2012 at 02:08 AM.
    kammce likes this.

  7. #7
    himanshu124 is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Re: Increament Operator Error

    Quote Originally Posted by pbrockway2 View Post
    Just my 2c, but this is not good Java to be learning.

    Not because the line fails to compile, but, rather, because by some mischance it might compile: and mean g0d knows what.

    [edit] Reading this again, this I should add... Welcome to the forums! And there's nothing wrong with asking about code like this.

    In fact examining such monstrosities is a good way of getting understanding about the darker corners of the language. It's just that I don't think it a good place to start and that even if it was good (valid) Java, it would be bad (unclear) Java.
    Thanx pbrockway2. Will keep your advice in mind. Its just tat i come from a C background so had this weird curiosity of trying such a code ;)

Similar Threads

  1. Replies: 5
    Last Post: 01-22-2012, 12:02 AM
  2. Logical Operator error
    By MBD in forum New To Java
    Replies: 2
    Last Post: 10-11-2011, 01:29 PM
  3. Replies: 3
    Last Post: 05-02-2011, 08:51 AM
  4. Replies: 1
    Last Post: 03-10-2010, 02:53 PM
  5. Replies: 4
    Last Post: 11-27-2008, 07:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •