C + +, Java, Python who is a compiled language, who is an interpreted language?

Source: Internet
Author: User

Recently, the major Internet companies online written tests, programming topics in the compiler only support C + +, Java, and even some support javascrpit and Pascal, is not support Python. Let me always used to be accustomed to the python I spit blood, so today, or familiar with Java, lest continue to be abused. In the course of learning, you see such a controversy as "Java, Python who is a compiled language, who is an explanatory language?" ”。 I have a lot of information on the Internet, but also a combination of their own understanding, below to share with you.

  In general, the dividing line between compiled and interpreted languages is no longer so obvious, and it should be avoided to classify language simply as "compiled" and "interpreted".

  We began by saying that C + + is a compiled language, because, in contrast to an explanatory language such as Ruby, C + + requires the compiler to compile the source code into intermediate files (. o and. obj) and then generate machine code through connectors and assemblers, a series of basic sequences of operations that can be directly performed by the computer. These machine codes are our usual EXE files.

We use Ruby as an example to talk about the interpreted language, the program from the source code to be executed by the computer, but also go through the above steps. The difference is that C/E + + will save the machine code (i.e. EXE file) from the source code, and Ruby will simply throw these generated basic sequence of operations (Ruby virtual machine) instructions to the Ruby virtual machine to execute and then generate the action. This is what we call the interpretive language.

So what we see is that the compiled language is compiled and then run, and the explanatory language directly "runs" the source code.

Back to the beginning of the question, if the "No Save machine code for EXE file" as a distinction, then Java and Python are interpreted language.

But specifically, Java and Python are very different. Java code from the source program to execute, the process is: the compiler (JAVAC) to convert the source code into bytecode, and then the Interpreter (Java.exe) to the computer to understand the bytecode to the machine code to execute, the process of "Save machine code as EXE" behavior (This is not entirely accurate, as described below). Where both the compiler and the interpreter are part of the Java Virtual Machine (JVM), it is possible to "compile once, execute everywhere" because different hardware differs from the Os,java interpreter. So the JVM is the key to the Java cross-platform feature.

A Java Virtual machine (JVM) is a specification for computer devices that can be implemented in different ways (software or hardware). The instruction set for compiling the virtual machine is very similar to the instruction set of the compiled microprocessor. A Java Virtual machine consists of a set of bytecode instructions, a set of registers, a stack, a garbage-collected heap, and a storage method domain

For Python, the source code to execute also passes through the following procedure: source code---> Bytecode---> Machine code. In the same way as in Java, there is no "machine code saved as EXE" in the process. Unlike Java, Python uses virtual machines that are implemented in other languages, such as the fact that the python we use is actually CPython, i.e. its virtual machine is implemented by C, which is responsible for compiling the Python source code into bytecode and interpreting the execution. In addition, there are Jypython, IronPython and so on.

Then why do you do that?

Because the C extension is easily written for your Python code in CPython, because it is ultimately performed by the C interpreter . Jython, on the other hand,makes it easy to work with other Java programs: Without additional work, you can import any Java class and use other Java classes in your Jython program .

Through the above analysis, it is clear that each language has its own advantages and disadvantages.

It is also necessary to note that Python is a strongly typed language . One might ask, in Python, that this can be written without an error:

I=1Print(i) I="helloWorld"print(i)

Obviously, such a classmate made the mistake of Python implementation. In the actual code above, I is just a point, each assignment, actually just changes the point of I.

In fact, the Python feature here is that he is a dynamic type language. A dynamic type language is a language that determines the data type at run time, as opposed to a static type. VBScript and Python are dynamic types because they determine that the type of a variable is the first time you assign it a value. A static type language is a language that determines the data type during compilation. Most static type languages guarantee this by requiring that their data types be declared before any variables are used. Java and C are statically typed languages.

Python is a mandatory type definition. Refers to join we have an integer that, if not explicitly converted, cannot be treated as a string, so it is clear that C + + and Java are strongly typed languages. Weakly typed languages are the opposite of strong types. VBScript is a weak type. In VBScript, we can concatenate the string ' 12 ' and the integer 3 to get the string ' 123 ', which can then be treated as an integer 123, all of which do not require any display transformations.

To sum up,Python is a dynamic, strongly typed language .

  Now go back to the question of "the boundaries of interpretation and compilation are not particularly clear".

      • Java needs to pre-compile the code into virtual machine instructions, and then run these virtual machine instructions, and some textbooks will be mixed or semi-compiled.
      • Like Python and Lua, it's even more difficult to point out, you can directly explain the source code to run, or you can compile it as a virtual machine instruction and then run.
      • The results of PHP compilation can be cached by the Web server, and can even be translated into C + + before compiling.
      • The. NET CLR runtime is part of Windows, and the code for the compiled. NET family language directly generates the executable file and is then executed "directly", seemingly with no significant difference from C.
      • JavaScript can be compiled into machine code by the V8 engine and then executed, if under node. JS, this compilation result is cached, you say it is compiled with the implementation of the C what is the difference

  We go back to the topic of "Java compilation, the execution of the machine code is not saved as exe behavior."

Some of the answers to Java's understanding remain in ancient times or textbooks.
In fact, it is now a bit of a struggle to classify programming languages with compiled and interpreted types.
Java's first process is Javac compilation, of course the target file is bytecode. There are three possible ways of handling this:
1. At runtime, the bytecode is executed by the JVM
2. At run time, some code may be translated by JIT to the target machine instruction (in method as the translation unit, will be saved, the second execution without translation) directly executed;
3. RTSJ. After Javac, perform the AOT two compilation to generate the static target platform code (typically IBM Webshpere REAL time).

Sometimes, it is possible that the above three ways are used at the same time. At least, 1 and 2 are used simultaneously, and 3 need to be manually specified by the programmer. Obviously, the static target platform code is generated, which, according to the previous definition, belongs to the compiled language .
  So the discussion language is a bit more refined, strong type, weak type, static, dynamic, gc-based, manual management of memory, there is no VM.

Reference Links:

1. What is the difference between compiling and interpreting a program? http://www.zhihu.com/question/21486706

2. Why are there so many python? Http://www.oschina.net/translate/why-are-there-so-many-pythons

3. Is Java a compiled or interpreted language? http://www.zhihu.com/question/19608553

4. CSDN discussion, Python is a strongly typed language http://bbs.csdn.net/topics/310260114

  

C + +, Java, Python who is a compiled language, who is an interpreted language?

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.