JDK 1.5

Source: Internet
Author: User
Tags deprecated iterable

1995.5.23 the birth of the Java language

While Sun launched the Java language, it also launched a series of development tools, such as the JDK
(Java Development Kit)
Jvm
Java API


Time Version API usage
1996 JDK1.0 250 is primarily used in the development of desktop-based applications and applets
1997 JDK1.1 500 graphical user interface programming
1998 JDK1.2 2300 J2se ee j2me
JDK1.3
JDK1.4
2004 JDK1.5 3500 Tiger language itself has changed a lot
More stable, safer and more efficient
2005 JDK1.6
JDK1.7

javaSE1.5

Java5 new Features:
Automatic Packing/Unpacking
Enhanced for Loop
Type-Safe enumeration
Variable length parameters
Static Import
Formatted output
Generic type
Annnotation (note)

First, autoboxing/unboxing
-----------------------------------------------------------------
1. What are boxing and unpacking operations?
Boxing action: Converts the base data type to the wrapper class it corresponds to
Unpacking operations: Converting wrapper classes to corresponding base data types

Wrapper class:
Located under the Java.lang package

Wrapper classes for 8 basic data types
BYTE byte
Short Short
int Integer
Long Long
Char Character
float float
Double Double
Boolean Boolean

Wrapper classes fall into two categories
Sub-class of 1.Number:
Byte, short, Integer, Long, Float, Double
2. Other types:
Character, Boolean

Number class
Intvalue ()
Doublevalue ()
.......

*value () returns the base data type represented by the reference type
Character
Charvalue ()
Boolean
Booleanvalue ()

2. How do I carry out the packing and unpacking operation?
int a = 10;
Integer B = null;
b = New Integer (a);
A = B.intvalue ();

3. Automatic box packing/unpacking
Auto-Boxing: Basic data types are automatically converted to wrapper classes (Int--integer)
Auto-Unpacking: The wrapper class is automatically converted to its corresponding base data type (Integer--int)

4. Why do I need a packing/unpacking operation?
Automatic boxing/unpacking operations greatly simplifies the use of basic data types and reference types

5. Problems that exist
Automatic unpacking:
Call the *value () method, same as manual unpacking
Automatic Boxing:
is not done by means of new, by calling the ValueOf () method
Cache Pool
integer-127~128
short-127~128
long-127~128
Byte Cache All
Boolean All Cache
Float is not cached
Double is not cached
Character only the characters Ascii/unicode encoded <127 are cached


Second, enhanced for loop
-------------------------------------------------------------------
For loops that appear before
for (int i=0;i<10;i++) {
Some operation
}

jdk1.5 introduces an enhanced for loop to a large extent simplifying our
Traversal of an array or collection
Grammar:
for (type Element:arr) {
SYSTEM.OUT.PRINTLN (Element);
}
Type: types, data types in the array or collection to be traversed
element: Elements, temporary variables when traversing
Arr: the reference to the array or collection to traverse

The collection must implement the Iterable interface
Collection inherits from Iterable interface

Ctrl+shift+o
alt+/Tips

Disadvantages:
1). The enhanced for loop itself cannot explicitly point to the index where the element is located
2). Forced type conversion required


Third, Type-safe enumeration
-----------------------------------------------------------------
JDK1.5 introduces a new type of "class"--enum type
Enum class Interface annotation

Why?
Define a class that represents the week
int weekday=0

An enumeration type can be introduced to control the illegal value of the source program, only a few
One of the fixed values, if beyond this range, the compiler is wrong in compiling the times.

public class Gender {
private String name;
Priavte static Final MALE = new Gender ("male");
Priavte static Final FEMALE = new Gender ("female");

Public Gender () {}

Private Gender (String name) {
THIS.name = name;
}

public static Gender getinstance (int i) {
switch (i) {
Case 0:
return MALE;
Case 1:
return FEMALE;
Default
return null;
}
}

Getter Setter
}
Gender G1 = Gender.male;
Gender G1 = new Gender ("male");
Gender g2 = new Gender ("female");
Gender g2 = new Gender ("No male or female");

