4: Object wrapper class for basic data Types
(1) For more convenient operation of each basic data type, Java provides many of its properties and methods for our use.
(2) Use:
The advantage of encapsulating basic data types into objects is that you can define more features in the object to manipulate the data.
One of the most common operations: for transformations between the base data type and the string.
A: Easy to operate B: for converting to and from strings (3) Basic data type and object type correspondence
BYTE byte
Short Short
int Integer
Long Long
float float
Double Double
Boolean Boolean
Method of Constructing Char Character (4)
Field Summary:
The static int Max_value constant with a 2^31-1 value that represents a constant with the maximum static int min_value value that the int type can represent -2^31, which represents the minimum value that an int type can represent Stati The C class<integer> type represents the Class instance Integer (int value) of the base type int constructs a newly allocated integer object that represents the specified int value. Inreger (string s) Note: s must be a string of pure digits. Otherwise there will be abnormal numberformatexception (5) several commonly used methods
Integer.tobinarystring ();
Returns the string representation of an integer parameter as a binary (radix 2) unsigned integer.
Integer.tooctalstring ();
Returns the string representation of an integer parameter in the form of a eight-in (cardinality 8) unsigned integer.
Integer.tohexstring ();
Returns the string representation of an integer parameter in the form of a 16-in (cardinality 16) unsigned integer.
The static int integer.parseint (string s) resolves the string argument as a signed decimal integer.
The string must be a numeric string within the INT type range
static int Integer.parseint (String s,int Basic)
Resolves a string parameter to a signed integer using the cardinality specified by the second parameter.
The string must be a numeric string within the INT type range
The short Shortvalue () returns the value of the integer in the short type.
int Intvalue () returns the value of the integer with the int type.
The static integer valueof (int num) Returns an Integer instance that represents the specified int value.
The static integer valueof (string s) returns an integer object that holds the value of the specified String.
Static Integer valueof (String s, int radix)
Returns an integer object in which the cardinality provided with the second parameter is saved
The value extracted from the specified string when parsing. (6) Type conversion
INT--Integer
int num = 20;
A:integer i = new Integer (num);
B:integer i = integer.valueof (num);
Integer--INT
Integer i = new integer (20);
A:int num = I.intvalue ();
INT--String
int num = 20;
a:string s = string.valueof (num);
b:string s = "" +num;
c:string s = integer.tostring (num);
String--INT
String s = "20";
A:int num = Integer.parseint (s);
B:integer i = new Integer (s); or Integer i = integer.valueof (s);
int num = I.intvalue ();
5. Assemble frame:
(1) Why the collection class appears.
Object-oriented embodiment of things are in the form of objects, in order to facilitate the operation of multiple objects, the object is stored. A collection is one of the most common ways to store objects. (2) The array and the collection are all containers, and they are different.
The array length is fixed, and the collection length is a mutable array value that can store the object and store the base data type; The collection can only store object arrays storage data types are fixed, and collections store data types that are not fixed (3) The characteristics of the collection class:
A collection can store only the length of the object collection is a mutable set that stores different types of objects (4) Collection Class Framework (important ...). To distinguish between several containers:
Collection: Top-level interface |--->list: list, elements are ordered (elements with a corner index), can have duplicate elements, can have null elements. |--->arraylist (JDK1.2): the underlying data structure is the array , the characteristics of the query speed (because with the angle), but the speed is slightly slower, because when the element for a long time, additions and deletions of an element, all elements of the angle must change the thread is not the same step. The default length is 10, and when the length is exceeded, the collection length is extended by 50%.
|--->linkedlist (JDK1.2): the underlying data structure of the linked list data structure (that is, one of the following elements record before), features: Slow query speed, because each element only know the first element, but added and deleted faster because of the element more, adding and deleting one, The thread is not synchronized as long as the elements back and forth are connected again. |--->vector (JDK1.0): the underlying data structure is an array of data structures. It is characterized by the slow speed of query and deletion. The default length is 10, and when the length is exceeded, the collection length is extended by 100%. Thread synchronization. (vector function is identical with ArrayList function, has been replaced by ArrayList)
List use attention.
|--->arraylist:
(1) When the ArrayList inside the element is not required, that is, only ordered on the line, (2) when the elements into the ArrayList requirements do not repeat, such as the deposit of student objects, when the same name as the same person, then do not store inside. When you define a student object, you need to copy the Equals method public Boolean equals (object obj) {if (! obj instanceof Student)) return false; Student stu = (Student) obj; Return This.name.equals (stu.name) &&this.age==stu.age; To the ArrayList collection by add to the student object, the collection at the bottom of their own will call the Student class Equals method, Judge repeat students are not deposited. Note: For the list collection, whether add, contains, or remove methods, determine whether the elements are the same, are by the replication Equals method to judge.
|--->linkedlist
(1) Linkledist-specific method: Boolean Offerfirst (e e) Inserts the specified element at the beginning of this list. Boolean offerlast (e e) Inserts the specified element at the end of this list. E Peekfirst () gets but does not remove the first element of this list, or returns null if the list is empty. E Peeklast () gets but does not remove the last element of this list, or returns null if the list is empty. E Pollfirst () gets and removes the first element of this list, or returns null if the list is empty. E Polllast () gets and removes the last element of this list, or returns null if the list is empty. (2) through the unique method of linkledist, some data can be accessed in special way, such as stack and queue.
In general, what kind of implementation classes are used under the list interface? If you want to do this quickly, consider using LinkedList if you require a quick query, consider using the ArrayList if you require thread safety, consider using vectors.
|--->set: collection, elements are unordered (because there are no indexes) and elements cannot be duplicated. can have null elements.
|--->hashset (JDK1.2): the underlying data structures are hash tables, fast access, unique elements, and threads that are not synchronized.
The only principle of the guaranteed element:
Determine whether the element's hashcode value is the same, and then determine whether the Equals method of the two element is true
(The custom elements that are stored in the hashset are to be overwritten hashcode and equals methods,
To ensure the uniqueness of the element. )
|--->treeset: the bottom data structure type two fork tree. You can sort the elements in a set collection. The element is orderly and thread is out of sync.
basis for ensuring element uniqueness: CompareTo method return 0
The first way to sort treeset: make the element itself comparable, such as eight basic data types or strings,
Implement Compareble interface, overlay CompareTo method,
This method is the natural order of the elements
The first way to sort TreeSet: When the element itself is not comparable (for example, when storing student objects) or
Comparison is not the comparison we need (like the length of the string),
At this point you need to make the collection itself customizable and comparative.
How to make the collection itself comparable. When the collection is initialized, you can
Let the collection have a comparative approach. That defines a class,
Implement comparator interface, overlay compare method.
Set Collection usage Considerations:
(1) HashSet: The hashcode of the elements that are stored in HashSet is different from the new way, but usually we define objects, such as student objects, although they are the two students of new, but when they are the same name and age, we think of the same object, In order to ensure the uniqueness of the element, we usually hashset the hashcode and the Equals method in the class that defines the object when we store the elements in the collection. public int hashcode () {return name.hashcode () +age*39; public boolean equals (Object obj) {if (! obj instanceof Student)) return false; Student stu = (Student) obj; Return This.name.equals (stu.name) &&this.age==stu.age; }
HashSet is how to guarantee the uniqueness of the element.
If the hashcode value of the two elements is different, the Equals method is not invoked
If the hashcode value of the two elements is the same, continue to determine if the equals returns true;
The hashcode and Equals methods are defined in the custom object class, but not by our manual invocation
Instead of storing elements in the HashSet collection, the bottom of the collection calls Hashcode and equals
It takes the object to judge itself, to determine whether two elements are the same element.
(2) Treeset:treeset requirements to the inside of the elements to have a comparative, otherwise it will be an error. The first way to sort treeset: Let the element itself have a comparative definition object class, implement the Compareble interface, and make a replication CompareTo method, which is the natural order of the element class Student implements comparable { private String name; private int age; Public Student (String Name,int age) {this.name=name: this.age=age;} public String GetName () {return name;} public int Getage () {return age.} public int compareTo (Object obj) {if (!) obj instanceof Student)) throw new RuntimeException ("Not a student object. "); Student stu = (Student) obj; int num = this.age-stu.age; if (num==0)