Java Advanced Application Programming Chapter I tool class

Source: Internet
Author: User
Tags wrapper stringbuffer

One, the Java API

Introduction to the Java API

1. API (application Programming Interface) application interface

2. Java API is the Java class of various functions provided by JDK

3. JDK Help document Java_api_cn.chm official address http://www.oracle.com/technetwork/java/api-141528.html

Such as:

Java.lang
Java language Pack (contains strings, Math, System, and so on), in any class, the classes in that package are automatically imported.
Java.util
Contains a number of useful tools (including list, calendar, date, and other classes)
java.awt
Graphical user Interface Pack.
java.io
A class that provides multiple input/output functions.

java.net
A class that provides network application functionality.

Second, Object

(i) Object Introduction and method

1. The object class is a superclass of all classes, and object is the only class in the Java language that does not have a parent class.

2. A class may not be a direct subclass of the object class, but it must be a subclass of the object class, and each class in Java is extended from object.

3. The methods defined in the object class can be used in all classes.

(ii) Common methods of Object class

1, public boolean equals (Object obj): Compares the values referenced by two objects for equality (compare addresses). Indicates whether the object calling the method is equal to the Obj object. That is, the address is equal.

2,public int hashcode (): The method returns the hash code of the object, which is a hexadecimal integer representing the object, compared to the ID number of the object. The hash code returned by Hashcode () for each call to the same object must be the same during the run of the program, but the same program is executed more than once, and the hash code of the same object is not necessarily the same during the execution of the program and the next execution. In fact, the default hash code is the memory address of the object through some kind of conversion, so different objects will have different hash code

3. public string toString (): Returns the class name @hashcode; Actually, returning such a string has little practical meaning. A generic subclass overrides the method, allowing it to return meaningful text.

The difference between equals () and = =

  It is important to understand the difference between the Equals () method and the = = operator.
By default (that is, when not overridden) equals () can only compare reference types, and "= =" can compare reference types and compare basic types.
The Equals () method inherits from the object class, which is the value that compares the reference
Usually quilt method overrides, no longer compare the referenced value
  the "= =" Operator:
Compare basic data types: equivalent to arithmetic equals
Compare reference data types: compare referenced values and cannot be overwritten.

Four, Packaging class

 1. The Java programming language does not treat basic data types as objects. The Java programming language provides wrapper classes to treat basic data types as objects.

2. The function wrapper class can accomplish the conversion between data types (except Boolean), especially the basic data type and the conversion of string type.

3. The wrapper class contains values that correspond to the basic data types, encapsulates the method of converting between string and basic data types, and some properties and methods that are useful when dealing with these basic data types.

(a) Introduction of packaging class

The base data type is not part of the object hierarchy. Sometimes you need to handle these basic data types as you do with objects, and you can "wrap" them with the appropriate "wrapper class".

(b) string and basic data type, package type conversion diagram

1. Conversion of basic data type to wrapper class

int pInt = 500;
Integer wInt = new Integer (PINT);

2. String conversion to wrapper class

      string is converted to wrapper class by construction method

String sInt = "500";
Integer wInt = new Integer (SINT);

The string is converted to a wrapper class by wrapping the valueof (string s) of the class

String sInt = "500";
Integer wInt = integer.valueof (sInt);
Note: strings cannot be converted to character in either of these ways

    3. The wrapper class is converted to the basic data type

Wrapper classes are converted to basic data types through the Xxxvalue () method

Integer wInt = new Integer (500);
int pInt = Wint.intvalue ();

4. The wrapper class is converted to a string

Wrapper classes are converted to strings by the ToString () method

Integer wInt = new Integer (500);
String sInt = wint.tostring ();

5. String conversion to basic data type

String converted to base data type by PARSEXXX (string s) method

String sInt = "500";
int pInt = Integer.parsetiint (sInt);

(c) Automatic box packing and automatic unpacking

    1. when you perform a basic data type and a corresponding wrapper class conversion, the system automatically

    2. JDK has been introduced since version 5.0

    3. Easy Program Writing

int pInt = 500;
Integer wInt = pInt;
int n = wInt;

