Java Training Notes (ii) ——-abstract class-interface-generics-collection

Source: Internet
Author: User

1.1 Variable parameters of the method
After JDK1.5, the number of parameters can vary when defining a method
Syntax: Add 3 points after the last data type
Attention:
1. The variable parameter can only be at the end of the parameter list;
2. A method can contain a maximum of one variable parameter;
3. The nature of a mutable parameter is an array, so when you call a method that contains a mutable parameter, you can either transfer to multiple arguments or pass in an array.
1.2 Packing and unpacking
The basic data type and the corresponding encapsulation class can be automatically converted, which is essentially the Java automatic boxing and unpacking process.
-boxing refers to the process of converting a basic type data value into a corresponding encapsulated class object, encapsulating the data in the stack into objects stored in the heap.
-unboxing is the process of boxing the reverse process of converting encapsulated objects into basic data values, and storing the data values in the heap on the stack.
1.3Math and date

The math class contains methods for basic mathematical operations, which are static and can be called directly without instantiation.
ABS (Double A) ceil () downward rounding, floor () rounding up, round (),
1.4 Final keyword
Represents immutable, modified variable methods and classes
A modifier variable that represents a constant
Modification method, cannot be overridden by quilt class
Modifier class, cannot be inherited

1.5 single-Case mode


public class Singletondemo {

public static void Main (string[] args) {
Singleton S1 = singleton.getinstance ();
Singleton s2 = singleton.getinstance ();
if (S1==S2)
{
System.out.println ("same object");
}

}
}

Class singleton{
private static Singleton instance = NULL;
Private Singleton () {}
public static Singleton getinstance () {

if (instance = = null) {
Instance = new Singleton ();
}
return instance;
}
}


1.6 Abstract class
To define an abstract class:
The abstract keyword cannot be used to modify variables and construct methods, that is, without abstract variables and abstract construction methods. The method of modifying an abstract is only meaningful if the quilt class overrides it. Otherwise, this method never has a method body, so the abstract method cannot be defined as private, that is, private and abstract cannot be modified simultaneously, and avstract cannot modify the same method with static,final or native at the same time.
Abstract classes cannot be instantiated and can only be inherited as parent classes. From a semantic point of view, an abstract class is a parent class abstracted from multiple classes with the same characteristics.


1.7 Comparison of abstract classes and interfaces:


Similarity: Interfaces and abstract classes cannot be instantiated, and their type variables can point to variables of their implementation classes or subclasses.
The difference: The interface embodies the specification, is the system each module should follow the standard.
Abstract class embodies the template design. Abstract classes can be used as intermediate products in the system, rather than as final products, and need to be further perfected.
The variables defined in the interface are the final static constants.
The method defined in the interface defaults to public by default
Abstract classes and classes are default defaults

Interface does not contain a constructor method
Abstract classes can contain construction methods, which are constructed not to create objects, but to be called by their subclasses to complete the initialization operation.
A class can have at most one direct parent class, including abstract classes
A class can implement multiple interfaces

1.8instanceof keywords
Two reference variables of the same type may behave differently when the same method is called.
Grammar:
Reference type variable instanceof reference type

1.9 generic type
Starting with jdk5.0, Java introduces the concept of "parameterized type", which is to parameterize data types, that is, to define data types as parameters when writing code, which are specified before they are used.
Generic-Class Syntax:
[Accessor] class name < type parameter list >{
Class body ...
}

Wildcard characters:

When declaring and creating a generic instance object, you should have an argument for this generic type, or the compiler will present a generic warning.
Suppose you now define a method in which the parameters of the method need to be generic, but the type parameter is indeterminate, and if you use the object type to resolve it, a compile-time error occurs.
What is a wildcard character? To express.


Bounded types:
-Use the Extends keyword to declare the upper bound of the type parameter;
-Use the SUPER keyword to declare the lower bound of the type parameter;

Upper bound:
Syntax: [Accessor] class name < type parameter extends parent class >{
Class body ....

}

Lower bound:
<? Super string>

Limitations of generics:
Java compile-time type erase
-The generic type parameter can only be a class type;
-the same generic class can have multiple versions, and instances of different versions of generic classes are incompatible.
-When defining generics, the type parameter is just a placeholder and cannot be instantiated directly, for example: New T () is wrong.
-You cannot instantiate a generic array unless it is a type with no upper bound, such as:
"generic<string>[] A = new generic<string>[10] is wrong", and "generic<?>[" A=new generic<?> [10] " is allowed.
-generic classes cannot inherit Throwable and its subclasses, that is, the generic class cannot be an exception class, cannot throw or catch exception objects of a generic class, for example: "Class Genericexception<t> extends Exception" "catch (T E) "is all wrong.


Collection Class
1.set interface
Set is unordered and does not repeat.
Common implementation classes: HashSet, TreeSet, Enumset

HashSet elements are not duplicated
TreeSet elements are not duplicated and can be sorted naturally


2.Queue interface and its implementation class
Queuing queue FIFO

Element offer Peek Poll remove

Deque interface, dual-ended queue
AddFirst addlast GetFirst getlast Offerfirst


LinkedList both list and deque, thread is unsafe.


Arraydeque:

Priorityqueue:


Map interface
Map and collection interface, mainly in the Key-value key value pair mapping relationship:

-hashmap based on hash algorithm, unordered map collection
-treemap by key Natural sort, or custom sort

TreeMap not allowed to be empty

Java Training Notes (ii) ——-abstract class-interface-generics-collection

Related Article

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.