CS61B(1): Intro, Hello Java World

This is the lecture note of CS61B - Lecture 1.

Overview

Welcome to CS61B!

This course will teach you

  • how to write code that runs efficiently.
    • Good algorithms.
    • Good data structures.
  • how to write code efficiently.
    • Designing, building, testing and debugging large programs.
    • Use of programming tools.
      • git, IntelliJ, JUnit, and various command line tools.
    • Java (not the focus of the course!)

This course assumes solid foundation in programming fundamentals, including OOP, recursion, lists and trees. You can learn CS61A if you do not have these concepts.

Hello Java World

Python program vs. Java program

1
2
3
# in Python

print("hello world")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// in Java

public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world!");
}
}

/*
1. All code in Java must be part of a class. *
(This is not completely true, e.g. we can also declare “interfaces”.
We will talk about them later on.)
2. We delimit the begin and end of segments of code using { and }.
3. All statements in Java must end in a semi-colon.
4. For code to run we need "public static void main(String[] args)".
This main method can call other methods/classes in the program.
*/

Java is an object-oriented language. Every Java file must contain either a class, interface, or enum.

Static Typing

  • Python variables vs. Java variables
1
2
3
4
5
6
7
8
9
10
11
12
x = 0 
while x < 10:
print(x)
x += 1

x = "horse"
print(x)

# print(5 + "horse") # error when running


# Python code can crash due to type errors when its running!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class HelloNumbers {
public static void main(String[] args) {
int x = 0;
while (x < 10) {
System.out.println(x);
x += 1;
}

// x = "horse"; // error when compiling: cannot convert from String to int
}
}

/*
1. Before Java variables can be used, they must be declared.
2. Java variables must have a specific type.
3. Java variable types can never change.
4. Types are verified (by compiler) before the code even runs!
*/

Declaring Functions

  • Python functions vs. Java functions
1
2
3
4
5
6
7
def larger(x, y):
"""Returns the larger of x and y."""
if x > y:
return x
return y

print(larger(-5, 10)) # 10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class LargerDemo {

/** Returns the larger of x and y. */
public static int larger(int x, int y) {
if (x > y) {
return x;
}
return y;
}

public static void main(String[] args) {
System.out.println(larger(-5, 10)); // 10
}
}

/*
1. Function must be declared as part of a class in Java.
A function that is part of a class is called a "method".
So in Java, all functions are methods.
2. To define a function a Java, we use "public static".
We will see alternate ways of defining functions later.
3. All parameters of a function must have a declared type,
and the return value of the function also must have a declared type.
Functions in Java return only one value!
(Python can return multiple values.)
*/