The basic types of Java wrapper class Integer class how to use?

1, an overview of
the Integer class wraps a basic value in an object type int
Integer class provides a number of methods that can convert between types of type int and String
Package zhengshu;
public class IntegerDm {
public static void main (String [] args) {
System.out.println (Integer.toBinaryString (100)); // converted into a binary number: 1100100
System.out.println (Integer.toOctalString (100)); // convert octal number: 144
System.out.println (Integer.toHexString (100)); // converted to hexadecimal numbers: 64
System.out.println ( "----------------------- ---- ");
System.out.println (Integer.MAX_VALUE); // int representative of the maximum value that can be represented
System.out.println (Integer.MIN_VALUE); // int can represent the minimum value of the representative
}
}
String converted to an int type:
Package zhengshu;
public class IntegerDm {
public static void main (String [] args) {
int I1 = 100;
System.out.println ( "i1 values:" + I1);
Integer Integer new new = I2 (I1);
System.out.println ( "I2 values:" + I2);
String S1 = "100";
Integer i3 = new Integer (s1) ; // converted into a digital string, the string is provided by an array of characters
System.out.println ( "i3 values:" + I3);
}
}
2, constructor
public Integer (int value)
public Integer (String S)
. 3, member methods
public int intValue ()
public static int the parseInt (String S)
public static String toString (int I)
public static Integer valueOf (int I)
public static Integer valueOf ( S String)
Package zhengshu;
public class IntegerDm {
public static void main (String [] args) {
// int -> Stirng
int I1 = 100;
S1 = String "" + I1; // int -> Stirng
System.out.println (S1); // output string
String s2 = String.valueOf (i1); // int -> Stirng, returns the given parameter Number object original value
System.out.println (S2);
Integer Integer new new = I2 (I1);
System.out.println (I2);
String S3 = Integer.toString (I1);
System.out.println (S3);
System.out.println ( "------------------------");
// String -> int
String ST1 = "200 is";
Integer IN1 = Integer new new (ST1);
System.out.println (IN1);
int in1.intValue IN2 = ();
System.out.println (IN2);
System.out.println (the Integer.parseInt (ST1)); // the parseInt () method is used to string argument as a signed decimal integer parsing
}
}
4, common basic binary conversion
public static string toBinaryString (int i)
toOctalString static String public (I int)
public static String toHexString (I int)
. 5, the decimal to hexadecimal other
public static String toString (I int, int the radix)
. 6, hexadecimal to decimal other
public static int parseInt (String s , the radix int)
Package zhengshu;
public class IntegerDm {
public static void main (String [] args) {
System.out.println (Integer.toBinaryString (100)); // binary decimal turn
System.out.println (Integer.toOctalString (100)); // octal, decimal turn
System.out.println (Integer.toHexString (100)); // convert hexadecimal to decimal
System.out.println ( "----------- ---------------- ");
// decimal go to another band
System.out.println (Integer.toString (100,2)); // turn decimal binary
System. out.println (Integer.toString (100,8)); // octal, decimal turn
System.out.println (Integer.toString (100,16)); // convert hexadecimal to decimal
System.out.println (Integer.toString (100,7)); // Decimal turn septenary
System.out .println (Integer.toString (100,30)); // turn three decimal decimal
System.out.println ( "======================== ==== ");
// other binary to decimal
System.out.println (Integer.parseInt (" 100 ", 10)); // 10 decimal digits that are 100, 100
System.out.println (Integer.parseInt ( "100", 2 )); // the binary number is 100,4
System.out.println (the Integer.parseInt ( "100", 8)); // 8 into that figure system is 100,64
System.out.println (the Integer.parseInt ( "100", 16)); // hexadecimal numbers that are 100,256
}
}
7, the automatic boxing and unboxing
Integer x = new Integer (4); can be written Integer x = 4; (autoboxing)
X = X +. 5; (automatic unpacking), by the method intValue
JDK 1.5 new features (autoboxing): the basic types to wrapper class type
JDK 1.5 new features (automatic unpacking): to convert the base type packaging type
zhengshu Package;
public class IntegerDm {
public static void main (String [] args) {
byte B1 = 100;
byte B2 = B1;
Integer I1 = new new Integer (200 is); // define a wrapper type variable int I1
Integer I2 300 =;
I2 = I2 + 400;
System.out.println ( "I2 values:" + i2); // output: 700
Integer Integer.valueOf I3 = (500); // autoboxing
i3 = Integer. valueOf (i3.intValue () + 60) ; // automatically unpacking, then autoboxing
System.out.println ((new StringBuilder ( "value of i3:")) append (i3) .toString ().) ; // output: 560.
}
}
Package zhengshu;
public class IntegerDm {
public static void main (String [] args) {
Integer I1 = new new Integer (128);
Integer I2 = new new Integer (128);
System.out.println ( i1 == i2);
System.out.println (i1.equals (I2));
System.out.println ( "-----------------------");
Integer I3 = 128;
Integer I4 = 128;
System.out.println (== I3 I4);
System.out.println (i3.equals (I4));
System.out.println ( "========== =============== ");
Integer i5 = 127;
Integer I6 = 127;
System.out.println (i5 == I6);
System.out.println (i5.equals ( I6));
System.out.println ( "+++++++++++++++++++++++++");
int i7 = 1280;
Integer I8 = 1280;
the System .out.println (i7 == I8);
// data for -128 to 127, a data buffer pool made, if the data is within this range, each time does not create a new space
}
}
Note:
Integer data direct assignment, if between -128 to 127, retrieves data directly from the buffer pool
in use, Integer x = null; a NullPointerException above code will appear (null pointer exception)
8, the basic data types in order to more operations, Java class corresponding to the type provided for each of the basic data types: the type of packaging.
 Byte byte
 Short Short
 int Integer
 Long Long
 a float the Float
 Double Double
 char Character
 Boolean Boolean
