Java Experience--java class

Source: Internet
Author: User
Tags stringbuffer

1. The Java application programming interface (Applicationprogramming INTERFACE,API) is a Java Program class library developed by Sun to provide the platform and tools used by Java programmers. The classes and interfaces in these class libraries make it easy to implement various functions in the program.

Classification of 2.Java packages

Μ Java Core package (Java key package)

Μ Java Expansion Pack (Java extension package)

¯ Common Java Core Package

Μ Java.lang Package: A Java language pack that contains classes related to language and data types. When the program is running, the compiler automatically introduces the package

Μ java.awt Packages and Javax.swing packages: provides classes that create graphical interface elements that allow you to control the appearance of the application interface

Μ java.io Package: Java input/Output package, containing classes related to input and output operations, providing support for reading and writing to different input/output devices

Μ Java.util Package: A utility package that provides classes that implement different functions.

Μ java.net Package: Java Network package, providing classes related to network operations

Μ java.sql Package: Contains classes/interfaces related to database programming

Note: In addition to the automatic introduction of class systems in the Java.lang package, to use classes in other packages, you should introduce the appropriate packages at the beginning of the program.

For example: import java.io.*; Import java.util.Date;

3. Java.lang.Object class

the Java.lang.Object class is the highest-level parent class for all Java classes.

Μ property: No attributes defined

Μ Method

