基本语法
[易错]double和float加减运算的精度
Java的设计者给编程人员提供了一个很有用的类BigDecimal,可以完善float和double类无法进行精确计算的缺憾
BigDecimal a1 = new BigDecimal(Double.toString(a));
BigDecimal a1 = new BigDecimal(Float.toString(a));代码:
public class Test { public static void testDouble() { double a = 1; double b = 0.9; BigDecimal a1 = new BigDecimal(Double.toString(a)); BigDecimal b1 = new BigDecimal(Double.toString(b)); BigDecimal a2 = new BigDecimal(a); BigDecimal b2 = new BigDecimal(b); double c = a1.subtract(b1).doubleValue(); double d = a2.subtract(b2).doubleValue(); System.out.println("double a-b=\t" + (a - b)); System.out.println("double c=\t" + c); System.out.println("double d=\t" + d); } public static void testFloat() { float a = 1f; float b = 0.9f; BigDecimal a1 = new BigDecimal(Float.toString(a)); BigDecimal b1 = new BigDecimal(Float.toString(b)); BigDecimal a2 = new BigDecimal(a); BigDecimal b2 = new BigDecimal(b); float c = a1.subtract(b1).floatValue(); float d = a2.subtract(b2).floatValue(); System.out.println("float a-b=\t" + (a - b)); System.out.println("float c=\t" + c); System.out.println("float d=\t" + d); } public static void main(String[] args) { System.out.println("-------------double--------------"); testDouble(); System.out.println("-------------float--------------"); testFloat(); } }
结果:
-------------double-------------- double a-b= 0.09999999999999998 double c= 0.1 double d= 0.09999999999999998 -------------float-------------- float a-b= 0.100000024 float c= 0.1 float d= 0.100000024
[JNI] native关键字
看java源码Object.java时,看到对clone方法的声明,用关键字native
:
// java源码Object.java对clone方法的声明
protected native Object clone() throws CloneNotSupportedException;
度娘一下,native关键字,说明调用的是Java本地接口,即 JNI (Java Native Interface):
Arrays.asList()转换List后,不支持remove和add
- 运行时,调用remove()/add()/set()方法抛出
java.lang.UnsupportedOperationException
异常。
(异常来自父类java.util.AbstractList类) - 改用
new ArrayList<?>(Arrays.asList(fixArray[]))
方式可避免异常
参考:java容器 – java.lang.UnsupportedOperationException
编译报错:No enclosing instance of type XXX is accessible.
main()方法中,调用内部类new方法报错。
参考:验证参数传递引发的Java编译错误“No enclosing instance of type XXX is accessible” 及匿名内部类
- 两种解决方法:
- 将内部类声明为static(不推荐)
- 通过外部类实例调用内部类new方法:
x.new A()
,其中x
是外部类的实例(推荐)
编译报错:Cannot refer to a non-final variable * inside an inner class defined in a different method
JAVA错误:Cannot refer to a non-final variable * inside an inner class defined in a different method
虚拟机
运行时报错:Exception in thread “main” java.lang.NoClassDefFoundError
Eclipse生成的java class文件通过java命令行调用提示Exception in thread “main” java.lang.NoClassDefFoundError
也有人踩过这个坑:
package导致出现java.lang.NoClassDefFoundError (wrong name)
Todo:这个是什么原因,估计以后要好好看看虚拟机原理了
GUI
编译报错:cannot add to layout: constraint must be a string
CardLayout卡片布局,Frame调用add方法报错:cannot add to layout: constraint must be a string
frame.add(panel);
报错信息:
Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string at java.awt.CardLayout.addLayoutComponent(Unknown Source) at java.awt.Container.addImpl(Unknown Source) at java.awt.Container.add(Unknown Source)
解决方法:
使用卡片布局,调用Frame.add()方法,必须有一个字符串描述加入的组件,改为:frame.add(panel,"penel1");
JDBC
MySQL
- WARN: Establishing SSL connection without server’s identity verification is not recommended.
- ERROR: java.sql.SQLException: No database selected
- java.sql.SQLException: Access denied for user ”@’localhost’ (using password: NO)
- ERROR: [Microsoft][ODBC 驱动程序管理器] 在指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配
参考:64位系统下的ODBC配置注意事项