Java API and Object

Source: Internet
Author: User

Api:

The Java API is the class that is provided to us in the JDK, which encapsulates the underlying code implementation, and we don't need to be concerned about how these classes are implemented, just how these classes can be used.

How to use the source file:

An overview of the object class:

The object class is the root class of the class hierarchy, which is the parent class of all classes. All of the method subclasses described in it can be used. All classes when creating objects, the final parent class to find is object.

Object class: Super () in constructor method calls the object () null argument construct

Equals method:

The equals method, which is used to compare two objects, is actually compared with the memory address of two objects. the Equals method inside the object class is used internally by the = = comparison operator.

/* Describe the person in this class, and define the function according to age to determine whether it is a peer because you want to compare the properties of the specified class, as long as you override the Equals method in Object to compare */class person extends in the method body based on the class's attribute value Object{int Age;//The Equals method of the parent class, which implements its own comparison by public boolean equals (object obj) {//Determines whether the object that is currently calling the Equals method and the object passed in is the same if (this = = obj) { return true;} Determines whether the passed in object is a person type if (! ( obj instanceof person) {return false;} Invokes a subclass-unique method that transforms obj down to a perosn reference, accessing its properties person P = (person) obj;return this.age = = P.age;}}

Note : When copying the Equals method in an object, be sure to note that the public boolean equals (object obj) argument is of type object, and the type conversion is necessary when invoking the properties of the object. Type judgments must be made before the conversion .

The override of the Equals method can be generated directly in eclipse, generating methods:

Right-click--source--generate hashcode () and Equals () in the class

Equals () In object compares the address.

ToString Method:

The tostring method returns the string representation of the object, in fact the string content is the type of the object [email protected]+ memory address value.

The result returned by the ToString method is a memory address, so the method needs to be overridden

Class person extends the object{int age;//Override the ToString method public String ToString () by the property of the Person class. () {return "person [age=" + Age + "]" ;}}

The override of the ToString method can be generated directly in eclipse, generating methods:

Right-click--source--generate toString () in the class

ToString (): String-object--string represents the return value type of the method, and Object represents the class to which the method belongs.

String class: The String class represents strings, consisting of fianl-decorated character arrays,

Strings are constants, their values cannot be changed after they are created, and once the string is determined, the string is generated in the memory area, and the string itself cannot be changed, but the address value recorded in the STR variable can be changed.

STR1 no longer points to 0x001, but to 0x002, (note that instead of changing ABC to EFG, the address points to a change)

The string has a large number of overloaded construction methods. The creation of a string object can be accomplished through the construction of the string class.

Create an object by using double quotation marks
String s3 = "ABC";
Creates an object by using new, String s4 = new String ("abc"); System.out.println (S3==S4);//falsesystem.out.println (S3.equals (S4));//true,

L S3 is created with only one object in memory. This object is in the string constant pool

L S4 Created, there are two objects in memory. A new object in the heap, a string itself object, in a string constant pool

A character array that already exists in the string class will no longer create new, just re-point the address

The constructor method is used to complete the creation of a string object

Construction method: String (byte[] bytes,int offset,int length)

Byte array week is the GBK encoding table (positive go ASCII table, negative number is Chinese character (two bytes of a Chinese character))

AscII (48 for 0, then push down, 65 for a, then push down, 97 for a, and then push down)

string S1 = new String (); Create a String object with no content byte[] bys = new byte[]{97,98,99,100}; String s2 = new string (bys); Creates a string object that takes an array element as a string of contents string s3 = new String (bys, 1, 3); Create a String object, a part of the array elements as the contents of the string, the parameter offset to the starting index of the array element, the parameter length is a few elements char[] CHS = new char[]{' A ', ' B ', ' C ', ' d ', ' e '}; String S4 = new string (CHS); Creates a string object that takes an array element as the contents of the String s5 = new String (CHS, 0, 3);//Creates a string object, takes a part of the array element as the contents of the string, and the parameter offset to the starting index position of the array element, The parameter count is for several elements of string s6 = new String ("abc"); Creates a string object with the content of the strings ABC

Method lookup for the String class

L How many characters are there in a string ?

 

String str = "ABCDE"; int len = Str.length (); System.out.println ("len=" +len);

  

L gets a partial string.

String str = "ABCDE"; String S1 = str.substring (1); Returns a new string that contains all characters starting at the specified position to the end of the string s2 = str.substring (2, 4);//Returns a new string with the content starting at the specified position and ending all characters System.out.println (" Str= "+str); System.out.println ("s1=" +s1); System.out.println ("s2=" +s2);

  

Method lookup exercises in the String class

Whether the string begins with the specified string. End of the same

String str = "Stringdemo.java"; Boolean B1 = Str.startswith ("Demo");//Determines whether Boolean b2 = Str.startswith ("string") starts with the given string; Boolean b3 = Str.endswith ("java");//Determines whether to end with a given string

  

Whether the string contains another string.

String str = "ABCDE"; int index = Str.indexof ("BCD"); Determines whether the specified string is included, returns the index of the first occurrence of the string, does not contain the return -1boolean B2 = Str.contains ("BCD");//Determines whether the specified string is included, contains a return of true, does not contain a return of false

  

L converts a string into an array of characters. or a byte array.

String str = "ABCDE"; char[] CHS = Str.tochararray (); byte[] bytes = Str.getbytes ();

  

L Determine if the contents of two strings are the same

String str = "ABCDE"; String str2 = "ABCDE"; String str3 = "Hello"; Boolean B1 = Str.equals (STR2); Boolean b2 = Str.equals (STR3);

  

L GET the contents of the string object

String str = new string ("Hello"); System.out.println (Str.tostring ()); System.out.pintln (str);

 

The String class method uses the practice

L Title: Gets the number of uppercase, lowercase, and digits in the specified string.

L Idea: 1. In order to count the number of uppercase letters, lowercase characters, and numbers. Create a variable of 3 counts.

2. In order to get to each character in the string, the string is traversed to get each character.

3. Determine the resulting character, if the character is uppercase, the number of uppercase +1, if the character is lowercase, the number of lowercase letters +1, if the character is a number, the number is +1.

4. Displays the number of uppercase, lowercase, and digits

L Code:

public static void Method (String str) {int bigcount = 0;//uppercase letters INT smallcount = 0;//lowercase letters int numbercount = 0;//number of digits for (int i=0; i < str.length (); i++) {         char ch = str.charat (i);//Gets the character at the specified position if (ch>= ' A ' && ch<= ' Z ') { bigcount++;} else if (ch>= ' a ' && ch<= ' z ') {smallcount++;} else if (ch>= ' 0 ' && ch<= ' 9 ') {    numbercount++ ;}} System.out.println ("Number of capital letters:" +bigcount); System.out.println ("Number of lowercase letters:" +smallcount); System.out.println ("Number of Numbers:" +numbercount);}

L Title Two: Converts the first letter in a string to uppercase, the other letters to lowercase, and prints the changed string.

L Idea: 1. Divide the string into two parts, the first part is the first letter in the string, and the second part is the remaining string.

2. Convert the first part of the string to uppercase, and the second part of the string to lowercase letters

3. Concatenate the two-part strings together to get a complete string

L Code:

public static string convert (String str) {//Gets the first part of string start = str.substring (0,1);//Gets the second part of string end = Str.substring (1);//Convert the first part of the string to uppercase, and the second part of the string to the lowercase letter string big = Start.touppercase (); String small = End.tolowercase ();//Connect two parts of the string together to get a complete string return big+small;}

L Title three: Query the large string, the number of times the specified small string occurs. The number of times "Java" is queried in "Hellojava,nihaojava,javazhenbang".

L thought: 1. In a large string, find the location of the small string, the number of occurrences +1

2. Continue looking after the last occurrence of a small string, and you need to change the contents of the large string to the last query.

3. Go back to the first step and continue to find the location of the small string until the string is not queried.

L Code:

public static int GetCount (string big, string small) {int count = 0;//The number of occurrences of a small string int index = -1;//The position of a small string/*while the loop condition of the three steps: Step one. Big.indexof (small) Gets the small string of positions that appear in the large string step two. Assign the position of the small string to the variable index step three. Determine if the position appears to be-1, if the position equals-1, the large string has not been queried the small string, if the position is not equal to 1, then, the loop, complete the number of times to modify the operation */while (index = big.indexof (small))! =-1) { count++;//Number of occurrences +1//change large string content big = big.substring (index+1);} return count;}

  

  

  

Java API and Object

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.