(1). String toString (): Returns information about the current object (the type name the object belongs to and the object's hash code)

you can override the ToString () method in your own defined Java class as needed to provide more appropriate descriptive information.

Note:

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

System.out.println (o); The two are equivalent ...

The tosring method in object is to convert an object to a string output (with a hash code), when output: System.out.println (o), and System.out.println (o.tostring ()) effect 。

(2). Boolean Equals () method: Used to compare the equivalence of two objects. For non-null reference values, the method returns true only if the two reference variables are references to the same object. (You can override the quilt class, especially the string class that is overridden under the Lang package, to compare content)

Note: The Equals () method returns True if two objects are equivalent when comparing data for certain reference types such as String, Java.io.File, Java.util.Data, and encapsulating classes.

if the string variable assignment is the same, only one copy is saved in memory.

For example: (1) String s1= "ABC";  String s2= "ABC";     S1 and S2 point to the same object; System.out.println (S1==S2); The result is true

System.out.println (s1.equals (S2));//The result is also ture;

(2) string s1=new string ("abc");  String S2=new string ("abc");    S1 and S2 point to different object System.out.println (S1==S2); The result is false

System.out.println (s1.equals (S2));//The result is ture;

(3). Hashcode () Method: Returns the hash code (hashcode) of the current object.

The hash code can be understood as an integer number that the system automatically creates for each Java object, and the hash code of any two different Java objects must be different, and the same integer must be returned consistently during one execution of the Java program when the Hashcode () method is called multiple times on the same object.

4. String-related types

Μ java.lang.String: Represents a string of 16-bit Unicode characters used to record and manipulate textual information.

Μ Note: Once an object of the string class is created, its contents cannot be changed. All of the methods mentioned above will create and return a new string object

(1). Construction method
public string (); Initializes a newly created string method that represents an empty sequence of strings (""), also known as an empty string.
Public String (byte[] bytes), decoding the specified byte array with the default character set of the current platform (the default character set of the file system in the operating system), and parsing it out the annoying characters construct a new String object in the original order.
public string (char[] value), copying the contents of the string value, constructing a new string object, and modifying the character array after creation does not affect the newly created string.
public string (string original); Initializes a new string object with an existing string object original to implement the copy function of the string object.
A public string (Stringbufferstringbuffer), a new string object is constructed using the contents of the object StringBuffer, and modifications to the StringBuffer object after creation do not affect the newly created string.

(2). Other methods
Provides string concatenation, conversion, and truncation capabilities

Public string concat (String str): Implements string concatenation functionality.

Public string Replace (char Oldchar,charnewchar): string content substitution, new old.

Public string subString (int beginindex) and public string subString (int beginindex,int endindex): Extracts a string.

Public string toLowerCase (): Converts a string to lowercase, forming a new string.

Public string toUpperCase (): Converts a string to uppercase, forming a new string.

Public string toLowerCase (): Filters the spaces at the beginning and end of a string.

* * * search and find function

Public char charAt (int index): Returns the character at the specified index position.

Public Boolean EndsWith (string suffer): Determines whether the current string ends with the specified substring.

Public Booleane StartsWith (string suffer): Determines whether the current string starts with the specified strings.

public int indexOf (int ch) and public int indexOf (String str): finds the index position (subscript) where the specified string or string appears in the current string.

public int LastIndexOf (int ch) and public int lastindexof (String str): finds the index position (subscript) of the last occurrence of the specified string or string in the current string.

public int Length (): Returns the string length, that is, the number of characters it contains.

* * Content comparison function

Public Boolean equals (Object AnObject): Compares the current string to the specified object's equivalence.

Public Boolean equalstgnorecase (String anstring): Compares the contents of two strings to the same, ignoring case.

public int CompareTo (string anstring): Compares the size of a string in dictionary order.

* * Split string

Public string[] Split (string regex): Splits a string based on a regular expression (that is, a match of a given expression).

5. We often need to modify the contents of a string, and using the string type is not possible, but it is inefficient (because its object cannot be changed once the content is created). You can only keep creating new objects and destroying old objects in this case java.lang.StringBuffer is more appropriate. Java.lang.StringBuffer class: Represents a sequence of Unicode-encoded characters whose contents can be modified, and the contents and length of the saved string can be modified after the object is created. Virtually every stringbuffer has a variable-capacity string buffer, which can be automatically expanded as the content increases, or can be set directly, depending on the size of the memory or the number of characters that can be accommodated.

Construction Method:

Public StringBuffer (): Constructs a character buffer with no characters, with an initial capacity of.

Public stringbuffer (int capacity): Constructs a string buffer with no characters, but with the specified initial capacity.

Public StringBuffer (String str): Constructs a string buffer and initializes its contents to the specified string content.

Common methods:

append (): Appends information to the string buffer. The method has a variety of load forms that converts parameter values of any type to string types and then appends to the end of the original sequence of characters in the buffer.

Insert (): Converts a parameter value into a string type into the specified position of the current character sequence. Similar to append (), there are several forms of loading.

Public Sringbuffer Reverse (): reverses (reverses) processing of the current character sequence.

Public voidsetcharat (int index,char ch): Sets the character at the specified index in the character sequence.

6. Java.lang.StringTokenizer Class (non-book content): decomposes the current string by default or specified delimiter (that is, delimited tags) into multiple fragments. You can also use the split () method of the string class to separate existing strings

Construction method
Public StringTokenizer (String str)
Public StringTokenizer (String str,string Delim)

U Common Methods
public boolean Hasmoretokens ()
Public String nexttoken ()

7. Java.lang.System class and Java.lang.Runtime class

Μ System class: Java does not support global functions and global variables, and its designers collect some important system-related functions and variables all members of the class in the systems class are static, and when you want to reference these variables and methods, the system. Property system. Method ()

U exit (int x) method: Terminates the currently running Java Virtual Machine (JVM). A parameter represents a status code, according to convention, a number not 0 indicates an abnormal termination.

Currenttimemillis () method: Obtains a number of milliseconds since 0 o'clock January 1, 1970, which can be used to obtain the execution time of the program.

Μ Runtime class: This class encapsulates the instance process information that is started by the Java command itself---the Java Virtual machine process. A virtual machine corresponds to a runtime instance object. Many of its methods and the duplication in system, can not directly create the object of the class, only through the Runtime.getruntime () method to obtain an instance of the object, the Java virtual machine itself is an operating system of a process, this process can start other programs, A process initiated in this way is called a subprocess, and the child process can be started by the EXCU () method of the runtime object, which returns a value of the process object that represents the subprocess.

rutimer=runtime.getruntime ();

processp=r.exec ("C:\\windows\\system32\\notepad.exe");

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.