float类型
float类型运算
- float类型:离散得,有限得,有舍入误差,接近但不等于
- 最好完全避免float进行比较
public class test {
public static String getType(Object o){
return o.getClass().getName();
}
public static void main(String[] args) {
float a = 0.1f;
double b = 1.0/10;
System.out.println(a);
System.out.println(getType(a));
System.out.println(b);
System.out.println(getType(b));
System.out.println(a==b);
System.out.println("=========================================");
float c = 4234234234234f;
float d = c + 1;
System.out.println(c);
System.out.println(d);
System.out.println(c==d);
}
}
0.1
java.lang.Float
0.1
java.lang.Double
false
=========================================
4.2342343E12
4.2342343E12
true
类型转换
-
由于java是强类型语言,所以要进行有些运算时,需要进行类型转换
-
低------------------------------------------------>高
byte, short, char, int, long, float, double
-
运算中,不同类型得数据先转换为同意同一类型,然后进行运算
-
-
强制转换(高 到 低)
public class test { public static void main(String[] args) { int i = 128; byte b_i = (byte)i; // 高转低 可能导致内存溢出(byte区间是-128~127) System.out.println(i); System.out.println(b_i); } }
128 -128
-
自动转换(低 到 高)
public class test { public static void main(String[] args) { int i = 130; float b_i = i; // 低转高 自动转换,无需强制转换 System.out.println(i); System.out.println(b_i); } }
130 130.0
-
操作数比较大,导致内存溢出问题
public class test { public static void main(String[] args) { int money = 10_0000_0000; // 数字可用下划线分割,并不会影响输出 int years = 20; int sum1 = money * years; System.out.println(sum1); long sum2 = money * years; // 内部计算完成后在传给sum2转换为long类型,计算时内存已经溢出 System.out.println(sum2); long sum3 = ((long)money) * years; // 将其中一个数据先转换成long类型,才能保证计算时内存不会溢出 System.out.println(sum3); } }
-1474836480 -1474836480 20000000000
变量
- 类变量
- 实例变量
- 局部变量
public class test {
static int j = 0; // 类变量
String str = "hello"; // 实例变量
public void method(){
int i = 0; // 局部变量
}
}
- 变量例子
public class test {
// 类变量
static double salary = 10000;
// 实例变量
String name;
int age;
boolean flag;
public static void main(String[] args) {
// 局部变量,必须声明和初始化值
int i =10;
System.out.println(i);
System.out.println("==================");
//变量类型 变量名字 = new test();
test test = new test();
System.out.println(test.name); // 实例变量String默认输出null
System.out.println(test.age); // 实例变量int默认输出0
System.out.println(test.flag); // 实例变量boolean默认输出false
// 类变量
System.out.println(salary);
}
}
10
==================
null
0
false
10000.0
变量得命名规范
- 类成员变量:首字母小写和驼峰原则:monthSalary、lastMonth
- 局部变量:首字母小写和驼峰原则
- 常量:大写字母和下划线:PI,MAX_VALUE
- 类名:首字母大写和驼峰原则:Man,GoodMan
- 方法名:首字母小写和驼峰原则:run(),runRun()
常量
- 常量修饰符为:final
- 常量确定值后不可修改
public class test{
static final double PI = 3.14; // static 和 final 为修饰符,不分前后顺序
// final static double pi = 3.14; 也可以
public static void main(String[] args) {
System.out.println(PI);
}
}
运算符号
计算输出类型
public class test{
public static String getType(Object o){
return o.getClass().getName();
}
public static void main(String[] args) {
long a = 5234523454352L;
int b = 123;
short c = 10;
byte d = 8;
System.out.println(a+b+c+d);
System.out.println(getType(a+b+c+d));
System.out.println(b+c+d);
System.out.println(getType(b+c+d));
System.out.println(c+d);
System.out.println(getType(c+d));
}
}
运算结果为最大得数据类型,小于int的则输出int
5234523454493
java.lang.Long
141
java.lang.Integer
18
java.lang.Integer
自增,自减(一元运算符)
public class test{
public static void main(String[] args) {
int a = 3;
int b = a++;
int c = ++a;
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
5
3
5
3.幂运算
public class test {
public static void main(String[] args) {
double pow = Math.pow(3, 2);
System.out.println(pow);
}
}
9.0
短路运算
public class Test {
public static void main(String[] args) {
int c = 5;
boolean d = (c < 4) && (c++ < 4); // c<4为假,直接输出假,不执行后面运算
System.out.println(d);
System.out.println(c);
System.out.println("============");
boolean e = (c++ < 4) && (c < 4); // c++<4为真,继续执行接下来的运算
System.out.println(c);
}
}
false
5
============
6
位运算
- 位运算直接与二进制(底层)打交道,效率最高
- << :左移(相当于 *2) | >>:右移(相当于 /2)
快速计算 2*8
public class Test {
public static void main(String[] args) {
/*
0000 0010 2
0000 0100 4
0000 1000 8
*/
System.out.println(2<<3);
}
}
16
连接运算符(+)
- 当进行运算时,遇见String与数值进行连接时,后面的计算自动转换为String类型
public class Test {
public static void main(String[] args) {
int a = 10;
int b = 20;
/*
当进行运算时,遇见String与数值进行连接时,后面的计算自动转换为String类型
*/
System.out.println("" + a + b);
System.out.println(a + b + "" + a + b);
}
}
1020
301020
三元运算符
- x ? y : z ,如果x==true,则输出y,否则输出z
public class Test {
public static void main(String[] args) {
int score1 = 100;
int score2 = 50;
String result1 = score1 < 60 ? "不及格" : "及格";
String result2 = score2 < 60 ? "不及格" : "及格";
System.out.println(result1);
System.out.println(result2);
}
}
及格
不及格