Packing class:Wrapper classes for basic data types
Basic data types |
Packing class |
Byte |
Byte |
Short |
Short |
Int |
Integer |
Long |
Long |
Char |
Character |
Float |
Float |
Double |
Double |
Boolean |
Boolean |
Basic data type wrapper class in addition to the character class, the other 7 have two construction methods
One of the constructor methods is the basic data type itself, and the other method of construction is the String type, and Character less is this one, that is, public Character (String c) {}; The wrapper class Boolean operation is Boolean B = new Boolean ("true"), and the other is false by default except for TRUe and case-insensitive;wrapper class to basic type:Call the base data type value () Method Example:integer a = new Integer ();//The Integer wrapper class is called Intvalue () to successfully convert to int data typeint b = A.intvalue (); basic type to wrapper class:Directly new to its wrapper class, for example:integer a = new integer (+);JAVA5 began to provide the basic types of packaging class to the basic type of the class to wrap the basic type of the wrapper class can assign a value of a basic data type directly to its wrapper class object example:Integer a = +;wrapper class to basic typeYou can assign a wrapper class object directly to a variable of a basic data typeint b = A;is the value of integer equal? 1. When the assignment is as follows: integer a = new integer (16); Integer a2 = new Integer (16); System.out.println ("a1 = = A2" + (A1 = = A2)); False if you use this mode of creating objects then the integer values do not want to wait because they are two objects 2. If you use the new feature of Java5: Integer a = 16; Integer A2 = 16; System.out.println ("a1 = = A2" + (A1 = = A2)); True if you use this pattern then they are only equal, but their values are equal only within this [-128,127] interval byte,short,int,long their buffers are the same [-128,127]; Type conversions: String------ ---> int static int parseint (String s);You can use the parseint (string s) method to parse a string argument as a decimal integer
1 //string------> int2 inti = Integer.parseint ("0012");3SYSTEM.OUT.PRINTLN ("string------> int" + "\ T" +i);4 5 //int------> String integer class static string toString (int i);6 //String class public static string valueOf (int i)7 //using tostring (int i), the valueOf (int i) method can implement int------> string8 9 TenString s =integer.tostring (i); OneSYSTEM.OUT.PRINTLN ("int------> string" + "\ T" +s); A - intI1 = 123; -SYSTEM.OUT.PRINTLN ("int------> string" + "\ T" +string.valueof (I1));
Object classThe public parent class of all the parent class, once a class does not show the inheritance of a class, its immediate parent class must be object, all objects (including arrays) implement the method of this class all data types can receive common methods with object: Boolean equals (Object obj) Indicates whether an object is equal to this objectequals is the default comparison of the address in the heap, like = =, are compared to the address= = Used to determine if two variables are equal must point to the same object is true, only two objects that have a parent-child or peer relationship can be compared
1System.out.println (NewInteger (1). Equals (NewInteger (1)));//true2 //because the integer compares the value, the integer class overrides the Equals method of the parent class of the object class3 //the method of the integer class after overriding Equals method4 Public Booleanequals (Object obj) {5 if(objinstanceofInteger) {6 returnValue = =((Integer) obj). Intvalue ();7 }8 return false;9 }TenSystem.out.println (NewObject (). Equals (NewObject ()));//false One //the other string class also overrides the Equals () method A intHashcode ()//returns the hash code value of the object, with a different hashcode value for each object - -System.out.println (NewObject (). Hashcode ());//1599065238 theSystem.out.println (NewObject (). Hashcode ());//1879096508
String toString () returns the text of the object that describes the state of the object as a string. The ToString () method is called by default when you print an object directly
Packing class, Object class--java notes (eight)