Chapter 1-3
Java 中 char
占据两个字节 (也就是宽字节,本身就超出了 ASCII 可以表示的范围)
6,原来Unicode的转变是发生在代码分析之前的
换句话说,就是先把这些Unicode在源码基础上替换,然后再继续编译
所以有后面说的看起来是一个字符串,但实际上是两个字符串相连;在写注释的时候也有可能无意间换行掉了
"\u0022 + \u0022" // is not a string like:"+". Instead, an empty string
In Java, var
as auto
in C/C++. And defination and declaration 是一起的
只能用 initialized 变量,不然会报错。Note that the var
keyword can only be used with local variables inside methods. You must always declare the types of parameters and fields.
we will use the var
notation in those cases where the type is obvious from the right-hand side without any knowledge of the Java API. But we won’t use var
with numeric types such as int
, long
, or double
so that you don’t have to look out for the difference between 0
, 0L
, and 0.0
.
Cause, const is a reserved keyword, so we use final
to modify a constant
Set up a class constant with the keywords static final
.
floorMod(position+adjustment, mod)
method always yields a value between 0 and “mod”
Iplicit type conversion happens in such precendence:
double > float > long > int
都是 有则变则,
Unlike C/C++, boolean is not a numeric type in Java.
b ? 1 : 0; // so if want to convert bool to int, don't cast
也因此,不要像 C 那样,直接在条件判断里放变量
int x = 1;
x += 3.5; // equivalent to x = (int) (x + 3.5)
讲道理这里是先把x变double,再加,再变成int
不过看起来是把3.5变成3再加
位运算的shift的移位,也是模运算体系的
有 >>>
保证补位为0而非符号 (反过来的没有,反过来不存在这种情况)
没有逗号运算符,但是不妨碍for中可以使用
Chapter 4
Classes
The bits of data in an object are called its instance fields, and the procedures that operate on the data are called its methods.
A specific object will have specific values of its instance fields. The set of those values is the current state of the object. Whenever you invoke a method on an object, its state may change.
The concept of extending a class to obtain another class is called inheritance. a “cosmic superclass” called Object
. All other classes extend this class.
Objects
three key characteristics of objects:
- The object’s behavior—what can you do with this object, or what methods can you apply to it?
- The object’s state—how does the object react when you invoke those methods?
- The object’s identity—how is the object distinguished from others that may have the same behavior and state?
⚠️ These key characteristics can influence each other. For example, the state of an object can influence its behavior.
⚠️ If an object’s state changed without a method call on that object, someone broke encapsulation.