不同类型的变量赋值给其他类型的变量,需要进行类型转换。
public static void main(String[] args) {
// 自动类型转换
byte a = 30;
int b = a;
System.out.println(a); //
System.out.println(b); //30
int c = 33;
double d = c;
System.out.println(c); //33
System.out.println(d); //33
}
public static void main(String[] args) {
// 表达式类型转换
byte a = 10;
int b = 20;
double c = 30.0;
double sum = a + b + c;
System.out.println(sum); //60.0
// byte short char运算中直接转换成int类型
byte i = 1;
byte j = 2;
// byte k = i + j; //报错,应该是int类型
int k = i + j; //3
System.out.println(k);
}
// 1.int:127-->char:127
int a = 127;
byte b = (byte)a;
System.out.println(a); //127
System.out.println(b); //127
// 2.int:128-->char:-128 溢出
// 二进制:int:1000 0000-->char:1000 0000
// 最高位表示符号位,则补码1000 0000转原码:-1000 0000
int c = 128;
byte d = (byte)c;
System.out.println(c); //128
System.out.println(d); //-128
// 3.int:1500-->char:-36 丢失溢出
//二进制:int:0000 0000 0000 0000 0000 0101 1101 1100-->char:1101 1100
//最高位表示符号位,则补码1101 1100转原码-0010 0100
int c1 = 1500;
byte d1 = (byte)c;
System.out.println(c1); //1500
System.out.println(d1); //-36
double e1 = 31.2;
int f1 = (int)e1;
System.out.println(e1); //31.2
System.out.println(f1); //31
符号 | 作用 |
---|---|
+ | 加 |
- | 减 |
* | 乘 |
/ | 除。整数做除法,结果只保留整数部分(要保留小数可以在前面*1.0) |
% | 取余。整数做除法,结果只保留两数相除的余数 |
public static void main(String[] args) {
//基本算术运算符+-*/%
int a = 9;
int b = 2;
System.out.println(a + b); //11
System.out.println(a - b); //7
System.out.println(a * b); //18
// 整数做除法,结果只保留整数部分
// 要保留小数可以在前面*1.0
System.out.println(a / b); //4
System.out.println(a * 1.0/ b); //4.5
System.out.println(3 / 2); //1
System.out.println(3 * 1.0 / 2); //1.5
System.out.println(3 / 2 * 1.0); //1.0
System.out.println(a % b); //1
}
// 数值拆分
int data = 123;
int ge = data % 10;
System.out.println(ge); //3
int shi = data / 10 % 10;
System.out.println(shi); //2
int bai = data / 100;
System.out.println(bai); //1
int a = 5;
System.out.println(5 + a); //10
System.out.println(a + 'a'); //102
System.out.println(a + "" + 'a'); //5a
System.out.println("abc" + 'a'); //abca
System.out.println("abc" + a); //abc5
System.out.println("abc" + + 'a'); //abc97
System.out.println("abc" + (a + 'a')); //abc102
System.out.println(a + 'a' + "abc"); //102abc
System.out.println("abc" + 5 + 'a'); //abc5a
System.out.println("abc" + a + 'a'); //abc5a
System.out.println(15 + "abc"+ 15); //15abc15
public static void main(String[] args) {
int a = 10;
int a1 = a++; //先赋值,再+1
System.out.println(a); //11
System.out.println(a1);//10
a = 10;
int a2 = ++a; //先+1,再赋值
System.out.println(a);//11
System.out.println(a2);//11
int b = 5;
int b1 = b--; //先赋值,再-1
System.out.println(b);//4
System.out.println(b1);//5
b = 5;
int b2 = --b; //先-1,再赋值
System.out.println(b);//4
System.out.println(b2);//4
}
public static void main(String[] args) {
//拓展案例
int k = 3;
int p = 5;
int rs = k++ + ++k - --p + p-- - k-- + ++p + 2;
// 4 5 4
// 4 3 4
// 3 + 5 - 4 + 4 - 5 + 4 + 2
System.out.println(k); //4
System.out.println(p); //4
System.out.println(rs); //9
}
public static void main(String[] args) {
int a = 9;
int b = 2;
a += b;
System.out.println(a); //11
byte i = 10;
byte j = 20;
//i = (byte)(i + j); // 在表达式中,byte, short, char是直接转换成int类型参与运算的。
i += j;//隐含了强制类型转换 i = (type i)(i + j) 等价于上式
System.out.println(i); //30
a -= b;
System.out.println(a); //9
a *= b;
System.out.println(a); //18
a /= b;
System.out.println(a); //9
a %= b;
System.out.println(a); //1
}
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println(a == b); //false
System.out.println(a != b); //true
System.out.println(a >= b); //false
System.out.println(a <= b); //true
System.out.println(a > b); //false
System.out.println(a < b); //true
}
double size = 7.9;
double storage = 8.0;
System.out.println(size > 6.9 & storage > 6.0); //true
System.out.println(size > 6.9 | storage > 6.0); //true
System.out.println(! (size > 6.9)); //false
System.out.println(size > 6.9 ^ storage > 6.0); //false
int a = 10;
System.out.println(1 > 2 && ++a > 2); //false
System.out.println(a); //10
System.out.println(2>1 || ++a > 2); //true
System.out.println(a); //10
条件表达式? 值1 : 值2;
public static void main(String[] args) {
double score = 99;
System.out.println(score > 60 ? "通过" : "挂科"); //通过
//String rs = score > 99 ? "通过" : "挂科";
//System.out.println(rs);
//求max
int a = 20;
int b = 13;
System.out.println(a > b ? a : b); //20
}
//求max
int a = 20;
int b = 13;
int c = 52;
//int t = a > b ? a : b;
//int p = t > c ? t : c;
//System.out.println(p); //52
System.out.println(a > b? (a > c? a : c) : (b > c ? b : c)); //52
优先级 | 运算符 |
---|---|
1 | () |
2 | ! 、-、++、– |
3 | *、/、% |
4 | +、- |
5 | <<、>>、>>> |
6 | <、<=、>、>=、instanceof |
7 | ==、!= |
8 | & |
9 | ^ |
10 | | |
11 | && |
12 | || |
13 | ? : |
14 | =、+=、-=、*=、/=、%=、&= |
public static void main(String[] args) {
// &&优先级高,10 > 3 && 10 < 3 为false。但是 10 > 3 为true true||false 为 true
System.out.println(10 > 3 || 10 > 3 && 10 < 3); //true
// ()优先级高,(10 > 3 || 10 > 3)为true。但是 10 < 3 为false true&&false 为 false
System.out.println((10 > 3 || 10 > 3) && 10 < 3);//false
}
javac:编译工具。java:执行工具。JVM:java虚拟机。API:核心类库(应用程序编程接口)
// 1.导包(并不需要手写,通过工具进行自动导包)
import java.util.Scanner;
public class Api {
public static void main(String[] args) {
// 2.实例化对象:用于接收Scanner获取的数据。
Scanner sc = new Scanner(System.in);
// 3.调用对象的功能,等待接收用户输入数据。
System.out.println("请输入您的年龄和姓名:");
int age = sc.nextInt(); //>>>18
String name = sc.next(); //>>>怪兽
System.out.println("欢迎" + name + ",您的年龄是:" + age); //欢迎怪兽,您的年龄是:18
}
}