What?
1). definition of enum type
public enum Gender {
Male,female;
}
Each of these enumeration elements is an instance of the enumeration type

2). Use of enumerations
Gender male = Gender.male;
Gender female = Gender.female;

3). Enumeration traversal
Each enumeration type provides two static methods by default values () and the valueof () method
For (Gender g:gender.values ()) {

}
The values () method returns a containing all enumerations in the enumeration type
An array of elements.
ValueOf (String name) can get the enumeration type by enumerating the elements
An instance of the.

4). The difference between EUNM and enum
A> Each enumeration type defined by using the Enum keyword inherits by default
to the Java.lang.Enum class.
The enumeration elements in the B> enumeration class are instances of the enumeration class, and the default
For public static final decoration.

The Enum class has only one protected construction method
Protected Enum (String Name,int ordinal) {}
Name: Represents the names of enumeration elements
Ordinal: The number representing the enumeration element, numbering starting from 0, according to the declaration
Sequential numbering
Name (): Gets the name of the enumeration element
Ordinal (): Gets the number of the enumeration element in the list

5) Properties and methods of enum types
You can define properties and methods in an enumeration class.
Rules:
Must be defined after an enumeration element declaration

6) How enumeration types are constructed
A. The construction method must be defined after the element declaration
B. The construction method can only be private, not write the default is private
C. element if you want to invoke a parameter-constructed method, you can add an element after the
"(parameter)"

7). Enumeration inheritance
The enum type implicitly inherits the Java.lang.Enum class by default, so it is no longer possible to
Inherit other classes.

8). Enumerate Implementation Interfaces
There are two ways of doing this:
1. Implement an interface in an enumeration class like the normal class
2. Each enumeration element implements this interface separately

9). Abstract method in Enumeration
You can define one or more abstract methods in an enumeration type, but each
Enumeration elements must implement these abstract methods separately

). Switch support for enumerations
Switch (byte)
switch (int)
Switch (short)
Switch (char)
Switch (enum type)

Red,yellow,green

11). Class set support for enumerations
Java.util under the Bag
Enummap
The map interface is implemented and the basic operation is the same as the map
The enumeration type that specifies the key value when the constructor method is called
Enumset
Implements the set interface
Structuring method Privatization
All of these methods are static methods and can be allof/of by ...
method to instantiate the Enumset


Iv. Variable Arguments
-------------------------------------------------------------------
Variable-length parameters allow us to declare a method that can receive a variable number of parameters
public int sum (int a,int b) {
return a+b;
}
public int sum (int a,int b,int c) {
return a+b+c;
}

public int sum (int[] a) {
Iterating and adding an array
An array must be declared first
}
Int[] A = new int[]{1,2,3,4};
Sum (a);
---------------------------------------------
public int sum (int ... param) {
Variable parameters are processed in the same way as arrays
No need to declare an array
Direct reference
}
sum (in);
SUM (2,3,4);
sum (); null

After you use a mutable parameter, you can pass in one or more types by type when calling a method
A parameter of the type or an array parameter of that type.

Conditions of Use:
1. If there is an array parameter in the parameter list, you can no longer use the variable parameter
Coexistence.
2. You can define at most one variable parameter in a method, and must be in a parameter
The last of the list.


Five, static import
-------------------------------------------------------------------
Before jdk1.5, to use a static member of a class, you must provide the static member
The class.
jdk1.5 introduce static import: You can make static members of the imported class directly in the current class
As you can see, you do not need to provide the class name.
Import static .....

Overuse can reduce the readability of your code to some extent.


Vi. formatted output (printf)
--------------------------------------------------------------------
Presence in C language
printf ("This is%d", 10);
Java formatted output syntax is stricter than C
Java formatted output is supported by Java.util.Formatter

Formatter
printf-style format string interpreter
Main methods:
Format (String format,object. args) {}
First parameter: A formatted string containing a format specifier
Second parameter: variable parameter corresponding to the format specifier
eg
Format ("This is%1$s,%d", "Test", 100);

