Common Java classes
Array (Array class ):
First, remember that the Java array is a class. It has internal methods and attributes.
Arrays in Java belong to the reference data type.
There are two methods to initialize arrays in Java. One is the direct assignment method, and the other is the new operator (the characteristics of the class ).
String (String and StringBuffer ):
Java uses String and StringBuffer to complete various String operations.
They are also classes, and classes also have methods and attributes.
String class:
Notes:
1. The String class is final and cannot be inherited.
2. The essence of the String class is the character array char [], and its value cannot be changed.
3. A special method for creating a String object is to directly specify a String object, such as String x = "abc" and "abc. X is the address of the "abc" object, which is also called the reference of the "abc" object.
4. String objects can be connected through "+. A new string is generated after concatenation. You can also use concat () to concatenate data.
There are many ways to create strings. There are three types of constructor:
First, use the new keyword to create a String, such as String s1 = new String ("abc ");
Second, directly specify. For example, String s2 = "abc ";
Third, generate a new string using concatenation. For example, String s3 = "AB" + "c ";
The String type cannot be changed, that is, when you want to change a String object, such
String name = "tom ";
Name = "madding ";
The VM will not change the original object, but will generate a new String object, and then let the name point to it. If the original "tom" does not have any object to reference it, the garbage collection mechanism of the virtual machine will receive it.
It is said that this can improve efficiency !!!
For more information about the running mechanism of the Java String class, see:
Think about it: Since the String class cannot be changed (private), in fact, many times the same variables are used to express different meanings, so how can we change the attributes of a string variable (this change is similar to the change mentioned above )?
The obvious method is to concatenate with "+" or use the concat () method.
However, if a string is frequently changed, this will lead to the generation and destruction of a large number of string objects, and the performance will be greatly reduced. Therefore, Java provides a solution: StringBuffer class
StringBuffer class:
StringBuffer is variable, so no new object will be created during concatenation.
Create:
StringBuffer sb = new StringBufffer ();
Sb. append ("hello ");
Sb. append ("world ");
Put forward a special class: Object class.
The Object class is the parent class of all classes.
The toString () method returns the class name @ [the unsigned hex representation of the object's hash code]. Generally, the custom class overwrites toString ().