2015-09-10 Day Issue

Source: Internet
Author: User

1.string.valueof (Boolean B): Converts a Boolean variable B to a string?? The string here is the object of the String class????

CTRL + Left click ValueOf () to view the source code

1  /**2 * Returns The string representation of the <code>boolean</code> argument.3      *4      * @paramb a <code>boolean</code>.5      * @returnIf the argument is <code>true</code> a string equal to6 * <code> "true" </code> is returned; otherwise, a string equal to7 * <code> "false" </code> is returned.8      */9      Public StaticString ValueOf (Booleanb) {Ten         returnB? "True": "false"; One}

The essence of the ValueOf (Boolean) method is to return a reference to the string Object "true" (in a constant pool) when you pass True to the Boolean type . Returns a reference to the string object "False" when you pass the Boolean type to False (in a constant pool).  

(1). " Converts a Boolean variable B to a string "pair, essentially obtaining a reference to a Boolean variable corresponding to a String object.

(2). " The string here is the object of the String class???? "To

2.

1  Public class Person2 {3      Public Static voidMain (string[] args) {4         5String intstr= "123";//Boxing: Assigning string type data to the wrapper class variable Intstr??? More accurately, a reference to a string type of reference variable INTSTR is assigned to the object in the constant pool ("123").6System.out.println ("post-boxing =" +intstr);//How does the output result be shaped? Not a string type??? 7         intIt1=integer.parseint (INTSTR);//convert intstr to int type?? Because INTSTR is a variable of the string class, why not String.parseint (INTSTR)??? 8         intIt2=NewInteger (INTSTR);//This is to create an object new Integer (), and the new Integer (INTSTR) is to pass a shaping InStr to the Integer constructor method? But how does it work, and how do you know how to do it after the argument? Here Intstr is a wrapper class, how can you pass in a wrapper class as an actual parameter to pass the argument??9 System.out.println (it2);TenString floatstr= "456"; One         floatFt1=float.parsefloat (FLOATSTR);//convert floatstr to float type???  A         floatFt2=NewFloat (FLOATSTR);//??? - System.out.println (ft2); -String ftstr=string.valueof (2.345f); theSystem.out.println ("ftstr=" +ftstr); -String dbstr=string.valueof (3.344); -System.out.println ("dbstr=" +dbstr); -String boolstr=string.valueof (true); +System.out.println ("bool+" +boolstr.touppercase ()); -          //true is a string variable????  +     } A}

(1) Boxing: Assigning string type data to the wrapper class variable Intstr???

More accurately, a reference to the object ("123") in a constant pool is assigned to the reference variable intstr of type string. And the string class, not the wrapper class!

(2) System.out.println ("After boxing =" +intstr);//How does the output result be plastic? Not a string type???

Do you think so: "After boxing =" +intstr This is a string ah, why I see in the console is boxed 123? It looks as if I passed in a string type but the output is an integer!

CTRL + Left println () method to view the source code

1  /**2 * Prints a String and then terminate the line. This method behaves as3 * Though it invokes <code>{@link#print (String)}</code> and then4 * <code>{@link#println ()}</code>.5      *6      * @paramx The <code>String</code> to is printed.7      */8      Public voidprintln (String x) {9         synchronized( This) {
Output this string to the consoleTen print (x);
Line break One newLine (); A } -}

The method explains it very clearly that you are printing the string x that you passed in to the console. There is no "output integral type" argument.

(3) int it1=integer.parseint (INTSTR);//convert intstr to int type?? Because INTSTR is a variable of the string class, why not String.parseint (INTSTR)???

This is a design problem for the class. The integer class is designed to handle operations related to type int. The string class is designed to handle string-related operations. It is estimated that the designer believes that the "convert a number in string form to a number of type int" is an operation that is related to an int type. So this method is written in the integer class.

(4.1) int it2=new integer (INTSTR);//This is the creation of an object new integer (), and the new Integer (INTSTR) is to pass a shaping InStr to the Integer constructor method?

is a string type that is passed the reference InStr, not the instr of the Shaping Oh. You don't think the type of InStr has been changed.

(4.2) But what is the specific structure of the method, and how do you know how to do it after the argument?

When you encounter a new object () such as New Integer (INTSTR); This form of code, if you want to view the object's constructor, you only need ... CTRL + Left-click the object name (for example, click Integer) to see the corresponding constructor definition, according to the definition of the constructor, you can know how the constructor is defined after passing in the parameters.

The following is the definition of the constructor that you see when you click New Integer (INTSTR), in integer:

1  /**2 * Constructs a newly allocated {@codeInteger} object that3 * Represents the {@codeint} value indicated by the4      * {@codeString} parameter. The string is converted5      * {@codeint} value in exactly the manner used by the6      * {@codeparseint} method for Radix.7      *8      * @params The {@codeString} to is converted to an9      *                 {@codeInteger}.Ten      * @exceptionNumberFormatException if the {@codeString} does not One * contain a parsable integer. A      * @seeJava.lang.integer#parseint (java.lang.String, int.) -      */ -      PublicInteger (String s)throwsNumberFormatException { the          This. Value = parseint (S, 10); -}

(4.3) Here intstr is a wrapper class, how can you pass in a wrapper class as an actual parameter to pass the argument??

2015-09-10 Day Issue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.