count++和count=count+1的区别
区别在于jdk编译的时候 百度知道 编译时,jdk会向上自动转换 比如说 int t =1; t++; 等同于 t=t+1; //1 是int,和int型相加,int + int 赋值给int时,编译没有错误 long t= 1L; t++; 等同于 t=t+1; //1 是int,和long 型相加,由于有int 向上自动转换成 long,int(这时转换成了long) + long 赋值给long时,编译没有错误 short t=1; t++; 不等同于 t=t+1; //1 是int,和short类型相加,由于 short 向上自动转换成 int,short(这时转换成了int + int 赋值给short 时,编译肯定会出现错误! 这里,自动向高级转换时,是t的“值”自动转换了,并非是其类型真的变了
public class Test {
/**
* @param args
*/
count++
public static void main(String[] args) {
//纳秒级
double startTime= System.nanoTime();int count=0;
for(int i=0;i<1000000;i++)
{
count++;
}
System.out.println(count);
double endTime=System.nanoTime();
System.out.println(endTime-startTime);
}
经过测试 运行949225.0纳秒
}
public class Test {
/**
* @param args
*/
count=count+1
public static void main(String[] args) {
double startTime=System.nanoTime(); ;
int count=0;
for(int i=0;i<1000000;i++)
{
count=count+1;
}
System.out.println(count);
double endTime=System.nanoTime();
System.out.println(endTime-startTime);
}
运行974291.0纳秒
可以看出 count++比count=count+1运行效率快
运行时间是相对的,你会发现最开始两次和以后运行时间不同,但是count++运行时间大于count=count+1;如果数比较小,运行时间可以忽略不计
因为运行时间和以下有关
1)JVM配置,特别是内存, 2)cpu占用情况,可供JVM使用的cpu份额 3)代码。尽量考虑用RandomAccessFile 4)运行时期,I/O占用情况