package operator;
/**
* 算术运算符
*/
public class OperatorDemo1 {
public static void main(String[] args) {
//定义两个变量
int a = 6;
int b = 4;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);
//除法得到的是商,取余得到是余数
//整数相除只能得到整数,要想得到小数,必须有浮点数的参数
System.out.println(6.0 / 4);
}
}
image.png
package operator;
/**
* 字符的"+"操作
*/
public class OperatorDemo2 {
public static void main(String[] args) {
//定义两个变量
int i = 10;
char c = 'A'; //'A'的值是65
c = 'a'; //'a'的值是97
c = '0'; //'0'的值是48
System.out.println(i + c);
//char ch = i + c;
//char类型会被自动提升为int类型
int j = i + c;
System.out.println(j);
//int k = 10 + 13.14;
double d = 10 + 13.14;
}
}
image.png
package operator;
/**
* 字符串的"+"操作
*/
public class OperatorDemo3 {
public static void main(String[] args) {
System.out.println("hello" + "World"); //helloWorld
System.out.println("hello" + 626); //hello626
System.out.println(626 + "day"); //626day
System.out.println("hello" + 6 + 26); //hello626
System.out.println(2019 + 1 + "年"); //2020年
}
}
image.png
package operator;
/**
* 赋值运算符
*/
public class OperatorDemo4 {
public static void main(String[] args) {
//把10赋值给int类型的变量i
int i = 10;
System.out.println("i:" + i);
// += 把左边和右边的数据做加法操作,结果赋值给左边
i += 20;
//i = i + 20;
System.out.println("i:" + i);
//注意:扩展的赋值运算符底层隐含了强制类型转换
short s = 10;
s += 20;
//s = (short)(s + 20)
System.out.println("s:" + s);
}
}
image.png
package operator;
/**
* 自增自减运算符
*/
public class OperatorDemo5 {
public static void main(String[] args) {
//定义变量
int i = 10;
System.out.println("i:" + i); //10
//单独使用
//i++
//++i
//System.out.println("i:" + i);
//参与操作使用
//int j = i++;
int j = ++i;
System.out.println("i:" + i); //30
System.out.println("j:" + j); //30
}
}
image.png
image.png
image.png
image.png
网友评论