Basic Concepts of Java fundamentals (i)

Source: Internet
Author: User
Tags visibility

1. Advantages of the Java language

1) Java is a purely object-oriented language. all things are objects.

2) platform independence. the Java language can be "compiled once and run everywhere." "Because Java is an interpreted language, the compiler will turn Java code into" intermediate code, "and then explain it on the JVM. Therefore, Java can be very good cross-platform execution, with good portability.

3) Java provides a number of built-in class libraries , which simplifies developer program design and shortens project development time. For example, Java provides support for multithreading and provides a garbage collector.

4) provides support for Web application development.

5) It has good safety and robustness. Java's strong-typing mechanism, garbage collector, exception handling, and security check mechanisms make Java-written programs very robust.

6) The removal of C + + difficult to understand, easy to confuse features, such as header files, pointers, structures, multiple inheritance, etc., making the program more rigorous, concise.

2. The similarities and differences between Java and C + +

Both Java and C + + are object-oriented languages that use object-oriented thinking (such as encapsulation, inheritance, polymorphism, and so on), because object-oriented is characterized by many very good features (inheritance, composition, etc.), so both have good reusability.

1" Java is interpreted language , which runs as follows: The program source code is compiled into bytecode by the Java compiler, The JVM then interprets the execution. and C + + is a compiled language, the source code is compiled and linked to generate executable binary code. Java is slower to execute than C + +, but Java can be executed across platforms, and C + + cannot.

2) Java is a purely object-oriented language, and all code (including functions, variables, etc.) must be implemented in the class. except for the basic data types. In addition, there are no global variables or global functions in Java.

3) Java removes the concept of pointers, which effectively prevents C + + A system problem that can be caused by manipulating pointers in the language, which makes the program more secure.

4) Java does not support multiple inheritance, the use of interfaces in Java for multiple inheritance purposes, Interfaces also have polymorphic properties.

5) C + + requires a developer to manage the allocation of memory, Java provides a garbage manager for automatic garbage collection, and does not require program display to manage the allocation of memory.

3. Main () method

Publi cstatic void Main (string[] args) is the entry method for the Java program, and the JVM looks for the main () method when it runs the program.

1) The Mian () method must have public and static modifiers. The return value in the method is Void,args for the parameter output.

2) The main () method can be modified with final or synchronized, and public and static do not have a sequential relationship. such as static public void main (string[] args) is also reasonable

4. The order in which Java programs are initialized

In the Java language, when an object is instantiated, all member variables of the class in which the object resides are initialized first, and only the constructors of the class that contains the object are called to create the object after all class members have completed initialization.

The initialization of a Java program generally follows 3 principles (descending order of precedence):

① static objects (variables) take precedence over non-static objects (variables), where static objects (variables) are initialized only once.

The ② parent class takes precedence over subclasses for initialization.

③ are initialized in the order in which member variables are defined, even if the variable definitions walk in the method definition, they are initialized before any methods (including constructors) are called.

Order of initialization:

Parent class static variable > parent class static code block > Subclass static variable > subclass static code block > parent non-static variable > parent non-static code block > Parent Constructor > Subclass non-static variable > subclass non-static code block > Sub-class constructors

5. Scope of Java

in Java, there are 3 main types of variables: member variables , static variables , and local variables .

1) The scope of a member variable of a class is the same as the scope of the instantiated object of the class, and when the class is instantiated, the member variable allocates space in memory and initializes it until the end of the life cycle of the instantiated object, and the lifetime of the member variable ends.

2) statically modified member variables are called static or global variables, and unlike member variables, static variables are not dependent on a particular instance, but are shared by all instances, that is, whenever a class is loaded, the JVM allocates space for the static variables of the class. . Therefore, static variables can be accessed through the class name and variable name.

3) The scope and visibility of the local variable is within the curly braces in which it is located.

Scope of member variables

Scope and visibility

Current class

With a package


Other Package







< P align= "Center" >

Span style= "font-size:14px" >x




①public. Indicates that the member variable or method is visible to all classes or objects and can be accessed directly by all classes or objects.

②private. Indicates that the member variable or method is private and that only the current class has access to it, except for other classes or objects that do not have access rights.

③protected. Indicates that the member variable or method is visible to itself and its subclasses, or to classes within the same package.

④default. Indicates that the member variable or method is visible only to itself and to classes that are within the same package. (Accessible if the parent class is in the same package as the child class; otherwise, no access)

6. Interface

Because Java does not support multiple inheritance, that is, a class can have only one parent class, in order to overcome the disadvantages of single inheritance, Java introduces the concept of interfaces. An interface is a collection of abstract method definitions and is a special kind of abstract class. the interface contains only the definition of the method, and there is no implementation of the method. All methods in an interface are abstract. The scope modifier for members in an interface is public, and the constant value that the interface can only give defaults to the public static final decoration.

