Java Summary Basics

Source: Internet
Author: User

Permissions Keyword:

Public: can be accessed by all other classes, different packages

Protected: Members of the current class, in the same package, in different packages, the child class is visible to the parent class protected, inheriting the class

Default: Classes in the same package can be accessed and declared without modifiers, which are considered friendly. Same package

Private: can only be accessed and modified by themselves

Can modify member variables and methods in a class, but only public and friendly can modify the class

(encapsulation, abstraction, polymorphism, inheritance)

Encapsulation: Hides the internal implementation process, providing callers with a way to access the results, such as JavaBean setter and getter.

Abstract: Methods in a class: (To be inherited, and then to override methods, must be public or protected)

    1. Abstract method, no method body;

    2. Concrete method, there is method body.

Method in the interface:

1. The method is implicitly specified as the public abstract method and can only be the public abstract method ();

interface: closed for modification, open for expansion

The variable defaults to public static final;

The default method is public abstract;

Static:

1, modify the inner class, into a static inner class, modify the ordinary class, the initialization of loading, wasting memory;

2, modify the member variables, the class is loaded to create, share data; (static variables are defined in the class, outside the method body)

3, the modification method, through the class name direct access;

4, static code block, to prevent each time to new an object;
5, Static guide package, do not enter the class name using methods and variables, 1.5.

Final:

1, decoration class, can not be inherited, can not be modified, such as String, Integer, math, etc.;

2, the modification method, sub-class can not be rewritten, locking method, hidden as private;

3, the modification instance variable, the reference cannot change, the content can change;

4. Can modify local variables and method parameters (only anonymous inner class and local inner class can access final decorated parameters and variables after decoration)

Transient

Modifies instance variables to ensure that they are not serialized.

Native

The keyword describes the method of its modification as an original ecological method, and the corresponding implementation of the method is not in the current file, but in files implemented in other languages such as C and C + +, such as the operating system.

The Java language itself cannot access and manipulate the underlying operating system, but other languages can be invoked through the JNI interface to enable access to the underlying

Ordinary methods can be modified with native without a method body.

Volatile:

1, to ensure that the modified variable in the multi-threaded visibility, that other threads can immediately know the change of a thread. It is the core of the java.util.concurrent package;

2, the CPU cache appears mainly in order to solve the CPU speed and memory read and write speed mismatch contradiction;

Assign a value to a member variable at the time you create the object, the assignment is made, and it is executed before the constructor

Use constructors to assign values to member variables, and you can pass different arguments each time you call. Maintainability is strong.

Executing the parent class static code executes the subclass static code:

Initialization of static modified properties at compile time (when class is loaded), can be changed after initialization

Static and "This, super" are at the same time, static is irrelevant to the specific object, and this, super is related to the specific object

Java rules: Subclasses inherit the parent class, and the constructor method of the subclass must call Super (), the constructor of the parent class, and must be placed in the first row of the construction method.

Initializes the parent class member variable (which we often call an assignment statement), a common code block.

Initializing the parent class constructor
Initializes a subclass member variable, a normal block of code.

Initialize subclass Constructors

Summary: If we want to share some kind of data among all the objects in a class, we might as well declare the data as static member variables. If you do not want all functions outside the class to have access to the data, you can define it as a private member, then only the public members of that class can access it. Access is premised on the need to create an object that belongs to the class.

The advantage of static member variables and static methods is that they can be called without creating an object. Variables do not want to be called by external classes and can be privatized.

Memory allocation:

Buyers: Storage object reference, local variable basic data type, fast, automatic destruction;
Heap: A basic data type that stores new objects, arrays, and member variables;
Chang: Belongs to the method area, storing final variables and methods;
Static domain: belongs to the method area, storing static variables and methods;
Method Area: Stores the binary class byte code.

Demo Demo=new demo ();

This statement, in fact, consists of four actions:

1) The "new demo" On the right, is the demo class as a template, in the heap space to create a demo object.
2) The end of the () means that immediately after the object is created, the constructor of the demo class is called to initialize the object that was just generated.
3) The "demo demo" On the left creates a demo class reference variable, which is stored in the stack space. That is, the object reference used to point to the demo object.
4) The "=" operator causes the object reference to point to the demo object that was just created.

Cloneable interface and the Clone () method of object:

A reference in Java that assigns the reference to the right of the equal sign to the left side of the equal sign, which points to the same piece of memory

1, Cloneable interface (an empty interface, the classic labeled interface, like serialization, tell the JVM.) Use native method, Shallow clone)

Summary of three sentences:

(1) This class implements the Cloneable interface to indicate that the clone () method of object can legitimately replicate the class instance by field

(2) if the Clone () method of object is called on an instance that does not implement the Cloneable interface, it causes the clonenotsupporteddexception to be thrown

(3) By convention, a class that implements this interface should use a public method to override the Clone () method of object, and the Clone () method of object is a protected method

2. The clone () method of Object

Creates and returns a copy of this object. For any object x, an expression:

(1) x.clone ()! = X is True

(2) X.clone (). GetClass () = = X.getclass () is True

(3) X.clone (). Equals (x) is generally true, but this is not a requirement that must be met

12345678 Stutend stutend2 = (Stutend) stutend1.clone();classStutend implements Cloneable{    privateString name;    privateintage;    publicObject clone() throwsCloneNotSupportedException    {        returnsuper.clone();    }

StringBuffer and StringBuilder:

String variables (Synchronized, thread-safe) are all methods of synchronizing the Synchronized method.
If you want to turn into a string type, you can call the ToString () method of StringBuffer.

The Append method always adds these characters to the end of the buffer;
The Insert method adds characters at the specified point.

1, String s = "a" + "B" + "C", equivalent to stringbuffer SF = new StringBuilder ("a"). Append ("B"). Append ("C");(fast)

2, but note that if the concatenation of strings from another string object, Java compiler will not automatically convert, the speed is slow.
String a = "a";
String B = "B";
String c = "C";
String d = a + B + C; (slow)

If you use a small number of string operations, use string;

If you frequently manipulate a large number of strings, use the

1: Global variables or require multi-threading support use StringBuffer; (after all, there is really not much to synchronize strings, and multiple threads manipulate a list to add data.) )

2: Local variables or single threads that do not involve thread safety are StringBuilder.

In subclasses the This and super differences:

In the subclass of inheritance, the Super.add () and This.add () are all the methods of the parent class of the tune, no difference,

If you rewrite the This.add () method of the default tune class, Super defaults to the method of the parent class,

THIS.name = name, resolving the problem of member variables and parameter names.

Package folder:

JAR-CVF X.jar *

Javac-encoding Utf-8 Test.java

Hashcode of String:

1, the calculation uses the prime number 31, uses 31 the reason may be in order to better distribute the hash address, and 31 only occupies 5bits, reduces the same hashcode value, facilitates the quick search;

2, realize the Equals, must realize hashcode. equals equal, hashcode must be equal.

System.load (string filename) and System.loadlibrary (string libname)

1. system.load filename parameter must be the full pathname;

2, system.loadlibrary refers to the system library.

Java Summary Basics

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.