Summary
- Hello World
- Static Typing
- Declaring Function
Hello World
- All code in Java must be part of a class.
- We delimit the beginning and end of segments of code using { and }.
- All statements in Java must end in a semi-colon.
- For code to run we need public static void main(String[] args)
# hello.py
print("hello world")
// hello.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world");
}
}
Static Typing
- 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!!!
# hellonumbers.py
x = 0
while x < 10:
print(x)
x = x + 1
x = "horse"
print(x)
print(5 + "horse")
// hellonumbers.java
public class HelloNumbers {
public static void main(String[] args) {
int x = 0;
while (x < 10) {
System.out.println(x);
x = x + 1;
}
x = "horse";
}
}
To summarize, static typing has the following advantages:
- 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.
The Bad:
- Code is more verbose.
- Code is less general.
Declaring Function
- Functions 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.
- To define a function in Java, we use "public static".
We will see alternate ways of defining functions later.
- 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!
# Intro - larger.py
def larger(x, y):
"""Returns the larger of x and y."""
if (x > y):
return x
return y
print(larger(-5, 10))
// LargerDemo.java
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.5, 10));
}
}
Code Style, Comments, Javadoc
In this course, we'll work hard to try to keep our code readable. Some of the most important features of good coding style are:
- 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 */.
The golden rule is this: Write your code so that it is easy for a stranger to understand.
Comments
-
Methods should have javadoc comments explaining the behavior, parameters (using @PARAM tags or otherwise), and return type.
-
Methods that return non-void values must describe them in their Javadoc comment either with a “@return” tag or in a phrase in running text that contains the word “return”, “returning”, or “returns”.
-
Each Javadoc comment must start with a properly formed sentence, starting with a capital letter and ending with a period.
Limits
-
No file may be longer than 2000 lines.
-
No line may be longer than 100 characters.
-
No method may be longer than 80 lines.
-
No method may have more than 8 parameters.
-
Every file must contain exactly one outer class (nested classes are OK).
style guide Author: Alan Yao
Summary
Hello World
Static Typing
To summarize, static typing has the following advantages:
The Bad:
Declaring Function
A function that is part of a class is called a "method".
So in Java, all functions are methods.
We will see alternate ways of defining functions later.
and the return value of the function must have a declared type.
Functions in Java return only one value!
Code Style, Comments, Javadoc
In this course, we'll work hard to try to keep our code readable. Some of the most important features of good coding style are:
The golden rule is this: Write your code so that it is easy for a stranger to understand.
Comments
Methods should have javadoc comments explaining the behavior, parameters (using @PARAM tags or otherwise), and return type.
Methods that return non-void values must describe them in their Javadoc comment either with a “@return” tag or in a phrase in running text that contains the word “return”, “returning”, or “returns”.
Each Javadoc comment must start with a properly formed sentence, starting with a capital letter and ending with a period.
Limits
No file may be longer than 2000 lines.
No line may be longer than 100 characters.
No method may be longer than 80 lines.
No method may have more than 8 parameters.
Every file must contain exactly one outer class (nested classes are OK).
style guide Author: Alan Yao