Results 1 to 7 of 7
Thread: Increament Operator Error
- 08-18-2012, 11:24 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 4
- Rep Power
- 0
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.
Please help me understand how the "++" operator works.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; } }Last edited by himanshu124; 08-18-2012 at 11:45 AM.
- 08-18-2012, 11:47 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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".
Website: Learn Java by Examples
- 08-18-2012, 12:03 PM #3
Member
- Join Date
- Aug 2012
- Posts
- 4
- Rep Power
- 0
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.
- 08-18-2012, 12:21 PM #4
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Increament Operator Error
Maybe the following example makes it clearer regarding variable or value.
1. variable means something like this:
2. value means something like this:Java Code:int i = 10; int j = ++i; // this works
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.Java Code:int j = ++10; // this will not work
Website: Learn Java by Examples
- 08-18-2012, 12:40 PM #5
Member
- Join Date
- Aug 2012
- Posts
- 4
- Rep Power
- 0
Re: Increament Operator Error
Thanks this cleared all my doubts about ++ operator.
- 08-19-2012, 01:00 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Re: Increament Operator Error
Just my 2c, but this is not good Java to be learning.i started learning JAVA a few days back.
Java Code:int increamentResult4=++(i=i++);
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 01:08 AM.
- 08-20-2012, 06:05 AM #7
Member
- Join Date
- Aug 2012
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
multiplication using increment operator (with out arthematic operator)
By mallikanala in forum New To JavaReplies: 5Last Post: 01-21-2012, 11:02 PM -
Logical Operator error
By MBD in forum New To JavaReplies: 2Last Post: 10-11-2011, 12:29 PM -
Error Message - "Operator || cannot be applied to java.lang.String,java.lang.String"
By Crazz in forum NetBeansReplies: 3Last Post: 05-02-2011, 07:51 AM -
> Operator cannot be applied error and return incompatible types error
By corney_16 in forum New To JavaReplies: 1Last Post: 03-10-2010, 01:53 PM -
Error Message: operator * cannot be applied to java.lang.String, int
By MICHAELABICK in forum Java AppletsReplies: 4Last Post: 11-27-2008, 06:09 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks