Java Basics Summary

Source: Internet
Author: User

One, Java data Type 1.1 data type division

1.2 Basic data types

1.2.1 Default values for basic data types

1.3 commonly used escape characters

1.4 Conversion of data types:

(1) automatic type conversion and forced type conversion.

(2) Any data type will be converted to string.

Operators, expressions, and statement 2.1 operators

(1) assignment operator: =

(2) Unary operators: + (plus sign),-(minus),!

(3) Arithmetic operators: + 、-、 *,/,%

(4) Relational operators:>, <, >=, <=, = =,! =

(5) Increment and decrement operators: + + 、--

(6) Logical operators:&, &&, |, | |

(7) Parenthesis Operator: ()

Third, judgment and Cycle 3.1 program structure 3.1.1 sequence structure

The program executes from top to bottom, running to the end.

3.1.2 Selection Structure

Decide which statement to execute depending on whether the condition is established or not

(1) if...else, If...else If...else

(2) Switch Case statement

(3) Three mesh operator? :

3.1.3 Cycle Structure

Loops are repeated execution of a piece of code

(1) While loop

(2) Do...while cycle

(3) For loop

3.2 Interrupt Statement

(1) Break jumps out of the current loop.

(2) Continue interrupts one cycle execution.

Iv. definition and use of Arrays 4.1 array definitions

A set of related variables.

4.2 Dynamic initialization of a one-dimensional array 4.2.1 array

How to declare:

(1) data type array name [] = null

(2) data type [] array name = NULL

Open Array Space:

Array name = new data type [length]

Static initialization of 4.2.2 arrays

Data type array name [] = {Initial value 0, initial value 1, initial value 2 ... Initial value N}

4.3 Dynamic initialization of a two-dimensional array 4.3.1 array

How to declare:

(1) data type array name [] = null

(2) data type [] array name = NULL

Open Array Space:

Array name = new data type [number of rows] [number of columns]

Static initialization of 4.3.2 arrays

Data type array name [] = {{No. 0 row initial value},{1th row initial value} ... {nth row of initial value}}

4.4 Multi-dimensional arrays

One-dimensional arrays, two-dimensional arrays, and so on.

4.5 declaration of an object array 4.5.1 an array of objects

Class object array name []=new class [array length];

4.6, array of reference passing 4.6.1 receive and return array

A method can receive an array, or it can return an array, and if the method receives an array, the modification of the method array will be preserved.

Some methods in the 4.6.2 array

(1) Array sorting: Java.util.Arrays.sort ();

(2) Array copy: System.arraycopy ();

V. Method declaration and definition of use of 5.1 method

A code snippet that can be called repeatedly.

The definition format of the 5.1.1 method

public static return value type method name (type parameter 1, type parameter 2, ...) {

The body of the method

Program statements:

[Return expression];

}

Naming conventions for 5.1.2 methods

The first letter is lowercase, and then the first letter of the word is capitalized.

5.2 Overloading of methods

Method name is the same, but the parameter type or number is different, by passing the parameters of different types or the number of different calls, only the return type is not called overload.

5.3 Return statement

(1) Returns the result of a method.

(2) End a method.

Vi. Object-Oriented design 6.1 object-oriented three major features

(1) Package

(2) Inheritance

(3) polymorphic

6.2 Classes and objects

A class is a description of something, an abstraction, a conceptual definition, an object that is an individual of the actual existence of such a thing, and thus also an instance.

Definition of the 6.2.1 class

Class Name {

Data type properties;

......

Public return value type method name (type parameter 1, type parameter 2, ...) {

Program statements:

[Return expression];

}

}

Creation and use of 6.2.2 objects

(1) class name Object name = NULL;

Object name = new class name ();

(2) class name Object name = new class name ();

6.3 The role of Java encapsulation and polymorphism 6.3.1 encapsulation

Protect the content to ensure that certain properties are not visible externally.

Implementation of the 6.3.2 package

Encapsulates a property: Private property Type property name;

Encapsulation for method: Private method return value method name (parameter list) {}

6.3.3 access to encapsulated content

If the encapsulated property needs to be accessed, it is necessary to write setter and getter methods, the setter method can be added to the validation, the getter method simply retrieve the content.

6.3.4 polymorphism

Polymorphism is one of the most important profiles in object-oriented, with the following two main objects in Java:

(1) Overloading and overriding of methods

(2) Polymorphism of the object

    1. Transition up: Parent Parent Class object = Subclass instance; (Auto-complete)
    2. Downward transformation: Subclass Subclass Object = (subclass) Parent class object; (mandatory)
6.4 Construction method and anonymous object 6.4.1 construction method

Class Name {

Access permission class name (Type 1, Parameter 1, type 2, Parameter 2,...) {

program statements;

...//Construction method no return value

}

Attention:

(1) The name of the construction method must be the same as the class name.

(2) The declaration of a construction method cannot have any declaration of a return value type.

(3) You cannot use return in the constructor method to return a value.

6.4.2 Constructor Method overloading

The overloaded procedure is the same as a normal method: the type or number of arguments is different.

6.4.3 Anonymous Objects

Without a name, if you use it only once in Java, you can define it as an anonymous object.

6.5 This and the Super keyword 6.5.1 this keyword function

(1) Represents a property in a class

(2) You can use this to call the constructor of this class

(3) This indicates the current object

6.5.2 using this call construction method

If there are multiple construction methods in a class, you can call each other with the This keyword.

6.5.3 Use this to represent the current object

Current object: The object that is currently calling the method

6.5.4 Super keyword

The Super keyword can access the contents of a parent class from a subclass.

6.5.5 the difference between this and super

6.6 Static keyword 6.6.1 using the static Declaration property

Static can declare global properties.

Attention:

(1) A property or method that uses the static declaration can be called directly by the class name.

(2) The static method can only access the properties or methods of the static declaration.

6.6.2 using the static Declaration method

Methods that use the static declaration can be called directly by the class name.

6.7 Final keyword

(1) Final in Java means final, also known as the end-device.

(2) You can use Final declaration classes, properties, methods.

(3) A variable in the joint declaration using the static final keyword is called a global constant.

Attention:

A. A class that uses final declaration cannot have subclasses;

B. The use of the final declaration method can not be overridden by the quilt class;

c A variable that uses final declaration becomes a constant, and the constant cannot be modified.

6.8 instanceof Keywords

(1) In Java, you can use the INSTANCEOF keyword to determine exactly which class an object is an instance of:

Object Instanceof Class-Return Boolean type

(2) It is best to use the INSTANCEOF keyword for validation before the object is down.

(3) A class is best never to inherit a class that has already been implemented, but only to inherit an abstract class or implement an interface.

6.9 inheriting 6.9.1 Classes of inheritance format

Use the extends keyword in Java to complete the inheritance relationship of the class, manipulating the format:

Class parent Class {}//define parent class

Class Subclass extends Parent class {}//using extends keyword for inheritance

Limitations of 6.9.2 Inheritance

Only single inheritance is allowed in Java, multiple inheritance cannot be used, but multilayer inheritance is allowed.

6.9.3 Access Restrictions

Four types of access rights:

Private, default, public, protected

Size Relationship: Private<default<protected<public

Attention:

A subclass cannot directly access a private member of a parent class, but a subclass can call a non-private method in the parent class, but cannot call a private member directly in the parent class.

6.10 Overloading of methods with overwrite 6.10.1 method overwrite

Method overwrite means that a subclass defines a method with the same name in the parent class, but must take into account permissions when the method is overwriting.

6.10.2 Method Overloading

Is that you can create multiple methods in a class that have the same name but have different parameters and different definitions. The method is called polymorphism by the number of different arguments passed to them and by the type of parameter to determine which method to use. When overloaded, the method name is the same, but the parameter type and number are different, and the return value type can be the same or different. Overloading (overloading) is a representation of polymorphism in a class.

The difference between 6.10.3 method overloading and overwrite

The overridden overriding and overloaded overloading of a method are different manifestations of Java polymorphism. Overriding overriding is a representation of polymorphism between a parent class and a subclass, and overloading overloading is a representation of polymorphism in a class. If you define a method in a subclass that has the same name and arguments as its parent class, we say that the method is overridden (overriding). This method is used by the object of the child class.

If the subclass's method name and parameter type and number are the same as the parent class, the subclass's return value type must be the same as the parent class, and if more than one method with the same name is defined in a class, they either have a different number of arguments or have different parameter types, which is called a method overload. The overloaded method is to change the type of the return value. That is, the overloaded return value types can be the same or different.

6.11 abstract class 6.11.1 concept:

A class that contains an abstract method is an abstract class.

Abstract methods: Methods that are declared only and not implemented are called abstract methods, and abstract methods must be declared with the abstract keyword.

6.11.2 Defining formats

Astract class abstract Name {

Properties

Access rights return value type method name (parameter) {

[Return value];

}

Access rights abstract return value type method name (parameter);

There is no method body in an abstract method.

}

6.11.3 usage

(1) When using the abstract class must have subclasses, subclasses still use the extends keyword to inherit an abstract class, the same will exist but the inheritance of the relationship. A subclass cannot inherit multiple abstract classes at the same time.

(2) You must never use the final keyword declaration in an abstract class.

(3) A constructor method is allowed in an abstract class and fully conforms to the instantiation process of the subclass object.

6.12 Interface 6.12.1 Concept

Interfaces are the most important concepts in Java, and interfaces can be understood as a special class, all of which are made up of global constants and common abstract methods.

6.12.2 Defining formats

Interface interface Name {

Global constants;

abstract methods;

}

6.12.3 Implementation Interface

As with abstract classes, interfaces must also be used through subclasses, and subclasses implement interfaces through the Implements keyword.

Implementation format:

Class subclass implements interface A, interface B, ... {

}

6.12.4 inheriting an abstract class implementation interface

The format is as follows:

Class subclass extends abstract class implements interface A, interface B, ... {}

6.12.5 usage

(1) An interface is a special class that contains only global constants and abstract methods, and abstract methods in an interface may not be added to the abstract and abstract methods in an abstraction class must have an abstract keyword declaration.

(2) A class can inherit only one parent class, but it is possible to implement multiple interfaces at the same time.

(3) An interface can inherit multiple interfaces at the same time to implement multiple inheritance of interfaces.

(4) interfaces, like abstractions, must depend on subclasses.

(5) An abstraction can implement multiple interfaces, but an interface cannot inherit an abstract class.

6.12.6 the relationship between abstract classes and interfaces

6.13 Java exception handling 6.13.1 Concepts

An exception is a flow of instruction that causes a program to run, an exception is an error that occurs during a program's run, is described in Java by a class, and an object is used to represent a specific exception.

The largest class in the exception is Throwable, which is divided into two subclasses: Exception, Error

Exception: The program can handle exceptions on its own.

Error: Indicates JVM errors, general procedures cannot handle.

6.13.2 Try...catch Exception Handling

A try statement block, which indicates that the code in the TRY statement block is monitored for exception when an exception is thrown, and throws an exception object when the code is being run.

The catch statement block captures the exception that occurs in the try code block and does exception handling in its code block, which takes a parameter of type Throwable, which indicates that the exception type can be caught. When an exception occurs in a try, the catch catches the exception that occurs and matches its own exception type, executes the code in the Catch block, and points the catch block argument to the thrown exception object. A catch statement can have multiple occurrences that match one of the many exceptions, and once the match is made, it is no longer attempted to match another catch block.

The exception object can be used to obtain complete JVM stack information at the time of the exception, as well as the exception information and the reason for the exception to occur.

A finally statement block is a block of statements immediately following a catch statement, which is always executed before the method returns, regardless of whether the try statement block has an exception. And this block of statements is always executed before the method returns.

6.13.2 Common Exception classes

Vii. Java Common Class 7.1 string class 7.1.1 instantiates a String object

(1) Direct assignment

(2) through the keyword new

7.1.2 String Content Comparison

Two ways of comparison:

(1) Use "= =" to complete, compare the address value.

(2) Use the "equals" method to complete the comparison is the specific content.

7.1.3 Common methods

(1) Change the string to a character array: ToCharArray ()

(2) The character array becomes a string: Sring ()

(3) Remove the specified character from the string: CharAt ()

(4) Get string length: Length ()

(5) Find whether the specified string exists: IndexOf ()

(6) Remove space: trim ()

(7) Character interception: SubString ()

(8) Split string: Split ()

(9) Case conversion: toUpperCase (), toLowerCase ()

(10) Determine whether to start or end with the specified string: Startwith (), EndsWith ()

(11) Case-insensitive comparison: Equalsignorecase ()

(12) string substitution: ReplaceAll ()

7.2 StringBuffer class 7.2.1 definition

StringBuffer is the use of buffers, itself is an action string, but unlike the string class, the contents of the string class once the declaration can not be changed, the change is the point of memory address, and the contents of StringBuffer can be changed.

7.2.2 Common methods

(1) Connection string operation: Append ()

(2) Add content to StringBuffer at any location: Insert ()

(3) String inversion operation: reverse ()

(4) Replace the contents of the specified range: replace ()

(5) String interception: SubString ()

(6) String deletion: Delete ()

(7) Find out whether the specified content exists: IndexOf ()

7.3 Date 7.3.1 Class

Direct output of its instanced object can be

7.3.2 Calendar class

The date can be accurate to milliseconds, and the time is displayed in the format you want.

Calendar.get (calendar.year));

Calendar.get (Calendar.month));

Calendar.get (calendar.date));

7.3.3 DateFormat Class

is a date-formatted class that specifically formats the operation of the date.

(1) Date DateFormat object: Dateformate.getdateinstance ()

(2) Date and Time DateFormat object: Dateformate.getdatetimeinstance ()

7.3.4 Simpledateformate Class

is a subclass of the DateFormat class, generally speaking, the DateFormat class is rarely used directly, but is done using the SimpleDateFormat class.

7.4 File class

Represents a class that is related to the file itself.

7.4.1 Common methods

(1) Create file: CreateNewFile ()

(2) Delete the development file: Delete ()

(3) Determine if the file exists: exists ()

(4) Create folder: MkDir ()

(5) List all files of the specified directory: List (), Listfiles ()

(6) Determine if a given path is a directory: Isdirectory ()

7.5 List Interface

The list interface can hold arbitrary data, and the contents of the list interface are allowed to be duplicated, extending the operation of the collection interface.

7.5.1 ArrayList

is a list subclass that is instantiated for the list interface.

Defined:

list< generic > Name =new arraylist< generics > ();

Method:

(1) Added: Add ()

(2) Delete: Remove ()

(3) Get value: Get ()

(4) Determine if it is empty: IsEmpty ()

(5) Interception part set: Sublist ()

(6) Find out whether the specified object exists: IndexOf ()

(7) Whether the lookup exists: contains ()

7.6 set interface 7.6.1 definition

The set interface is also a subclass of the collection interface, but unlike the collection or list interface, duplicate elements cannot be added to the set interface.

set< generic > Name =new HashSet < generics > ();

set< generic > Name =new TreeSet < generics > ();

7.6.2 Common Sub-categories

(1) Hash storage: HashSet

(2) Orderly storage: TreeSet

7.7 Iterator Interface 7.7.1 Introduction

When using the output of the collection, the following ideas must be formed: ' As long as the operation of the assembly output is encountered, it is certain to use the iterator interface '.

For Iterartor, because it is an interface in itself, you must rely on the collection interface to instantiate the instantiation.

Defined:

List<string> all=new arraylist<string> ();

Iterator<string> Iter=all.iterator ();

The function of the iterator interface is output from the front to back, which is one-way output, the main function is to complete the iterative output operation.

7.7.2 method

(1) Determine if there is content: Hasnext ()

(2) Output content: Next ()

(3) Delete the current object: Remove ()

7.8 Map Interface

A pair of objects is saved in the map, and the object is saved in the form of: Key->value.

7.8.1 Common subclasses in the map interface

(1) HashMap: Random storage, key does not allow duplication

(2) Hashtable: Random storage, key does not allow duplication

(3) TreeMap: can be sorted map collection, sorted by key in the collection, key does not allow duplicates

(4) Weakhashmap: Weak reference map collection, when some content in the collection is no longer used, you can clear out the useless data, you can use the GC to

(5) Identityhashmap:key can repeat the map collection

7.8.2 Map Method declaration

Map<string,string> map=new hashmap<string,string> ();

7.8.2 Map Basic methods

(1) Add content: Put ()

(2) Check out the value according to key: Get ()

(3) Determine whether the specified key or value exists: ContainsKey (), Containsvalue ()

(4) Get all the Key:keyset ()

(5) Get all the Value:values ()

The difference between 7.8.3 HashMap and Hashtable

7.8.4 TreeMap

Using TreeMap, you can easily complete the sorting operation.

7.9 Collection Tool Class: Common methods of Collections7.9.1

(1) operation of an empty collection: Emptylist (), Emptyset ()

(2) Input data: AddAll ()

(3) Two-part search: BinarySearch ()

(4) Replacement content: Repalceall ()

(5) Sort by: sort ()

(6) Exchange of specified content: Swap ()

7.10 Enumeration

The so-called enumeration, is to specify the scope of the specified value, all the content can only be obtained from the specified range.

7.10.1 defining an enumeration type

[Public] enum enum type name {

Enumeration Object 1, Enumeration object 2, ..., enumeration object n;

}

Java Basics Summary

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.