Class string comprehension and classic interview questions, __java

Source: Internet
Author: User


class String

Public final class String;
string classes represent strings.


All string literals in a Java program, such as "ABC", are implemented as instances of this class.
strings are constants, and their values cannot be changed after they are created.
A string buffer supports a mutable string. Because the String object is immutable, it can be shared.     
String str = "abc";
Equivalent to:
char data[] = {' A ', ' B ', ' C '};
String str = new string (data);


He rewrote the ToString method public
string toString ()
returns the object itself (it is already a string.) )


Construct method
string ()
Initializes a newly created string object that represents a sequence of empty characters.
string (byte[] bytes)
constructs a new string by decoding the specified byte array by using the platform's default character set.
string (byte[] bytes, int offset, int length)
constructs a new string by decoding the specified byte array using the platform's default character set.
string (char[] value)
assigns a new string to represent the sequence of characters currently contained in the character array argument.
string (char[] value, int offset, int count)
assigns a new string that contains characters from a substring of the character array argument. String
Original
Initializes a newly created string object that represents a sequence of characters that is the same as the argument;
in other words, the newly created string is a copy of the parameter string 。


public class Exp {/** * @param args */public static void main (string[] args) {//TODO auto-generated Method St
		UB String s = "abc";
		s = "Def"; Why s Change//"abc" "DEF" once initialized, their property values will not change//"abc" "DEF" represents the object,//When the "Def" is assigned to S, "ABC" becomes the Garbage System.out.println (s.tostring
		));
		
		System.out.println (s);
		Byte[] B1 = {97,98,99}; string S1 = new String (B1);//decoding System.out.println (S1);//abc ASCII table//project--properties View my current encoding as UTF-8///When making other code tables, a
		Scii code table includes byte[] b2 = {97,98,99,100,101,102};
		String s2 = new string (b2,1,4);
		SYSTEM.OUT.PRINTLN (S2);//bcde char data[] = {' A ', ' B ', ' C '};
		String s3 = new string (data);
		System.out.println (S3);//BCDE String nn = null;
		String mm = ""; System.out.println (Nn.isempty ());//java.lang.nullpointerexception System.out.println (Mm.isempty ());//true/* Null string "" and Null Difference * "" is a string constant is also a string class object, of course, you can call method in the string class * null is an empty constant, you can not call any method, otherwise java.lang.NullPointerException * A null constant can assign a value to any reference data type///int I= Null;cannot convert from null to int//integer i = Null;cannot convert from String to Integer//integer i = "";
		Cannot convert from String to Integer//int indexOf (int ch) string str = "Bcdefgwhale";
		int index = STR.INDEXOF (97); parameter accepts int type, when passing char, type automatically promotes int index = Str.indexof (' a ');
		
		SYSTEM.OUT.PRINTLN (index);
		STR 7th is W, starting from 1//str 6th for W, starting from 0 String str__w = str.substring (7);  System.out.println (str__w);//hale System.out.println (str.substring (6));//whale, starting from 0, contains index for (int i = 0; i <
			Str.length (); i + +) {char c = str.charat (i);
			System.out.println (c);
		System.out.println (Str.charat (i)); }
	}

}


Common face questions

String reference data type
equals method public
Boolean equals (object AnObject)
compares this string to the specified object. True if and only if the parameter is not NULL and
is a String object that represents the same sequence of characters as this object.


1. Judge whether the S1 and S2 defined as string are equal to
string S1 = "abc";
String s2 = "abc";
System.out.println (S1 = = s2); True
	 there is a constant pool in memory
	 when we create constants; The system first looks at the constants in the pool, and if so, does not create a direct reference to the constant.
	 S1 and S2 refer to a constant that records an address value
	 = = Determines whether the address of the reference data type is equal
System.out.println (s1.equals (S2)); True Public
	 Boolean Equals (object AnObject)
	 compares this string to the specified object. True if and only if the parameter is not NULL and
	 is a String object that represents the same sequence of characters as this object.

2. The following sentence creates several objects in memory,
string s1 = new String ("abc");	New heap memory
	 constant Pool an object "ABC"
	 heap memory a copy of an object "ABC"
	 this creates two objects


3. Judge whether the S1 and S2 defined as string are equal to
string S1 = new String ("abc"), the address
String s2 = "abc" of the Heap memory object, and the address
of the object in the constant pool is logged System.out.println (S1 = = s2);//false
System.out.println (s1.equals (S2));//true

4. Judge whether S1 and S2 defined as string are equal to
string s1 = ' a ' + ' B ' + ' C '; Java has a constant optimization mechanism that becomes an "abc"
String s2 = "abc" at compile time;
System.out.println (S1 = = s2); True
System.out.println (s1.equals (S2)); true

5. Judge whether the S3 and S2 defined as string are equal
The Java language provides special support for string concatenation symbols ("+") and for converting other objects to strings.
string concatenation is implemented through the StringBuilder (or StringBuffer) class and its Append method.
string conversions are implemented through the ToString method, which is defined by the Object class and can be inherited by all classes in Java. 
String S1 = "AB";
String s2 = "abc";
String s3 = "C" + S1;
Create a StringBuffer object in heap memory ~ ~/
/Then call his ToString () method called string Object
//s3 that is the string object
System.out.println (s3 = = s2); false
System.out.println (s3.equals (S2)); true





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.