Java Note 10-object Wrapper type string

Source: Internet
Author: User
Tags parse string

Outline:
Introduction to common methods in 1.java.lang.0bject
2. Description of the package type corresponding to the basic type
and the conversion between the basic type and the wrapper type
3.java.lang.string string Processing class
Java.lang.StringBuilder

Use of the method in the packing type
Use of methods in strings
Basic type wrapper type and string and StringBuilder
The mutual conversion between
-----------------------------------------------------------------------
Java.lang.Object
is the base class for all classes, and all classes either directly or indirectly inherit it

Common methods:
1.getClass ();//Gets the run-time type of the object


Java.lang.class<t>
It is used to describe classes, and through this class, we can get the information inside the class.
It is also the basic class of Java Reflection technology!!!

Note: No matter how many times a class is instantiated, it will always have only 1 class instances in Java!!!

Student S1 = new Student ();
Student s2 = new Student ();
S1==s2;//false
S1.getclass () ==s2.getclass ();//true

There are 5 ways to get class instances in Java:

1. Object name. GetClass ();

2. Class name.

3.class.forname ("Fully qualified name of the class");
This method requires us to force the exception to be handled, java.lang.ClassNotFoundException

4. Basic type. Class
Int.class

5. Package type. class
Integer.class
-------------------------------------------------------------------------------------------
Common Method 2:
Boolean equals (Object obj);

In Java.lang.Object, this method compares the memory address by default

In order to compare whether two objects are "equal", we usually go to
Overriding the Equals method

Steps to override the Equals method
1. Non-void sex
2. reflexivity
3. Consistency
4. transitivity
5. Symmetry

Attention:
Comparison of 1.== basic types with = =
= = Compare the memory address
Comparison of 2.equals object types with the Equals method, the default comparison is memory address
It needs to be rewritten
The 3.String type and date type have been overridden by this method!!!

S1.equals (S2);

@Override
public boolean equals (Object obj) {
1. Non-void sex
if (null==obj) {
return false;
}
2. reflexivity
if (this==obj) {
return true;
}
3. Consistency
if (obj instanceof Student) {
Student s = (Student) obj;
If the IDs of two objects are consistent, the same object is identified
Return S.getid () ==id;
}

return false;
}


int hashcode ();//Returns the hash value of the object
Main purpose: It is used to raise high-performance

1. Same object (Equals method True), hash value must be the same
1. Different hash values for the image are guaranteed as different as possible

Attributes that appear in the Equals method must also appear in the Hashcode Method!!!

3.toString () Method:
The default output is the memory address of the object

[Email protected]

GetClass () + ' @ ' +integer.tohexstring (hashcode ());

General override to output the string representation of an object
If the output object is not the above type, it means that it has rewritten the method itself.
or its parent overrides the ToString () method to meet the printing requirements!!!

4.protected object Clone ();//Create and return a copy of an object

1.protected modifier changed to public

2. Entity class to implement Java.lang.Cloneable interface
Otherwise it throws Java.lang.CloneNotSupportedException

Clone in Java.lang.Object is a shallow clone.

Shallow clone: Shallow copy
Copies the value of the base type to a copy, and the object type is shared
Of course: string and date types are excluded!!!

Deep clone: Deep copy
We need to encode it manually to copy all types of data to the copy.
There is no shared data type!!!

5.protected void Finalize ()
This method is called by the object's garbage collector when the garbage collector determines that there are no more references to the object.

Attention:
1. This method is not called by humans, but is called by the JVM.
2.JVM when retrieving a garbage object, it will call this method of the object first
3. The method executes regardless of whether an exception occurs in the program

4. Please distinguish final finally finalize
-----------------------------------------------------------------------------------------------
The base type and the package type it corresponds to
Byte->byte
Short->short
Int->integer
Long->long
Float->float
Double->double

Boolean->boolean
Char->character

The wrapper class about the number type has a top-level abstract parent class Java.lang.Number

Abstract the public methods in the parent class:
Byte Bytevalue ()
Returns the specified numeric value in byte form.
Abstract double Doublevalue ()
Returns the specified value in double form.
Abstract float Floatvalue ()
Returns the specified value in float form.
abstract int Intvalue ()
Returns the specified value as an int.
Abstract Long Longvalue ()
Returns the specified value as long.
Short Shortvalue ()
Returns the specified numeric value in short form.

All of these methods are used to convert the wrapper type to a basic type of

Java.lang.Integer

Construction Method:
Integer (int value);//Use basic type (int) to construct an integer object
Integer (string value);//The string here must be of type numeric, otherwise it will throw
Java.lang.NumberFormatException Numeric formatting exceptions

JDK5.0 Support Automatic unpacking later

Enclosure: Encapsulates the base type into its corresponding package type
Unpacking: Converting the wrapper type to the base type it corresponds to

Define an int type of data
int i = 10;
Automatic Seal box
Integer j = I;//ok
Integer x = 10;//ok

Automatic unpacking
int y = x;

Common methods:
1.boolean equals (Object obj);
Integer i = new Integer (10);
Integer j = new Integer (10);
I==j;//false
I.equals (j);//true

2.int intvalue ();//Will Integer->int

3.static int parseint (string value);//parse string to int type

4.String toString ();//integer->string

5.static Integer valueOf (int i);//int->value

6.static Integer valueOf (String value);

Summarize:
Int->integer:
= = "1. By integer construction
= = "2. Automatic sealing box
= = "3.valuOf (int i)

Integer->int
==> 1. Automatic unpacking
= = "2.int intvalue ()

Integer->string
= = String toString ();

String->int
= = int parseint (String value);

String->integer
=>integer valueOf (String value);

Int->string
In the =>java.lang.string class
String valueOf (int i);
-----------------------------------------------------------------------------
Java.lang.Character

1. Construction Method:
Character (char value);//char->character

Support for automatic unpacking

char c = ' a ';
Character cc = c;

2. Common methods:
1. Char charvalue ();

2.static boolean isdigit (char ch)
Determines whether the specified character is a number

3.static boolean isletter (char ch)
Determines whether the specified character is a letter

4.static boolean islowercase (char ch)
Determines whether the specified character is a lowercase letter

5.static boolean isuppercase (char ch)
Determines whether the specified character is uppercase

6.static boolean iswhitespace (char ch)
Determines whether the specified character is a white-space character based on the Java standard

7.String toString ()

8.static Character valueOf (char c);
-------------------------------------------------------------------------------------
Java.lang.Boolean

Construction Method:
Boolean (Boolean flag);
Boolean (String value);//if and only if the passed String is true or true the result is true

Support Automatic unpacking

Api
---------------------------------------------------------------------------------------

Java Note 10-object Wrapper type string

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.