Java Common Classes

Source: Internet
Author: User

Java Common Class 1.1 Object
    1. Equals (Object obj) method

Compare objects equality the implementation of the object class is equivalent to = =

own class to compare object equality, overriding equals () Method

    1. Protected Object Clone ()

Cloning an object's methods the class of the object being cloned must implement the Cloneable interface

    1. Finalize () method

This method is called by the object's garbage collector when the garbage collector determines that there are no more references to the object

    1. Hashcode () method

Returns the hash code value of the object

when we rewrite the Equals () method and determine that two objects are equal, it is best to also rewrite the Hascode () method so that the same object and hash code values are the same

    1. ToString () method

The ToString () method of the default call object when the object is printed directly

The tostring method output format for the object class:
GetClass (). GetName () + ' @ ' + integer.tohexstring (hashcode ())

own class to rewrite toString ()

1.2 String

Construction method of attention under the Java.lang package

    1. charAt (int index) returns the char value at the specified index.
    2. CompareTo (String anotherstring) compares two strings in a dictionary order.

The return value is int

When the first one is larger than the second, a positive integer is returned

When the first one is smaller than the second, a negative integer is returned

When the first one equals the second returns 0

Judging size, see results return value and 0 to compare

    1. Comparetoignorecase (String str) compares two strings in a dictionary order, regardless of the case.
    2. Concat (String str) connects the specified string to the end of this string.
    3. Contains (charsequence s)

Returns True when and only if this string contains a sequence of specified char values.

    1. EndsWith (string suffix) tests whether this string ends with the specified suffix.
    2. Equals (object AnObject) compares this string to the specified object.
    3. Equalsignorecase (String anotherstring) compares this string to another string, regardless of case.
    4. indexOf (int ch) returns the index of the specified character at the first occurrence of this string.
    5. IndexOf (String str) returns the index of the first occurrence of the specified substring in this string.

Determines the index position of a string at the first occurrence of a string

When not present, the return index is 1 and can be used to determine if the substring exists

int index3 = Str2.indexof ("xyz");

System.out.println (INDEX3);

    1. lastIndexOf (int ch) returns the index of the specified character at the last occurrence in this string.
    2. Length () returns the lengths of this string. length of array: Array Object . Length Properties
    3. Replace (char OldChar, char Newchar)

Returns a new string that is obtained by replacing all OldChar that appear in this string with Newchar.

    1. Split (string regex) splits this string based on the match of the given regular expression.
    2. StartsWith (string prefix) tests whether this string starts with the specified prefix.
    3. substring (int beginindex)

Returns a new string that is a substring of this string. The substring starts at the specified index and continues to the end of the string.

    1. substring (int beginindex, int endIndex)

Returns a new string that is a substring of this string. The substring starts at the specified Beginindex and continues to the character at index EndIndex-1.

    1. ToCharArray () Converts this string to a new character array.
    2. toLowerCase () converts all characters in this String to lowercase using the default locale's rules.
    3. toUpperCase () converts all characters in this String to uppercase using the default locale's rules.
    4. Trim () Returns a copy of the string, ignoring leading and trailing blanks.
    5. valueOf (int i) returns the string representation of the int parameter.
1.3 StringBuffer and StringBuilder

Java.lang under the Bag

    1. Append () Adds a sequence of characters to the StringBuffer object, returning the StringBuffer object reference after it was added.
    2. Insert () Inserts a sequence of characters at the specified position for the StringBuffer object, returning the modified StringBuffer object reference.
    3. Delete (int start,int end) can delete a sequence of characters from start to End-1, returning the modified StringBuffer object reference.
    4. Deletecharat (int index) removes the char at the specified position of this sequence
    5. Reverse () reverse the character sequence and returns the modified StringBuffer object reference.
    6. Setcharat (int Index,char ch) sets the character at the given index to Ch.

StringBuilder:

The introduction of the StringBuilder class after JDK5.0 can provide the same functionality as StringBuffer,

The difference:

A) The StringBuffer class is thread-safe, while StringBuilder is not, that is, not guaranteeing the synchronization of its objects, which is not secure in a multithreaded environment.

b) StringBuilder is better than stirngbuffer in performance.

public static void Main (string[] args) {

StringBuffer sb = new StringBuffer ();

SB + + "I";

Sb.append ("AM";);

Sb.append (TRUE);

System.out.println (SB);

}

A string type variable (pair) can be passed into the StringBuffer constructor method

Append method parameter can only be a string type variable (wrong)

The code runs the result: I am true (wrong)

StringBuffer object cannot use + = Assignment (yes)

