Java Summary fourth time//Common class

Source: Internet
Author: User
Tags date now string format

Vi. Common Categories

Main content: Object class, String class, Date class, encapsulation class

(i) Object class

The 1.Object class is the root parent class for all Java classes

2. If the parent class is not indicated in the declaration of the class with the extends keyword, the default parent class is the object class

public class Person {

...

}

Equivalent to:

public class Person extends Object {

...

}

( two ) = = operator and equals Method

= = Operator:

reference type comparison reference (whether to point to the same object);

Person P1=new person (); Person P2=new person ();

if (P1==P2) {...}

base type comparison value;int a=5; if (a==6) {...}

When comparing with "= =", the data types on both sides of the symbol must be the same (except the basic data type that can be converted automatically), otherwise the compilation error;

Equals method:

is a The method of the object class inherits the Equals () method because all classes inherit the object class.

Only reference types can be compared.

in the In the definition of the object class, the function is the same as "= =", and the comparison points to the same object. Format: Obj1.equals (OBJ2)

Note: for class File, String, Date, wrapper class (Wrapper Class), and many classes that override the Equals () method, are comparison types and content regardless of whether the reference points to the same object.

(iii)toString method

The 1.toString () method is defined in the object class, and its return value is a string type, with the contents of the class name and the reference address of the object.

2. The ToString () method is called automatically when a string is connected to another type of data.

For example:

Date Now=new date ();

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

equivalent

System.out.println ("now=" +now.tostring ());

3. You can override the ToString () method in a user-defined type as needed

like The string class overrides the ToString () method, which returns the value of the string.

s1= "Hello";

System.out.println (S1);

equivalent

System.out.println (S1.tostring ());

4. When converting a primitive type data to a string type, the ToString () method of the corresponding wrapper class is called for example:

int a=10;

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

common methods of 5.String

public Boolean startsWith (String s)

public Boolean EndsWith (string s): determines whether the prefix (suffix) of the current string object is the string specified by the parameter s

public int CompareTo (string s): compares the size of a string specified by a dictionary order with a parameter s

public int comparetoignorecase (String s)

Public Boolean contains (string s): determines whether the current string object contains the string specified by the parameter s

public int indexOf (string s): retrieves the string sfrom the beginningand returns The position of the first occurrence of S

IndexOf (String s, int startpoint)

LastIndexOf (String s)

public string substring (int startpoint): gets a substring of the current string

substring (int start, int end)

public string Trim (): gets a String object after the trailing space is removed

string and character array

String (char[]): Creates a string object with all the characters in the character array

String (char[],int offset,int length): Creates a string object with some characters in the character array

Public char[] ToCharArray (): Method for storing all characters in a string in an array of characters

string and byte array

String (byte[]): Constructs a string object with the specified byte array.

String (byte[],int offset,int length) : A portion of the specified byte array, that is, from the array start position Offset begins by taking a length of bytes to construct a string object.

Public byte[] GetBytes () : Converts the current string to a byte array using the platform's default character encoding.

Public byte[] GetBytes (string charsetname) : Converts the current string to a byte array by specifying a character encoding using the parameter.

Note : String s= "a"; A string was created

S=s+ "B"; in fact, the original "a" string object has been discarded, and now produces a string s+ "B"(also known as "AB").

6.StringBuffer class

String: Immutable sequence of characters

StringBuffer: Variable sequence of characters

string s = new String ("Hello");

StringBuffer buffer = new StringBuffer ("Hello");

Buffer.append ("World");

The StringBuffer class has three methods of construction:

StringBuffer ()

StringBuffer (int size)

StringBuffer (String s)

StringBuffer: variable character sequence, thread safety, low efficiency

StringBuilder: Variable character sequences, thread insecurity, high efficiency

(iv) Date&dateformat class

the Date class is in the java.util package. objects created using the parameterless construction method of the Date class can get the local current time.

SimpleDateFormat can be used for formatting dates.

Public SimpleDateFormat (String pattern): creates an object using the format specified by the parameter pattern.

Public String Format (date date): Formats the time object dateby the pattern specified in the construction method .

(v) Encapsulation class

Java Summary fourth time//Common class

Related Article

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.