Skip to content

Java basics

Java for AP CS A Lesson 1 1:47 English narration · English + 中文 subtitles burned in

space play · ←/→ 5s · j/l 10s · f fullscreen · ,/. speed

Chapters

Transcript
AP Computer Science A uses Java, and Java looks intimidating on day one because of the ceremony around your code. AP 计算机科学 A 用的是 Java, 而 Java 第一天看上去唬人,是因为你的代码外面裹着一层层"排场"。
So look at what is actually there. 那就看看里面到底有什么。
In the middle are your statements — the lines that do the work. 最中间是你的语句——真正干活的那几行。
Those are wrapped in main, which is where the program starts. 它们被包在 main 里面,程序就是从 main 开始跑的。
And main is wrapped in a class, because in Java everything lives inside a class. 而 main 又被包在一个类里面, 因为在 Java 里,一切都住在类里面。
Three layers, and only the middle one changes from program to program. 三层,而从一个程序到另一个程序,只有最里面那一层会变。
The line that gets something out is System dot out dot println. 让程序输出点东西的那一行是 System.out.println。
It prints what you give it and moves to a new line. 它把你给它的东西打印出来,然后换行。
And notice the second one: a plus sign between text and a number joins the number onto the end, so Grade twelve comes out as one line. 注意第二句:文字和数字之间的那个加号 把数字接到了文字后面, 所以 "Grade 12" 是作为一行输出的。
Two things to be careful of — the capital S in System, and that every statement ends with a semicolon. 有两处要小心——System 的大写 S, 以及每一条语句都要以分号结尾。
The other big difference from Python is types. 和 Python 的另一个大区别,是"类型"。
In Java every variable is declared with a type, and that type is fixed. 在 Java 里,每个变量声明时都要带一个类型,而且这个类型是固定的。
int holds a whole number. int 存整数。
double holds a decimal. double 存小数。
boolean holds true or false. boolean 存 true 或 false。
And String holds text. 而 String 存文本。
Once a variable is an int, it is an int — its type cannot change. 一个变量一旦是 int,它就是 int——它的类型不能改。
Four things to take with you. 带走四点。
One: every Java program lives inside a class. 第一:每个 Java 程序都住在一个类里面。
Two: main is where the program starts running. 第二:程序是从 main 开始跑的。
Three: System dot out dot println prints one line. 第三:System.out.println 打印一行。
Four: declare the type, and end statements with a semicolon. 第四:声明类型,并且语句要以分号结尾。
Now write your first Java program in the task below. 现在去下面的题里写你的第一个 Java 程序。

Log in or create account

IGCSE, A-Level & AP