Known c++ is Java plus, js is Java mini, this article is developed on the basis of c++ and js, so according to the squeeze criteria, we have a certain Java foundation.


An overview of the

Java evolved from c++, which was difficult to use due to Pointers and memory management issues, and Java made subtraction from c++, for example

  • Automatic memory management replaces manual
  • A reference type replaces a pointer. Note that a reference in c++ is an alias that cannot be changed after initialization
  • No support for operator overloading, no support for preprocessors, no support for GOto, no support for multiple inheritance
  • Bytecode is compiled to run on a cross-platform runtime, unlike c++, which requires platform-specific compilation
  • The type system is simple, eliminating data structures such as structures and unions

Java itself was not concise enough, hence go.

Js was designed to have a scripting language with a similar syntax to Java, so the syntax is similar, for example, to primitive types and reference types, which are based on Obejct

Compile and execute

The Java source code is a.java file, which is compiled into bytecode suffixed with.class and then run on the JVM runtime, which is similar to V8.

The basic unit

The Class of a Java program is not only a template for instantiating objects, it is also the basic unit of code organization in which all other logic and data types are organized. A. Java source file can contain multiple classes, but only one public. After compilation, each class generates a. Class file in each class.

Public class HelloWorld {/* First Java program * it will print the String HelloWorld */ public static void main(String[] args) { System.out.println("Hello World"); // Output Hello World}}Copy the code

The main function is also a startup function in Java, but still in a class. The concepts of other modifiers and parameters are similar to those in c++.

The data type

Data types are basic and reference types, and void can indicate that there is no return value and cannot be manipulated directly

Basic types of

Integer types

  • One byte
  • short
  • int
  • long

Floating point type

  • float
  • double

Character type, in single quotes

  • char

Boolean type

  • boolean

The following is the number of bytes used by different types. The Boolean value does not specify the number of bytes

Reference types

As with JS, reference types in Java are objects, such as arrays, and string arrays or instantiated classes, which default to NULL

Variables and constants

Variable types

There are three types of variables in Java

  • Class variables, that is static variables
  • The instance variables
  • Local variables, variables in class methods
public class Variable{ static int allClicks=0; // Class variable String STR ="hello world"; Public void method(){int I =0; // local variables}}Copy the code

Variable declarations

Expressions and statements

Operators and flow-control statements in Java are basically the same as in c++, which I won’t cover here.

class

Each part of the program logic is distributed in each class, the class itself contains a variety of fields to store data and methods, and many corresponding modifiers to specify the use of each subsegment. The basic usage is similar to c++, equivalent to the use of class c++ only in the file scope (and interface), generics and other c++ similar functions are not mentioned. Multiple class files can also be packaged into a JAR package.

Inner classes can also be nested within a class.

package

To further organize the code, there is also the concept of package, similar to package in GO, using package definitions to avoid naming conflicts in the global namespace, and using default packages if not declared. For example, there is a Helllo. World package where packages are independent of each other and have no dependencies

package hello.world;

public class Person {
    void hello() {
        System.out.println("Hello!");
    }
}
Copy the code

There are three ways to use packages, for example we want to access the Person class

  • Write all,hello.world.Person
  • Import using importimport hello.world.Person“, then use the Person class directly, or call the specific class name (in this case Person) *, to import all the classes in the package
  • useimport staticImport a static method of a class

The modifier

Modifiers are used to modify the class itself or the fields in it. There are two types of modifiers: access modifiers and non-access modifiers. There are four types of access modifiers

  • Default The default value can be used for classes, interfaces, variables, and methods
  • Private can be used with variables and methods
  • Public can be used with classes, interfaces, variables, and methods
  • Protected is available for variables and methods. For subclasses that are not in the same package, only the protected methods inherited from the subclass can be accessed. If they are in the same package, all the protected variable method subclasses of the parent class can be accessed

Non-access modifiers include

  • Static modifies methods and variables
  • Final decorates classes, methods, and variables. Modified classes cannot be inherited, methods cannot be redefined, and variables are constants
  • Abstract modifies classes and methods
  • Synchronized and volatile are used for threads

methods

In addition to regular methods, a class can have a constructor of the same name, and different constructors can be overridden based on the signature.

interface

An interface can be at the same level as a class, i.e. directly at the top of a file. An interface is used to define abstract methods, which are then implemented by class (implements).

inheritance

Extends is used when inheritance, and super is used to access fields of the parent class. If there is no extends, then extends Object is default. If the subclass defines the same method as the parent class, it is called Override. If @override is optional, an error will be reported if the signature and Override are inconsistent. Abstract classes are modified with abstract and can only be used for inherited classes. Abstract classes can have concrete code implementations, and interfaces can only provide abstract methods. Java also supports polymorphism.

Package management

In fact, Maven is not only a package management tool, but also a project management tool. Here we mainly look at the package management related. Maven’s POM.xml is the equivalent of PACKage. json for NPM, where defined dependencies can be pulled from remote repositories, which can of course be domestic mirrors.

Maven’s other project management features, similar to NPM Script, are described here


The end