Java Common Classes

Source: Internet
Author: User

Is 1.String the most basic type of data?

No, basic data type: Byte,short,int,long,float,double,char,boolean.

2.String s = "Hello"; s=s+ "world!"; After the execution of these two lines of code, did the content in the original string object change?

No, because the string is designed as an immutable class, all its objects are immutable objects.

In this code, s originally specified a string object, the content is "Hello", and then we do a + operation on S, then s does not point to the original object, but to another string object, the content is "helloword!", the original object is still there, but S does not point to it.

If you often make a variety of changes to a string, or an unforeseen modification, using string to represent a string can cause significant memory overhead, and you should use the StringBuffer class, which allows for modification.

Advantages of a variable: for example, because its object is read-only, there is no problem with multi-threaded concurrent access.

3. Can I inherit the String class?

The string class is the final class and cannot be inherited.

4.String s = new string ("XYZ"); several string Object created? What is the difference between the two?

Two, one is the instance of the string "xyz" that resides in the string constant pool, and the other is an instance of the same content as "XYZ" created and initialized with the new string ()

One, if "XYZ" already exists in the string constant pool, this creates only one object.

What is the difference between 5.String and StringBuffer?

String is an immutable string, and StringBuffer is a mutable string that uses stringbuffer when making a variety of changes to the string or unforeseen modifications.

The string overrides the Equals and Hashcode methods, and StringBuffer does not overwrite the Equals and Hashcode methods, so the problem occurs when the StringBuffer object is stored in the Java collection class.

6. Does the array have the length () method? Does the string have the length () method?

The array does not have the length () method, and the array has the length property. String has the length () method.

7. How many objects are created in the following statement: String s = "a" + "B" + "C" + "D";

Because Javac compilation can be optimized for expressions that add directly to string constants, only one object is created.

Does 8.String polygon amount automatically generate a string object?

Is that the string class has a certain particularity, when the JVM executes the double quote operator, it automatically creates a string object and returns a reference to the object.

9. What is the role of a string object pool?

Object pooling exists to avoid the frequent creation and destruction of objects that affect system performance. When the JVM runs the code that causes a string in double quotation marks, it goes to the object pool of string to check if there is an object of the same character sequence, if any, to the existing object, and if not, creates an object in the object pool and returns.

10. How do I create a string object using the specified character set?

Using the constructor of a string with character set encoding, the parameter is a byte array and the other is a specified character set encoding format.

Java Common Classes

Related Article

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.