Java基本类型


2019-11-03  19:03:48  by冲冲

1、两个float型相减丢失精度,如何解决?

使用BigDemical装饰器模式

public class Test {
public static void main(String[] args) {
float a = 2.030944f;
float b = 1.001085f;
System.out.println(a - b);// 1.0298591 BigDecimal c = new BigDecimal(Float.toString(a));
BigDecimal d = new BigDecimal(Float.toString(b)); BigDecimal resultSub = c.subtract(d); //减法
BigDecimal resultAdd = resultSub.add(d); //加法
BigDecimal resultMul = c.multiply(d); //乘法
BigDecimal resultDiv = c.divide(d); //除法 System.out.println(resultSub.floatValue());//1.029859
System.out.println(resultAdd.floatValue());//2.030944
}
}

2、方法的值传递和对象传递

public class Test {
public void change(String str, char ch[]) {
str = "test ok";
ch[0] = 'g';
} public static void main(String args[]) {
String str = new String("good");
char[] ch = {'a', 'b', 'c'};
Test ex = new Test();
ex.change(str, ch);
System.out.print(str + " and " + ch); //good and gbc
}
}

3、Integer类的==和equals()

public class Test {
public static void main(String[] args) {
Integer a = 1;
Integer b = 2;
Integer c = 3;
Integer d = 3;
Integer e = 321;
Integer f = 321;
Long g = 3L;
Long h = 2L;
// 当Integer处于-128~127,使用内存栈创建值,并将对象指向该值;当不处于该区间,在堆重新new对象。
System.out.println(c == d); //true
System.out.println(e == f); //false
// 自动拆箱
System.out.println(c == (a + b)); //true
System.out.println(c.equals(a + b)); //true
// 包装类的equals()方法会使用instanceof判断类型
System.out.println(g == (a + b)); //true
System.out.println(g.equals(a + b)); //false
System.out.println(g.equals(a + h)); //true
}
}

4、==和equals()

public class Test {
public static void main(String[] args) {
Long u = 127L;
Long v = 127L;
System.out.println(u == v); //true
//[-128,127]在栈内存创建,并指向对象;其余的重新new对象
u = 128L;
v = 128L;
System.out.println(u == v); //false
String x = new String("hello");
String y = "hello";
System.out.println(x.equals(y)); //true
System.out.println(x == y); //false
x = x.intern();
System.out.println(x==y); //true
}

5、多线程的start()和run()

public class Test {
public static void main(String[] args) {
Thread t = new Thread() {
public void run() {
pong();
}
};
t.run();
System.out.print("ping");
//线程要使用start()开启,仅仅调用run()依然在本线程运行,输出结果是pongping
} private static void pong() {
System.out.print("pong");
}
}

6、switch

public class Test {
public static void main(String[] args) {
System.out.println(getValue(2));
//输出结果10
//如果case没有break,则会继续往下执行
} public static int getValue(int i) {
int Test = 0;
switch (i) {
default:
System.out.println("default");
case 1:
Test = Test + i;
case 2:
Test = Test + i * 2;
case 3:
Test = Test + i * 3;
}
return Test;
}
}

7、变量作用域

public class MeaningOfThis {
public final int value = 4; public void doIt() {
int value = 6;
Runnable r = new Runnable() {
public final int value = 5; public void run() {
int value = 10;
System.out.println(this.value);
}
};
r.run();
} public static void main(String... args) {
MeaningOfThis m = new MeaningOfThis();
m.doIt();
//输出结果5,因为this指向对象r
}
}

8、父类和子类的构造方法关系

如果没有给类创建一个构造方法,java编译器会自动添加一个没有参数且方法体为空的构造方法。如果已经人为地添加一个构造方法,java编译器就不会添加构造方法,但是子类构造器就必须显式调用该父类的人为构造方法。

class Person {
String name = "No name"; public Person(String nm) {
name = nm;
}
} class Employee extends Person {
String empID = "0000"; public Employee(String id) {
empID = id;
}
} public class Test {
public static void main(String args[]) {
Employee e = new Employee("123");
System.out.println(e.empID);
//编译报错
}
}

修改方案1

class Employee extends Person {
String empID = "0000"; public Employee(String id) {
super("Railway Employee"); //父类没有默认的空参构造器,所以必须调用带参构造器
empID = id;
}
}

修改方案2

class Person {
String name = "No name"; public Person(String nm) {
name = nm;
} public Person() {} //给父类添加不带参数的构造器,子类就可以隐式调用
}

9、基本数据类型溢出

public class Test {
public static void main(String[] args) {
int a = Integer.MAX_VALUE;
long b = a + 1;
System.out.println(a);//2147483647
System.out.println(b);//-2147483648
}
}

10、finally是否执行?

public class Test {
public static void main(String[] args) {
System.out.println(findResult()); //3
} public static int findResult(){
try {
return 1;
} catch (Exception e) {
return 2;
} finally {
return 3;
}
}
}

11、溢出

public class Test {
public static void main(String[] args) {
byte b = 127;
b = (byte)(b+1);
System.out.println(b); //-128
}
}

12、+=

short s1 = 1;
s1 = s1+1; //s1+1得到int型的结果,同样是不能自动向下转换,编译报错
s1 += 1; //底层是 s1 = (short)s1 + 1;

最新文章

  1. spark 特殊函数
  2. less预处理的好处,补充关于之前发表的rem单位的运用于计算
  3. xUtils框架的使用
  4. Python基础:函数式编程
  5. css3选择器一
  6. jsf2入门视频 教程
  7. 宁波Uber优步司机奖励政策(1月18日~1月24日)
  8. json数据获取
  9. MySql中 where IN 字符串
  10. QWT与QT Designer
  11. bootstrap-treeview 在 bootstrap 4 不兼容解决办法及使用
  12. Python中的__init__和__new__
  13. Unity3D 粒子系统 属性
  14. mysql与cmd,中文乱码
  15. Shiro会话管理器与验证码实现(十四)
  16. System.getProperty("user.dir")
  17. spring boot项目配置RestTemplate超时时长
  18. WPF使用DataGridComboBoxColumn完成绑定
  19. [原]unity3d 纹理旋转
  20. EGOCache缓存框架详细讲解

热门文章

  1. 题解 AVL 树
  2. CF911G Mass Change Queries(线段树+暴力)
  3. CentOS 7安装docker环境
  4. FastAPI 学习之路(二十)接口文档配置相关
  5. Convolutional Neural Network-week2编程题1(Keras tutorial - 笑脸识别)
  6. 移动端 h5 uniapp 读,写,删本地文件或sd文件
  7. CCD摄像头视场角计算公式
  8. 21.6.4 test
  9. 查找最小生成树:克鲁斯克尔算法(Kruskal)算法
  10. popStar机机对战数据生成器代码(C#)