java零碎知识点整理(3)

关于java字符串String以及Hashcode的一些整理

hashcode即是对象地址的十进制形式,而equals方法即是在比较两个对象的hashcode是否相等

package com.test.wx.testequals;

public class TestEquals {

    public static void testEquals() {
        String a = "a";
        String b = "a";
        boolean result = a.equals(b);
        System.out.println(result);         // true
        System.out.println(a.hashCode() == b.hashCode());       // true
    }

    public static void testEqual() {
        String a = "a";
        String b = "a";
        if(a == b) {                        // true
            System.out.println("true");
            System.out.println(a.hashCode() == b.hashCode());
        }else {
            System.out.println("false");
        }
    }

    public static void testEquals2() {
        String a = "a";
        String b = a;
        // equals方法比较两个对象的hashcode,hashcode即对象的地址
        System.out.println(a.equals(b));
        // == 比较两个变量或者对象的值是否相等,equals比较两个对象的引用是否相等(是否指向同一个对象)
        System.out.println(a==b);
    }

    public static void normalTest() {
        int a = 12;
        String b = String.valueOf(a);
    }

    public static void testObjectHashcode() {
        User user1 = new User(1, "test");
        User user2 = new User(2, "test");
        System.out.println("user1 toString: "+user1);
        System.out.println("user1 hashcode: "+user1.hashCode());
        // hashcode即是对象地址的十进制形式
        System.out.println(Integer.valueOf("659e0bfd", 16));
        System.out.println(user2);
    }

    public static void hexToDeciaml() {
        long a = 1021653256;
        System.out.println(Long.toHexString(a));    //3ce53108
        System.out.println(Integer.valueOf("659e0bfd", 16));
    }

    public static void main(String[] args) {
//      testEquals();
//      testEqual();
//      testEquals2();
//      hexToDeciaml();
        testObjectHashcode();
    }

}

枚举enum整理

枚举enum是一种类型,不是类,所以不能被实例化,一般用来定义集合常量

package com.test.wx.testenum;

public class EnumTest {

    PrinterType printType;
    enum PrinterType { INKJET, DOTMATRIX, LASER };
    public EnumTest(PrinterType pType) {
        printType = pType;
    }

    public static void main(String[] args) {
//      PrinterType pType = new PrinterType();
        EnumTest enumTest = new EnumTest(PrinterType.LASER);
        System.out.println(enumTest.printType);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值