1.1 Essentials
Java
- consists class declaration “public class”
- all code in Java must be part of class
- curly braces “{“ and “}” to denote the beginning and end of section
- all statement in Java must end in a semi-colons “;”
- for code to run, need “public static void main(String[] args)”
- static typing
Compliation
Most common way to execute Java program:
Hello.java -> javac[Compiler] -> (generate)Hello.class -> java[Interpreter] -> run
why makes class file?
- .class file has been type checked, distributed code is safer.
- .class files simpler for machine to execute, distributed code is faster.
Variables and Loops
- before java variables can be used, they must be declared
- java variables must have a specific type
- java variable types can never change
- types are verified before the code even runs
exercise:1
2String h = 5 + "horse"; /** return 5horse */
int h = 5 + "horse"; /** compiler error, string can not change to int */
For python, it does not work for both ways as python does not need to declare type.1
2System.out.println(5+"10"); /** return string concatenate string 510 */
System.out.println(5+10); /** return sum of int 15 */
print methods in Java
- System.out.println -> print with newline
- System.out.print -> print without newline
- System.out.printf("%n") -> print new line
Function
- functions must be declared as part of a class in Java
- a function is called a method, all functions in Java are methods
- to define a function, use “public static”
- all parameters of a function must have a declared type, and the return value of the function must have a declared type.
- functions in Java return only one value
Exercise
- “break” keyword, completely terminates the innermost loop when it is called
- “continue” keyword, skips the current iteration of the loop, effectively jumping straight to the increment condition
- enhanced for loop is when index does not matter, no need to create index variable
1
2
3
4String[] a = {"cat", "dog", "laser horse", "ketchup", "horse", "horbse"};
for (String s : a) {
... /** s takes on the identity of each String in a exactly once */
}
Style guild
https://sp19.datastructur.es/materials/guides/style-guide.html
1.2 Objects
Defining and Using Classes
- a class that uses another class is called a “client” of that class, re-generate client class to reflect that class’s changes
- constructors in Java help to save time of mannually typing out potentially many instance variable assignments as it can been instantiated with parameters
- declare variable, which is to store value
- instantiate a class as Object
- assign instantiation to declared variable
- variable can invoke instance method / class name invoke static method
class(static) methods vs. instance(non-static) methods
- static methods are invoking using the class name, eg. Dog.makeNoise()
- can have static variable, better access by class name
- non-static methods are invoked by an instance name, eg. maya.makeNoise()
- static method access instance variable -> must specify variable
public static void main(String[] args)
public <- all methods start with this
static <- declare static method
void <- method return nothing, no return type
main <- main of method
String[] args <- parameter pass to main method1
2
3
4
5
6
7public class ArgsDemo {
public static void main(String[] args) {
System.out.println(args[0]);
}
}
java ArgsDemo hello world /** return hello */s
references
- https://joshhug.gitbooks.io/hug61b/content/chap1/chap11.html
- https://sp18.datastructur.es/materials/hw/hw0/hw0.html
- https://sp18.datastructur.es/materials/lab/lab1/lab1.html
- https://sp18.datastructur.es/materials/discussion/disc01.pdf
- https://sp19.datastructur.es/materials/proj/proj0/proj0
- https://sp19.datastructur.es/materials/discussion/disc02.pdf