1. Automatic box packing and unpacking.
The wrapper class in Java.
Wrapper classes are wrapping basic data in Java, and you can wrap a base type into a class type.
Basic data types
Four categories of eight.
1. Integer byte short int long
2. Float type float double
3. Character Char
4. Boolean Boolean
Packing class
Byte Short Integer Long
Float Double
Character
Boolean
Automatic unpacking
Assigning an Integer object directly to an int
Automatic Boxing
Assigns an int directly to an integer.
For an integer class that is automatically boxed, if the int value is between -128----127 A value that is taken out of the Integercache,
If not within this range is the new Integer ()
-------------------------------------------------------------
Enhanced for Loop
Its underlying implementation is the use of iterator.
What containers can use enhanced for? -----------iterable
The disadvantage of enhanced for is that it can only be traversed and cannot be manipulated during traversal.
Enhanced for format
For (type reference: Container) {
}
--------------------------------------------
About iterating over a collection.
List
1.Iterator
2. Enhanced for
3. General for
4.ListIterator
Set
1. Enhanced for
2.Iterator
Map
1.keySet---Get a set view of all keys
2.entryset--get the set view of Key-value
-------------------------------------------------------------------------
Variable parameters:
Format Type ... Variable
Essentially, this parameter is an array.
Pros: 1. You can pass an array when you pass a parameter, or you can pass multiple values.
2. You can not pass a value.
Arrays.aslist () can convert an array to a list collection.
Note: A fixed-length list collection is obtained.
Cause: Because the array length is fixed, the collection is converted from an array.
Why would you want to convert an array into a collection?
Provides richer operations in the collection.
Int[] arr = {1, 2, 3};
List List = Arrays.aslist (arr);
System.out.println (List.size ());
SYSTEM.OUT.PRINTLN (list);
Converts a int[] into a list collection using the Arrays.aslist method
The resulting set length is 1, the element in the collection is int[] why?
The elements in the collection are object.
Because the elements in the collection we are converting now are of type int and cannot be loaded directly into the collection (nor boxed)
The array object is then made to the collection object. List<int[]>
----------------------------------------------------------------------------------
Static import (Know)
Format: import static package name. Class Name. Method name (attribute name);
You can use this static member in a class without the class name. Simply write the method name or the property name as you like.
Static Import Disadvantages:
1. Code readability decreased.
2. Due to the repetition of the method name, static import will error in some specific cases.
----------------------------------------------------------------------
Generics (Focus)
What is a generic type?
A qualification for the data type.
The most applied place is the collection.
Why use generics?
1. Improve the security of our programs.
2. Solve the problem of the run phase in the compile phase.
3. No type casts are necessary
-----------------------------------------------
We learn the purpose of generics: The generics defined on the collection are used.
1. Basic use of generics on a collection.
Collection<e>
List<e>
Set<e>
Their role is to limit the types of elements in the collection.
Can we define generics on the class or on the interface ourselves?
OK. How to define?
Class name <t>{
}
How is a generic declared on a class used?
Member of the Class: member property---member method.
Member Properties: Class Property (Static) instance property (non-static)---Our Tube instance property is called a member property.
Member method: Class Method Instance method (member method)
A generic type declared on a class can be applied to a member method or member property.
Generics declared on a class cannot be used on static members of a class?
Cause: is a generic declared on a class that can only be passed by creating an object in.
A static member can be called without the need for an object.
Can we declare generics on a static method?
OK
Format
Modifier < generic > Return value Method Name () {}
Generics are applied to the design phase.
-----------------------------------------------------------------------------
Apply a generic advanced on a collection
A wildcard character. ?
About the app in the collection.
1.? Extends E
Represents the subclass type of type E or E.
Application: The AddAll method in the collection interface
2.? Super E
Represents the parent class type of type E or E.
Application: On the construction method of the TreeSet.
TreeSet Collection:
It is a set set that can be sorted.
TreeSet How to sort the collection?
1. Natural order
As long as this interface is implemented comparable, it means that the element of this type has a natural order
2. Comparator.
The class that implements this interface comparator can be compared according to the specified criteria.
When you specify a comparer, you can use a comparison of the parent class if the elements in the collection are compared according to the attributes in the parent class.
------------------------------------------------------------------
Generic erase mechanism
Written question:list<string> list=new arraylist<string> (). Requires an integer value to be added to the collection 10
list<string> list = new arraylist<string> ();
List.add (10);
List A = list;
A.add (10);
SYSTEM.OUT.PRINTLN (list);
Above, do not use in the written test, but use reflection to complete.
Generics are valid during the compile phase and have no effect at run time.
--------------------------------------------------------------------------------------------------------------- ----
Enumeration
What is an enumeration?
It is a special Java class.
Definition of Class
Class Name {}
Definition of the interface
Interface interface Name {}
Definition of enumeration
Enum enum Name {}
Example
public enum demo1{} also generates a class file.
We decompile it.
Define to the directory where our class files are executed Javap Demo1
The results obtained
Public final class Demo1 extends Java.lang.enum<cn.itcast.enumdemo.demo1> {
static {};
public static demo1[] values ();
public static Demo1 valueOf (java.lang.String);
}
Enumeration It is a Java class that inherits the Java.lang.Enum class.
What is the use of enumerations?
In our development, some data must be limited to a certain range of values, not beyond this range, then you can use the enumeration.
It's simpler than our traditional code writing.
------------------------------------------------------------
Syntax for enumerations
The enumeration class is also a special form of Java class.
Each enumerated value declared in an enumeration class represents an instance object of an enumeration class.
As with normal classes in Java, when declaring an enumeration class, you can also declare properties, methods, and constructors, but the constructor of the enumeration class must be private (this is not difficult to understand).
If the enumeration class has only one enumeration value, it can be used as a single-state design pattern.
Note: You can declare methods, properties, and constructor methods in enumerations. The construction method must be private.
If there is only an enumeration value in the enumeration, the last enumeration value may not be written after the end character.
If there are other members, you must add ";" to the last enumeration value.
1. The construction method must be private and can be overloaded.
2. Declare the property in the enumeration, declaring the method.
---------------------------------------------------
1: Enumeration is abstract by default and cannot be instantiated.
2: Abstract extraction method can be used in enumerations.
3: All members of the enumeration are the public static final type by default. And you must start the Declaration on the first line. Must be in, (comma) separate.
4: All enumerations, by default, are java.lang.Enum.
The 5:enum class is not only the public base class for all enumerations, but also a tool class.
Enum------It is used to declare an enumeration class.
An enum------A class in Java that represents the parent class of all enumerations.
Enumeration----equivalent to iterator traversal collection
--------------------------------------
An abstract method can be declared in an enumeration class.
If an abstract method exists in an enumeration class, these abstract methods must be overridden for each enumeration value.
-------------------------------------------------------------------------------
The enumeration method.
1. The enumeration class comes with
VALUES ()-----Get all the enumeration values in the enumeration class
ValueOf ()------
2. The enumeration class inherits from the Java.lang.Enum class.
The name method---Get the string representation of the enumeration value.
Ordinal ()----Get the ordinal of the enumeration value
ValueOf (Class c,string name) converts a string to an enumeration value of the specified enumeration type
----------------------------------------------------
Enumeration Knowledge Point Mastery
1. When the enumeration is used, how to define the enumeration.
2. What can be included in an enumeration
3. Methods that are commonly used in enumerations.
------------------------------------------------------------------------------------------------------------
Reflection:
What is reflection?
Anatomy of each component in a Java class is mapped into a Java object
Why use reflection?
More for frames and components, write-up common programs with high reusability
1.Class class.
The class represents our bytecode file.
Three different ways.
1. Class name.
2. Object. GetClass ();
3.class.forname ();
Create in development using a third class.forname (); why?
This method has low coupling and no dependence.
------------------------------------------
2.Constructor (Constructor object)
The role of the constructor is to create objects.
Constructor represents the constructor object, which we can instantiate after we get it.
1. How to get constructor object.
GetConstructor ()-----Get the specified constructor
GetConstructors ();---get all the constructors
2. Notes on constructor
1. If the construction method is not public. Then to get its constructor object use the Getdeclaredconstuctor () method.
2. For privatization permissions, a permission check must be set before use.
public void Setaccessible (Boolean flag)
A value of TRUE indicates that the reflected object should cancel the Java language access check when it is used
3. In development, when we get the class, we typically instantiate the object of the class directly.
2.Field
3.Method
1.
2. How is the static method called?
Static method calls do not need to use objects, write null directly
Staticmethod.invoke (null, parameter);
How do I invoke the parameters of the method if it is an array?
When invoking invoke, the second argument is cast to object, or the outer layer is wrapped in an array.
------------------------------------------------------------------------------
jdk1.5 characteristics