简单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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通