算术运算符+的用法
A:加法
B:正号
C:字符串连接符
public class OperatorDemo3 {
public static void main(String[] args) {
//加法
System.out.println(3+4);
//正号
System.out.println(+4);
System.out.println('a');
System.out.println('a'+1);
//字符串连接符
System.out.println("hello"+'a'+1);
System.out.println('a'+1+"hello");
}
}
运行结果如下:
7
4
a
98
helloa1
98hello
----------------------------------分割线--------------------------------------------
赋值运算符的面试题
/*
* 面试题:
* short s=1;s=s+1;
* short s=1;s+=1;
* 上面两个代码有没有问题,如果有,哪里有问题。
*
* 为什么第二个没有问题呢?
* 扩展的赋值运算符其实隐含了一个强制类型转换。
* s+=1;
* 不是等价于s=s+1;
* 而是等价于s=(s的数据类型) (s+1);
* */
public class OperatorTest {
public static void main(String[] args) {
short s=1;
s=s+1;//编译器通不过,从int转换到short可能会有损失
System.out.println(s);
}
}
运行结果如下:
java: 不兼容的类型: 从int转换到short可能会有损失
再看如下程序:
public class OperatorTest {
public static void main(String[] args) {
short s=1;
s+=1;
Syst