JDK5 new features added (examples are given below):
 autoboxing: Basic types into packaging type
 automatic unpacking: the packaging type conversion basic type
NOTE:
 when using Integer x = null set if code null pointer exception.
9, Integer type static member methods (mainly used D, E two methods)
 A, String public static toBinaryString (int I)
 B, static public String toOctalString (int I)
 C, static public String toHexString (int I)
 will int type is converted to common basic types such as binary string: Integer.toHexString (100)
 D, static public string toString (I int, int radix) the int type into a string of binary radix specified type, into range of 2 to 36 prepared as 0-9, az a total number of 36,
 E, public static int parseInt (String s, int radix) to a string of decimal radix hex
 F, public static int parseInt (String s) will be converted to an int type string, similar methods may be implemented using a string type is converted to the basic type, int-String type conversion following examples have described.
 G, public static String toString (int i) XM rebate www.fx61.com/brokerlist/xm.html the int type into a string type
 H, public static Integer valueOf (int i) is converted to an int type Integer type
 I , public static Integer valueOf (String s ) String to convert the type Integer type
10, Integer type static member variable
 Integer.MAX_VALUE // int maximum range
 Integer.MIN_VALUE // int minimum range
11, Integer class method members
 public int intValue (); return to the int type Integer type
12, Character type
 Character class wraps a primitive type char object in a single field,
 public static Boolean isUpperCase (char CH) uppercase //
 public static boolean isLowerCase ( char ch) // lowercase
 public static boolean isDigit (char ch) // digital
 public static char toUpperCase (char ch) // converted to uppercase
 public static char toLowerCase (char ch) // lowercase characters
13, int and String mutual conversion examples
package test07_Integer ;
conversion of type String and // int
//
public class IntegerDemo {
public static void main (String [] args) {
// INT-> String
int Number = 96;
// mode. 1
String S1 = "" Number +;
System.out.println ( "S1:" + S1);
// embodiment 2 (common)
String String.valueOf S2 = (Number);
System.out.println ( "S2:" + S2);
// embodiment. 3
String Integer.toString = S3 (Number);
System.out.println ( "S3:" + S3);
// INT- embodiment. 4> Integer -> String
Integer Integer.valueOf I = (Number);
I.ToString S4 = String ();
System.out.println ( "S4:" + S4);
// Sting-> int
String S = "96";
// a manner
int i1 = Integer.parseInt (s);
System.out.println ( "I1:" + I1);
// embodiment 2
Integer Integer.valueOf II = (I1);
int ii.intValue I2 = ();
System.out.println ( "I2:" + I2) ;
// Note that this method, if a string type converted to float
float Float.parseFloat as F = (S);
System.out.println ( "F:" F +);
// Similarly string into a substantially type package can call the static method corresponding to the type of the basic types
// basic types of x = basic type of package type .parse (String S);
}
}
14, automatically unpacking, storage instance automatic packing:

test07_Integer Package;
public class IntegerDemo2 {
public static void main (String [] args) {
// autoboxing: the basic data types into packaging type
Integer II = 100;
// automatically unpacking: the packaging type to the basic type
II + = 200 is;
System.out.println ( "II:" + II);
// Integer data direct assignment reason, if between -128 and 127 can obtain data directly from the data pool, not within this range data need to call new integer () method to create the object
Integer ii1 = 127;
Integer ii2 = 127;
// comparison values and ii1 ii2 of
System.out.println (ii1.equals (ii2)); // to true
// Comparative ii1 and ii2 address value
System.out.println (II1 == ii2); // to true
Integer II3 = 128;
Integer II4 = 128;
System.out.println (ii3.equals (II4)); // to true
the System.out .println (== II3 II4); // to false
}
}

Guess you like

Origin blog.51cto.com/14511863/2439230