Java Introductory Learning Note 1 (Definition of class, code compilation execution)

Source: Internet
Author: User

This article explains the basic implementation process of Java code

Let's put aside a variety of Java Ides, develop tools, and use only text editors to highlight the most essential things.

In the Linux environment, we edit a file:

Vim Helloworld.java

Edit the following code:

1  Public classhelloworld{2 String name;3      PublicHelloWorld (String name) {4          This. Name =name;5     }6      Public voidPhelloworld () {7System.out.print ("Hellworld," + This. name + "\ n");8     }9      Public Static voidMain (String []args) {TenHelloWorld test =NewHelloWorld ("Lee"); One Test.phelloworld (); A     } -}

To find out what this code means, please go here.

At this point the code is just a string stored in the file, the file Helloworld.java is called the source program.

The implementation of Java code, such as (in fact, many interpreted languages such as Python code execution mechanism is similar):

First, execute in the shell:

Javac Helloworld.java

The source program that holds the string is Helloworld.java compiled into a byte-code helloworld.class that can be understood by the Java interpreter.

The following is the byte code to be run by the interpreter, executing:

Java HelloWorld

Results:

Explain the concept of bytecode here.

For the C + + language, the compiler compiles the string code directly into binary code that can be run directly by the operating system.

For languages like Java,python, their compilers first translate the string code into bytecode that can be run by the Java/python interpreter, and the real runtime is that the interpreter translates these bytecode into binary codes that the operating system knows.

Here is the image representation of the string code, bytecode, binary code.

String code:

String code to the human look.

 public  class      helloworld{String name;  public   HelloWorld (String name) {    Span style= "COLOR: #0000ff" >this . Name = name;  public  void   Phelloworld () {System.out.print (" Hellworld, "+ this . Name +" \ n "     public  static  void   main (String []args) {HelloWorld Test  = new  HelloWorld (" Lee "

BYTE code:

Javap-c can display the Java bytecode comparison image, the first digit of each column represents the execution address number of the byte code.

Reading from the top is the interpreter's process of executing code.

Byte code to the Java interpreter to see.

[[Email protected]192workspace]$ JAVAP-c Helloworld.class Compiled from"Helloworld.java"Public class HelloWorld {java.lang.String name;    Public HelloWorld (java.lang.String); Code:0: Aload_01: Invokespecial #1                  //Method java/lang/object. " <init> ":() V       4: Aload_05: Aload_16: Putfield #2                  //Field name:ljava/lang/string;       9: Return public void Phelloworld (); Code:0: Getstatic #3                  //Field Java/lang/system.out:ljava/io/printstream;       3: New #4                  //class Java/lang/stringbuilder       6: DUP7: Invokespecial #5                  //Method Java/lang/stringbuilder. " <init> ":() V      Ten: LDC #6                  //String Hellworld,       A: Invokevirtual #7                  //Method java/lang/stringbuilder.append: (ljava/lang/string;) Ljava/lang/stringbuilder;       the: Aload_0 -: GetField #2                  //Field name:ljava/lang/string;       +: Invokevirtual #7                  //Method java/lang/stringbuilder.append: (ljava/lang/string;) Ljava/lang/stringbuilder;       A: LDC #8                  //String \ n       -: Invokevirtual #7                  //Method java/lang/stringbuilder.append: (ljava/lang/string;) Ljava/lang/stringbuilder;       -: Invokevirtual #9                  //Method java/lang/stringbuilder.tostring: () ljava/lang/string;       -: Invokevirtual #Ten                 //Method java/io/printstream.print: (ljava/lang/string;) V       -: Return public static void Main (java.lang.string[]); Code:0: New # One                 //class HelloWorld       3: DUP4: LDC # A                 //String Lee       6: Invokespecial # -                 //Method "<init>":(ljava/lang/string;) V       9: Astore_1Ten: Aload_1 One: Invokevirtual # -                 //Method Phelloworld: () V       -: Return}

Binary code:

Binary code is interpreted by the interpreter of the bytecode, can be directly referred to the operating system, CPU execution.

Look at the binary code of the LS file (the code corresponding to the LS Directive) in the Linux bin directory:

[[Email protected]192bin]$ Hexdumpls| More0000000457f 464c0102 0001 0000 0000 0000 00000000010 0002003e0001 00004b480040 0000 00000000020 0040 0000 0000 0000c4180001 0000 00000000030 0000 0000 0040 0038 0009 0040001e 001d0000040 0006 0000 0005 0000 0040 0000 0000 00000000050 0040 0040 0000 0000 0040 0040 0000 0000
...
...

The above is a binary code 16 binary representation. For example, 45 means binary 01000101.

Attached: Code explanation

Helloworld.java

1  Public classhelloworld{2 String name;3      PublicHelloWorld (String name) {4          This. Name =name;5     }6      Public voidPhelloworld () {7System.out.print ("Hellworld," + This. name + "\ n");8     }9      Public Static voidMain (String []args) {TenHelloWorld test =NewHelloWorld ("Lee"); One Test.phelloworld (); A     } -}

The 1th line of code establishes a public class HelloWorld

Java requires that only one public class be available in each file, and that the class name of the common class must match the file name.

For example, this file is named Helloworld.java, then it must be public class HelloWorld, others are not.

If you use Eclipse development, when you build good one. java files, Eclipse automatically adds the code for your public class with the file name.

Line 2nd declares a variable of type string name.

The 第3-5 row defines the constructor for the class.

Constructors are functions that are automatically called by default when this class is instantiated. Constructors do not have to be defined, and if not defined, Java invokes a default constructor.

The Java definition constructor defines a method that has the same name as the class name and has no return value.

This in this.name refers to an instance of this class that is instantiated.

For example, in line 10th, when I instantiate this class, I name the instance test, then This=test,this.name=test.name.

To say a little more here, in fact, each method (function) in a class has a default parameter of this, such as:

void
Equivalent to publicvoid Test(this)

This is when the compiler compiles the code to the bytecode by the compiler itself, all referring to the instance of the class instantiation.

A 第6-8 line defines a method of a class, called a function. void indicates the return type of the function, not void.

If a function has a return value, the function return type must be defined as the type of the return value, such as:

1  Public int Test () {2     return 13}45 public String test1 () {6      return "Hello,world"7 }

The 第9-12 line defines a main function, which is the entry for the Java file execution.

This main function is unique in this public class and is unique in this file.

The 10th row instantiates the class.

Java instantiates a class with the new keyword. The HelloWorld on the left indicates that the type of the test variable is the HelloWorld class, and the HelloWorld class is instantiated and assigned to test on the right side of the = sign.

Line 11th invokes the method Phelloworld.java in the instance's method call directly using the symbol "." , as with Python.

In fact, there are many similarities between Python and Java. Or that there is a lot of common ground between languages. Proficient in a language, learning other languages will be more effective.

If the above description is wrong, please note in the comments, because I also began to study soon.

Java Introductory Learning Note 1 (Definition of class, code compilation execution)

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.