/*** Packaging class/Package class: Convert basic data types to objects! * Each basic data type has a corresponding wrapper class! are located in the Java.lang package! * Function: Provide us with a series of methods of operation! * Example: Can the basic data type be stored in the collection??? No! Must be a wrapper class! * * Basic Data type wrapper class * Byte byte * Short short * int Integer * Long long * float float * Double Double * Char Character * Boolean Boolean **/ Public classbasistest {@Test Public voidtest01 () {//all wrapper classes have the corresponding basic data type as Parameters! To construct an instance! Integer i=NewInteger (1); Double D=NewDouble (5); Boolean b=NewBoolean (true); Character C=NewCharacter (' a '); } @Test Public voidtest02 () {//In addition to the character class, all other wrapper classes can pass a string as a parameter! Integer i=NewInteger ("1"); Double D=NewDouble ("5"); Boolean b=NewBoolean ("true"); //Character c=new Character ("a"); Compile error } /** Byte,short,integer,float,double,long * All inherit the number of the parent class * When their arguments are string to construct an instance, if passing null is thrown numberform atexception * * Boolean NULL is able to run normally * Character compile the error*/@Test Public voidtest03 () {//integer i=new integer (null); //double d=new double (null); //Character c=new Character (null); Compile error//when the argument is of type string, except for the case of true, the rest is falseBoolean b=NewBoolean (NULL); System.out.println (b); } @Test Public voidtest04 () {String num= "12345a"; SYSTEM.OUT.PRINTLN (Num+1);//123451//paseint with Integer (string of numeric type)SYSTEM.OUT.PRINTLN (Integer.parseint (num) +1); } /** VALUEOF () only pass values that can be converted to the corresponding base data type * Except for the character class, there are methods for other wrapper classes (string, wrapper class)*/@Test Public voidtest05 () {Integer I=integer.valueof (20); Integer a=integer.valueof ("20"); Integer b=integer.valueof ("abc");//NumberFormatExceptionInteger c=integer.valueof ("20.0");//NumberFormatException } //conversions between basic data types and corresponding wrapper classes we call boxing and unpacking operations.@Test Public voidtest06 () {intA =5; Integer I=NewInteger (a);//equivalent to boxingA=i.intvalue ();//equivalent to unboxingInteger b=5;//equivalent to boxing intNum=b;//equivalent to unboxing } //All packaging classes have a corresponding method called wrapper class value () Effect: Convert to basic data type (unboxing)@Test Public voidtest07 () {Double a=NewDouble (50); Double b=NewDouble (50.5); Double C=NewDouble ("50.5"); DoubleD=c.doublevalue ();//Convert to basic data typeSystem.out.println (d); Float F=NewFloat (58.0); floatE=f.floatvalue ();//Convert to basic data typeCharacter T =NewCharacter (' a '); System.out.println (T.charvalue ()+1); Boolean L=NewBoolean ("true"); System.out.println (L.booleanvalue ()); } @Test Public voidtest08 () {/** In addition to character, each wrapper class has a corresponding parse method*/System.out.println (Integer.parseint ("123")); System.out.println (Double.parsedouble ("123")); System.out.println (Boolean.parseboolean ("123")); System.out.println (Long.parselong ("123")); /*int n =null; Compile error Integer an=null;*/} @Test Public voidtest09 () {Integer a=50; Integer b=NewInteger (50); intC=50; System.out.println (A==B);//falseSystem.out.println (C==B);//true both the base data type and the corresponding wrapper class comparison are trueSystem.out.println (C==a);//true} @Test Public voidtest10 () {/*integer a=new Integer (50); Integer b=new integer (50); System.out.println (A==B); False*/ /*Integer a=127; Integer b=127; System.out.println (A==B); True*/Integer a= 128;//the bottom layer executes the valueOf () New Integer (Integer b=128; System.out.println (A==B);//false} @Test Public voidtest11 () {System.out.println ("Fetch value up:" +math.ceil (50.1)); System.out.println ("Value down:" +math.floor (50.2)); System.out.println ("Absolute Value:" +math.abs (-50)); System.out.println ("Maximum value:" +math.max (50, 60)); System.out.println ("Minimum Value:" +math.min (50, 60)); System.out.println ("Random Number:" + (int) (Math.random () *10)); Random Random=NewRandom (); for(inti = 1; I <=100; i++) {System.out.println (Random.nextint ()); //interval of int value} System.out.println ("*********************"); for(inti = 1; I <=100; i++) {System.out.println (Random.nextint (100));//0-99 does not contain } } //Random number@Test Public voidtest12 () {Scanner Scanner=NewScanner (system.in); System.out.println ("Please enter a 4-digit number:"); String Num=Scanner.next (); //converts a string type num to int intrealnum=integer.parseint (num); //Get hundreds intBai=realnum/100%10; Random Random=NewRandom (); //Define a tag Booleanflag=false; for(inti = 1; I <=100; i++) { intA= Random.nextint (10); System.out.println ("+i+" random number of times: "+a); if(a==Bai) { //found theflag=true; Break; } } if(flag) {System.out.println ("Winning."); }Else{System.out.println ("Next effort ..."); } } }
Wrapper classes for basic data types