Week 01: Java Basics, Class Method, Static
1.1 Intro, Hello World Java
Introduction: What is 61B about?
Writing codes that runs efficiently. (Algorithms and data structures.)
Writing code efficiently.
First Java Program
Hello World
All codes must be in a class.
Curly braces and semi-colons.
Variables have declared types and must be declared before use.
Functions must have a return type or declared as void function.
The compiler ensures type consistency.
Compilation
The common way to run a Java program is to run it through two programs: javac
and java
.
Defining Functions
Since all Java code is part of a class, we must define functions so that they belong to some class. Functions that are part of a class are commonly called "methods".
For instance, here's a piece of code in Java which defining a function that could compare two integers and return the larger one.
Code Style
In this course, we will aim to make our code readable. Here are some tips to achieve that goal:
Consistent style
Comments (use
//
for line comments and/*
and*/
for block comments)Avoidance of repetitive code
Descriptive naming (variables, functions, classes)
Moreover, we could use Javadoc to automatically generate documents.
1.2 Defining and Using Classes
Defining Classes
Every method is associated with some class.
To run a class, we must define a main method.
The code above can't be run directly because there's no main method.
However, the method could be called from another class.
Object Instantiation
Classes can contain not just methods, but also data, such as the
size
of each dog.Classes can be instantiated as objects. For example, we will create a single Dog class, and then create instances of this Dog.
Here's a Dog class which provdes a blueprint of Dog objects.
Array of Objects
To create an array of objects:
First use the new keyword to create the array.
Then use new again for each object that you want to put in the array.
Staitc vs. Non-static
Static in C:
In function, the value of this variable is maintained every time the function is invoked
In a .c file, the function/global variable is only accessible in this file
In Jave, static generally means a variable/method "shared by every instance" and is therefore invoked by the class name.
Key difference between static and non-static methods:
Static method are invoked using the class name.
Instance methods are invoked using an instance name.
Static method can't access instance variables, but are more simple.
Sometimes, a class may contain both static and non-static methods.
We could declare static variables which are properties shared by all instances of the class.
An inner class can also be static. Its difference with a non-static nested class is:
A static nested class may be instantiated without instantiating the outer class;
A static inner class can only access the static members of the outer class, while a non-static class can access all the members.
A variable or method defined in a class is also called a member of that class.
Static members are accessed using class name.
Non-static members cannot be invoked using class name.
Static methods must access instance variables via a specific instance.
A deeper look at: public static void main(String[] args)
public
: So far, all of our methods start with this keyword.static
: It is a static method, not associated with any particular instance.void
: It has no return type.main
: This is the name of the method.String[] args
: This is a parameter that is passed to the main method.
For example, this program prints out the first command line argument.
Using Libraries
In CS 61B, we will use libraries include:
The built-in Java libraries such as Math, String, Integer, List, Map.
The Princeton standard library such as StdDraw, StdAudio, In.
Last updated