Detailed Explanation of Advanced Computer Language in the Language Chapter (2) in Technology and Architecture

foreword

Why write a special chapter on high-level computer languages. Because it has many features, many categories, many details, and many high-level computer languages. He is complicated. See how Niaocai puts these categories, features, details, and complex factors in a simple way

Classification

  1. Object Oriented and Process Oriented
  2. static language and dynamic language
  3. Interpreted and compiled languages
  4. scripting language
  5. professional language

scripting language

Wondering if this language is a scripting language is easy. As long as you can code the command line and run it

muqi@lenovo:~$ python
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1+1
2

Encoded files can be run directly

if __name__ == '__main__':
       print(1+1)

[email protected]://Test$ python test.py 
2

Counterexample

The encoding file of java is .java ending. This file must be compiled via javac. Generate .class files.

Static and dynamic typing, weak and strong typing

Type refers to the type of the variable, or the type returned by the method

statement

It is only where classes, methods, and fields are defined, with those characteristics.

public static final transient  Integer niaocai = 1;

The property niaocai is of type Integer and is initialized to 1, declared as static, immutable (final), not serializable (transient)

assign

public static  transient  Integer niaocai = 1;

public void nc(){
	this.niaocai = 2;
}

The attribute niaocai is initialized to 1, and if the nc method is called, it will be reassigned to 2

dynamic type

  1. The return of variables and methods does not need to declare a specific type, as long as the declaration is a variable or method,
  2. Language variables may not need to be declared
  3. The type of the variable is determined by the type of the variable value, and the type of the method is determined by the specific return type
  4. The value of the variable can be changed to a different type
num = "niaocaim"
class Niaocai:
        num = "niaocai" # 没有self,通过等号直接识别成变量,并没有声明成变量
        def __init__(self):
                print("num" , self.num ,  type(self.num))
                self.num = 1          # self.num 直接定义变量,不用声明
                print("num" , self.num ,  type(self.num)) # 变量的类型是由变量值的类型决定的
                self.num = "num"      # 变量的值可以改成不同的类型
                print("num" , self.num ,  type(self.num)) # 变量的类型是由变量值的类型决定的
                print("num" , num ,  type(num))

if __name__ == '__main__':
        Niaocai()

num niaocai <class 'str'>
num 1 <class 'int'>
num num <class 'str'>
num niaocaim <class 'str'>

static type

  1. A variable must declare a concrete type
  2. After a variable is declared, the type of the variable cannot be changed.
  3. In addition to the declared type, variables can also have other declarations
@Test
public void ttt (){
	Integer num = 2; //Integer是具体类型 变量必须声明具体的类型
	System.out.println(num.getClass().getName());

	num = "str" ;// 此处异常 变量声明之后,不能改变变量的类型
}

java.lang.Integer

java.lang.Error: 无法解析的编译问题:
	类型不匹配:不能从 String 转换为 Integer
	at com.niaocaia.blog.spring.core.ordere.OrdereTest.ttt(OrdereTest.java:58)

Strong typing

Between different types, cannot do operations

>>> "1"+2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects

weakly typed

Operations can be performed between different types. Note that between different languages ​​and between language versions, there is a difference between integers and characters.

JavaScript
1.1+"12";var d = 1.1+"12";typeof(d);
1.112
"string"
convert character to int
>>> 1.1+"12"
13.1
convert character to byte
>>> 1+'1'
50

References Language Type

Summarize

If purely static typing versus dynamic typing, choose between weak typing versus strong typing. Birds will give priority to the combination of static types and strong types, followed by the combination of static types and weak types. Weakly typed code can go wrong. It feels weird to be able to operate between different types.

Interpreted and compiled languages

Compiled language:
  1. The code written in a high-level language can be directly executed by the platform after the platform (operating system, virtual machine) is compiled. compiled language
  2. The virtual machine can compile the executed code into mechanical code that is directly run by the operating system, which can also be called a compiled language. The JIT module of jvm is responsible for compiling class code into binary mechanical code
Interpreted language:
  1. Dynamic languages ​​are implemented based on other languages, and the code is parsed and executed by the implementation language.

Niaocai simulates what a compiled language is based on jvm, and writes a very simple interpreted language, temporarily named niaocai, which only implements addition, subtraction, multiplication and division.

    @Test
    public void explain() throws Exception {
	String s = "12+24*12/12";

	List<String> list = splitStr(s);
	System.out.println(list.toString());
	List<String> newList = new ArrayList<>();
	String str = list.get(0), newStr = null;
	boolean d = true;
	for(int i = 1 ; i<list.size() ; i++){
	    newStr = list.get(i);
	    if(newStr.length() != 1){
		if(i == 1){
		    newList.add(str);
		}
		newList.add(newStr);
		str = list.get(++i);
		d = true;
		continue;
	    }
	    if(newStr.equals("*")){
		str = (Long.valueOf(str)*Long.valueOf(list.get(++i)))+"";
		d = false;
	    }else if(newStr.equals("/")){
		str = (Long.valueOf(str)/Long.valueOf(list.get(++i)))+"";
		d = false;
	    }else{
		if(i == 1){
		    newList.add(str);
		}
		newList.add(newStr);
		str = list.get(++i);
		d = true;
	    }
	}
	if(!d){
	    newList.add(str);
	}
	Long re = Long.valueOf(newList.get(0)),two;
	String one  ;
	for(int i = 1 ; ;){
	    one = newList.get(i);
	    two = Long.valueOf(newList.get(++i));
	    if(one.equals("+")){
		re = re+two;
	    }else if(one.equals("-")){
		re = re-two;
	    }
	    if(i == newList.size()-1){
		break;
	    }
	    i++;
	}
	System.out.println(  re );
    }

