i = i + 1& i ++ [英] Difference Between i = i+1 & i++

查看:171
本文介绍了i = i + 1& i ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解决我的困惑在这里是我的代码:

  byte i = 0; 

i ++;
System.out.println(i);

结果: 1

  byte i = 0; 

i = i + 1;
System.out.println(i);

生成编译时错误: 类型不匹配:无法从int转换为byte



当我将它转换为字节时: i = 1);



p>执行此示例我理解i = i + 1& i ++执行不能同样opearation所以现在我想知道它们之间的确切的差别... !!!

解决方案

最后我找到我的问题(问题)高级理解,所以这里描述它来容易理解其他人!



1> i = i + 1



<在两个或多个变量之间执行,则其返回值的类型取决于以下方程:


返回值类型=(int,VARIABLE的MAXIMUM RANGE -1,MAXIMUM RANGE
OF VARIABLE-N)


此处,返回值类型: int



对于等式(int,byte,byte) 因此int有最大范围..



字节 - > 1字节大小&
int - > 4 byte size



这就是为什么上面的exe不能存储返回值的字节直接它需要 external类型转换 i =(byte)(i + 1)



在java中,每当增量&递减操作执行像这样,那么它的返回值的类型取决于这个方程:


返回值类型=(VARIABLE TYPE,VALUE + 1) / p>

这些方程表示内部类型转换在此情况下执行



必须记住:



case 1 / em>


Can anyone solve my confusion here is my code :

byte i = 0;

i++;
System.out.println(i);

Result: 1

byte i = 0;

i = i+1;
System.out.println(i);

Generate compile time error: Type mismatch: cannot convert from int to byte

When I convert that to byte like: i = (byte) (i+1); then happily getting result 1

Performing this example i am understand i = i+1 & i++ perform can't same opearation so now i want to know what is exactally difference between them ...!!!

解决方案

Finally i am found my problems(questions) superior understand so here describe it to understands others easily..!!!

1> i = i + 1

In java whenever any arithmetic operation performs between two or more variables then it's return value's type depends upon like this equation :

RETURN VALUE TYPE = (int, MAXIMUM RANGE OF VARIABLE-1, MAXIMUM RANGE OF VARIABLE-N )

here, Return value type : int

For eqution (int, byte, byte) so int have maximum range..

byte -> 1 byte size & int -> 4 byte size

That's why above exe i can't store that return value in byte directly it's require external type casting i = (byte) (i+1)

2> i++

In java whenever increment & decrement operation perform like these then it's return value's type depends upon this equation:

RETURN VALUE TYPE = (VARIABLE TYPE, VALUE + 1)

these equation denote internal type casting perform in this case

MUST REMEMBER:

case 1: External type cast require if needed

case 2: internal type casting perform automatically

这篇关于i = i + 1&amp; i ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