简单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 @   M1stF0rest  阅读(77)  评论(1编辑  收藏  举报
编辑推荐:
· 基于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最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示