Some classes should be familiar with Java!

Source: Internet
Author: User

1 throwable class

Public classThrowable
Extends object
Implements serializable
 
 

ThrowableClass is the superclass of all errors or exceptions in Java. The Java virtual machine or JavaThrowStatement throw. Similarly, only this class or one of its subclasses can beCatchThe parameter type in the clause.

Two subclass instances,ErrorAndException, Usually used to indicate exceptions. Generally, these instances are newly created in the context of an exception and contain relevant information (such as stack trace data ).

Throwable contains a snapshot of the thread execution stack when its thread is created. It also contains a message string that provides more information about the error. Finally, it can containCause): Another throwable that causes this throw. This cause facility first appeared in version 1.4. It is also calledException chainFacility, because cause itself also has cause, and so on, an exception chain is formed. Every exception is caused by another exception.

One reason for throwable cause is that the class that throws it to be built in the lower-layer abstraction, and the high-level operation fails due to the failure of the lower-layer operation. It is a bad design method to let throwable thrown by the lower layer spread out, because it is usually irrelevant to the abstraction provided by the higher layer. In addition, this will associate the high-level API with its implementation details, assuming that the low-level exception is a checked exception. Throw a "encapsulated exception" (that is, an exception that contains cause) to allow the upper-level to communicate failure details with the caller, without causing any of the above shortcomings. This method retains the flexibility to change the high-level implementation without changing its API (especially when an exception set is thrown through its method ).

 

Another cause of throwable cause is that the method to throw it must comply with the common interface, while the common interface does not allow the method to directly throw the cause. For example, assume that the persistent set conformsCollectionInterface, and its persistence inJava. Io. Assume thatPutInternal methods can be thrownIoexception. The implementation can communicate with the callerIoexceptionThe detailed message is encapsulated by a suitable non-checked exception.IoexceptionTo make it conformCollectionInterface. (The Persistence set specification should indicate that it can throw this exception .)

Cause can be associated with throwable in two ways: using a constructor that regards cause as a parameter, or usingInitcause (throwable)Method. For new throwable classes that wish to associate cause with them, a constructor with a cause should be provided and delegated (possibly indirectly) toThrowableConstructor. For example:

Try {lowlevelop ();} catch (lowlevelexception le) {Throw new highlevelexception (LE); // chaining-aware constructor}

2 system class

Public final classSystem
Extends object
 
 

SystemThe class contains some useful class fields and methods. It cannot be instantiated.

InSystemThe facilities provided by the class include standard input, standard output, and error output streams, access to externally defined attributes and environment variables, and methods for loading files and libraries; there is also a practical method to quickly copy part of the array.

3 string type

Public final classString
Extends object
Implements serializable, comparable <string>, charsequence
 
 

StringClass represents a string. JavaProgramAll character strings (such"ABC".

Strings are constants, and their values cannot be changed after creation. The string buffer supports variable strings. Because string objects are unchangeable, they can be shared. For example:

 

String STR = "ABC ";

It is equivalent:

 

Char data [] = {'A', 'B', 'C'}; string STR = new string (data );

The following provides more examples of how to use strings:

 

System. out. println ("ABC"); string CDE = "CDE"; system. out. println ("ABC" + CDE); string c = "ABC ". substring (2, 3); string d = CDE. substring (1, 2 );

StringClass includes the following methods: Check a single character of the sequence; compare string; search string; extract sub-string; create a string copy, in this copy, all characters are converted to uppercase or lowercase letters. Case-sensitive ing based onCharacterThe Unicode standard version specified by the class.

The Java language provides special support for String concatenation symbols ("+") and conversion from other objects to strings. String concatenation is throughStringbuilder(OrStringbuffer) Class and itsAppendMethod. String Conversion is throughTostringMethod.ObjectClass definition, and can be inherited by all classes in Java. For more information about String concatenation and conversion, see 《The Java Language Specification.

Unless otherwise statedNullConstructor or method passed to this class will throwNullpointerexception.

StringRepresents a string in UTF-16 format, whereSupplementary CharacterByProxy item pair(For more information, seeCharacterClass ). Index value refersChar CodeUnit, so add characters inStringWhich occupies two locations.

StringClass provides Unicode code points (characters) and Unicode code units (I .e.,CharValue.

4 stringbuffer variable string class

Public final classStringbuffer
Extends object
Implements serializable, charsequence
 

A variable string of thread-safe characters. AStringBut cannot be modified. Although it contains a specific character sequence at any time point, the length and content of the sequence can be changed by calling some methods.

The string buffer can be safely used for multiple threads. These methods can be synchronized as necessary, so all the operations on any specific instance are in serial order, this sequence is consistent with the method call sequence of each involved thread.

StringbufferThe main operation on isAppendAndInsertMethods to accept any type of data. Each method can effectively convert the given data to a string, and then append or insert the character of the string into the string buffer.AppendThe method always adds these characters to the end of the buffer.InsertThe method adds characters to the specified vertex.

For example, ifZReference the current content"Start"String buffer object, this method is calledZ. append ("Le ")Causes the string buffer to contain"Startle", AndZ. insert (4, "Le ")Will change the string buffer to include"Starlet".

Generally, if Sb referencesStringbuilderFor exampleSB. append (X)AndSB. insert (sb. Length (), X)With the same effect.

When an operation related to the source sequence (such as an append or insert operation in the source sequence) occurs, the operation is only performed on the string buffer for this operation, rather than on the source.

Each string buffer has a certain capacity. As long as the length of the Character Sequence contained in the string buffer does not exceed this capacity, no new internal buffer array needs to be allocated. If the internal buffer overflow occurs, the capacity increases automatically. JDK 5 adds an equivalent class for a single thread, that isStringbuilder. Compared with this class, it is usually preferred to useStringbuilderClass, because it supports all the same operations, but because it does not execute synchronization, so the speed is faster.

5 stringbuilder class -- note the difference between stringbuilder and stringbuilder

Public final classStringbuilder
Extends object
Implements serializable, charsequence
 
 

A variable character sequence. This class providesStringbufferCompatible APIs, but cannot be synchronized. This class is designed for useStringbufferIs used when the string buffer is used by a single thread (this is common ). If possible, we recommend that you use this class first, because in most implementations, it is betterStringbufferFast.

InStringbuilderThe main operation on isAppendAndInsertMethods to accept any type of data. Each method can effectively convert the given data to a string, and then append or insert the character of the string to the string generator.AppendThe method always adds these characters to the end of the generator.InsertThe method adds characters to the specified vertex.

For example, ifZReference the current content"Start"String generator object, this method is calledZ. append ("Le ")Will make the string generator contain"Startle", AndZ. insert (4, "Le ")Will change the string generator to include"Starlet".

Generally, if Sb referencesStringbuilderSB. append (X)AndSB. insert (sb. Length (), X)With the same effect. Each string generator has a certain capacity. As long as the length of the Character Sequence contained in the string generator does not exceed this capacity, no new internal buffer needs to be allocated. If the internal buffer overflow occurs, the capacity increases automatically.

SetStringbuilderIt is not safe to use instances for multiple threads. If such synchronization is required, we recommend that you useStringbuffer.

6. Java root class Object

Public classObject
 
 

ClassObjectIs the root class of the class hierarchy. Each class usesObjectAs a superclass. All objects (including arrays) implement this class method.

 

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.