本文将从基本数据类型、控制语句、类等方面来简述Java语言的基本语法。
1 基本数据类型 1.1 整数 1个字节为8位2进制数,能容下的数据为2^8 = 256种,unsigned表示无符号变量,不存在负值,故unsigned int的范围是0 ~ 255,以此类推
类型
字节数
范围
byte
1
-128 ~ 127
short
2
-32768 ~ 32767
int
4
-2147439648 ~ 2147438647
long
8
/
unsigned byte
1
0 ~ 255
unsigned short
2
0 ~ 65535
unsigned int
4
0 ~ 4294967295
unsigned long
8
/
1.2 浮点数
类型
字节数
指数位
符号位
尾数位
指数范围
数据范围
精度
float
4
8
1
23
-128 ~ 127
±3.4E-38 ~ 3.4E38
6-7
double
8
11
1
52
-1024 ~ 1023
±1.7E-308 ~ 1.7E308
15-16
尾数位计算2E23 = 8388608,故float最多能保留7位有效数字,绝对精准地保留6位;2E11 = 256,故float的指数范围为-128 ~ 127,因此2^127 = 3.4E38就是数据范围,可以此类推(没有unsigned)
1.3 字符和字符串
字符类型char占1个字节,’1’,’a’,’#’,’ ‘等都是字符
字符串类型String由长度决定占字节数,”asdas 124”是9个字节的字符串
1.4 boolean类型
1.5 操作符
‘+’ , ‘-‘ , ‘*’ , ‘/‘ , ‘%’ , ‘>’ , ‘==’等都是操作符
1.6 数组
1 2 3 4 int array[100]; int[] array = new int[100]; array[0] = 5; int length = array.length();
2 控制语句 2.1 逻辑判断 1 if (condition) { action1; } else { action2; }
2.2 循环 2.2.1 循环体结构 1 2 3 for (initial; condition; iteration) {} for (T iterator : Collection<T>) {} while (condition) {}
2.2.2 控制语句
return <返回值>: 从⽅法中返回
break: 退出当前循环
throw: 抛出异常
3 类型模型 类 :一个模板,它描述一类对象的行为和状态对象 :类的一个实例,有具体的状态方法 :类中定义的该类的实例对象所具有的行为静态方法 :隶属于类本身的方法
3.1 类 一般情况下,一个文件中存放一个public类,类的结构如下:
1 2 3 4 5 6 7 //程序包名 package com.company; //类声明 public class Student { //成员变量及方法 }
3.2 对象 语义上是表达一个Java类的尸体,可以赋值到一个变量,通过new语句进行创建 以Student类为例,首先在Student类中定义成员变量及构造函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public class Student { //成员变量 private String name; private int weight; //无参的构造方法1 public Student() { this.name = "default"; this.weight = 0; } //含参的构造方法2 public Student(String name, int weight) { this.name = name; this.weight = weight; } //静态构造方法 public static Student newStudent(String name, int weight){ return Student(name, weight); } }
在Main函数中创建对象:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.company; //包名 import com.company.Student; //对包内的Student类进行导入 public class Main { public static void main(String[] args) { //Student default,构造方法1 Student def = new Student(); //Student Jeff,构造方法2 Student jef = new Student("Jeff",120); //Student Jimmy,静态构造方法 Student jimmy =Student.newStudent("Jimmy", 130); } }
this必须强调的两种情况:1.参数重名 2.方法内定义重名
3.3 修饰符及方法 3.3.1 访问修饰符 修饰符用于控制变量和类的作用及其访问限制 ,作用关系如下表:
public
protected
default
private
同一个类
√
√
√
√
同一个包
√
√
√
×
子父类
√
√
×
×
不同包
√
×
×
×
default修饰符应用的场景比较少,如UnitTest
成员变量一般用private修饰,能增加其安全性,对其进行保护。这样外部方法就不能通过jef.name等方式直接访问成员变量。相对地,通过getter和setter函数为外部方法提供可读和可写接口:
1 2 3 4 5 6 7 8 9 10 11 12 public class Student { ... //getter方法为外部提供可读接口 public String getName(){ return name; } //setter方法为外部提供可写接口 public void setName(String name) { this.name = name; } }
从而外部方法可以通过以下方式对Student类的成员变量进行访问:
1 2 3 4 5 6 7 8 9 10 11 12 public class Main { public static void main(String[] args) { ... //可读 System.out.println(def.getName()); //System.out.println(def.name);错误,不具有访问权限 //可写 jef.setName("David"); //jef.name = "David";错误,不具有访问权限 } }
3.3.2 静态修饰符
static :把方法或者成员变量设置为类共享 ,可以通过类名直接访问,声明方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public class Student { ... //静态变量 public static int number = 0; //构造方法改变静态变量,对创建的实例对象进行计数 public Student() { this.name = "default"; this.weight = 0; number++; } public Student(String name, int weight) { this.name = name; this.weight = weight; number++; } //静态方法 public static int getNumber() { return number; } }
要注意 的是,静态方法没有this ,非静态变量不能在静态方法中访问,但静态变量可在非静态方法中访问
3.3.3 final修饰符 final修饰符主要有三个作用:
final <类>; ->防止类被继承
final <变量>; ->防止变量被修改引用到另外一个对象,可称为常量
final <方法>; ->防止方法被重载
final <变量> 必须在声明或者构造函数中定义,用声明定义示例:
1 2 3 4 public class Student { ... final private String name = "default"; }
用构造函数定义示例:
1 2 3 4 5 6 7 8 public class Student { ... final private String name; public Student(String name, int weight) { this.name = name; this.weight = weight; } }
但是,当声明中定义了final成员变量,就不可以在构造函数中修改其值及引用
这里有个问题:常量指向的对象能否被修改? final <对象> 可以从下探究:
1 2 3 4 5 6 7 8 public class Main { public static void main(String[] args) { ... final Student fin = new Student("FIN",150); //fin = jef; 错误,重新引用 fin.setName("FII"); //正确,可以改变对象内成员 } }
因此,常量指向的对象本身不可以被修改,但其成员变量可修改
3.4 注意 变量可以通过直接传值于变量,但对象 不能直接传值于对象,简单赋值只能进行引用,即浅拷贝 ,指向统一对象:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class Student { ... public void workout() { weight = weight - 5; } } public class Main{ public static void main(String[] args) { ... Student jef = new Student("Jeff",120); Student david = jef; david.workout(); System.out.println(david.getWeight()); System.out.println(jef.getWeight()); } }
其结果均为115,可见jef和david均指向同一对象,其成员变量同时改变,该方法属于浅拷贝
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public class Student implements Cloneable{ //将类声明附加可克隆功能 ... public Object clone() { //克隆方法 Object o = null; try { o = super.clone(); } catch (CloneNotSupportedException e) { System.out.println(e.toString()); } return o; } } public class Main { public static void main(String[] args) { ... Student jeff = new Student("Jeff",120); Student david = (Student)jeff.clone(); //深拷贝测试 david.workout(); System.out.println(david.getWeight()); System.out.println(jeff.getWeight()); } }
程序输出结果为115和120,说明david与jef是独立的对象,其变化互不影响,深拷贝成功