Java foundation -API and the Object class and the String class

Java is the API ( the API: A pplication ( application ) P rogramming ( program ) the I nterface ( Interface ) )

Java API is JDK provided to the class we use these classes to the underlying code that implements the package up, we do not need to be concerned about how these classes are implemented, only we need to learn how to use these classes.

1.Object class

 Object class is a Java root class language, that is the parent of all classes. All methods described its subclasses can be used. All classes when you create objects, and ultimately find a parent class is Object .

  equals method

  equals method for comparing two objects are the same , it is actually two objects using the memory address in the comparison. Object class equals method is used internally == Comparison operators.

  

In development to compare whether two objects are the same, often compared according to the attributes of the object, which is often necessary in developing Subclasses override equals methods were compared according to the attribute value of the object.

The following code demonstrates:

 

/*

 

  Describe people in this class, and define the function to determine whether the age peers

 

  Due to be compared according to the attributes of the specified class, then just cover Object in the equals method

 

  The class attribute values ​​compared in the method body

 

 */

 

class Person extends Object{

 

int age ;

 

// replication parent class equals method to realize their comparative way

 

public boolean equals(Object obj) {

 

// determine the current call equals whether the object methods and the object is passed in the same

 

if(this == obj){

 

return true;

 

}

 

// determine whether the object is passed in Person type

 

if(!(obj instanceof Person)){

 

return false;

 

}

 

// The obj downcast Perosn reference to access its properties

 

Person p = (Person)obj;

 

return this.age == p.age;

 

}

 

}

 

Note : In the replication Object of equals time method, we must pay attention public boolean equals (Object obj) parameter is Object type, properties of the object when invoked, must be cast, the type is determined to be prior to the conversion.

 

 

 toString method

toString method returns a string representation of the object , is the fact that the content type of the object character string + @ + memory address value.

 

Since toString result of the method is to return the memory address, and in development, often we need to give the corresponding string representation according to properties of the object, and therefore it needs to be rewritten.

 

class Person extends Object{

 

int age ;

 

// The Person override attribute class toString method

 

public String toString() {

 

return "Person [age=" + age + "]";

 

}

 

}

 

 

2.String class

String class represents character strings. Java All string literals (e.g., the program "ABC" ) are implemented as an instance of this class.

 

// demo string:

 

String str  = "oracle";

 

str = " Oracle ";

The nature of the string is an array of characters.

Code demonstrates:

String s3 = "abc";

 

String s4 = new String("abc");

 

System.out.println(s3==s4);//false

 

System.out.println(s3.equals(s4));//true,

 

// Because String rewrite the equals method, we established the same string based on their own judgment (to judge by the character string object)

 

 

 

s3 and s4 of the way to create what difference will it make?

 

s3 create, only one object in memory. This object in a string constant pool

 

 s4 created, there are two objects in memory. A new object in the heap, a target string itself, the string constant pool

 

 

String constructors

 

  

String s1 = new String (); // create a String object string has no content

 

byte[] bys = new byte[]{97,98,99,100};

String s2 = new String (bys) ; // create a String object as the contents of the string array elements

String s3 = new String (bys, 1, 3); // create a String object array elements as part of the contents of the string, the parameter offset is the starting location indexed array element, parameter length of several elements to be

 

char[] chs = new char[]{’a’,’b’,’c’,’d’,’e’};

String s4 = new String (chs) ; // create a String object as the contents of the string array elements

String s5 = new String (chs, 0, 3); // create a String object array elements as part of the contents of the string, the parameter offset is the starting location indexed array element, the parameter count for the number of elements to be

 

String s6 = new String ( "abc "); // create a String object string contents as abc

 

 

 

The object is a string, then it must be the method defined data operations around the object

String functions:

1) Returns the length of this string (length () / int)

String str = "abcde";

 

int len = str.length();

 

System.out.println("len="+len);

 

 

2) Get partial string (substring () / String)

String str = "abcde";

 

String s1 = str.substring (1); // returns a new string, all characters of the contents of the specified position to the end of the string

 

String s2 = str.substring (2, 4 ); // returns a new string, the content of the specified position to the end of all the characters in the specified position

 

System.out.println("str="+str);

 

System.out.println("s1="+s1);

 

System.out.println("s2="+s2);

 

3) Test this string specified string start and end (startsWith (), endWith () / boolean)

String str = "StringDemo.java";

boolean b1 = str.startsWith ( "Demo" ); // determines whether or not a given string beginning

boolean b2 = str.startsWith("String");

boolean b3 = str.endsWith ( "java" ); // end of the string is determined whether a given

 

4) determines if this string contains the specified string (contains () / boolean)

String str = "abcde";

boolean b2 = str.contains ( "bcd" ); // determines whether or not the specified character string, comprising a return to true , does not include the return false

 

5) returns the index of the first occurrence of the character string does not include the return -1 (indexOf () / int)

String str = "abcde"; int index = str.indexOf ( "bcd"); // determines whether or not the specified character string, the index of the first occurrence of the string returned contains, does not include the return -1

 

6) The character string is converted into a byte array (getBytes () / byte [])

 

String str = "abcde";

byte[] bytes = str.getBytes();

 

7) is converted into a character string array (toCharArray () / char [])

String str = "abcde";

char[] chs = str.toCharArray();

 

8) Analyzing the content of two strings are identical (equals () / boolean) {equalsIgnoreCase () is case insensitive}

String str = "abcde";

String str2 = "abcde";

String str3 = "hello";

boolean b1 = str.equals(str2);

boolean b2 = str.equals(str3);

 

 

StringBuffer类

  API said string variable string buffer support

 

 StringBuffer is a string of buffers, i.e., that it is a container, which may hold a lot of strings. Wherein the string and can perform various operations.

 StringBuffer code demonstrates some of the features available API to learn :()

Create a string buffer object. It used to store data.

 

StringBuffer sb = new StringBuffer();

 

sb.append ( "haha"); // add the string

 

sb.insert (2, "it") ; // inserted at the specified location

 

sb.delete (1, 4); // delete

 

sb.replace (1, 4, "cast "); // replace the contents within the specified range

 

String str = sb.toString();

 

In our development, we will encounter case after calling a method that returns an object. Then using the returned object to continue calling method. This time, we can put the code together, as now append method, like the following code:

Create a string buffer object. It used to store data.

StringBuffer sb = new StringBuffer();

adding data. After the continuous adding data, to operate on the final data buffer, it can be translated into strings.

String str = sb.append(true).append("hehe").toString();

 

 

 

StringBuilder

  It is also a variable sequence of characters. With such a StringBuffer compatible API , but it does not guarantee synchronization. The class is designed as StringBuffer a simple alternative, the string buffer is used when using a single thread (which is common). If possible, we recommend the use of such priority, because in most implementations, it is more than StringBuffer faster.

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/sunlangui/p/11487911.html