Java 5.0 tips for the tiger Program (1)

Source: Internet
Author: User

I read a book "Java 5.0 tiger program master Tips" and below are my notes: there are 10 parts:
1. Generic
2. Enumerated
3. autoboxing and Unboxing
4. vararg
5. Annotation
6. for/in statements
7. Static import
8. Format
9. Threading
Ps: Source Code address of the book: http://www.oreilly.com/catalog/javaadn/ 文;
The following are summarized in sequence ::::
I. I have never heard of Generic before and do not know what it is;
Purpose:
Generic provides support for Java type security. It is also the cornerstone of many new features of Java5.0, supporting vararg, annotation enumeration, collection, and many parallel functions in languages.
List of Type-safe:
Syntax:
List ListOfStrings = new sort list ();
Example 1:
Package com. oreilly. tiger. ch02;

Import java. util. ArrayList;
Import java. util. List;

Public class BadIdea {

Private static List Ints = new ArrayList ();

Public static void fillList (List List ){
For (Integer I: list ){
Ints. add (I );
}
}

Public static void printList (){
For (Integer I: ints ){
System. out. println (I );
}
}

Public static void main (String [] args ){
List MyInts = new ArrayList ();
MyInts. add (1 );
MyInts. add (2 );
MyInts. add (3 );

System. out. println ("Filling list and printing in normal way ...");
FillList (myInts );
PrintList ();

Try {
List list = (List) BadIdea. class. getDeclaredField ("ints"). get (null );
List. add ("Illegal Value! ");
} Catch (Exception e ){
E. printStackTrace ();
}

System. out. println ("Printing with illegal values in list ...");
PrintList ();
}
}
Conclusion: The acceptable types of list can be defined to avoid frequent type conversion. However, List and other collection classes still cannot accept primitive values;
Type-safe MapSyntax:
Map Strings = new HashMap <String, String> (); // key and value are all String
Map Strings = new HashMap <String, Object> (); // key isString, value is Object;
Map Strings = new HashMap <Lang, String> (); // key is Long, value is String
Interating over parameterized Type
The For/in loop provides a method to avoid using the java. util. Iterator class;
Example:
Package com. oreilly. tiger. ch02;

Import java. io. IOException;
Import java. io. PrintStream;
Import java. util. HashMap;
Import java. util. Iterator;
Import java. util. Collections list;
Import java. util. List;
Import java. util. Map;

Public class GenericsTester {

Public GenericsTester (){
}

Public void testTypeSafeMaps (PrintStream out) throws IOException {
Map Squares = new HashMap ();

For (int I = 0; I <100; I ++ ){
Squares. put (I, I * I );
}

For (int I = 0; I <10; I ++ ){
Int n = I * 3;
Out. println ("The square of" + n + "is" + squares. get (n ));
}
}

Public void testTypeSafeLists (PrintStream out) throws IOException {
List listOfStrings = getListOfStrings ();
For (Iterator I = listOfStrings. iterator (); I. hasNext ();){
String item = (String) I. next (); // here is the key
 
// Work with that string
}

List OnlyStrings = new rule list ();
OnlyStrings. add ("Legal addition ");
/**
* Uncomment these two lines for an error
OnlyStrings. add (new StringBuilder ("Illegal Addition "));
OnlyStrings. add (25 );
*/
}

Public void testTypeSafeIterators (PrintStream out) throws IOException {
List ListOfStrings = new sort list ();
ListOfStrings. add ("Happy ");
ListOfStrings. add ("Birthday ");
ListOfStrings. add ("");
ListOfStrings. add ("You ");

For (Iterator I = listOfStrings. iterator (); I. hasNext ();){
String s = I. next ();
Out. println (s );
}

PrintListOfStrings (getListOfStrings (), out );
}

Private List getList (){
List list = new vertex List ();
List. add (3 );
List. add ("Blind ");
List. add ("Mice ");

Return list;
}

Private List GetListOfStrings (){
List List = new external list ();
List. add ("Hello ");
List. add ("World ");
List. add ("How ");
List. add ("Are ");
List. add ("You? ");

Return list;
}

Public void testTypeSafeReturnValues (PrintStream out) throws IOException {
List &

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.