Java Fourth Week learning Diary (thread)

Source: Internet
Author: User

1. The role of static import: Simplify writing
Static import can act on all static members of a class
Static import Format: import static package name. Class Name
Things to keep in mind when importing statically:
1. If a statically imported member has the same name as a member of this class, then the static members of this class are used by default, and if you need to make a member that uses a static import, you need to precede the static member with the class name.


2. Enhanced for Loop
Enhances the role of the For loop, simplifying the writing format of iterators
Enhance the scope of the FOR loop: If you are an object that implements the Iterabl interface or an array object, you can use the enhanced for loop
To enhance the format of a For loop:
For (Variable type variable name: traversed target) {

}
Considerations for enhancing the For loop:
1. To enhance the bottom of the for loop or to use an iterator traversal, but do not need to get this iterator, so in the process of using the enhanced for loop variable element is not allowed to use the collection object to modify the number of elements of the collection.
2. The iterator traversal element differs from the enhanced for loop traversal element: The element of the collection can be deleted when iterating through the elements of the collection, while the Remove method of the iterator cannot be invoked when the enhanced for loop iterates over the elements of the collection.
3. The difference between the normal for loop and the enhanced for loop: The normal for loop can have no traversed target, and the enhanced for loop must have a traversal target.
The 4.Map collection does not implement the Iterable interface, so the map collection cannot use the enhanced for loop directly, and if you need to use an enhanced for loop, you need to traverse the collection with collection. Usually an array object, the memory address is the beginning of the brackets.


3. Variable parameters
The format of the mutable parameter is:
Data Type ... Variable name
Variable parameters to note the details:
1. If the parameter of a function is used with a variable parameter, then the method can be called to pass arguments or not pass arguments.
2. A mutable parameter is actually an array object.
3. The variable parameter must be in the last one in the formal parameter.
4. A function can have at most one variable parameter, because the variable parameter is positioned at the last position in the formal parameter

4. Automatic box packing and unpacking
Auto-Boxing: Automatically converts basic data types in Java into Object-type data.
Java is an object-oriented language, and everything can be described using classes, and sun uses some classes
Auto-unpacking: Converts data of reference types into basic types of data.
Describes the eight basic data type standards in Java.
Basic data type Wrapper type
BYTE byte
Short Short
int Integer
。。。

Small knowledge Point: The integer class internally maintains a buffer array, which stores the -127~128 data in an array, and if the data you get falls within that range, get the corresponding data directly from the buffer.


5. Enumeration values

Problem: Some methods receive data that must be within a fixed range.

Solution: Customize a class, and then privatize the constructor to create an object of this class in the custom class to resolve.

JDK1.5 proposes a new solution to the above problem, which is to use the enumeration class to solve. Some methods at run time, the data he needs is not arbitrary, but must be a certain range of values, JDK1.5 after the enumeration class to be resolved.

The definition format of the enumeration class:

Enum class Name {

Enumeration values

}

Enumerate the details to be noted:

1. The enumeration class is also a special class.

2. The default modifier for enumeration values is public ststic final.

3. The data type of the enumeration value is the data type of the class to which the enumeration value belongs, and the enumeration value refers to the object of the class, in fact the enumeration value is the object of the enumeration class.

4. How to construct the enumeration class The default modifier is private.

5. An enumeration class can define its own member variables and member functions.

6. The enumeration class can customize the constructor, but the constructor's modifier must be private, otherwise it is possible to create an object of the enumeration class without the existence meaning of the enumeration class.

7. Enumeration classes can have abstract methods, but enumeration values must be implemented in an abstract way.

8. The enumeration value must be in the first statement of the enumeration class or an error.

To enumerate the applications of a class:

For the situation:

Some methods at run time, the data he needs is not arbitrary, but must be a certain range of values.

For example: direction sex season week

Type used by the switch statement:

byte char short int String enumeration type

enumseason{Spring,summer,autumn,winter;}enumperson2{Student,worker;} Publiceclass Demo8 { Public Static voidMain (string[] args) {Season Season=Season.summer; Switch(season) { CaseSpring///Just write the enumeration values,It is not necessary to declare which class the enumeration value belongs to. System.out.println ("Spring ...");  Break;  CaseSummer:System.out.println ("Summer ...");  Break;  CaseAutumn:System.out.println ("Autumn ...");  Break;  CaseWinter:System.out.println ("Winter ...");  Break; }    }}

IO stream (Input output)

The main function of IO technology is to solve the problem of data transmission between equipment and equipment. For example: Keyboard---> Memory memory data---> HDD

Application Scenarios for IO technology:

Export reports, upload photos, download, interpret XML files

Data is saved to the hard disk, and the data can be saved permanently. Data is typically saved to the hard disk as a file.

So Sun uses a file class to describe the files or folders.

How to construct the file class:

File (file parent, String child)
Creates a new File instance based on the parent abstract pathname and child pathname string.
File (String pathname) specifies the path to a file or folder to create a Files object.
Creates a new File instance by converting the given pathname string to an abstract path name.
File (string parent, String child)
Creates a new File instance based on the parent pathname string and the child pathname string.

Java Fourth Week learning Diary (thread)

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.