【Head First Java 读书笔记】(一)基本概念

Java的工作方式

 

你要做的事情就是会编写源代码

Java的程序结构

类存于源文件里面

方法存在类中

语句存于方法中

剖析类

当Java虚拟机启动执行时,它会寻找你在命令列中所指定的类,然后它会锁定像下面这样一个特定的方法:

public static void main(String[] args){
    //code
}

接着java虚拟机就会执行main方法在花括号间的函数所有指令。每个Java程序最少都会有一个类以及一个main()。每个应用程序只有一个main()函数

编写带有main()的类

在Java中的所有东西都会属于某个类。源文件为.java,编译之后成为新的类文件.class ,真正被执行的是类。

总之main()就是程序的起点。不管你的程序有多大(不管有多少个类)一定会有一个main()来作为程序的起点。

QAQ

为何所有的东西都得包含在类中?

因为Java是面对对象的语言,它不像以前的程序语言那样。类是对象的蓝图,而Java中的绝大多是东西都是对象。

每个类都需要加上一个main()吗?
一个程序只要一个main来作为运行。

其他语言可以用整数类型(0代表)来做判断,Java里面也可以么?

不行,Java中的integer与boolean两种类型并不相容。

专家术语学习机

package chapter1;

public class PhraseOMatic {

    public static void main(String[] args) {
        String[] wordListOne = { "24/7", "multiTier", "30,000 foot", "B-to-B",
                "win-win", "front-end", "web-based", "parvasive", "smart",
                "sixsigma", "critical-path", "dymatic" };

        String[] wordListTwo = { "empowered", "sticky", "value-added",
                "oriented", "centric", "distributed", "clustered", "branded",
                "outside-the-box", "positioned", "networked", "focused",
                "leveraged", "aligned", "targeted", "shared", "cooperative",
                "accelerated" };

        String[] wordListThree = { "process", "tippingpoint", "solution",
                "architecture", "core competency", "strategy", "mindshare",
                "portal", "space", "vision", "paradigm", "mission" };
        int oneLength = wordListOne.length;
        int twoLength = wordListTwo.length;
        int threeLength = wordListThree.length;

        int rand1 = (int) (Math.random() * oneLength);
        int rand2 = (int) (Math.random() * twoLength);
        int rand3 = (int) (Math.random() * threeLength);

        String phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " "
                + wordListThree[rand3];

        System.out.println("What we need is a " + phrase);
    }

}

random()方法返回介于0到1之间的值。

编译器与JVM谁比较重要?

编译器就是把源码编译成二进制代码,即字节码,并检查语法错误,程序员不可能自己去写二进制的代码,所以需要编译器。Java是个强类型的语言,这代表编译器不能容许变量保存类型的数据,这是很关键的类型安全性功能,编译器能让大部分的错误在到你那边之前就被抓到,有些数据类型的错误会在运行时发生,但这也是为了要容许动态绑定这样的功能。Java可以在执行期引起连程序员也没有预期会碰到的类型,所以我编译器保留一些运用性,编译器的工作就是确保铁定不能跑的东西不会过关。

但是如果没有JVM Java程序就无法启动

原文地址:https://www.cnblogs.com/six-moon/p/4667082.html