In Java, some interfaces do not declare any methods internally. That is, the class that implements these interfaces does not need to override any methods, and these interfaces, which are not declared by any method, are also known as identity interfaces, and the identity interface does not have any semantic requirements for the class that implements it, and it acts only as an identity to indicate that the class that implements it belongs to a particular type.

The existing identity interfaces in the Java class Library are cloneable and serializable, and are often used with instanceof to determine whether the type of the instance object implements a given interface.

public class man implements Cloneable{public static void Main (string[] args) {mans m = new Man (); System.out.println (M instanceof cloneable);}

7. Clone () Method--Shallow copy and deep copy

When Java handles a basic data type, it is performed by value passing (copying of input parameters), and other types are executed by reference (passing a reference to the object). Object handling is a reference pass when the function is called and is passed by reference when the "=" assignment is used.

All classes in Java inherit from the object class by default, and a clone () method is provided in the object class. The effect of this method is

1) The class that implements clone first needs to inherit the Cloneable interface, the Cloneable interface is actually an identity interface, and there is no interface method.

2) override the Clone () method in the class.

3) Call Super.clone () in the Clone method. Regardless of the inheritance structure of the Clone class, Super.clone () invokes the Clone () method of the Java.lang.Object class directly or indirectly.

4) point the shallow copy reference to the new clone of the prototype object.

Shallow copy differs from deep copy: whether the object's reference still points to the original object .

Class Test{public int i;public stringbuffer s;}

8. Reflection Mechanism

The reflection mechanism is a very important feature in Java that allows a program to self-check at run time, while also allowing its internal members to operate.

The reflection mechanism provides the following functions: Get the class to which an object belongs, get all member variables and methods of a class, create an object at run time, and invoke the method of the object at run time.

public class main implements cloneable{public static void Main (string[] args) {try{//uses a radiation mechanism to load class B = Class.forName ("b") ; A = (a) b.newinstance (); A.print ();} catch (Exception e) {e.printstacktrace ();}}} Class a{public void print () {System.out.println ("A");}} Class B extends a{public void print () {System.out.println ("B");}}

how to obtain class classes:

1) class. forname ("Path to the class"), as shown in the example above.

2) class name.

3) example. GetClass ()

Java methods for creating objects:

1) Instantiate an object with the new statement

2) Create an object from the reflection mechanism, see the example above

3) Create an object from the Clone () method, see 7

4) Create an object by deserializing it

9. Implement functions like function pointers in C language

In C language, there is a very important concept-function pointers, the most important function is to implement the callback function. The so-called callback function means that the function is first registered somewhere, and it will be called at a later time when it is needed.

interfaces and classes can be used in Java to achieve the same effect. Specifically, you should define an interface and then declare the method to be called in the interface, then implement the interface, and finally pass an object of the implementation class as a parameter to the calling program, which invokes the specified function through this parameter to implement the function of the callback function.

Interface Intcompare{public Boolean cmp (int x,int y);} Class CMP1 implements Intcompare{public boolean cmp (int x,int y) {if (x > Y) {return true;} Else{return false;}}} Class CMP2 implements Intcompare{public boolean cmp (int x,int y) {if (x > Y) {return false;} Else{return true;}}} public class selectsort_s {public static void Selectsort (int[] A, intcompare cmp) {for (int i = 0; i < a.length; i++) {for (Int J =i+1; J < A.length; J + +) {if (cmp.cmp (A[i], a[j])) {swap (A,I,J);}}} public static void Swap (int[] A, int x, int y) {int temp = a[x];a[x] = a[y];a[y] = temp;} public static void Main (string[] args) {int[] array = {4,2,1,6,3,6,0,-5,1,1}; Selectsort (Array, new CMP1 ());      Ascending sort//selectsort (array, new CMP2 ());      Descending sort for (int i = 0; i < Array.Length; i++) {System.out.printf ("%d", Array[i]);}}

     The above example defines an interface intcompare that is used to compare size, which actually acts as a function pointer in the C language, and when used, the developer can pass in the custom class according to the actual requirements. In the above example, there are two Cmp1 and CMP2 have implemented this interface, respectively, in order to achieve the ascending sort and the above example defines a size to compare the interface intcompare, which actually serves as a function pointer in C language function, when used, Developers can pass in custom classes based on actual requirements. In the example above, each of the two CMP1 and CMP2 implemented this interface, respectively, used in order to achieve ascending and descending sorting. used when sorting in descending order. 

Reference documents:

Java Programmer interview Written book

Basic Concepts of Java fundamentals (i)

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: 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.