Common methods and notes for string, object, and packaging classes

Source: Internet
Author: User

String type

Two instantiation methods of 1.1string

A. directly assign the value string STR = "hello ";

B. instantiate a String object through the constructor

1.2 comparison of equal strings

Str. Equals (str1)

Public Boolean equals (string str1)

 

1.3 string constants are anonymous objects of the string class

Small tips: In the future development process, if you want to determine whether the user-entered string is equivalent to a specific string,

Be sure to write a specific string in front to avoid nullpointerexception.

1.4 differences between two types of string instantiation

  • Direct Value assignment:

String str1 = "hello ";

String str2 = "hello ";

String str3 = "hello ";

System. Out. println (str1 = str2); // true

System. Out. println (str1 = str3); // true

 

The design of the string class uses the shared design mode.

A String object pool (Object array) is automatically maintained at the JVM underlying layer ).

If you use the direct value assignment mode to instantiate a String object, this object will be automatically saved to the object pool,

If you continue to use the direct value assignment mode to declare the string and object, go to the object pool to check whether the specified content exists, as shown in

If yes, direct reference is provided. If not, open up a new space and save it

2. Constructor

 

In the string class, there is a way to enter the pool for Public String intern (); (string manual operations into the pool :)

Interview question: explain the difference between the two Object Instantiation methods in the string class

1. Direct Value assignment: only a heap memory space is opened up, and the string object can be automatically saved in the object pool for future use.

2. constructor: Two heap memory spaces are created, one of which becomes a spam space and is not automatically saved in the object pool. You can use the intern () method to manually enter the pool.

Therefore, the first method is to assign values directly.

1.5 The string cannot be changed

Once defined, the string cannot be changed.

All languages are character Arrays for the underlying implementation of strings. The biggest defect of arrays is fixed length. When defining a String constant, its content cannot be changed.

 

Principle: 1. assign values directly when using strings. (2) Use equals () for string comparison. 3. Do not change the string too much.

1.6 characters and string ******

String <-> char

  • Character array char [] <-> string

Public String (char [] value1 );

Public String (char [] value1, int offset, int count );

  • Convert a string to a single character according to the index

String-> char

Public char charat (INT index) returns the characters with specified Indexes

  • Convert string to character array

String-> char []

Public char [] yochararray ();

 

6. bytes and string

6.1 byte []-> string

String class Constructor

Public String (byte [] bytes)

 

String-> byte []

* *************** Public byte [] getbytes () ******************

Public byte [] getbytes (string charset): convert to a byte array according to the specified encoding.

7. String comparison

7.1 case-insensitive comparison

Public Boolean inclusignorecase (string anotherstring)

7.2 compare the relationship between two strings

Public int compareto (string anotherstring)

  • > 0: indicates that the current string is greater than the target string.
  • = 0: the two are equal.
  • <0: indicates that the current string is smaller than the target string.

8. string SEARCH

8.1 determine whether the specified string exists

Public Boolean contains (charsequence S)

8.2 determine whether a specified string starts

Public Boolean startswith (string prefix)

Public Boolean startswith (string prefix, int offset)

8.3 determine whether the end of a specified string has been specified

Public Boolean endswith (string suffix)

9. String replacement

Public String replaceall (string RegEx, string replacement)

Replace all the target strings

Str. replaceall ("L", "_"): replace all L values-

10. String splitting (******)

Public String []. Split (string. RegEx): splits all strings.

Public String []. Split (string. RegEx, Int. Limit \\

Eg :\\.

11. String truncation (*****************)

Public string substring (INT beginindex): truncates from the specified index to the end.

Public string substring [int beginindex, int endindex): captures part of the content from the specified index.

12. Other operations on strings

A. Remove the Left and Right spaces of the string and retain the middle spaces.

B. the string is case-insensitive (all are case-insensitive)

Public String touppercase ();

Public String tolowercase ();

C. Determine whether the string is a null string ("", not null)

Public Boolean isempty ();

3. Two Sb (stringbuffer, stringbuilder)-interview

1 String concatenation method public synchronized stringbuffer append (various data types );

2. Conversion between stringbuffeer and string-> stringbuffer:

A. append ()

B. Call stringbuffer construction method new stringbuffer ("");

Stringbuffer-> string

Call stringbuffer. tostring ();

3. Common stringbuffer operations

3.1 string Inversion

Reverse (): stringbuffer

3.2 delete data within a specified range

Delete (INT start, int end ):

Stringbuffer insert (INT offset, various data types ):

Please explain the differences between string, stringbuffer, and stringbuilder in stringbuffer:

1. The content of the string cannot be changed, and the two Sb can be changed.

2. stringbuffer (jdk1.0) adopts synchronous processing to ensure thread security and low efficiency.

Stringbuilder (jdk1.5) adopts asynchronous processing. The thread is not secure and the efficiency is high. When "+" is performed on the string object ",

During compilation, the string class is changed to stringbuilder for append () processing.

Object Class (realfather)-Unified highest parameters

Object is a default class provided by JDK. In Java, all classes except object classes have inheritance relationships.

By default, the object parent class is inherited. That is, all class objects can be received using objects.

4.1 tostring ()-Get Object Information

  • The system outputs the tostring () method of the default call object.

The object class tostring () is just a simple output of the currently referenced class name and object address.

To obtain the attributes of this class in the class, overwrite tostring ();

4.2 equlas ()-object comparison

4.3 receive reference data type.

Objects can receive all referenced data types: arrays, classes, and interfaces.

5. Packaging class-using an object to receive everything the packaging class encapsulates the basic data type into the class.

5.1 Classification of packages

A. Numeric packaging class (direct subclass of number): byte, double, short, long, float, INTEGER (INT)

A type conversion exception occurs. numberformatexception

B. Object Type packaging class (direct subclass of object): Boolean, character (char)

5.2 packing and unpacking

Packing: change the basic data type to a packaging object. Use the constructor provided by each packaging class.

Unbox: extract the basic data type encapsulated in the package class. Use xxvalue ();

For integer Var =? In the value assignment of [-128-127], the integer object is generated in the constant pool and the existing object is used for recovery. All data outside the range is generated on the stack and no existing object is used for recovery.

 

Result: True.

False

Alibaba code specification: Int or integer?

  • [Force] All pojo classes (simple Java classes, which only contain member variables, constructor, and getter/setter) must use the wrapped data type.
  • [Mandatory] The return values and parameters of the RPC method must use the encapsulated data type.
  • [Recommended] All local variables use the basic data type.

5.3 Conversion between packages and strings

5.3.1 string-> Basic Data Type

Packaging class. Parsexx (STR );

String-> INT: integer. parseint (STR );

5.3.2 basic data type-> string

A. "" + Basic Data Types

B. The static method valueof (basic type) provided by the string class, this method does not produce garbage

 

Common methods and notes for string, object, and packaging classes

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.