Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Increament operator error Posted by himanshu124 on 18 Aug 2012 at 2:16 AM
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.
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.
Report
Re: Increament operator error Posted by Katherine_Delo on 23 Aug 2012 at 2:55 AM
public class IntArithmeticOperations {
public static void main(String args[]){
System.out.println("Arithmetic Operations Sample");

int i=10; You have initialized the code which is good

int increamentResult=++i;//This is ok
this is saying add 1 which increments the value i to 11

int increamentResult1=++(i);//This is also ok
the brackets doesn't matter in this one its the same as before but you're adding 1 again which makes the value 12

int increamentResult2=doit(++i); //This is also ok
this is calling the doit method (which just goes down to see if there's anything in the method doit. You've only returned i so it comes back to this statement and increments it...so now its 13.

int increamentResult3=++(++i); //This gives a compilation
error

this is causing an error because it wants a variable. You're not calling any methods and you can't just put ++ and (++i). make these statements in different lines
e.g
int increamentResult3=i++;
int incrementResult4 = doits(++i);

int increamentResult4=++(i=i++); //This gives a compilation error
int increamentResult5=i++;

//make another method

int incrementResult5 = doits(i=i++);

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;
}
}
hope i didn't confuse you too much :) i'm new to java too
Report
Re: Increament operator error Posted by Katherine_Delo on 23 Aug 2012 at 2:58 AM
public class IntArithmeticOperations {
public static void main(String args[]){
System.out.println("Arithmetic Operations Sample");

int i=10; You have initialized the code which is good

int increamentResult=++i;//This is ok
this is saying add 1 which increments the value i to 11

int increamentResult1=++(i);//This is also ok
the brackets doesn't matter in this one its the same as before but you're adding 1 again which makes the value 12

int increamentResult2=doit(++i); //This is also ok
this is calling the doit method (which just goes down to see if there's anything in the method doit. You've only returned i so it comes back to this statement and increments it...so now its 13.

int increamentResult3=++(++i); //This gives a compilation
error

this is causing an error because it wants a variable. You're not calling any methods and you can't just put ++ and (++i). make these statements in different lines
e.g
int increamentResult3=i++;
int incrementResult4 = doits(++i);

int increamentResult4=++(i=i++); //This gives a compilation error
int increamentResult5=i++;

//make another method

int incrementResult5 = doits(i=i++);

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;
}
}
hope i didn't confuse you too much :) i'm new to java too
Report
Re: Increament operator error Posted by Katherine_Delo on 23 Aug 2012 at 3:00 AM
public class IntArithmeticOperations {
public static void main(String args[]){
System.out.println("Arithmetic Operations Sample");

int i=10; You have initialized the code which is good

int increamentResult=++i;//This is ok
this is saying add 1 which increments the value i to 11

int increamentResult1=++(i);//This is also ok
the brackets doesn't matter in this one its the same as before but you're adding 1 again which makes the value 12

int increamentResult2=doit(++i); //This is also ok
this is calling the doit method (which just goes down to see if there's anything in the method doit. You've only returned i so it comes back to this statement and increments it...so now its 13.

int increamentResult3=++(++i); //This gives a compilation
error

this is causing an error because it wants a variable. You're not calling any methods and you can't just put ++ and (++i). make these statements in different lines
e.g
int increamentResult3=i++;
int incrementResult4 = doits(++i);

int increamentResult4=++(i=i++); //This gives a compilation error
int increamentResult5=i++;

//make another method

int incrementResult5 = doits(i=i++);

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;
}
}
hope i didn't confuse you too much :) i'm new to java too



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.