java的语法知识_简单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 bothnotes 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值