Java基础知识

1、volatile关键字,无锁同步使用的关键字,被volatile描述的关键字,程序在执行的时候强制读取的是cpu内存里的数据

2、除了String类的字面常量会用到常量池,Java基础类型的包装类也会用到常量池,包括Byte,Short,Integer,Long,Character,Boolean;

要注意Byte,Short,Integer,Long,Character这5种整型的包装类也只是在对应值小于等于127时才可使用对象池,也即对象不负责创建和管理大于127的这些类的对象

另外double和float并没有实现常量池技术。

简单类型例如Int、boolean及它们的封装类等,包括String=”1”,jvm都会建立常量池,但是对于通过new创建的变量则不会创建常量。

简单类型的变量与他的封装类相比,如果值相等,它俩==应该是true;

3、String的intern()方法执行的过程是判断常量池中有没有与该String对象相等的字符串,如果有则返回常量池中的地址,没有的话则将此对象放入到常量池中,并返回该对象的地址。
下面代码因为jvm已经用过java,所有常量池中有了,所以是false.

String s1 = new StringBuffer("hello").append("world").toString();
        System.out.println(s1.intern() == s1);//true

        String s2 = new StringBuffer("ja").append("va").toString();
        System.out.println(s2.intern() == s2);//false

4、double和float只能用于科学计算,不能进行小数计算,一旦有小数计算,这两种类型的数据会根据最接近的二进制数据进行计算,会有误差,例如double a = 5.9;double b = 0.1;a * b = 0.5900000000000001;

在《Effective Java》这本书中也提到这个原 则,float和double只能用来做科学计算或者是工程计算,在商业计算中我们要用java.math.BigDecimal,而且非要用 String来够造BigDecimal不可!

例子


不怎么重要的基础:

一个interface可以被多个类实现,interface里所有方法都得被实现类实现,如果接口同时被多个类继承,interface不能被直接new,直接new也得在后面写上实现,所以它的实现就取决于是new的是哪个实现类,但是在spring里有点特殊,spring用interface一般用来定义service接口,往往实现类都会被加上@Service注解,同时也需要在配置文件里加上扫描实现类的包,这样才能在其他实现类或者action里使用@Autowired达到直接注入接口,其实也就相当于spring在我们使用@Autowired的时候直接把接口给实例了,至于为什么知道实例化的是哪个子类,当然spring也知道,因为在spring里一个接口如果有实现类上加上了@Service注解,那么此实现类对应的interface就只能有一个实现类被加上@Service,如果非要使用那个没有加@Service注解的实现类,那就不能在引用的地方不能加@Autowired,不然spring又热心的帮我们了。

java常用对数字的处理

1、 ROUND_UP:向上取整(丢掉小数,整数加1) 远离零方向舍入。向绝对值最大的方向舍入,只要舍弃位非0即进位。

2、ROUND_DOWN:向下取整(丢掉小数)。趋向零方向舍入。向绝对值最小的方向输入,所有的位都要舍弃,不存在进位情况。

3、ROUND_CEILING:向正无穷方向走,始终不会减少计算值。如果 BigDecimal 为正,则舍入行为与 ROUND_UP 相同;如果为负,则舍入行为与 ROUND_DOWN 相同。Math.round()方法就是使用的此模式。

4、ROUND_FLOOR:向负无穷方向舍入。向负无穷方向靠拢。若是正数,舍入行为类似于ROUND_DOWN;若为负数,舍入行为类似于ROUND_UP。

5、 HALF_UP:四舍五入,最近数字舍入(5进)。

6、 HALF_DOWN:四舍六入,最近数字舍入(5舍)。

7、 HAIL_EVEN:银行家舍入法。四舍六入五偶舍。即舍弃位4舍6入,当为5时看前一位,奇进偶舍。向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则向相邻的偶数舍入。
     也就是说,如果舍弃部分左边的数字为奇数,则舍入行为与 ROUND_HALF_UP 相同; 如果为偶数,则舍入行为与 ROUND_HALF_DOWN 相同。
     注意,在重复进行一系列计算时,此舍入模式可以将累加错误减到最小。

8、ROUND_UNNECESSARY 断言请求的操作具有精确的结果,因此不需要舍入。如果对获得精确结果的操作指定此舍入模式,则抛出ArithmeticException。

简单数据类型与常量池之间的关系

package com.galaxy.fym.test;

/**
 * Created by fengyiming on 2016/12/8.
 */
public class Test4 {

    public static void main(String[] args) throws Exception {
        equal();

    }

    public static void equal(){
        int intValue = 10;
        int otherIntVlue = 10;
        System.out.println("two same int ==:" + (intValue == otherIntVlue));
        intValue = 128;
        otherIntVlue = 128;
        System.out.println("when int value > 127 ,two same int ==:" + (intValue == otherIntVlue));
        String stringValue = new String("123");
        String otherStringValue = new String("123");
        System.out.println("two same new string ==:" + (stringValue == otherStringValue));
        System.out.println("two same new string equal:" + (stringValue.equals(otherStringValue)));
        stringValue = "123";
        otherStringValue = "123";
        System.out.println("two same string ==:" + (stringValue == otherStringValue));
        System.out.println("two same string equal:" + (stringValue.equals(otherStringValue)));
        Integer integerValue = 10;
        Integer otherIntegerVlue = 10;
        System.out.println("two same integer ==:" + (integerValue == otherIntegerVlue));
        integerValue = new Integer(10);
        otherIntegerVlue = new Integer(10);
        System.out.println("two same new integer ==:" + (integerValue == otherIntegerVlue));
        System.out.println("two same new integer equal:" + (integerValue.equals(otherIntegerVlue)));
        integerValue = 128;
        otherIntegerVlue = 128;
        System.out.println("when integer value > 127 ,two same integer ==:" + (integerValue == otherIntegerVlue));
        System.out.println("when integer value > 127 ,two same integer equal:" + (integerValue.equals(otherIntegerVlue)));
        intValue = 10;
        integerValue = 10;
        System.out.println("two same int and integer ==:" + (intValue == integerValue));
        integerValue = new Integer(10);
        System.out.println("two same int and new integer ==:" + (intValue == integerValue));
        otherIntVlue = 128;
        otherIntegerVlue = new Integer(128);
        System.out.println("when integer value > 127 ,two same int and new new integer ==:" + (otherIntVlue == otherIntegerVlue));
        double doubleValue = 10d;
        double otherDoubleValue = 10d;
        System.out.println("two same double ==:" + (doubleValue == otherDoubleValue));
        Double doubleValue1 = 10d;
        Double otherDoubleValue1 = 10d;
        System.out.println("two same Double ==:" + (doubleValue1 == otherDoubleValue1));
        System.out.println("two same double and Double ==:" + (doubleValue == doubleValue1));
        doubleValue1 = new Double(10);
        System.out.println("two same double and new Double ==:" + (doubleValue == doubleValue1));

    }
}




two same int ==:true
when int value > 127 ,two same int ==:true
two same new string ==:false
two same new string equal:true
two same string ==:true
two same string equal:true
two same integer ==:true
two same new integer ==:false
two same new integer equal:true
when integer value > 127 ,two same integer ==:false
when integer value > 127 ,two same integer equal:true
two same int and integer ==:true
two same int and new integer ==:true
when integer value > 127 ,two same int and new new integer ==:true
two same double ==:true
two same Double ==:false
two same double and Double ==:true
two same double and new Double ==:true

使用BigDecimal的时候尽量使用public BigDecimal(String val);来完成构造BigDecimal对象,因为有的时候像double会造成精度异常

    private static BigDecimal MIN_VALUE = new BigDecimal("0.01");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值