1. 创建 java 文件名需与类名一致。
2. 执行命令 javac 和 java
javac 后面跟着的是 java 文件的文件名,例如 helloword.java。该命令用于将 java 源文件编译成 class 字节码文件,如 javac helloword.java。
运行 javac 命令后,如果成功编译没有错误的话,会出现一个 helloworld.class 的文件。
java 后面跟着的是 java 文件中的类名,例如 helloworld 就是类名,如:java helloworld,表示执行当前类。注意:java 命令后面不要加 .class。
3. 一个 java 程序可以认为是一系列对象的集合,而这些对象通过调用彼此的方法来协同工作。
对象:对象是类的一个实例,有状态和行为。
类:类是一个模版,它描述一类对象的行为和状态。
方法:方法就是行为,一个类可以有很多方法。逻辑运算、数据修改以及所有动作都是在方法中完成的。
实例变量:每个对象都有独特的实例变量,对象的状态由这些实例变量的值决定。
4. 基本语法
1. 大小写敏感
2. 类名:所有的类名的首字母应该大些。如果类名由若干单词组成,那么每个单词的首字母应该大写。
3. 方法名:所有的方法名都应该以小写字母开头。如果方法名含有若干单词,则后面每个单词的首字母大写。
4. 源文件名:源文件名必须和类名相同。当保存文件的时候,应该使用类名作为文件名保存,文件的后缀是.java。(如果文件名和类名不同会导致编译错误)。
5. 主方法入口:所有的Java程序由 public static void main(String []args) 方法开始执行。
5. Java 源程序与编译型运行的区别
虽然 Java 程序的执行过程中包含解释型的过程,但是比一般高级的解释型语言如 Python 要快速很多,接近 C++。
6. 一个类包含的变量类型
局部变量:在方法、构造方法或者语句块中定义的变量被称为局部变量。变量声明和初始化都是在方法中,方法结束后,变量就会自动销毁。访问修饰符不能用于局部变量。
成员变量(实例变量):成员变量是定义在类中,方法体之外的变量。这种变量在创建对象的时候实例化。成员变量能够被类中方法、构造方法和特定类的语句块访问。通过已创建的对象就可以直接访问成员变量。成员变量可以声明在使用前或者使用后。成员变量具有默认值,数值型变量的默认值是0,布尔型变量的默认值是false,引用类型变量的默认值是null。变量的值可以在声明时指定,也可以在构造方法中指定。
类变量(静态变量):类变量也声明在类中,方法体之外,但必须声明为 static 类型。无论一个类创建了多少对象,类只拥有类变量的一份拷贝。静态变量除了被声明为常量外很少使用。常常的声明方式为 public static final ,可以通过 ClassName.VariableName 的方式访问。静态变量在程序开始时创建,在程序结束时销毁。
7. 创建对象
1. 声明:声明一个对象,包括对象名称和对象类型。
2. 实例化:使用关键字 new 来创建一个对象。
3. 初始化:使用 new 创建对象时,会调用构造方法初始化对象。
8. 源文件的声明规则
1. 一个源文件只能有一个 public 类
2. 一个源文件可以有多个非 public 类
3. 源文件的名称应该和 public 类的类名保持一致。
4. 如果一个类定义在某个包中,那么 package 语句应该在源文件的首行。
5. 如果源文件包含 import 语句,那么应该放在 package 语句和类定义之间。如果没有 package 语句,那么 import 语句应该在源文件最前面。
6. import 语句和 package 语句对源文件中定义的所有类都有效。在同一源文件中,不能给不同的类不同的包声明。
7. Java 包主要用来对类和接口进行分类。在 Java 中如果给出一个完整的限定名,包括包名、类名,那么 Java 编译器就很容易地定位到源代码或者类。Import 语句就是用来提供一个合理的路径,使得编译器可以找到某个类。例如下面的命令行将会命令编译器载入 java_installation/java/io 路径下的所有类:import java.io.*;
9. Java 基本数据类型
1. 内置数据类型
Java语言提供了八种基本类型。六种数字类型(四个整数型,两个浮点型),一种字符类型,还有一种布尔型。
byte:
- byte 数据类型是8位、有符号的,以二进制补码表示的整数;
- 最小值是 -128(-2^7);
- 最大值是 127(2^7-1);
- 默认值是 0;
- byte 类型用在大型数组中节约空间,主要代替整数,因为 byte 变量占用的空间只有 int 类型的四分之一;
- 例子:byte a = 100,byte b = -50。
short:
- short 数据类型是 16 位、有符号的以二进制补码表示的整数
- 最小值是 -32768(-2^15);
- 最大值是 32767(2^15 - 1);
- Short 数据类型也可以像 byte 那样节省空间。一个short变量是int型变量所占空间的二分之一;
- 默认值是 0;
- 例子:short s = 1000,short r = -20000。
int:
- int 数据类型是32位、有符号的以二进制补码表示的整数;
- 最小值是 -2,147,483,648(-2^31);
- 最大值是 2,147,483,647(2^31 - 1);
- 一般地整型变量默认为 int 类型;
- 默认值是 0 ;
- 例子:int a = 100000, int b = -200000。
long:
- long 数据类型是 64 位、有符号的以二进制补码表示的整数;
- 最小值是 -9,223,372,036,854,775,808(-2^63);
- 最大值是 9,223,372,036,854,775,807(2^63 -1);
- 这种类型主要使用在需要比较大整数的系统上;
- 默认值是 0L;
- 注意:在初始化long类型的变量时一定要在数值后面加上L,否则将这个数作为整型解析,出现范围溢出的错误。
- 例子: long a = 100000L,Long b = -200000L。
"L"理论上不分大小写,但是若写成"l"容易与数字"1"混淆,不容易分辩。所以最好大写。
float:
- float 数据类型是单精度、32位、符合IEEE 754标准的浮点数;
- float 在储存大型浮点数组的时候可节省内存空间;
- 默认值是 0.0f;
- 浮点数不能用来表示精确的值,如货币;
- 例子:float f1 = 234.5f。
double:
- double 数据类型是双精度、64 位、符合IEEE 754标准的浮点数;
- 浮点数的默认类型为double类型;
- double类型同样不能表示精确的值,如货币;
- 默认值是 0.0d;
- 例子:double d1 = 123.4。
boolean:
- boolean数据类型表示一位的信息;
- 只有两个取值:true 和 false;
- 这种类型只作为一种标志来记录 true/false 情况;
- 默认值是 false;
- 例子:boolean one = true。
char:
- char类型是一个单一的 16 位 Unicode 字符;
- 最小值是 \u0000(即为0);
- 最大值是 \uffff(即为65,535);
- char 数据类型可以储存任何字符;
- 例子:char letter = 'A';。
2. 引用类型
1. 在 Java 中,引用类型的变量非常类似于C/C++的指针。引用类型指向一个对象,指向对象的变量就是引用变量。
2. 对象、数组都是引用数据类型。
3. 所有引用类型的默认值都是NULL。
3. 用 final 关键字来修饰常量。
4. 自动类型转换规则
整型、实型(常量)、字符型数据可以混合运算。运算中,不同类型的数据先转化为同一类型,然后进行运算。
转换从低级到高级。
低 ------------------------------------> 高 byte,short,char—> int —> long—> float —> double
注意:不能对boolean类型进行类型转换。
5. 强制类型转换用于高精度、高容量的数据类型转换为低精度、低容量的,用于在变量或者数据前加括号内数据类型。格式:(type) value type。
10. Java修饰符
1. 访问控制修饰符
Java中,可以使用访问控制符来保护对类、变量、方法和构造方法的访问。Java支持4种不同的访问权限。
默认的,也称为 default,在同一包内可见,不使用任何修饰符。对同一个包内的类是可见的,接口里的变量都隐式声明为 public static final,接口里的方法默认访问权限为 public。
私有的,以 private 修饰符指定,在同一类内可见。被声明为 private 的方法、变量和构造方法只能被所属类访问,并且类和接口不能声明为 private。声明为私有访问类型的变量只能通过类中公共的 getter 方法被外部类访问。
共有的,以 public 修饰符指定,对所有类可见。
受保护的,以 protected 修饰符指定,对同一包内的类和所有子类可见。
修饰符 | 当前类 | 同一包内 | 子孙类 | 其他包 |
---|---|---|---|---|
public | Y | Y | Y | Y |
protected | Y | Y | Y | N |
default | Y | Y | N | N |
private | Y | N | N | N |
访问控制和继承:
1. 父类中声明为 public 的方法在子类中也必须是 public。
2. 父类中声明为 protected 的方法在子类中要么声明为 protected,要么声明为 public,不能声明为 private。
3. 父类中声明为 private 的方法,不能够被继承。
11. Java 运算符
>>>> 按位右移补零操作符。左操作数的值按右操作数指定的位数右移,移动得到的空位以零填充。
(expression) ? value : vlaue 条件运算符,前面的条件需要打括号。
12. Java 循环结构
Java 增强for循环 for (声明语句 : 表达式) { //代码句子 }
声明语句:声明新的局部变量,该变量的类型必须和数组的元素类型相匹配。其作用域限定在循环语句块。
表达式:表达式就是要访问的数组名,或者是返回值为数组的方法。
13. Number & Math 类
当需要使用数字时,我们通常使用内置数据类型,如:byte、int、long等。但在实际开发过程中,经常会遇到使用对象,而不是数字类型的情况,所以每一个内置数据类型都有对应的包装类。包括 Integer、Long、Byte、Double、Float、Short 都是抽象类 Number 的子类。这种由编译器特别支持的包装称为装箱,所以当内置数据类型被当作对象使用时,编译器会把内置类型装箱为包装类。相似的,编译器也可以把一个对象拆箱为内置类型。Number 类属于 java.lang 包。
Math 类包含的方法:
序号 | 方法与描述 |
---|---|
1 | xxxValue() 将 Number 对象转换为xxx数据类型的值并返回。 |
2 | compareTo() 将number对象与参数比较。 |
3 | equals() 判断number对象是否与参数相等。 |
4 | valueOf() 返回一个 Number 对象指定的内置数据类型 |
5 | toString() 以字符串形式返回值。 |
6 | parseInt() 将字符串解析为int类型。 |
7 | abs() 返回参数的绝对值。 |
8 | ceil() 对整形变量向左取整,返回类型为double型。 |
9 | floor() 对整型变量向右取整。返回类型为double类型。 |
10 | rint() 返回与参数最接近的整数。返回类型为double。 |
11 | round() 返回一个最接近的int、long型值。 |
12 | min() 返回两个参数中的最小值。 |
13 | max() 返回两个参数中的最大值。 |
14 | exp() 返回自然数底数e的参数次方。 |
15 | log() 返回参数的自然数底数的对数值。 |
16 | pow() 返回第一个参数的第二个参数次方。 |
17 | sqrt() 求参数的算术平方根。 |
18 | sin() 求指定double类型参数的正弦值。 |
19 | cos() 求指定double类型参数的余弦值。 |
20 | tan() 求指定double类型参数的正切值。 |
21 | asin() 求指定double类型参数的反正弦值。 |
22 | acos() 求指定double类型参数的反余弦值。 |
23 | atan() 求指定double类型参数的反正切值。 |
24 | atan2() 将笛卡尔坐标转换为极坐标,并返回极坐标的角度值。 |
25 | toDegrees() 将参数转化为角度。 |
26 | toRadians() 将角度转换为弧度。 |
27 | random() 返回一个随机数。 |
14. Character 类
Character 类用于对单个字符进行操作,它是基本数据类型 char 的包装类。其方法如下:
序号 | 方法与描述 |
---|---|
1 | isLetter() 是否是一个字母 |
2 | isDigit() 是否是一个数字字符 |
3 | isWhitespace() 是否是一个空格 |
4 | isUpperCase() 是否是大写字母 |
5 | isLowerCase() 是否是小写字母 |
6 | toUpperCase() 指定字母的大写形式 |
7 | toLowerCase() 指定字母的小写形式 |
8 | toString() 返回字符的字符串形式,字符串的长度仅为1 |
15. String 类
在 Java 中,字符串属于对象,Java 提供了 String 类来创建和操作字符串。创建字符串最简单的方法是:String greeting = "菜鸟教程";
String 类是不可改变的,一旦创建了 String 对象,那它的值就无法改变了。如果需要对字符串做很多修改,就应该选择 StringBuffer 类。String 类的方法包括: