• 2015-09-10日问题(一)


    1.String.valueOf(boolean b) : 将 boolean 变量 b 转换成字符串 ??这里的字符串是String类的对象????

    ctrl+左键单击valueOf()查看源码

     1  /**
     2      * Returns the string representation of the <code>boolean</code> argument.
     3      *
     4      * @param   b   a <code>boolean</code>.
     5      * @return  if the argument is <code>true</code>, a string equal to
     6      *          <code>"true"</code> is returned; otherwise, a string equal to
     7      *          <code>"false"</code> is returned.
     8      */
     9     public static String valueOf(boolean b) {
    10         return b ? "true" : "false";
    11     }

    valueOf(boolean)方法的实质是:当你传入boolean类型的true时返回(常量池中)字符串对象"true"的引用。当你传入boolean类型的false时返回(常量池中)字符串对象"false"的引用。 

    (1).“将 boolean 变量 b 转换成字符串”对,实质是就是获取到了boolean变量对应字符串对象的引用。

    (2).“这里的字符串是String类的对象????” 对

    2.

     1 public class Person
     2 {
     3     public static void main(String[] args) {
     4         
     5         String intstr="123";//装箱:将string类型数据赋值给包装类变量intstr???更准确的说法是:将常量池中对象("123")的引用赋值给String类型的引用变量intstr.
     6         System.out.println("装箱后="+intstr);//怎么输出结果是整形的?不是字符串类型的么???
     7         int it1=Integer.parseInt(intstr);//将intstr转换为int类型的??因为intstr是String类的变量,为何不是String.parseInt(intstr)???
     8         int it2=new Integer(intstr);//这是创建一个对象new Integer(),那new Integer(intstr)是向Integer构造方法中传一个整形instr?但是它的构造方法具体是什么,传参之后是怎么执行的怎么能知道?这里intstr是包装类,怎么能传入一个包装类作为一个实参来传参呢??
     9         System.out.println(it2);
    10         String floatstr="456";
    11         float ft1=Float.parseFloat(floatstr);//将floatstr转换为Float类型的???
    12         float ft2=new Float(floatstr);//???
    13         System.out.println(ft2);
    14         String ftstr=String.valueOf(2.345f);
    15         System.out.println("ftstr="+ftstr);
    16         String dbstr=String.valueOf(3.344);
    17         System.out.println("dbstr="+dbstr);
    18         String boolstr=String.valueOf(true);
    19         System.out.println("bool+"+boolstr.toUpperCase());
    20          //TRUE是string变量???? 这个没懂什么意思
    21     }
    22 }

    (1)装箱:将string类型数据赋值给包装类变量intstr???

       更准确的说法是:将常量池中对象("123")的引用赋值给String类型的引用变量intstr.而且String类,不是包装类!

    (2)System.out.println("装箱后="+intstr);//怎么输出结果是整形的?不是字符串类型的么???

         你是不是这么认为的:"装箱后="+intstr这是个字符串啊,为何我在控制台看到的是  装箱后123 ?看起来好像我传入了字符串类型但是输出的却是整数!

     ctrl+左键println()方法查看源码

     1  /**
     2      * Prints a String and then terminate the line.  This method behaves as
     3      * though it invokes <code>{@link #print(String)}</code> and then
     4      * <code>{@link #println()}</code>.
     5      *
     6      * @param x  The <code>String</code> to be printed.
     7      */
     8     public void println(String x) {
     9         synchronized (this) {
    //输出这个字符串到控制台
    10 print(x);
    //换行
    11 newLine(); 12 } 13 }

    方法解释很清楚咯,是将你传入的字符串x打印到控制台。没有“输出整型”这种说法。

    (3)int it1=Integer.parseInt(intstr);//将intstr转换为int类型的??因为intstr是String类的变量,为何不是String.parseInt(intstr)???

      这是类的设计问题。Integer类被设计用来处理和int类型相关的操作。而String类被设计用来处理和字符串相关的操作。估计是设计者认为“将字符串形式的数字转化为int类型的数字”这个操作跟靠近int类型相关的操作。所以将这个方法写到了Integer类中。

    (4.1)int it2=new Integer(intstr);//这是创建一个对象new Integer(),那new Integer(intstr)是向Integer构造方法中传一个整形instr?

         是传递的一个String类型的引用instr,不是整形的instr哦。你不会是以为instr的类型被改变了把。。。

    (4.2)但是它的构造方法具体是什么,传参之后是怎么执行的怎么能知道?

         当你遇到new 对象()例如new Integer(intstr);这种形式的代码时,如果想查看对象的构造器,只需要。。。ctrl+左键单击 对象名(比如单击Integer)就可以查看到对应的构造器定义了,根据构造器的定义,就可以知道在传入参数后,这个构造器是如何定义的了。

        下面是单击new Integer(intstr);中Integer时,查看到的构造器的定义:

     1  /**
     2      * Constructs a newly allocated {@code Integer} object that
     3      * represents the {@code int} value indicated by the
     4      * {@code String} parameter. The string is converted to an
     5      * {@code int} value in exactly the manner used by the
     6      * {@code parseInt} method for radix 10.
     7      *
     8      * @param      s   the {@code String} to be converted to an
     9      *                 {@code Integer}.
    10      * @exception  NumberFormatException  if the {@code String} does not
    11      *               contain a parsable integer.
    12      * @see        java.lang.Integer#parseInt(java.lang.String, int)
    13      */
    14     public Integer(String s) throws NumberFormatException {
    15         this.value = parseInt(s, 10);
    16     }

       

    (4.3)这里intstr是包装类,怎么能传入一个包装类作为一个实参来传参呢??

           首先String类不是包装类。而且intstr是一个String类型的引用,也不是包装类.public Integer(String s)这里传入的是一个String类型的引用变量而不是你说的传入的包装类。

    5.float ft1=Float.parseFloat(floatstr);//将floatstr转换为Float类型的??? 

    对。不过你这里写错了哦是float不是Float

  • 相关阅读:
    Python·安装扩展包的几种方法
    Arduino系列硬件资源介绍
    树莓派USB摄像头的使用
    树莓派frp服务器和客户端配置教程
    树莓派frp内网穿透
    用Windows远程桌面连接树莓派的方法
    控制窗体的位置和大小
    树霉派更换软件镜像源
    I2C的库函数应用示例
    I2C总线的Arduino库函数
  • 原文地址:https://www.cnblogs.com/AbsurdFantasy/p/4799252.html
Copyright © 2020-2023  润新知