Basic concepts and common sense

#What are the features of the Java language?

  • Easy to learn
  • Object oriented (encapsulation, inheritance, polymorphism)
  • Platform independence
  • Support multithreading (C++ language does not have built-in multithreading, generally is to call the operating system multithreading)
  • Reliability, safety
  • Support network programming (TODO: need specific review later)
  • Compilation and interpretation coexist

Compile and interpret

What are compiled and interpreted languages

The computer can not understand high-level language, let alone execute high-level language, he can only understand machine language, all any program written using high-level language to be run by the computer must first be converted into computer language, this conversion has two ways:

1. Once compiled, the compiler can run on this platform, such as C, C++, Objective

2. Compile at runtime, such as Python

This is because the Java language has the characteristics of both compiled and interpreted languages. Because Java programs go through a two-step process of compilation and interpretation, programs written in Java need to go through the compilation step to generate bytecode (.class files), which must be interpreted and executed by the Java interpreter.

#The JDK and JRE

The JDK, which stands for JAVA Development Kit, is a full-featured SDK that has everything the JRE has, along with a compiler (Javac) and tools (such as Javadoc and JDB). It can create and compile programs.

JRE is the Java runtime environment. It is the collection of everything you need to run a compiled Java program, including the Java Virtual Machine (JVM), Java class libraries, Java commands, and other basic artifacts. However, it cannot be used to create new programs.

The process of a Java program from source to run is shown below:

When the JIT compiler completes the first compilation, it saves the bytecode corresponding machine code to PerM Gen, which can be used directly the next time

The difference between Java and C++?

Java and c++ are both object-oriented languages that support encapsulation inheritance, polymorphism, and different places

  • Java does not provide Pointers to access memory
  • Java classes are single inherited (interfaces can be multiple inherited), and c++ supports multiple inheritance
  • Java has automatic memory management garbage collection (GC), which does not require programmers to manually free unwanted memory. C ++ supports both method overloading and operator overloading, but Java only supports method overloading (operator overloading adds complexity that is inconsistent with Java’s original design philosophy)
  • C ++ supports both method overloading and operator overloading, but Java only supports method overloading (operator overloading adds complexity that is inconsistent with Java’s original design philosophy)

The basic grammar

#Character constants and string constants

  1. Form: A character constant is a single character caused by a single quote, and a string constant is zero or more characters caused by a double quote.
  2. A character constant is equivalent to an integer value (ASCII value) and can participate in an expression operation. A string constant represents an address value (where the string is stored in memory).
  3. Memory size: character constants are only 2 bytes; String constants take up several bytes.

The increment and decrement operator

The (++) (–) ++ and — operators can be placed before or after a variable. When the operator is placed before a variable (prefix), it increments/decrement and then assigns; When an operator is placed after a variable (suffix), the value is assigned first and then incremented/subtracted.

The symbol is added and subtracted before, and the symbol is added and subtracted after

public class TestMain { public static void main(String[] args) { int i = 0; System.out.println(++i); / /}} - > 1Copy the code
public class TestMain { public static void main(String[] args) { int i = 0; System.out.println(++i); / / - > 0}}Copy the code

Why can’t static methods call non-static members?

  1. Static methods are class-specific, allocate memory when the class is loaded, and can be accessed directly by the class name. Non-static members belong to instance objects, exist only after the object is instantiated, and need to be accessed through the instance object of the class.
  2. When a non-static member of a class does not exist, it is an illegal operation to invoke a non-static member that does not exist in memory

The difference between overloading and overwriting

Overloading occurs in the same class, the method name must be the same, the parameter type, number, sequence method return value and modifier can be different

Overrides occur at run time, when subclasses rewrite the implementation of the parent class’s accessible methods

  1. The method name and parameter list must be the same, the return value type of the subclass method should be smaller or equal to that of the parent class method, the range of exceptions thrown should be smaller than or equal to that of the parent class, and the range of access modifiers should be greater than or equal to that of the parent class.
  2. If the parent method access modifier isprivate/final/staticA subclass cannot override the method, but isstaticDecorated methods can be declared again.
  3. The constructor cannot be overridden
The mark Overloaded methods Overriding methods
In scope The same class A subclass
The list of parameters Must be modified It must not be modified
The return type Can be modified A subclass method should return a value type smaller or equal to that of its parent
abnormal Can be modified The exception class declared to be thrown by a subclass method should be smaller or equal to the exception class declared to be thrown by a superclass method;
Access modifier Can be modified It must not be more restrictive (it can be lower)
Stage of development Compile time The run-time

HashCode () and equals ()

Several basic data types in Java

There are eight data types in Java:

  1. Six numeric types:

    • Four integer types:byte,short,int,long
    • Two floating point types:float,double
  2. One character type: char

  3. 1 Boolean type: Boolean.

Basic data types are stored directly in the local variable table of the virtual stack, while wrapper types are object types, and object instances generally reside in the heap. The local variable table mainly stores basic data types known at compile time (Boolean, byte, CHAR, short, int, float, long, double) and object references (reference type, which is different from the object itself, may be a reference pointer to the starting address of the object. It may also point to a handle representing an object or other location associated with this object.)