String class to create strings
String greeting = "Beijing";
The compiler uses this value to create a String object. As with other objects, you can use keywords and construction methods to create a String object. Note The string type is a special reference type (with final in front), so the string class is immutable, so once you create a string object, its value cannot be changed. If you need to make a lot of changes to the string, you should choose to use the StringBuffer & StringBuilder class.
String s = "Google";
System.out.println ("s =" + s);
s = "Runoob";
System.out.println ("s =" + s);
Google
Runoob
The result is a change, but why does the door say string objects are immutable? The reason is that s in the instance is just a reference to a String object, not the object itself, when executing s = "Runoob"; A new object "Runoob" was created, and the original "Google" is still in memory.
String length
One accessor method for the string class is the length () method, which returns the number of characters the string object contains.
public class Stringdemo {public
static void main (string args[]) {
string site = ' www.runing.com ';
int len = Site.length ();
System.out.println ("Length:" + len);
}
14
Connection string
Using the Concat method
String1.concat (string2);
Use the "+" operator
"Hello," + "runing" + "!"
Common string Method Strings lookup
1 char charAt (int index) returns the char value at the specified index.
public class Test {public
static void main (string args[]) {
string s = ' www.runoob.com ';
char result = S.charat (8);
SYSTEM.OUT.PRINTLN (result);
}
The result is: O
String comparisons
1 int compareTo (string anotherstring) compares two strings in dictionary order.
Compares two strings in a dictionary order.
Returns a value of 0 if the argument string is equal to this string;
If the string is less than the string parameter, a value less than 0 is returned;
If this string is greater than the string parameter, a value greater than 0 is returned.
2 int comparetoignorecase (string str) compares two strings in dictionary order, regardless of case.
Other common classes see rookie tutorials, thanks for the support of this tutorial
Http://www.runoob.com/java/java-string.html StringBuffer and StringBuilder class
When you modify a string, you need to use the StringBuffer and StringBuilder classes. Unlike the String class, objects of the StringBuffer and StringBuilder classes can be modified many times without creating new unused objects. The StringBuilder class is proposed in Java 5, and the biggest difference between it and StringBuffer is that the StringBuilder method is not thread-safe (cannot be synchronized). Because StringBuilder has a speed advantage over StringBuffer, the StringBuilder class is recommended in most cases. However, in cases where the application requires thread safety, the StringBuffer class must be used.
public class test{public
static void Main (String args[]) {
StringBuffer sbuffer = new StringBuffer (" I can append string oh: ");
Sbuffer.append ("www");
Sbuffer.append (". Runoob");
Sbuffer.append (". com");
System.out.println (Sbuffer);
}
Run Result: I can append strings behind me oh: www.runoob.com
Java StringBuffer and string is a certain difference, first of all, string is final modified, his length is immutable, even if you call the string concat method, it is also a string concatenation and recreate an object, the splicing of the S The value of the Tring is assigned to the newly created object, and the length of the StringBuffer is variable, and the StringBuffer append method is invoked to change the length of the stringbuffer and, compared to the stringbuffer,string Once the length change occurs, it is very memory intensive. The string in the written question (wrong Libon)
1, after sequentially executing the following program statement, the value of B is ()
String a= "Hello";
String b=a.substring (0,2); He
The substring method returns a string containing a substring from start to the last (not including end). The difference between length and capacity
The definition has stringbuffer S1=newstringbuffer (Ten), S1.append ("1234") s1.length () and s1.capacity () respectively?
Parse: Length Returns the current length, if the string length is not initialized long, capacity returns the length of the initialization, if the string length after append exceeds the initialization length, capacity returns the length of the growth.
PS: If not explicitly initialized, the default is to
The default size for StringBuffer and StringBuilder is 16
Default size for ArrayList and LinkedList 10
StringBuffer s = new StringBuffer (x); X is the initial capacity length
S.append ("Y"); "Y" denotes a string with a length of Y
Length always returns the current length, that is, y;
For S.capacity ():
1. When Y <=x, the value is X
Container capacity needs to be extended in the following situations
2. When x<=y<=2*x+2, the value is 2*x+2
3. When y>=2*x+2, the value is Y String, the difference between stringbuffer,stringbuilder
In Java, String, StringBuffer, and StringBuilder are often used as string classes in programming, and the difference between them is often the question they ask in an interview. Now to summarize, look at their differences and the same. 1. Variable and not variable
The string class uses a character array to hold the strings, as follows, because there is a "final" modifier, so you know that the string object is immutable.
Private final char value[];
A String is an immutable object that, once created, cannot modify its value. . The modification of a string object that already exists is to re-create a new object and then save the new value in.
Both StringBuilder and StringBuffer inherit from the Abstractstringbuilder class, and in Abstractstringbuilder the string is also saved using a character array, as follows: Both objects are mutable.
Char[] value;
StringBuffer: is a mutable object that, when modified, does not recreate objects like string, it can only be created by constructors, such as: stringbuffer SB = new StringBuffer ();
The value cannot be paid by the assignment symbol. , such as SB = "Welcome to here!"; /error
After the object is established, the memory space is allocated in memory and a null is initially saved. When assigning values to StringBuffer, it can be passed through its Append method. Sb.append ("Hello"); 2. Whether multithreading security
Objects in string are immutable and can be understood as constants, apparently thread-safe.
Abstractstringbuilder is the common parent class of StringBuilder and StringBuffer, which defines the basic operations of strings, such as expandcapacity, append, inserts, IndexOf, and other public methods.
StringBuffer is thread-safe by adding a synchronous lock to a method or by synchronizing the method that is invoked. Look at the following source code:
1 Public synchronized StringBuffer reverse () {
2 super. Reverse ();
3 return this;
4}
5
6 public int indexOf (String str) {
7 return indexOf (str, 0); There is a public synchronized int indexOf (String str, int fromindex) method
8}
StringBuilder does not have a synchronized lock on the method, so it is not thread safe.
3.StringBuilder and StringBuffer in common
StringBuilder and StringBuffer have a common parent class Abstractstringbuilder (abstract Class).
One of the differences between an abstract class and an interface is that a common method of subclasses can be defined in an abstract class, and subclasses simply need to add new functionality and do not have to repeat the existing methods, while the interface simply defines the method's declarations and constants.
StringBuilder, StringBuffer methods call public methods in Abstractstringbuilder, such as Super.append (...). Just stringbuffer will add synchronized keyword on the method and synchronize.
Finally, if the program is not multithreaded, then using StringBuilder is more efficient than stringbuffer.
Efficiency comparison String < StringBuffer < StringBuilder, but string is the most efficient (compiler-optimized effect) when string S1 = "This are only" + "simple" + "test".
See my another blog here, about compiler optimization problems
Special use of http://blog.csdn.net/sinat_33087001/article/details/74228500 null
public class TestClass {
private static void TestMethod () {
System.out.println ("TestMethod");
}
public static void Main (string[] args) {
((TestClass) null). TestMethod ();
}
Operation Normal, Output TestMethod
Parsing: null can be cast to any type (not any type of object), so it can be used to execute static methods. The difference between replace and ReplaceAll:
1 The parameters of Replace are char and charsequence, that is, can support the replacement of characters, but also support the replacement of strings (charsequence that is, the meaning of the string sequence, in other words, the string);
2 The parameters of ReplaceAll are regex, that is, based on the replacement of regular expressions, for example, the ReplaceAll ("\d", "*") can be used to convert all the numeric characters of a string into asterisks;
public static void Main (string[] args) {
String classfile = "Com.jd.". ReplaceAll (".", "/") + "Myclass.class";
System.out.println (Classfile);
}
Output Result://///////myclass.class
In the title ".", in the regular expression of any symbol, so the answer is C, five letters, 2 dots, two spaces are replaced.