码迷,mamicode.com
首页 > 其他好文 > 详细

2015-09-10日问题

时间:2015-09-10 22:25:53      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:

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是包装类,怎么能传入一个包装类作为一个实参来传参呢??

 

2015-09-10日问题

标签:

原文地址:http://www.cnblogs.com/AbsurdFantasy/p/4799252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!