Re: Java API used in some of the details

1, Math.round (11.5) is equal to how much? Math.round (- 11.5) and is equal to how much?

  Math.round (11.5) return value is 12, Math.round (-11.5) -11 return value. Principle is rounded and rounding with 0.5 on the parameters.

 

2, switch whether acting on byte, whether acting on the long, whether acting on String?

  Java5 before the switch (expr), expr can only be a byte, short, char, int. Since Java 5, Java was introduced enumerated types, expr may be an enum type.

  Starting with Java 7, expr can be a string (String), but long integer (long) in all current versions are not allowed.

  Why Java root cause of the switch can not be used long is converted to long int lose precision, resulting in inaccurate data, so Java's switch logic rules do not allow the use of long.

 

3, the array has no length () method? String has no length () method? Is there a list of length?

  Arrays are not length () method, but the length attributes. String has a length () method. The list also no length method, there are size method. JavaScript, the length of the string was obtained by the length property, which is easily confused and Java.

 


4, under what circumstances the "+" operator string connection string append method invocation performance than StringBuffer / StringBuilder objects better?

  The compiler will "+" converted into StringBuilder, but the position was created StringBuilder object inside the for statement. This means that each time through the loop, it will create a StringBuilder object (for this example, is the creation of 10 StringBuilder object), although Java has a garbage collector, the collector but working time is uncertain. If you continue to produce such rubbish, then still take up a lot of resources. The solution to this problem is to use StringBuilder directly in the program to concatenate strings,

intern 1. String object () method will be a string constant pool object corresponding to a reference version (if there is a string constant pool String object equals the result is true), if no corresponding character constant pool string, the string is added to the constant pool, and then returns a reference string constant pool;

+ 2. string operations which essentially creates a StringBuilder append operation object, and then the piecing process StringBuilder object with the object to a String toString method, it is possible to obtain the JVM class files corresponding to the command javap -c XxxTest.class bytecode instructions can be seen.

 

 

  

Guess you like

Origin www.cnblogs.com/noperx/p/11360759.html