/*在学习java的过程中,我发现原来c++对表达式的解释顺序和java的有很大不同c++是循规蹈矩的从左到右。java的是扫描一边将优先级高的先算了。下面是我做的一个试验。*/
public class TestGreeting {
public static void main (String[] args)
{
add1();
add11();
add2();
add22();
}
public static void add1()
{
System.out.println("(++i)+(++i)+(++i)");//4
int i=3,j;
j=++i;
System.out.println("i="+i);//4
System.out.println("j="+j);//4
i=3;
j=(++i)+(++i);
System.out.println("i="+i);//5
System.out.println("j="+j);//9 4+5
i=3;
j=(++i)+(++i)+(++i);
System.out.println("i="+i);//6
System.out.println("j="+j);//15 4+5+6
/* borland c++ builder example
int i=3,j;
j=(i++);
ShowMessage("i="+IntToStr(i)); //4
ShowMessage("j="+IntToStr(j)); //3 3
i=3;
j=(i++)+(i++);
ShowMessage("i="+IntToStr(i)); //5
ShowMessage("j="+IntToStr(j)); //6 3+3
i=3;
j=(i++)+(i++)+(i++);
ShowMessage("i="+IntToStr(i)); //6
ShowMessage("j="+IntToStr(j)); //9 3+3+3
*/
}
public static void add11()
{
System.out.println("++i + ++i + ++i ");//4
int i=3,j;
j=++i;
System.out.println("i="+i);//4
System.out.println("j="+j);//4
i=3;
j= ++i + ++i ;
System.out.println("i="+i);//5
System.out.println("j="+j);//9 4+5
i=3;
j= ++i + ++i + ++i ;
System.out.println("i="+i);//6
System.out.println("j="+j);//15 4+5+6
}
public static void add2()
{
System.out.println("(i++)+(i++)+(i++)");//4
int i=3,j;
j=(i++);
System.out.println("i="+i);//7
System.out.println("j="+j);//6
i=3;
j=(i++)+(i++);
System.out.println("i="+i);//5
System.out.println("j="+j);//7 3+4
i=3;
j=(i++)+(i++)+(i++);
System.out.println("i="+i);//6
System.out.println("j="+j);//12 3+4+5
/*borland c++ builder example
int i=3,j;
j=(i++);
ShowMessage("i="+IntToStr(i)); //4
ShowMessage("j="+IntToStr(j)); //3 3
i=3;
j=(i++)+(i++);
ShowMessage("i="+IntToStr(i)); //5
ShowMessage("j="+IntToStr(j)); //6 3+3
i=3;
j=(i++)+(i++)+(i++);
ShowMessage("i="+IntToStr(i)); //6
ShowMessage("j="+IntToStr(j)); //9 3+3+3
*/
}
public static void add22()
{
System.out.println("i++ + i++ + i++");//4
int i=3,j;
j=i++;
System.out.println("i="+i);//7
System.out.println("j="+j);//6
i=3;
j= i++ + i++ ;
System.out.println("i="+i);//5
System.out.println("j="+j);//7 3+4
i=3;
j= i++ + i++ + i++;
System.out.println("i="+i);//6
System.out.println("j="+j);//12 3+4+5
}
}
本文地址:http://com.8s8s.com/it/it18086.htm