java封装数据类型——Byte
Byte 是基本类型byte的封装类型。与Integer类似,Byte也提供了很多相同的方法,如 decode、toString、intValue、floatValue等,而且很多方法还是直接类型转换为 int型进行操作的(比如: public static String toString(byte b) { return Integer.toString((int)b, ); } )。所以我们只是重点关注一些不同的方法或者特性。
1. 取值范围
我们知道,byte 表示的数据范围是 [-128, 127],那么Byte使用两个静态属性分别表示上界和下界:MIN_VALUE、MAX_VALUE
2. 缓存
与Integer相似,Byte也提供了缓存机制,源码如下:
- private static class ByteCache {
- private ByteCache(){}
- static final Byte cache[] = new Byte[-(-128) + 127 + 1];
- static {
- for(int i = 0; i < cache.length; i++)
- cache[i] = new Byte((byte)(i - 128));
- }
- }
可以看出,Byte的缓存也是 -128~127。我们已经知道,byte类型值范围就是 [-128, 127],所以Byte是对所有值都进行了缓存。
3. 构造方法和valueOf方法
Byte也提供两个构造方法,分别接受 byte 和 string 类型参数: public Byte(byte value) { this.value = value; } 和 public Byte(String s) throws NumberFormatException { this.value = parseByte(s, 10); } 。使用构造方法创建对象时,会直接分配内存。而 valueOf 方法会从缓存中获取,所以一般情况下推荐使用 valueOf 获取对象实例,以节省开支。
- public static Byte valueOf(byte b) {
- final int offset = 128;
- return ByteCache.cache[(int)b + offset];
- }
- public static Byte valueOf(String s, int radix)
- throws NumberFormatException {
- return valueOf(parseByte(s, radix));
- }
- public static Byte valueOf(String s) throws NumberFormatException {
- return valueOf(s, 10);
- }
4. hashCoe 方法
Byte 类型的 hashCode 值就是它表示的值(转int)
- @Override
- public int hashCode() {
- return Byte.hashCode(value);
- }
- /**
- * Returns a hash code for a {@code byte} value; compatible with
- * {@code Byte.hashCode()}.
- *
- * @param value the value to hash
- * @return a hash code value for a {@code byte} value.
- * @since 1.8
- */
- public static int hashCode(byte value) {
- return (int)value;
- }
5. compareTo 方法
Byte 也实现了 comparable 接口,可以比较大小。与 Integer 的 compareTo 有点不同的是,Integer 在当前值小于参数值时分别返回 -1、0、1,而Byte是返回正数、0、负数(当前值-参数值),当然,这也同样符合 comparable 的常规约定。
- public int compareTo(Byte anotherByte) {
- return compare(this.value, anotherByte.value);
- }
- /**
- * Compares two {@code byte} values numerically.
- * The value returned is identical to what would be returned by:
- * <pre>
- * Byte.valueOf(x).compareTo(Byte.valueOf(y))
- * </pre>
- *
- * @param x the first {@code byte} to compare
- * @param y the second {@code byte} 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(byte x, byte y) {
- return x - y;
- }
完!
java封装数据类型——Byte的更多相关文章
- java封装数据类型——Integer
今天来学习整型 int 的封装数据类型,Integer. 1. 定义 首先来看看定义.可以看到,Integer 继承 Number 抽象类,实现了 Comparable 接口.Number 类是常用数 ...
- java封装数据类型——Long
Long 是长整型 long 的封装数据类型.我们知道 long 相对于 int 的差异就是数据表示的范围扩大了,其它大部分特性都是一样的.所以 Long 跟 Integer 大部分方法都是相同的. ...
- java封装数据类型——Boolean
众所周知,java对常见的原始数据类型都提供了对应的封装类型,增加一些常用的特性(如 计算hash值.比较相等.类型转换等),以扩展他们对数据处理的能力,使得他们更好地适应面向对象编程的各种场景.今天 ...
- java封装数据类型——Integer 缓存策略验证
上一篇学习 Integer 类型源码,知道了它使用缓存策略,默认对 [-128, 127] 范围的对象进行类加载时自动创建缓存. Integer 源码学习:https://www.cnblogs.co ...
- C++11 Java基本数据类型以及转换
写在前面: 母语是Java,后来学了C++11,这两个语言的基本数据类型隐式转换不太一样,有点晕,整理一下 整理自网络和书籍,标明出处 C++ 基本数据类型 --http://www.cnblogs. ...
- 【转】java基本数据类型vs封装数据类型
1.基本概念 说java是面向对象的语言是正确的,但是她不纯,基本数据类型就不是对象. 基本数据类型可以大致分为三类:数据型:int.short.long.byte.float.double字符型:c ...
- Java基础-数据类型int,short,char,long,float,double,boolean,byte
Java语言是静态类型的(statical typed),也就是说所有变量和表达式的类型再编译时就已经完全确定.由于是statical typed,导致Java语言也是强类型(Strong typed ...
- Java中基本数据类型byte的溢出问题
Java中基本数据类型byte的溢出问题 问题源于:https://www.cnblogs.com/HuoHua2020/p/12326631.html 定义两个byte类型的数据,将其之和赋值给一个 ...
- Java中基本数据类型byte,short,char,int,long,float,double 取值范围
部分内容转自:java 彻底理解 byte char short int float long double 首先说byte: 这段是摘自jdk中 Byte.java中的源代码: /** * A co ...
随机推荐
- angular ts处理日期格式
引入DatePipe import {DatePipe} from '@angular/common'; 添加provider @Component({ providers: [DatePipe] } ...
- Python之Django之views中视图代码重复查询的优化
Django框架中views视图中如果多个函数都有同样的查询语句,例如: allcategory = Category.objects.all() remen = Article.objects.fi ...
- openresty开发系列20--lua的时间操作
openresty开发系列20--lua的时间操作 在 Lua 中,函数 time.date 和 difftime 提供了所有的日期和时间功能.在 OpenResty 的世界里,不推荐使用这里的标准时 ...
- centos7.6下编译安装zabbix4.0.10长期支持版
一.安装数据库,这里使用的是percona-server5..24版本 配置如下 [root@zabbix4_clone:~]# cat /etc/my.cnf # Example MySQL con ...
- ISO/IEC 9899:2011 条款6.5.6——加法操作符
6.5.6 加法操作符 语法 1.additive-expression: multiplication-expression additive-expression + multipli ...
- 004-行为型-07-备忘录模式(Memento)
一.概述 又叫做快照模式(Snapshot Pattern)或Token模式 保存对象的内部状态,并在需要的时候(undo/rollback)恢复对象以前的状态. 意图:在不破坏封装性的前提下,捕获一 ...
- Linux记录-shell自动化批量部署sql脚本并记录日志信息(转载)
#!/bin/bash #script_version=v110 db_host=127.0.0.1 db_port=3306 db_username=db_test_inst db_passwd=` ...
- linux下配置face_recognition
1.如linux下已有python2.7,但需要更新一下python 2.7至python2.x sudo add-apt-repository ppa:fkrull/deadsnakes-pytho ...
- Swift4.0复习枚举
1.枚举类型: “Swift编程语言中,枚举类型属于值类型,而不是引用类型.” 摘录来自: “大话Swift 4.0”. iBooks. 2.枚举类型和枚举对象的定义: enum Enumeratio ...
- 【tensorflow】tensorflow官网进不去,因为它的地址改变了
以前的网址是https://www.tensorflow.org/,当时得fq才能打开,现在这个我fq都打不开了. 现在新网址是https://tensorflow.google.cn/这个不fq都可 ...