赞
踩
基本类型有以下四种:
转换原则
int i=1;
if(i)
cout<<"true";
else
cout<<"false";
报运行时错误。int类型不能显示强转为boolean
1)boolean a=true;//这个a在JVM中占4个字节即:32位。
2)boolean[] b = new boolean[10];//数组时,每一个boolean在JVM中占一个字节。
int x=3;
int y=1;
if(x=y)
system.out.println(“Not equal”);
else
system.out.println(“Equal”);
}
以上语句C中正常、java编译报错
我来解析下:
其实这个是由于java和C语言的不同处理机制导致的:
C语言中
当if语句中的条件为赋值语句时,实际上是将赋值后的结果与0进行比较【左值】
if(1) 由于1>0 所以认为是true
java语言中,虽然也用了左值,但是不再与0比较,而是直接将1放入if()中
但是int类型,不能转换为boolean,所以会报错:“Type mismatch: cannot convert from int to boolean”

short a=128;
byte b=(byte)a;
a为2个字节:00000000 100000000
b为1个字节,支持【-128,127】:取底位一个字节100000000 ————>128溢出为-128
short s1=1;
s1=s1+1;//报错,s1+1返回为int型,不能直接赋值给short变量
short s1=1;
s1=(short)s1+1;//正确
但+=运算符不会产生类型转换,如:
short s=1;s+=1;正确
floor 返回不大于的最大整数,向下取整
round则是4舍5入的计算,入的时候是到大于它的整数,也就是+0.5后floor
ceil 则是不小于他的最小整数,向上取整。正入,负舍。
Math.floor Math.round Math.ceil
1.4 1 1 2
1.5 1 2 2
1.6 1 2 2
-1.4 -2 -1 -1
-1.5 -2 -1 -1
-1.6 -2 -2 -1
hello 5个字节
你好 4个字节
声明过程中不加numsize: int a[5] 错误
这点与C不一样,c语言不允许声明空数组
一维数组的声明:int a[]——–int [] a
二维数组的声明:int a[][]——int [][] a——int[] a []
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。