java的包装类 短命女 2023-01-19 07:21 155阅读 0赞 [java面向对象之三 多态][java_] ### 文章目录 ### * java的包装类 * * * Integer包装类 * * * Integer类的基本介绍 * Integer类的属性 * Integer类的构造器 * Integer包装类的方法 # java的包装类 # * 什么是包装类 对于基本数据类型来讲,其实就是一个数字,但是当给基本数据类型添加一些属性 方法和构造器,将基本数据类型对应进行一个封装,就产生了一个新的类,这个类被称为是包装类。 那么基本数据类型在前面中讲过有`byte short int long float double char boolean`,也就是将这些进行封装,基本数据类型和包装类的对应关系是怎样的呢? <table> <thead> <tr> <th>基本数据类型</th> <th>对应的包装类</th> </tr> </thead> <tbody> <tr> <td>byte</td> <td>Byte</td> </tr> <tr> <td>short</td> <td>Short</td> </tr> <tr> <td>int</td> <td>Integer</td> </tr> <tr> <td>long</td> <td>Long</td> </tr> <tr> <td>float</td> <td>Float</td> </tr> <tr> <td>double</td> <td>Double</td> </tr> <tr> <td>char</td> <td>Char</td> </tr> <tr> <td>boolean</td> <td>Boolean</td> </tr> </tbody> </table> 接下来以包装类中的Integer类为代表说明包装类的用法,除此之外的其他包装类也是大同小异的。 ### Integer包装类 ### ##### Integer类的基本介绍 ##### Integer包装类,是通过int基本数据类型进行封装得到的,可以通过查看java的API说明文档得到它的基本信息: ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NjcxNzMy_size_16_color_FFFFFF_t_70] Integer包装类在java.lang中,这将意味着可以直接使用,不需要进行导包 ![在这里插入图片描述][20210508165827493.png] Integer类被final修饰符修饰,那么这个类不可以有子类,不能被继承。 ##### Integer类的属性 ##### ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NjcxNzMy_size_16_color_FFFFFF_t_70 1] package cn.zhz.Instance; public class Test02 { public static void main(String[] args) { //属性 System.out.println(Integer.MAX_VALUE); System.out.println(Integer.MIN_VALUE); //物极必反:超出了Int类型的范围 System.out.println(Integer.MAX_VALUE + 1); System.out.println(Integer.MIN_VALUE - 1); } } 这里附上Integer类的源码 public final class Integer extends Number implements Comparable<Integer>, Constable, ConstantDesc { /** * A constant holding the minimum value an {@code int} can * have, -2<sup>31</sup>. */ @Native public static final int MIN_VALUE = 0x80000000; /** * A constant holding the maximum value an {@code int} can * have, 2<sup>31</sup>-1. */ @Native public static final int MAX_VALUE = 0x7fffffff; /** * The {@code Class} instance representing the primitive type * {@code int}. * * @since 1.1 */ @SuppressWarnings("unchecked") ##### Integer类的构造器 ##### ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NjcxNzMy_size_16_color_FFFFFF_t_70 2] package cn.zhz.Instance; public class Test03 { public static void main(String[] args) { Integer i1 = new Integer(13); System.out.println(i1.toString()); Integer i2 = new Integer("12"); System.out.println(i2); } } 接下来单拿出这两句代码进行详细说明: Integer i1 = new Integer(13); System.out.println(i1.toString()); 使用Ctrl+Alt+鼠标点击要查看的类,跳转到Integer的源代码,创建一个Integer类的实例,并且赋值为13 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NjcxNzMy_size_16_color_FFFFFF_t_70 3] 也就是value=13,`this.value = value`this代指的是当前对象,也就是将value=13赋值给i1. ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NjcxNzMy_size_16_color_FFFFFF_t_70 4] 调用Integer类中的toString方法,返回当前对象的value值并打印输出。 这里附上Integer类中的toString方法的源码: public static String toString(int i) { int size = stringSize(i); if (COMPACT_STRINGS) { byte[] buf = new byte[size]; getChars(i, size, buf); return new String(buf, LATIN1); } else { byte[] buf = new byte[size * 2]; StringUTF16.getChars(i, size, buf); return new String(buf, UTF16); } } * 自动装箱和自动拆箱 package cn.zhz.Instance; public class Test04 { public static void main(String[] args) { //自动装箱:int-->Integer Integer i = 12; System.out.println(i); //自动拆箱:Integer-->int Integer i2 = new Integer(123); int num = i2; System.out.println(num); } } ##### Integer包装类的方法 ##### Integer包装类中有几个方法比较常用,也是十分重要的: <table> <thead> <tr> <th>类型</th> <th>方法</th> <th>方法的说明</th> <th>返回值</th> </tr> </thead> <tbody> <tr> <td>int</td> <td>compareTo()</td> <td>比较两个整数对象的数值。</td> <td>只返回三个值,要么是0 -1 1</td> </tr> <tr> <td>boolean</td> <td>equals()</td> <td>将此对象与指定对象进行比较。</td> <td>返回true或者是false</td> </tr> <tr> <td>int</td> <td>intValue()</td> <td>以整数形式返回此整数的值。</td> <td></td> </tr> <tr> <td>static int</td> <td>parseInt()</td> <td>将输入的String类型的数据转换成int类型的数据</td> <td></td> </tr> <tr> <td>String</td> <td>toString()</td> <td>返回表示此整数值的字符串对象。</td> <td></td> </tr> </tbody> </table> package cn.zhz.Instance; public class Test05 { public static void main(String[] args) { //compareTo():只返回三个值,要么是0 -1 1 Integer i1 = new Integer(6); Integer i2 = new Integer(12); System.out.println(i1.compareTo(i2));//return (x < y) ? -1 ((x == y) ? 0 : 1) //equals():Integer是对object中的equals方法进行了重写,比较的是底层封装的那个value的值 //Integer对象是通过new关键字来创建对象 Integer i3 = new Integer(12); Integer i4 = new Integer(12); System.out.println(i3 == i4);//false 因为==比较的是两个对象的地址 boolean flag = i3.equals(i4); System.out.println(flag); //Integer对象是通过自动装箱来完成的 Integer i5 = 12; Integer i6 = 12; System.out.println(i5.equals(i6));//true System.out.println(i5 == i6); /* * 如果自动装箱在-128~127之间,那么比较的就是具体的数值,否则,比较的就是对象的地址 * */ //intvalue(): Integer i7 = 130; int i = i7.intValue(); System.out.println(i); //parseInt(String s):String -->int int i8 = Integer.parseInt("12"); System.out.println(i8); //toString(): Integer i10 = 130; System.out.println(i10.toString()); } } * Integer类中的compareTo()方法 Integer i1 = new Integer(6); Integer i2 = new Integer(12); System.out.println(i1.compareTo(i2));//return (x < y) ? -1 ((x == y) ? 0 : 1) ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NjcxNzMy_size_16_color_FFFFFF_t_70 5] 分别创建了两个Integer的对象i1赋值为6,i2赋值为12,调用Integer类中的compareTo方法,参数中Integer代表了6,anotherInteger代表了12,调用了compare方法,this代指当前对象也就是i1 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NjcxNzMy_size_16_color_FFFFFF_t_70 6] compare方法中x为6,y为12,源码中是运用三目运算符比较x与y的值的大小并决定返回的是0 -1还是1,也就是当x<y是返回-1,当x=y时返回0,当x>y返回1. * Integer类中的intValue()方法 //intvalue(): Integer i7 = 130; int i = i7.intValue(); System.out.println(i); Integer类中的intValue方法是将int类型的值进行一个返回。 附上intValue()方法的源码: private final int value; public int intValue() { return value; } [java与日期相关的类][java] [java_]: https://blog.csdn.net/qq_45671732/article/details/116430603 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NjcxNzMy_size_16_color_FFFFFF_t_70]: /images/20221021/7f43a47e33954662956e8808f45a5185.png [20210508165827493.png]: /images/20221021/b47cb296d9064346b9e8b04aa6e450f2.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NjcxNzMy_size_16_color_FFFFFF_t_70 1]: /images/20221021/fe51b1e1114940dc932e7de9719073ed.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NjcxNzMy_size_16_color_FFFFFF_t_70 2]: /images/20221021/3b18556119d3465db96796acdee4caad.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NjcxNzMy_size_16_color_FFFFFF_t_70 3]: /images/20221021/90b368b15dea40699335c33b3d77897b.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NjcxNzMy_size_16_color_FFFFFF_t_70 4]: /images/20221021/dc3993647d0a41cb887de7f2a95d32dd.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NjcxNzMy_size_16_color_FFFFFF_t_70 5]: /images/20221021/09e7a74d9c95410481d5e7689aee9f4b.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1NjcxNzMy_size_16_color_FFFFFF_t_70 6]: /images/20221021/0508fa16a4af4011afe99accf4d90044.png [java]: https://blog.csdn.net/qq_45671732/article/details/116563938
还没有评论,来说两句吧...