Summarize

By observation, you will find that the performance of interpreted languages ​​is definitely worse than that of compiled languages. I don't know why someone can come to a conclusion. Why did PayPal migrate from Java to Node.js, the performance is doubled, and the file code is reduced by 44% . What makes Node.js faster than Java? Why is NodeJS so fast?

Both node.js and java are implemented in c and c++. Node.js is an interpreted language, and java is a compiled language based on a virtual machine. The performance of java is definitely faster than that of node.js, which is beyond doubt. Why does the above test give abnormal results

  1. The default startup mode of jvm is client, then the mode is interpreted mode.
  2. Interpreted languages ​​perform better in simple tests than virtual machine-based interpreted object-oriented, statically typed, strongly typed languages
Say important things three times

The performance of interpreted object-oriented, statically typed, strongly typed languages ​​based on virtual machines is worse than that of directly interpreted languages. The performance of compiled languages ​​based on virtual machines is worse than that of compiled languages ​​based on virtual machines.

The performance of interpreted object-oriented, statically typed, strongly typed languages ​​based on virtual machines is worse than that of directly interpreted languages. The performance of compiled languages ​​based on virtual machines is worse than that of compiled languages ​​based on virtual machines.

The performance of interpreted object-oriented, statically typed, strongly typed languages ​​based on virtual machines is worse than that of directly interpreted languages. The performance of compiled languages ​​based on virtual machines is worse than that of compiled languages ​​based on virtual machines.

The above words are common sense. When doing a language performance test, first understand whether the type of the test language is compiled or interpreted, and whether it is directly or based on a virtual machine. Please don't confuse the test. The performance of python of interpretation type is not as good as that of java based on virtual machine compilation type, and the performance of java is definitely not as good as that of c++. This is common sense. Will the performance of node of the same interpreted type be better than java?

After the test, get the test result and please provide

  1. test environment
  2. Test configuration
  3. test code

If the above content cannot be provided, the test result is invalid and it is pure hooliganism

Remember about the performance of Master Jiang Chengxiao answering a questioner

Location: forget

time: forget

People: Questioner, Master Jiang

Ask a question: @江成晓 Dr. Jiang, I have a sql statement here that executes very slowly, and I can't find the reason. I have said a lot. How to deal with it

Master Jiang: No environment or parameters are provided, how do I know. In the characteristic scenario, the value needs to be modified some configuration, I can make the performance of this sql increase by 3 times, or decrease it by 3 times.

Please technical personnel, treat technology seriously, cautiously and responsibly, write the above simulated explanation code for the blog written, check the information for more than an hour, and write it for more than an hour

Object Oriented and Process Oriented

Object-oriented is developed based on process-oriented, not like the above types, they are opposite to each other

Object-oriented is more advanced than process-oriented

There must be a concept of instance, object, class, and a clear distinction.
object

everything that can be described

Such as souls, aliens, last year's Nobel Prize winner in physics, confirmed gravitational waves.

Objects are distinguished by indicators. The indicator distinguishes the point is the characteristic

For example, the difference between the elderly and children is the indicator of age. If they are more than 60 years old, they are elderly people, and if they are less than 10 years old, they are children.

Object does not have any identifiability

Object does not have a concrete value

Described, distinguish and define features through indicators, and identify methods through features, that is, objects

example

An identifiable object is an instance

Example

We all know that any color is composed of three basic colors red, cyan and yellow. Color is what can be described as red, blue, yellow is characteristic

    public class Colour{//
	
	private int red;
	
	private int yellow;
	
	private int young;

	private String calourName;
	
	public Colour(int red, int yellow, int young) {
	    this.red = red;
	    this.yellow = yellow;
	    this.young = young;
	    
	    this.calourName = "black";//这段计算过程
	}

	public String getCalourName(){
	    return this.calourName;
	}

	public int getRed() {
	    return red;
	}

	public int getYellow() {
	    return yellow;
	}

	public int getYoung() {
	    return young;
	}
	
    }

The instance now has specific values ​​for red, yellow, and blue, so it starts at this moment. There is an example.

Colour black = new Colour(1,1,1);
I'm sorry, because of the relationship between time and Zhang, I can't give you a detailed description of the three major elements of object-oriented. Niaocai really wants to dictate, and it really takes one or two working days to describe in detail.
Summarize

In layman's terms: in the java world, the class defined by the class keyword is the class, and the object created by the new keyword is the instance of this object. Be able to correctly understand and describe declarations, assignments, objects, and instances, which is conducive to communication at work.

Summarize

Object-oriented languages ​​must require a virtual machine. Only the compilation features of the virtual machine can be perfected, and the problems of inheritance, polymorphism, encapsulation, etc. can be completely solved, and it has a certain performance.

Scripting languages ​​must be interpreted languages, dynamic languages, process-oriented, semi-object-oriented, and mostly weakly typed

A static language must be object-oriented (except c), a static language, a language of choice that can be interpreted and compiled. If you choose to explain, the performance is really poor. Therefore, most static languages ​​are compiled languages.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325517275&siteId=291194637