1.4 Math

Java.lang under the Bag

Math.PI

public static double abs (double A) returns the absolute value of double values

public static double random () the value is greater than or equal to 0.0 and less than1.0

public static long round (double a) returns the closest parameter long . The result is rounded to an integer

public static double Ceil (double A)

Returns the smallest (nearest negative infinity) Double value, which is greater than or equal to the parameter and equals an integer

public static double floor (double a)

Returns the largest (closest to positive infinity) Double value, which is less than or equal to the parameter and equals an integer

Instance:

int a,b,c;

a= (int) Math.Round (-4.51);

b= (int) Math.ceil (-4.51);

c= (int) Math.floor (-4.51);

A b c result is-5-4-5

1.5 Random

Java.util under the Bag

public int Nextint () returns the next pseudo-random number

public int Nextint (int n)

Returns a pseudo-random number that is an int that is evenly distributed between 0 (inclusive) and a specified value (not included) from this random number generator sequence

1.6 Packing Class

Java.lang under the Bag

Java has 8 basic data types, and provides 8 package types, each corresponding to each basic data type

Effect: Converts a basic data class to a class to produce an object. Type conversions

(1) Collection use

A collection is a special object that can store other objects so that only objects can be saved

(2) Type conversion using

Web Development The parameters passed from the foreground are string strings, and we want to convert the string to a basic data type such as the id=1 background get parameter "1"------"INT 1

Start with the next stage of your development career with the knowledge points to determine your destiny

int----------Integer

Short------Short

BYTE-------byte

Long------Long

char------Character

Float-----Float

Double----Double

Boolean----Boolean

Integer to String

int i = Ten;----->string s = i+ ""; String.valueof (i) converting integers to strings

String to Integer

(1) String s = "ten";---->int integer.parseint (s)--->int

(2) Integer ig = new Integer (s); Ig.intvalue ()-->int

(3) public static Integer valueOf (String s)

(4) public static Integer valueOf (int i)

JDK1.5, new features of packaging class

Automatically package int------>integer Double----->double Integer i = 10;

Automatically unpacking an Integer------>int Double----->double Int j = i;

Instance:

Integer i1 = new Integer (3);

Integer i2 = new Integer (3);

System.out.println (I1==I2); False two object equality compare address unequal

int i3 = 3;

System.out.println (I1==I3);//True unpacking automatically

Integer i4 = 3; Automatic packaging

Integer i5 = 3;//Automatic Packaging

System.out.println (I4==I5);//true unpacking automatically

1.7 Date/calendar

Java.util.Date (System time class, we now use this) java.sql.Date (Database time Class)

Public Date ()

Assigns a Date object and initializes the object to indicate the time (in milliseconds) to allocate it.

Public date (long date)

Allocates a Date object and initializes the object to represent the specified number of milliseconds since the standard base time, which is January 1, 1970 00:00:00 GMT

Package COM.BW;

Import Java.util.Date;

public class Datetest {

public static void Main (string[] args) {

Date d = new Date ();

System.out.println (d);

System.out.println (D.tolocalestring ());

Long time1 = D.gettime ();//1970-1-1 00:00:00 GMT to the current total number of milliseconds

System.out.println (TIME1);

Long time2 = System.currenttimemillis ();

System.out.println (time2);//1970-1-1 00:00:00 GMT to the current total number of milliseconds

Date D2 = new Date (time2);

System.out.println (D2);

}

}

1.8 Dateformat/simpledateformat

Java.text Packet class DateFormat time format class

Public abstract class Dateformatextends Format abstract classes

Java.text
Class SimpleDateFormat

public class Simpledateformatextendsdateformat specific classes

Mode of Time:

Construction Method:

SimpleDateFormat ()

Constructed with the default schema and date format symbols for the default localeSimpleDateFormat

SimpleDateFormat (String pattern)

Constructs a date format symbol with the given pattern and default localeSimpleDateFormat

Instance:

Package COM.BW;

Import Java.text.SimpleDateFormat;

Import Java.util.Date;

public class Datetest {

public static void Main (string[] args) {

Date d = new Date ();

System.out.println (d);

System.out.println (D.tolocalestring ());

Long time1 = D.gettime ();//1970-1-1 00:00:00 GMT to the current total number of milliseconds

System.out.println (TIME1);

Long time2 = System.currenttimemillis ();

System.out.println (time2);//1970-1-1 00:00:00 GMT to the current total number of milliseconds

Date D2 = new Date (time2);

System.out.println (D2);

SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");

String str = Sdf.format (D2);

System.out.println (str);

}

}

Java Common Classes

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.