概述
Lecture 01 Introduction to Java and Algorithm
Introduction
What is this Course about?
-
Writing code that runs efficiently.
- Good algorithms.
- Good data structures.
-
Writing 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!)
Assumes solid foundation in programming fundamentals, including:
Object oriented programming, recursion, lists, and trees.
Why Study Algorithms or Data Structures?
-
Daily life is supported by them.
-
Major driver of current progress (?) of our civilization.
-
To become a better programmer.
“The difference between a bad programmer and a good one is whether [the programmer] considers code or data structures more important. Bad programmers worry about the code. Good programmers worry about data structures and their relationships.”
------ Linus Torvalds (Creator of Linux)
-
Being an efficient programmer means using the right data structures and algorithms for the job.
-
To understand the universe. Science is increasingly about simulation and complex data analysis rather than simple observations and clean equations.
Hello World!
Java and Object Orientation
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world");
}
}
/*
1. In Java, all code must be part of a class.
2. Classes are defined with "public class CLASSNAME"
3. We use { } to delineate the beginning and ending of things.
4. We must end lines with a semicolon.
5. The code we want to run must be inside
public static void main(String[] args).
*/
Java is an object oriented language with strict requirements:
-
Every Java file must contain a class declaration*.
-
All code lives inside a class*, even helper functions, global constants, etc.
-
To run a Java program, you typically define a main method using
public static void main(String[] args)
*: This is not completely true, e.g. we can also declare
interfaces
in.java
files that may contain code. We’ll cover these later.
Java and Static Typing
public class HelloNumbers {
public static void main(String[] args) {
int x = 0;
while (x < 10) {
System.out.println(x);
x = x + 1;
}
}
}
/*
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!!!
*/
/** Demonstrates creation of a method in Java. */
public class LargerDemo {
/** Returns the larger of x and y. */
public static larger(int x, int y) {
if (x > y) {
return x;
}
return y;
}
public static void main(String[] args) {
System.out.println(larger(-5, 10));
}
}
/*
1. Functions must be declared as part of a class in Java.
A function that is part of a class is also called a "method".
So in Java, all functions are methods.
2. To define a function in Java, we use "public static".
We will see alternate ways defining functions later.
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!
*/
Java is statically typed!
-
All variables, parameters, and methods must have a declared type.
-
That type can never change.
-
Expressions also have a type, e.g.
larger(5, 10) + 3
has type int. -
The compiler checks that all the types in your program are compatible before the program ever runs!
- e.g.
String x = larger(5, 10) + 3
will fail to compile. - This is unlike a language like Python, where type checks are performed DURING execution.
- e.g.
Reflections on Static Typing
The Good:
- Catches certain types of errors, making it easier on the programmer to debug their code.
- Type errors can (almost) never occur on the end user’s computer.
- Makes it easier to read and reason about code.
- Code can run more efficiently, e.g. no need to do expensive runtime type checks.
The Bad:
- Code is more verbose.
- Code is less general.
最后
以上就是任性飞鸟为你收集整理的ADS-Lecture 01 Introduction to Java and Algorithm的全部内容,希望文章能够帮你解决ADS-Lecture 01 Introduction to Java and Algorithm所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复