简单JAVA语法知识归纳

In the following study on data structures, i will use JAVA as the main coding language to learn this topic. Below is some basic introductions of JAVA, they serve as both notes and future reference.

Java and Object Orientation

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

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.

The notes above can be seen in this google doc: https://docs.google.com/presentation/d/1KY_fYB2n1SMX105YiM1QT58I-zNR0M823h71C9UXCxs/edit#slide=id.g397e8bf9f_03

 

 

Key Syntax Features. Our first programs reveal several important syntax features of Java:

  • All code lives inside a class.
  • The code that is executed is inside a function, a.k.a. method, called main.
  • Curly braces are used to denote the beginning and end of a section of code, e.g. a class or method declaration.
  • Statements end with semi-colons.
  • Variables have declared types, also called their “static type”.
  • Variables must be declared before use.
  • Functions must have a return type. If a function does not return anything, we use void,
  • The compiler ensures type consistency. If types are inconsistent, the program will not compile.

 

Static Typing. Static typing is (in my opinion) one of the best features of Java. It gives us a number of important advantages over languages without static typing:

  • Types are checked before the program is even run, allowing developers to catch type errors with ease.
  • If you write a program and distribute the compiled version, it is (mostly) guaranteed to be free of any type errors. This makes your code more reliable.
  • Every variable, parameter, and function has a declared type, making it easier for a programmer to understand and reason about code.

There are downside of static typing, to be discussed later.

Coding Style. Coding style is very important in 61B and the real world. Code should be appropriately commented as described in the textbook and lectures.

Command line compilation and execution. javac is used to compile programs. java is used to execute programs. We must always compile before execution.

The notes above can be seen on the website: https://fa20.datastructur.es/materials/lectures/lec1/lec1.html

 

posted @ 2020-12-19 18:31  M1stF0rest  阅读(75)  评论(1编辑  收藏  举报