Syntax for formatting specifiers:
%[argument_index$][flags][width][.precision]conversion
Argument_index: Represents the position of the parameter in the argument list
Flags: flag bits, used to represent the output format eg:-
Width: Indicates how wide the parameter occupies
Precision: A floating-point number representing an integer or fractional number
Conversion: Specific formatting characters

Date format specifier syntax:
%[argument_index$][flags][width]conversion
Conversion:t/ty TM

The string class and the PrintStream class also support formatted output
The string class adds a static method to format (string format,object: args)
PrintStream class added printf (String format,object ... args)


Seven, Generic
---------------------------------------------------------------
The nature of generics is a parameterized type, which means that the type of data we manipulate can be
To be specified as a parameter that can be used in the creation of classes, interfaces, and methods,
are called generic classes, generic interfaces, and generic methods, respectively.

Why?
Build a class that provides properties and prints out property values
Required property type is integer, String, Boolean ...

Before jdk1.5, in order to make the class universal, it is common to use object to replace other
Type.

Disadvantages of using object:
1. Forced type conversions are required
2. Easy to appear classcastexception

What?
By introducing generics, you can ensure that the compile-time type is secure and run-time is less thrown
The possibility of classcastexception.

How?
Generic class:
When declaring a class, specify a generic type parameter
public class Test<t1,t2> {
Private T1 foo;
}
The type parameter T can be any valid identifier
General Conventions:
T---Type
E---Element
K---Key
V---Value

Terms:
Arraylist<e>
ArrayList is called the primitive type of the generic class
E is the type parameter
<> read as TypeOf
arraylist<e> a generic type/parameter type called ArrayList

Use:
When you instantiate a generic class, you can only pass in a specific class type, and you cannot
Is the basic data type.
attribute types in a generic class can be determined based on the type of parameter passed in
When you do not specify a parameter type, the property type in the default class is Object

Class map<k,v>

Restricting generic usage categories
When instantiating a generic class, a preset can be instantiated with any type
The type of the parameter in the generic type, but if you want to restrict the use of a generic type,
Only a particular type or its subclasses can instantiate the generic class,
You can use the extends keyword to specify that the parameter type implements an interface
or inherit from a class.
Class Generic<t extends Collection> {}
generic<arraylist> g = new generic<arraylist> ();
If you do not specify a type by using the extends keyword, the default is
<t extends Object>


Generic pass-to-match declaration
Generic wildcard characters?
generic<string> foo = null;
Generic<integer> foo1 = null;
foo = new generic<string> ();
Foo1 = new generic<integer> ();

Statement
generic<? Extends number> foo = null;
foo = new generic<string> ();
foo = new generic<integer> ();

? : Can match any type
? Extends type: Indicates that the type is a subclass of a particular type
? Super Type: Indicates that the type is a parent class of a particular type

Use of <?> or < Extends a reference to the Someclass> declaration, only
To get the value of the specified object or empty its value without assigning it to him

Simplecollection
Int[] t;
int index;

Add ()
int GetLength ();
Get (int i);

Generic inheritance Category
public class Chid<t> extends parent<t> {}
The type declaration of a subclass must be consistent with the parent class

public class child extends parent<string>{}
public class Child<t> extends parent<string>{}


Generic interface
Use the same as a generic class
Syntax Definitions:
Interface intername< type parameter, type parameter > {}
eg
Public interface Test<t> {
public abstract void Fun (T t);
Public abstract T fun1 ();
}

Generic methods:
The type parameter of a generic method must be declared before it can be used
I). In a generic class, the type parameter is declared directly using the
II). In a non-generic class, you need to declare the type parameter before returning the value to use the


Eight, Annotation
----------------------------------------------------------------------
JDK1.5 introduced Annotation,annotation to provide some originally not belong to the source program
Data, which actually represents a kind of annotation syntax, a marker.
There are two categories depending on the role played:
Compile check: Only as a token to the compiler some information, let the compiler in the compiled
Time to do some special processing.
Code handling: The runtime can obtain annotaion information through reflection and perform the corresponding
The operation

