1、jvm,虚拟环境,可以运行java程度,在任意平台安装了jvm就可以使用跨平台运行同套代码
2、jre,程序运行包含jvm
3、jdk,开发者工具,包含jre,因此直接安装jdk即可进行代码开发
4、建议使用压缩版本,通过使用环境变量指定使用jdk版本,来解决不同项目使用不同jdk版本的问题
数据类型
package com.ruqi.variable;
public class varibale {
public static void main(String[] args) {
// 整型,任意数字数字时,默认是使用int类型,所以long类型数字要后面加上L或者l
byte sorce = 100;
short sorce2 = 1000;
int sorce3 = 100000;
long sorce4 = 100000000000000L;
// 浮点型,任意输入小数默认使用double类型,后面加F或者f表示浮点型
float sorce5 = 1.23F;
double sorce6 = 1.23;
// 字符类型,只能使用单引号,且只能有一个字符
char name = '1';
char name1 = '中';
// 字符串
String name2 = "中国";
// 布尔值
boolean rs = true;
boolean rs1 = false;
}
}
基本数据类型转换
1、自动类型转换,指类型范围小的变量,可以直接赋值给类型范围大的变量,字符转换的结果是二进制数字
package com.ruqi.Variable;
public class Varibale {
public static void main(String[] args) {
byte sorce = 100;
short sorce2 = sorce;
char sorce3 = 'a';
int socre4 = sorce3;
System.out.println(socre4);
}
}
/**
表达式的自动类型转换
●在表达式中,小范围类型的变量会自动转换成当前较大范围的类型再运算。
byte、short、char> int > long >float > double
注意事项:
●表达式的最终结果类型由表达式中的最高类型决定。
●在表达式中,byte、short、char 是直接转换成int类型参与运算的。
*/
public class Varibale {
public static void main(String[] args) {
byte a = 100;
byte b = 120;
int c = a + b ;//byte,short,char直接转换成int去计算,因此按计算结果最高类型来看,最终定义的数据类型是int
int d =1000;
double f = 2.99;
double h = a+b+c+d+f;
int g = (int)f; //强制转换,但会出现数据精度丢失的可能
各种数字类型转换成字符串型:
String s = String.valueOf( value); // 其中 value 为任意一种数字类型。
System.out.println(h);
}
}
运算符
算术运算符
package com.ruqi.Variable;
public class Varibale {
public static void main(String[] args) {
int a = 10;
int b = 3;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);//最高运算是int,所以结果3.333333会转成3
System.out.println(a * 1.0 / b);//最高运算是double,所以结果可以保留3.333333
System.out.println(a % b);
int a =5;
int b = a++;//先把a赋值给a,再加1
int c = ++a;//先加1,再赋值给c
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
赋值运行符,+=,-=,=,%=,*=
public class FuZhi {
public static void main(String[] args) {
byte a = 1;
byte b = 2;
a+=b; //等价于 a = (byte)(a+b)
}
}
逻辑运算符
package com.ruqi.hello;
public class FuZhi {
public static void main(String[] args) {
int a = 1;
System.out.println(a==1 | a==2 );// 或计算
System.out.println(a==1 & a==2 );// 且计算
System.out.println(a==1 ^ a==2 );// 异或计算,两个不同,为真
System.out.println(!true);// 非计算
System.out.println(a==1 && a++>1);//短路运算,前面为false,后面代码不执行
System.out.println(a);
System.out.println(a==2 || a++>1);//短路运算,true,后面代码不执行
System.out.println(a);
}
}
三元运行符
public class FuZhi {
public static void main(String[] args) {
int a = 100;
int b = 200;
int max = a > b ? a : b; //a > b判断为true,返回a,否则返回b
System.out.println(max);
}
}