欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java - Introduction

程序员文章站 2022-07-14 19:05:42
...

Java - Introduction


Java - Introduction

What is Java?
Java is well-known as a programming language for Internet applications.

Why is the language named “Java”?
One believable story is that the name was thought by a member of a development team. When he went out for coffee, It reminds him of delicious coffee drank in Java Island. Hence, he gave the name “Java” to the language and use coffee as logo.

What can Java do?
It can be used to build development tools, application programming interface(API), development technologies, interface toolkits and integration libraries.(Read more)

Java program example

public class Hello{
    public static void main(String[] args){
        String welcome = "Hello world";
        System.out.println(welcome);
    }
}

How does Java program work?
Java - Introduction
To run a Java program, first use the compiler to translate
the Java program into byte-code program. Then use JVM for your computer to translate byte-code instructions into machine language and to run the machine language instructions.

Compiler
A compiler is a program that translates a high-level language program, such as a Java program, into an equivalent low-level program, such as machine language.

If you installed Java Development Kit(JDK), you can use javac command to compile a Java program.

byte-code
The Java compiler translates Java program into a language called byte-code, which is the machine language for fictitious computer. It is easy to translate the byte-code into machine language of any particular computer. Each type of computer will have its own software to implement the Java Virtual Machine that translates and executes byte-code instructions. The byte-code of previous Java program.

// Compiled from Hello.java (version 1.8 : 52.0, super bit)
public class Hello {

  // Method descriptor #6 ()V
  // Stack: 1, Locals: 1
  public Hello();
    0  aload_0 [this]
    1  invokespecial java.lang.Object() [8]
    4  return
      Line numbers:
        [pc: 0, line: 2]
      Local variable table:
        [pc: 0, pc: 5] local: this index: 0 type: Hello

  // Method descriptor #15 ([Ljava/lang/String;)V
  // Stack: 2, Locals: 2
  public static void main(java.lang.String[] args);
     0  ldc <String "Hello world!"> [16]
     2  astore_1 [welcome]
     3  getstatic java.lang.System.out : java.io.PrintStream [18]
     6  aload_1 [welcome]
     7  invokevirtual java.io.PrintStream.println(java.lang.String) : void [24]
    10  return
      Line numbers:
        [pc: 0, line: 5]
        [pc: 3, line: 6]
        [pc: 10, line: 7]
      Local variable table:
        [pc: 0, pc: 11] local: args index: 0 type: java.lang.String[]
        [pc: 3, pc: 11] local: welcome index: 1 type: java.lang.String
}

JVM
JVM refers to the software that implements the fictitious computer. It can do translation through interpreter and Just-In-Time(JIT) compiler.

The interpreter translates byte-code instructions into machine language instructions and execute those instructions. It does this one instruction at a time.

JIT use a JIT compiler which uses a combination of interpretation and compilation. JIT does translation one chunk of instructions at a time.

Class loader
A Java program is made up of various classes. Each class is compiled separately. To run a program, the byte-code for these various classes need to be connected together. Class loader does this job.
Java - Introduction

Error message
A mistake in a program is called a bug. There are three types of bugs: syntax error, run-time error and logic error.

A syntax error is a grammatical mistake in a program. For example:

public class Hello{
    public static void main(String[] args){
        String welcome = "Hello world".
        System.out.println(welcome);
    }
}

The punctuation “.” is wrong. It should be “;”.

A runtime error is not detected until your program is run. For example:

public class Hello {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        int quotient = a/b;
        System.out.println("a/b = "+quotient);
    }
}

When running this program, it detects an “divided by 0” error.

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at Hello.main(Hello.java:7)

A logic error is a mistake in the underlying algorithm for your program. For example:

public class Hello {
    public static void main(String[] args) {
        int a = 2;
        int b = 3;
        int sum = a-b;
        System.out.println("a-b = "+sum);
    }
}

We write an algorithm to compute the sum of two integers. It should output 5. However, the result is -1. “-” is a mistake. Replace “-” with “+”. This is a logic error.

PS:
Welcome questions always and forever.