Five, String class

1. The string representation can be divided into two types: string and StringBuffer.

2, does not belong to 8 kinds of basic data type, but is a kind of reference type.

    3. The string object represents a sequence of immutable Unicode characters, and any modification to it actually results in a new string, and the contents of the string object cannot be changed once it is initialized.

    4. the StringBuffer object represents a set of Unicode character sequences that can be changed.

    5. The string class is a class of the final type.

(i) Creation of a String object

    How to construct a string class
1. Initializes a newly created String object that represents a sequence of empty characters.

String variable name = new string ();
2. Initializes a newly created String object that represents the same sequence of characters as the parameter

String variable name = new string (string value);
3. Assigns a new string that contains the sequence of characters currently contained in the string buffer parameter.

String variable name = new String (StringBuffer buf);
4. The string literal constant itself is a string object
String variable name = "Hello";

5. Initializes a newly created String object by using the specified charset solution byte array specified by code

String variable name = new string (byte[] bytes, string charsetname);

6. Initializes a newly created String object by using the specified charset solution specifies a byte array, constructs a new String

String variable name = new string (byte[] bytes, int offset, int length, Charset Charset);

7, the connection operator "+", you can convert various other types of data into a string, and connect back and forth into a new string

String str1 = "Hello";
String str2 = "World";
String str = str1 + str2; STR is "Hello World";

(ii) Conversion of the String class

  

(iii) methods commonly used in the String class

(iv) StringBuffer class

StringBuffer class for strings that can be changed by content

you can use StringBuffer to dynamically manipulate the contents of a string without generating Additional Objects

(v) Creation of StringBuffer objects

1. Constructs a string buffer with no characters, with an initial capacity of 16 characters

StringBuffer variable name = new StringBuffer ();

2. Constructs a string buffer with no characters, but with the specified initial capacity.

StringBuffer variable name = new StringBuffer (int capacity);

3. Constructs a string buffer and initializes its contents to the specified string content.

StringBuffer variable name = new StringBuffer (String value);

(vi) methods commonly used in the StringBuffer class

(vii) = =

When comparing the base data type, the value is compared

When comparing reference data types, the comparison is the address

(eight) equals

The native (not rewritten) equals method is = = =, which compares the values of two objects, is usually used to determine whether two objects are the same object, the return value is true two objects are the same object, when the subclass of object is rewritten and has other functions, The Equals method cannot be used to compare basic data types

(ix) Hashcode

The Hashcode hash table records the location where each object is stored, so the hash code can be used as a representation of the object memory address, typically using hashcode to compare whether two objects are not the same object, if the hashcode different two objects are not the same object

(10) Q: Why not hashcode compare two objects for the same object

A: There is a case where hashcode is the same and equals returns a result of false

(11) String, StringBuffer, StringBuilder

String immutable character sequence
StringBuffer, StringBuilder variable character sequence
StringBuffer thread safety But low efficiency, early introduction
StringBuilder thread Unsafe but high efficiency, late introduction

VI. Math class

The Math class provides a number of methods for mathematical operations

    The math class is the final class and therefore cannot inherit from the math class

    the methods in the math class are static methods, so it is not necessary to create an object of the math class You can use the method of the class directly

(i) Constants in the Math class

public static final Double PI

public static final Double PI

(ii) Common methods in the Math class

Seven, date class

(i)Date class

java.util.Date.

The date class represents the specified time information, which can be accurate to milliseconds.

Internationalization is not supported.

Many of the methods of this class are obsolete

Get current system time:

New Date ();

(ii) Calendar class

1, Java.util.Calendar.
Java.util.GregorianCalendar
2, the commonly used date processing object. You can set your own time zone and internationalization format.
3, is an abstract class.
4. The calendar abstract class defines enough methods to allow us to express the rules of the calendar.
5. Get an instance of the Calendar object

Calendar C = calendar.getinstance ();

6. Set the time represented by the calendar instance:

C.set (2011,6,16);

7. Gets the specified time attribute

C.get (calendar.year);

(iii) Calendar class Common Properties

  

Java Advanced Application Programming Chapter I tool class

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.