CS61B sp2018笔记 | Introduction to Java

Introduction to Java


Essentials

1. Reading

1.1 Hello World

  这门课程虽然是用Java教授数据结构,但重点不在Java语法本身,所以最开始只是简单的讲了一些Java的要点:

  • The program consists of a class declaration, which is declared using the keywords public class. In Java, all code lives inside of classes.
  • The code that is run is inside of a method called main, which is declared as public static void main(String[] args).
  • We use curly braces { and } to denote the beginning and the end of a section of code.
  • Statements must end with semi-colons.

  并且向Java基础薄弱的学生推荐了免费的电子书A Java Reference和经典的Head First Java

1.2 Running a Java Program

  基础的讲解用javac和java来编译运行程序,这也应该是所有初学Java的人必须经历的阶段。
这里写图片描述

1.3 Variables and Loops

  因为cs61a主要讲了python,所以Java的基础语法也是跟python对比着讲的。变量和循环也是大致讲解一下:

  • Our variable x must be declared before it is used, and it must be given a type!
  • Our loop definition is contained inside of curly braces, and the boolean expression that is tested is contained inside of parentheses.
  • Our print statement is just System.out.print instead of System.out.println. This means we should not include a newline.
  • Our print statement adds a number to a space. This makes sure the numbers don’t run into each other. Try removing the space to see what happens.
  • When we run it, our prompt ends up on the same line as the numbers (which you can fix in the following exercise if you’d like).

1.4 Static Typing

  作为静态语言,与python不同的是,Java的变量类型一旦声明就无法改变,比如下面的程序就会编译报错:

public class HelloNumbers {
    public static void main(String[] args) {
        int x = 0;
        while (x < 10) {
            System.out.print(x + " ");
            x = x + 1;
        }
        x = "horse";
    }
}

  不过,josh也说这是他最喜欢的Java特性之一,它有以下几个优点:

  • The compiler ensures that all types are compatible, making it easier for the programmer to debug their code.
  • Since the code is guaranteed to be free of type errors, users of your compiled programs will never run into type errors. For example, Android apps are written in Java, and are typically distributed only as .class files, i.e. in a compiled format. As a result, such applications should never crash due to a type error.
  • Every variable, parameter, and function has a declared type, making it easier for a programmer to understand and reason about code.

1.5 Defining Functions in Java

  Java中的函数通常叫做“方法”(method),在定义一个函数时,相比于python的def关键字,Java可以说是非常麻烦了,短期内,你可能会觉得public,static之类的关键字略显冗余,不过编写大型程序的时候你会发现它的优点。
  如果把python的程序比作像Dan Osman那样疯狂的攀登者,快速但是危险,那么Java就是系紧绳索、带好钢盔的攀登者,看起来笨重,不过安全可靠。

1.6 Code Style, Comments, Javadoc

  接下来列举了一些良好的代码风格,这在学习一门语言的初期尤为重要,包括可以自动生成HTML类型的Javadoc,不过没有深入讨论。其实最重要的golden rule就是Write your code so that it is easy for a stranger to understand.

  • Consistent style (spacing, variable naming, brace style, etc)
  • Size (lines that are not too wide, source files that are not too long)
  • Descriptive naming (variables, functions, classes), e.g. variables or functions with names like year or getUserName instead of x or f.
  • Avoidance of repetitive code: You should almost never have two significant blocks of code that are nearly identical except for a few changes.
  • Comments where appropriate. Line comments in Java use the // delimiter. Block (a.k.a. multi-line comments) comments use /* and */.

2. HomeWork

  HW0是用Java实现一些简单的小程序,涉及Reading中的语法以及数组、字符串,比较有意思的地方就是给出了python,matlab,schemeJava多种语言的实现,可以对比着学习。其实如果学习过其他编程语言完全可以忽略这个HW。

3. Lab 1 Setup: Setting Up Your Computer

  在课程初期cs61b给出了非常详细的环境配置方法,包括了Windows,macOS,Linux三种操作系统,安装JDK、安装python3、安装git以及配置环境变量,简直不能再详细了,而且全部都是最新版的,因为课程是今年初刚录的,如果早点发现这门课,完全不用再费劲心思百度。
  另外,还教了简单的Unix终端命令,大概就是cd,pwd,ls以及文件/文件夹的移动、创建、删除。

4. Lab 1: javac, java, git

  除了练习javac,java编译运行.java文件,这个lab主要教了git的使用,因为后续的课程要clone下老师的skeleton,有更新时还要及时pull,而且小组协作也要用到,所以git和github应该是必须要掌握的,其实我觉得廖雪峰讲的也不错,而且内容更多一些。

5. Discussion 1

  第一次的讨论没什么特别之处,简单看看就好。

猜你喜欢

转载自blog.csdn.net/qq_40950957/article/details/81149910