1.概述

转载:jdk源码分析-------------boolean

这篇文章小编只是对boolean类型的几个方法进行一次知识梳理,大家有好的意见可以在留言区留下宝贵的意见,小编会做出相应的调整。

1.1 booleanValue()

   /*** Returns the value of this {@code Boolean} object as a boolean* primitive.** @return  the primitive {@code boolean} value of this object.*/public boolean booleanValue() {return value;}

这个方法返回的是当前这个boolean类型对象的内容。

boolean b=true;
System.out.println(b.booleanValue());
----------------------------------------
true

1.2 toString()

/*** Returns a {@code String} object representing this Boolean's* value.  If this object represents the value {@code true},* a string equal to {@code "true"} is returned. Otherwise, a* string equal to {@code "false"} is returned.** @return  a string representation of this object.*/
public String toString() {return value ? "true" : "false";
}

这个方法返回类型是string,返回的是boolean对象的内容

boolean b=false;
System.out.println(b.booleanValue());
----------------------------------------
false

1.3 hashCode()

   /*** Returns a hash code for this {@code Boolean} object.** @return  the integer {@code 1231} if this object represents* {@code true}; returns the integer {@code 1237} if this* object represents {@code false}.*/@Overridepublic int hashCode() {return Boolean.hashCode(value);}/*** Returns a hash code for a {@code boolean} value; compatible with* {@code Boolean.hashCode()}.** @param value the value to hash* @return a hash code value for a {@code boolean} value.* @since 1.8*/public static int hashCode(boolean value) {return value ? 1231 : 1237;}

当调用hashCode返回的是true或者false的哈希值。在hashCode这个方法中通过三目表达式表现出来。

1.4 compareTo()

 /*** Compares this {@code Boolean} instance with another.** @param   b the {@code Boolean} instance to be compared* @return  zero if this object represents the same boolean value as the*          argument; a positive value if this object represents true*          and the argument represents false; and a negative value if*          this object represents false and the argument represents true* @throws  NullPointerException if the argument is {@code null}* @see     Comparable* @since  1.5*/public int compareTo(Boolean b) {return compare(this.value, b.value);}/*** Compares two {@code boolean} values.* The value returned is identical to what would be returned by:* <pre>*    Boolean.valueOf(x).compareTo(Boolean.valueOf(y))* </pre>** @param  x the first {@code boolean} to compare* @param  y the second {@code boolean} to compare* @return the value {@code 0} if {@code x == y};*         a value less than {@code 0} if {@code !x && y}; and*         a value greater than {@code 0} if {@code x && !y}* @since 1.7*/public static int compare(boolean x, boolean y) {return (x == y) ? 0 : (x ? 1 : -1);}

调用这个方法时会拿当前对象和要比较的对象value进行比较。compare方法最终定义只能返回1或者-1。

1.5 equals()

   /*** Returns {@code true} if and only if the argument is not* {@code null} and is a {@code Boolean} object that* represents the same {@code boolean} value as this object.** @param   obj   the object to compare with.* @return  {@code true} if the Boolean objects represent the*          same value; {@code false} otherwise.*/public boolean equals(Object obj) {if (obj instanceof Boolean) {return value == ((Boolean)obj).booleanValue();}return false;}

如果当前对象时boolean的实例,就返回这个对象的内容,否则返回false

N.扩展阅读

AtomicBoolean介绍与使用

