Variables and Data Types · 变量与数据类型
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| variable/ˈveərɪəbl/ | 变量 | biàn liàng |
| primitive types/ˈprɪmɪtɪv taɪps/ | 基本类型 | jī běn lèi xíng |
| reference types/ˈrefrəns taɪps/ | 引用类型 | yǐn yòng lèi xíng |
A variable is a labelled box
- A variable 变量 is a named place in memory that stores a value.
- Give it a type, a name, and (usually) a starting value:
int score = 0; - Read it or change it by name; the box holds the latest value.
- Every variable in Java must be declared with a type before use.
变量是一个贴标签的盒子
- 变量是内存中一个有名字的、存储值的地方。
- 给它一个类型、一个名字、以及(通常)一个初始值:
int score = 0; - 按名字读取或改变它;盒子保存着最新的值。
- Java 中每个变量在使用前都必须用类型声明。
The primitive types
- Java has eight primitive types 基本类型; four matter most for AP:
int— whole numbers (e.g.42).double— decimals (e.g.3.14).boolean—trueorfalse.char— a single character ('A').- Primitives store the actual value directly in the box.
基本类型
- Java 有八种基本类型;对 AP 最重要的有四种:
int——整数(如42)。double——小数(如3.14)。boolean——true或false。char——单个字符('A')。- 基本类型把实际的值直接存进盒子。
Reference types
- Non-primitive types (like
String) are reference types 引用类型. - A reference variable stores the address of an object, not the object itself.
String name = "Ada";—namepoints to a String object.- Primitive vs. reference is a distinction you'll use all course.
引用类型
- 非基本类型(如
String)是引用类型。 - 一个引用变量存储对象的地址,而非对象本身。
String name = "Ada";——name指向一个 String 对象。- 基本类型对引用类型是你整门课都会用到的区分。
Naming and declaring
- Names start with a letter; use camelCase (
highScore,numStudents). - Declaring reserves the box; initializing gives it a first value.
- A
finalvariable is a constant — its value can't change after it's set. - Choose clear names — code is read far more than it's written.
命名与声明
- 名字以字母开头;使用驼峰命名法(
highScore、numStudents)。 - 声明预留盒子;初始化给它第一个值。
- 一个
final变量是常量——设定后它的值不能改变。 - 选择清晰的名字——代码被阅读的次数远多于被书写。
int and double are different types — mind the division. int division throws away the remainder: 7 / 2 is 3, not 3.5. To get 3.5, at least one operand must be a double (7.0 / 2). Storing a decimal in an int won't even compile without a cast. Pick the type that matches the values you need.
int 和 double 是不同的类型——注意除法。int 除法丢掉余数:7 / 2 是 3,而非 3.5。要得到 3.5,至少一个操作数必须是 double(7.0 / 2)。把小数存进 int 若不做类型转换甚至无法编译。选择与你所需值相匹配的类型。
Tracing a few declarations:
int radius = 5;→ the boxradiusholds5.double pi = 3.14159;→piholds3.14159.radius = 10;→ the same box now holds10(the old5is gone).
追踪几个声明:
int radius = 5;→ 盒子radius保存5。double pi = 3.14159;→pi保存3.14159。radius = 10;→ 同一个盒子现在保存10(旧的5没了)。
A variable is a typed, named storage box. The core primitive types are int, double, boolean, and char, which store a value directly; reference types like String store the address of an object. Declare with a type, use camelCase, and remember int division drops the remainder.
变量是一个有类型、有名字的存储盒子。核心的基本类型是 int、double、boolean 和 char,它们直接存储值;像 String 这样的引用类型存储对象的地址。用类型声明,使用驼峰命名法,并记住 int 除法丢掉余数。
Tracing variable values · 追踪变量的值
Step through to watch each box take its value. · 逐步执行,观察每个盒子取到它的值。
Which type stores whole numbers like 42? · 哪种类型存储像 42 这样的整数?
int stores integers; double stores decimals. · int 存整数;double 存小数。
In Java, what is the value of the int expression 7 / 2? · 在 Java 中,int 表达式 7 / 2 的值是多少?
int division drops the remainder: 7/2 = 3. · int 除法丢掉余数:7/2 = 3。
A variable that stores the address of an object (not the value itself) has a... · 一个存储对象地址(而非值本身)的变量是……
String and other objects are reference types. · String 和其他对象是引用类型。
Which is the conventional Java naming style for a variable? · 哪个是 Java 变量的惯用命名风格?
Variables use camelCase in Java. · Java 变量使用驼峰命名法。
A variable declared final can have its value changed later. · 用 final 声明的变量之后可以改变它的值。
final makes it a constant — the value can't change. · final 使它成为常量——值不能改变。