本章主题
*语句和表达式
*变量和基本数据类型
*常量
*注释
*字面量
*表达式和运算符
*比较
*逻辑运算符
*语句和表达式
语句是导致事情发生的简单命令。产生返回值的语句叫表达式。
*变量和基本数据类型
变量有实例变量,类变量和本地变量。Java中没有全局变量,实例变量和类变量代替了它的作用。本地变量必须赋值,否则编译时会报错。实例变量,类变量有默认值:数值类型0;字符串类型'\0';布尔类型:false;对象类型:null;
变量名要以字母、下划线或者$开头,后面可以包含字母和数字的组合。区分大小写。
本书变量的命名规则:第一个单词小写,后面的单词一个字母大写。
变量类型可以为基本类型,类或者接口名,数组。
8种基本类型,内置于java中,在所有操作系统中占用的位数相同。4种整型如下,注意没有无符号类型。
Type Size Values That Can Be Stored
byte 8 bits -128 to 127
short 16 bits -32,768 to 32,767
int 32 bits -2,147,483,648 to 2,147,483,647
long 64 bits -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
另有float,double,char,Boolean。如果算上void,有九种基本类型。
float的范围:1.4E-45 to 3.4E+38
double的范围:4.9E-324 to 1.7E+308
注意这些类型和Boolean,Char类等不是一回事。
超类对象可以访问子类。
*常量
final float PI = 3.141592;
final boolean DEBUG = false;
final int PENALTY = 25;
public class Variables {
public static void main(String[] arguments) {
final char UP = 'U';
byte initialLevel = 12;
short location = 13250;
int score = 3500100;
boolean newGame = true;
System.out.println("Level: " + initialLevel);
System.out.println("Up: " + UP);
}
}
System.out.print()与System.out.println类似,只是没有添加换行。
*注释
注释类型,//,/*,/**同事可以用于java的文档,可以被JDK中的javadoc识别,会生成HTML记录
*字面量
字面量:整型数值自动俺int处理,如果int无法容纳,则使用long。强制为long:4L。浮点数默认按double处理,强制:3.1415927F。布尔型只有true和false两个值,没有其他语言中的0,1等。转义字符表见教材。字符类型是16位的。8进制:0777;16进制:0x12。指数类型的如下:
double x = 12e22;
double y = 19E-95;
转义字符 含义
\n New line
\t Tab
\b Backspace
\r Carriage return
\f Formfeed
\\ Backslash
\’ Single quotation mark
\” Double quotation mark
\d Octal
\xd Hexadecimal
\ud Unicode character
字符串常量在java中是对象,不适存储在数组中的。
*表达式和运算符
算数运算符:
Operator Meaning Example
+ Addition 3 + 4
- Subtraction 5 - 7
* Multiplication 5 * 5
/ Division 14 / 7
% Modulus 20 % 7
赋值可以这样:x = y = z = 7;结合顺序是从右到左。
Expression Meaning
x += y x = x + y
x -= y x = x - y
x *= y x = x * y
x /= y x = x / y
*比较
Operator Meaning Example
== Equal x == 3
!= Not equal x != 3
< Less than x < 3
> Greater than x > 3
<= Less than or equal to x <= 3
>= Greater than or equal to x >= 3
*逻辑运算符
AND, OR, XOR NOT
Java中&和&&都是与,不过&不管前面计算是否成功,后面的也要计算,类似的有|。+还可以连接字符串等。
运算优先级:
n Increment and decrement operations
n Arithmetic operations
n Comparisons
n Logical operations
n Assignment expressions
如果优先级相同,则从左至右
Operator Notes
. [] () Parentheses (“()”) are used to group expressions; a period (“.”) is
used for access to methods and variables within objects and
classes (discussed tomorrow); square brackets (“[]”) are used for
arrays. (This operator is discussed later in the week.)
++ — ! ~ instanceof The instanceof operator returns true or false based on whether
the object is an instance of the named class or any of that class’s
subclasses (discussed tomorrow).
new (type)expression The new operator is used for creating new instances of classes;
“()” in this case are for casting a value to another type. (You learn
about both of these tomorrow.)
* / % Multiplication, division, modulus.
+ - Addition, subtraction.
<< >> >>> Bitwise left and right shift.
< > <= >= Relational comparison tests.
== != Equality.
& AND
^ XOR
| OR
&& Logical AND
|| Logical OR
? : Shorthand for if-then-else (discussed on Day 5).
= += -= *= /= %= ^= Various assignments.
&= |= <<= >>= >>>= More assignments.
阅读(3267) | 评论(0) | 转发(0) |