【基础】关于Java中变量间的赋值

  1. Java是强类型的语言。

  2. 赋值的情况1:相同类型的变量间,可以赋值

int num1 = 10;
int num2 = num1;

String s1 = "hello";
String s2 = s1;

int[] arr = new int[10];
int[][] arr1 = new int[10][3];
arr = arr1;//编译失败
  1. 赋值情况2:基本数据类型之间,存在自动类型提升规则(容量小的变量—>容量大的变量)
byte b = 10;
int i = b;
char c = 'a';
int j = c;
double d = c;
  1. 赋值情况3:引用数据类型之间,存在多态性:子类对象赋给父类的引用变量 (未讲)
Student s = new Student();
Person p = null;
p = s;
  1. 赋值情况4:(是上述情况2,情况3的逆运算) 使用强制类型转换符 (未讲)
int i = 10;
byte b = i;//编译不通过
byte b = (byte)i;//编译通过

Person p = new Student();
Student s = (Student)p;
发布了41 篇原创文章 · 获赞 5 · 访问量 1089

猜你喜欢

转载自blog.csdn.net/qq_43771096/article/details/104477851