1. Introduction to Java

1.1 Essentials

Java

  1. consists class declaration “public class”
  2. all code in Java must be part of class
  3. curly braces “{“ and “}” to denote the beginning and end of section
  4. all statement in Java must end in a semi-colons “;”
  5. for code to run, need “public static void main(String[] args)”
  6. static typing

Compliation

Most common way to execute Java program:

Hello.java -> javac[Compiler] -> (generate)Hello.class -> java[Interpreter] -> run

why makes class file?

  1. .class file has been type checked, distributed code is safer.
  2. .class files simpler for machine to execute, distributed code is faster.

Variables and Loops

  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 before the code even runs

exercise:

1
2
String 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
2
System.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

  1. functions must be declared as part of a class in Java
    • a function is called a method, all functions in Java are methods
  2. to define a function, use “public static”
  3. 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
    4
    String[] 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
  1. declare variable, which is to store value
  2. instantiate a class as Object
  3. assign instantiation to declared variable
  4. 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 method

1
2
3
4
5
6
7
public class ArgsDemo {
public static void main(String[] args) {
System.out.println(args[0]);
}
}

java ArgsDemo hello world /** return hello */s

references

# java

Commentaires

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×