【java】java boolean 源码分析相关推荐

  1. java.util.ServiceLoader源码分析

    java.util.ServiceLoader源码分析 回顾: ServiceLoader类的使用(具体参考博客http://blog.csdn.net/liangyihuai/article/det ...

  2. 并发编程5:Java 阻塞队列源码分析(下)

    上一篇 并发编程4:Java 阻塞队列源码分析(上) 我们了解了 ArrayBlockingQueue, LinkedBlockingQueue 和 PriorityBlockingQueue,这篇文 ...

  3. Java并发-ReentrantReadWriteLock源码分析

    ReentrantLock实现了标准的互斥重入锁,任一时刻只有一个线程能获得锁.考虑这样一个场景:大部分时间都是读操作,写操作很少发生:我们知道,读操作是不会修改共享数据的,如果实现互斥锁,那么即使都 ...

  4. Java集合类框架源码分析 之 LinkedList源码解析 【4】

    上一篇介绍了ArrayList的源码分析[点击看文章],既然ArrayList都已经做了介绍,那么作为他同胞兄弟的LinkedList,当然必须也配拥有姓名! Talk is cheap,show m ...

  5. Java中ArrayList源码分析

    一.简介 ArrayList是一个数组队列,相当于动态数组.每个ArrayList实例都有自己的容量,该容量至少和所存储数据的个数一样大小,在每次添加数据时,它会使用ensureCapacity()保 ...

  6. Java容器 | 基于源码分析List集合体系

    一.容器之List集合 List集合体系应该是日常开发中最常用的API,而且通常是作为面试压轴问题(JVM.集合.并发),集合这块代码的整体设计也是融合很多编程思想,对于程序员来说具有很高的参考和借鉴 ...

  7. java并发:join源码分析

    join join join是Thread方法,它的作用是A线程中子线程B在运行之后调用了B.join(),A线程会阻塞直至B线程执行结束 join源码(只有继承Thread类才能使用) 基于open ...

  8. 【Java】equals源码分析

    源码分析 This class consists of static utility methods for operating on objects. These utilities include ...

  9. JAVA集合专题+源码分析

    文章目录 Java集合专题 集合和数组的区别 数组 集合 区别 集合体系结构介绍 单列集合 [Collection ] Collection接口 迭代器 迭代器原理 增强for循环 List接口 对集 ...

最新文章

  1. 如何通过引入硬注意力机制来学习视觉问答任务?
  2. linux算法设计,红黑树的原理分析和算法设计
  3. es6 --- 使用yield*命令遍历完全二叉树
  4. 机器学习笔记(五):逻辑回归
  5. Uber开源深度概率编程语言Pyro,AI实验室蛰伏一年首现身
  6. C#编程(二十五)----------接口
  7. Ubuntu enca命令可以用来查看文件的编码格式
  8. 【Kettle】Win7启动Spoon.bat闪退
  9. 【bzoj4173】数学
  10. html微信录制视频,微信视频的时候怎么录制屏幕?这个方法超简单
  11. 电商屌丝生活,有点甜、有点咸
  12. 本地Remix与私链节点相连
  13. c 语言中析构函数,详解C++中的析构函数
  14. Python 如何被证明是 2021 年的转折点语言
  15. VMware VCP认证常见问答题
  16. sap客户信贷_信贷控制配置全过程-SAP
  17. Python_内置函数之round的幺蛾子
  18. 【imessage苹果推】苹果邮箱推邮箱Apple Push IMESSage SMS
  19. 前端小白训练营DAY3
  20. 1011 最大公约数GCD

热门文章

  1. iPhone 13系列相机有大升级:全系支持光学防抖功能
  2. 史上最贵!iPhone 12S系列9月亮相,全系标配激光雷达
  3. 苹果因不附赠充电头被罚200万美元,网友:该,大快人心!
  4. 国家网信办:督促知乎、豆瓣等20家知识社区问答平台开展自查自纠
  5. 武汉街头出现手机无线充电路灯,极速快充,但需注意这一点!
  6. AMD官宣350亿美元收购赛灵思 赛灵思大涨8.56%
  7. 今晚7点见!红魔5G游戏手机联手热门手游打造专属主题
  8. iPad加价抢购,iPhone 11面临缺货,苹果的供应链困局来了
  9. 京东总部4号楼即将投入使用:建面超过13万平方米 耗时644天
  10. 顺丰拟发行58亿可转债:15亿投入航空运力,12亿还贷款