字符转换
public class Demo02 {
public static void main(String[] args) {
/*
注意点:
1.不能对Boolean值进行转换
2.不能把对象类型转换为不相干的类型
3.把高容量转换为低容量的时候,强制转换
4.转换的时候可能存在内存溢出,或者精度问题
*/
int i=128;
byte b=(byte)i;//内存溢出
double d=i;
//强制转换 (类型)变量名 高-->低
//自动转换 低-->高
System.out.println(i);//128
System.out.println(b);//-128
System.out.println(d);//128.0
System.out.println((int)23.7);//23
System.out.println((int)-45.89f);//-45
char c='a';
int e=c+1;
System.out.println(e);//98
System.out.println((char)e);//b
}
}
public class Demo03 {
public static void main(String[] args) {
//JDK7新特性,数字之间可用下划线分隔
//大型数字注意溢出
int money=10_0000_0000;
int years=20;
int total=money*years;
System.out.println(money);//1000000000
System.out.println(total);//-1474836480
long total2=money*years;//默认是int,转换之前已经出错
long total3=money*((long)years);
System.out.println(total2);//-1474836480
System.out.println(total3);//20000000000
}
}
运算符
package operator;
public class Demo02 {
public static void main(String[] args) {
/*
* A=0011 1100
* B=0000 1101
* -----------
* A&B=0000 1100
* A|B=0011 1101
* A^B=0011 0001(异或 相同为0 不同为1)
* ~B=1111 0010
*
* 2*8=16
* << *2
* >> /2
* */
System.out.println(2<<3);//2*(2^3)=16
//字符串连接符 +,String
int a=10;
int b=20;
System.out.println(a+b);//30
System.out.println(""+a+b);//1020
System.out.println(a+b+"");//30
//三元运算符
//x?a:b
//如果x==true,则结果为a,否则为b
int score=50;
String type= score<60?"不及格":"及格";
System.out.println(type);
}
}