(i) annotation built in the system
jdk1.5 the system built three annotation, they are used to do the compilation check
Located under the Java.lang package
1) @Override
Represents an override/overwrite operation that enforces that the method identified by the @override is indeed overwritten
A method with the same name in the parent class.
2) @Deprecated
Represents an obsolete, deprecated operation.
Usage:
A. Defines a class that is not recommended for use
B. A deprecated method is defined in the parent class, and when the subclass overrides, the compiler
Issue a warning.
3) @SupprepssWarnings
Suppresses/suppresses warnings, prevents the compiler from issuing warnings, and suppresses multiple warnings at the same time
@SuppressWarnings ({"Rawtypes", "unchecked", "deprecation"})
Unchecked: Indicates an unchecked operation
Deprecation: Indicates an obsolete operation
Rawtypes: Represents a generic type
All: All

@Override @Depracated is marker Annotation, which is the name itself to the compiler
Provide information
@SuppressWarnings ({"Unchecked", "Rawtypes"})

(ii) Definition and use of custom annotation
Custom annotation are generally used for code processing.
Syntax Definitions:
@interface Annotationname {
}
Public @interface Override {
}
custom annotation, which inherently automatically inherits the Java.lang.annotation.Annotation
Interface.

The similarities and differences between annotation and interfaces:
I.annotation is an interface
1. The variable and the interface, only the public static final
2. As in the method and interface, only public abstract
Ii. Annotation is a special interface.
Methods in 1.annotation must have no arguments, cannot throw exceptions, must have
return value, the return value type has a limit
2. You can set a default value for the return value of a method
The method in 3.annotation is called a property
4. Use the form of the mark when using eg: @MyAnnotation
5. Perform different operations depending on the retention time (advanced features)

Properties of the annotation
Grammar:
Type Arrname () [Default value];

The type types can be:
Basic data types
String type
Class type
Enum type
Annotation type
And their one-dimensional array form

Use of annotation
I. If the property does not specify a default value, you must assign a value to the property when used
II. If there is only one property and the property name is value, you can use it without specifying
The property name.
III. In addition to the Value property other properties have default values, you can also do not specify when using
The property name.

(iii) Advanced features---meta-annotations
Meta-annotations: Annotations used to identify annotations

1. @Retention
Used to specify the annotation's retention range, which is evaluated by java.lang.annotation.
Provided by Retentionpolicy.

public enum Retentionpolicy {
source,//remain in Java source files only
class,//reserved in class file, not read by JVM, default
RUNTIME; Reserved at run time (retained in class file, runtime read by JVM)
}

@Retention (Retentionpolicy.class)
Public @interface Myannotation {

}

A custom annotation can be used to function by reflection mechanism at run time
Get the appropriate information.

Java.lang.reflect.AnnotatedElement
Used to determine whether a specified type of annotation exists on the specified element
Public abstract Boolean isannotationpresent (Class annotationtype)
Public abstract Annotation getannotation (Class annotationtype)
Public abstract annotation[] Getannotations ()


2. @Target
Used to specify the use range of the annotation, which is evaluated by java.lang.annotation.
ElementType offers

Public @interface Target {
Elementtype[] Value ();
}

@Target ({Elementtype.method,elementtype.field})
Public @interfact Myannotation {
}

public enum Elmenettype {
Annotation_type,//annotation class declaration on
CONSTRUCTOR,//on the constructor
FIELD,//instance variable on
Local_variable,//on local variables
method,//methods Declaration on
Package,//packet declaration on
PARAMETER,//parameter declaration on
Type//class, interface, and declaration of the enumeration class
}

For the use of the package, it must be used in the Package-info.java file


3. @Documented
Using annotation marked with @documented, the Javadoc document is generated
Annotation information will also be added.

eg
@Documented
Public @interface myannotation{
}

4. @Inherited
Indicates whether a annotation will be inherited by subclasses of the class that uses the annotation

JDK 1.5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.