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?

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.
这里写图片描述

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值