- Automatic unboxing of basic data and the mode of sharing meta-design
1.1 Automatic Boxing
An integer between -128~127, mounted in an area of memory.
Integers that exceed this range are installed in different memory areas.
1.2 Automatic unpacking
Integer i1=12;
System.out.println (I1 + 5);//17
1.3 Enjoy meta mode flyweight
| If a lot of small objects, they have a lot of the same things, then they can be turned into a thing, and something different becomes an external property, as a method parameter passed in. |
PackageCom.itcast.day1; Public classAutobox { Public Static voidMain (string[] args) {//between -128~127Integer i1=12; Integer I2=12; System.out.println (I1==I2);//true I1,i2 is placed in the same memory areaSystem.out.println (I1 + 5);// -//greater than 127Integer i3=134; Integer I4=134; SYSTEM.OUT.PRINTLN (i3= = i4);//false I3,i4 is placed in a different memory area//This is not automatic boxing, but instead of manually calling valueof this static method,//turns a primitive type integer into a wrapper type object. Integer i5=integer.valueof (15); Integer I6=integer.valueof (15); SYSTEM.OUT.PRINTLN (i5= = I6);//trueInteger i7=integer.valueof (156); Integer i8=integer.valueof (156); SYSTEM.OUT.PRINTLN (i7= = i8);//false /*** Within the packaging type, the use of the module, that is: * * If a lot of small objects, they have a lot of the same things, * that can turn them into a thing; * There are different things that become external properties that are passed in as arguments to the method. */ } }
2. Java Enumeration
What is the 2.1 enumeration? Why use enumerations?
What is the definition of a variable that defines the day of the week or gender?
Let's say that 1-7 represents Monday to Sunday, but someone might write an int weekday=0;
Enumeration is to make a variable of a type only one of several fixed values, otherwise the compiler will error.
Enumeration allows the compiler to control the illegal values that are filled out in the source program at compile time, which is not possible in the development phase of normal variables.
2.2 Using the normal class how to implement the enumeration function, define a weekday class to simulate the enumeration function.
Private method of construction
Each element represents the static final with a common static member
There can be several common or abstract methods, for example, to provide the NextDay method must be abstract.
Using abstract methods to define NextDay is to convert a large number of if,else statements into a single class.
Impersonation Enumeration
PackageCom.itcast.day1; Public Abstract classWeekDay {//Privatization Structure PrivateWeekDay () {}//abstract method, put in subclass to implement Public AbstractWeekDay nextday (); Public Final StaticWeekDay su=NewWeekDay () {@Override PublicWeekDay NextDay () {returnMO; } @Override PublicString toString () {return"SU"; } }; Public Final StaticWeekDay mo=NewWeekDay () {@Override PublicWeekDay NextDay () {returnTU; } @Override PublicString toString () {return"MO"; } }; Public Final StaticWeekDay tu=NewWeekDay () {@Override PublicWeekDay NextDay () {returnWE; } @Override PublicString toString () {return"TU"; } }; Public Final StaticWeekDay we=NewWeekDay () {@Override PublicWeekDay NextDay () {returnTH; } @Override PublicString toString () {return"WE"; } }; Public Final StaticWeekDay th=NewWeekDay () {@Override PublicWeekDay NextDay () {returnFR; } @Override PublicString toString () {return"TH"; } }; Public Final StaticWeekDay fr=NewWeekDay () {@Override PublicWeekDay NextDay () {returnSA; } @Override PublicString toString () {return"FR"; } }; Public Final StaticWeekDay sa=NewWeekDay () {@Override PublicWeekDay NextDay () {returnSU; } @Override PublicString toString () {return"SA"; } }; }
Package Com.itcast.day1; // Test Public class enumtest { publicstaticvoid main (string[] args) { WeekDay WeekDay=weekday.mo; System.out.println (WeekDay); System.out.println (Weekday.nextday ()); } }
Basic application of 2.3 enumeration
PackageCom.itcast.day1; Public enumWeekDay2 {sun,mon,tus,wen,thi,fri,sta;} PackageCom.itcast.day1; Public classSunenumtest { Public Static voidMain (string[] args) {WeekDay2 WeekDay=Weekday2.mon; System.out.println (WeekDay);//MONSystem.out.println (Weekday.name ());//MONSystem.out.println (Weekday.ordinal ());//1System.out.println (weekday.valueof ("MON"));//MONSystem.out.println (Weekday.values ());//An array of enumeration elements [lcom.itcast.day1.weekday2;@133c5982System.out.println (Weekday.values (). length);//Number of elements 7 } }
2.4 Implementing an enumeration with construction methods
//enum
package com.itcast.day1;< /span>public enum WeekDay2 {SUN ( 2), MON (), Tus,wen,thi,fri,sta; // guaranteed to be written at the front // construction needs to be private, not known by outsiders private WeekDay2 () {System.out.println ("First");} // constructor without arguments private WeekDay2 (int day) {System.out.println ("Second");} //
}
Test
Package Com.itcast.day1; Public class sunenumtest { publicstaticvoid main (string[] args) { WeekDay2 WeekDay=weekday2.mon; }}
Results Print:
Second
First
First
First
First
First
First
- Enumerations are equivalent to a class, which can also define construction methods, member variables, common methods, and abstract methods.
- The enumeration element must be at the very beginning of the enumeration body, used after enumerating the list of elements, and separated from other members.
- If you put the member method or variable in the enumeration before the enumeration element, the compiler error.
Enumeration of construction methods
- The construction method must be defined as private.
- If you have more than one construction method, how do you choose which construction method?
- The enumeration elements, like the effect of Mon and Mon (), call the default constructor method.
Enumeration with methods
- Defining enumerations Trafficlamp
- Implementing the normal Next method
- Implement the abstract next method: Each element is an instance object generated by the enumeration class, respectively, that is defined by a method similar to an inner class.
- To increase the construction method on the representation time
- When an enumeration has only one member, it can be implemented as a single instance.
- The construction method does not have to write itself, the default is private without parameters.
- Newinstance does not have to write on its own, it is created when the enumeration is initialized.
PackageCom.itcast.day1; Public classEnumtest { Public Static voidMain (string[] args) {Trafficlamp TFL=trafficlamp.red; System.out.println (TfL); System.out.println (Tfl.nextlamp ()); } Public enumtrafficlamp{RED (30) {@Override PublicTrafficlamp Nextlamp () {returnGREEN; }}, GREEN (45) {@Override PublicTrafficlamp Nextlamp () {returnYELLOW; }}, YELLOW (5) {@Override PublicTrafficlamp Nextlamp () {returnRED; } }; Private intTime ; Public AbstractTrafficlamp Nextlamp (); PrivateTrafficlamp (intTime) { This. time=Time ;} @Override PublicString toString () {return This==red? " RED: "+ This. Time: This==green? " GREEN: "+ This. Time: "YELLOW:" + This. Time; } } }
Java Basics-Temperature so new (02)