基本结构
// 单行注释
/* 多行注释 /
/* 特殊多行注释 */
/**
* 可以用来自动创建文档的注释
*/
public class Hello {
public static void main(String[] args){
// 向屏幕输出文本:
System.out.println("Hello, Sam!");
/* 多行注释开始
注释内容
注释结束
*/
}
} // class定义结束
程序的基本单位 类-class,对象名字为 Hello (命名规则:大写字母开头,下划线等,与文件名一致)
public 修饰符 表明对象是公开的
类的对象内部 方法,static void main(String[] args(Java入口程序规定的方法必须是静态方法,方法名必须为main,括号内的参数必须是String数组。)
Java 程序基本机构
Java程序的基本结构:
// A code block
var foo = 'bar';
public class Goodbye {
public static void main(String[] args) {
// 向屏幕输出文本:
System.out.println("Hello, Byebye!");
/* 多行注释开始
注释内容
注释结束 */
}
} // class定义结束
public 是访问修饰符,class是公开
class 内部可以定义若干方法,方法名:main,返回值为void,表示没有任何返回值
1.整数类型:byte,short,int,long
2.浮点数类型:float(f),double
3.字符类型:char(’’)使用单引号
4.布尔类型:boolean
变量和数据类型
基本类型的变量:例如 int x=1
引用类型的变量:
public class Hello {
public static void main(String[] args){
int x = 500; // 变量定义,表明类型
float f1 = 3.14f; // 对于浮点型的数据,要加上f后缀
float f2 = 3.14e5f; // 科学计数法表示的3.14x10^5
char a = 'S'; // 字符类型,单引号只有一个字符
char qq = '中'; // 可以表示其他字符
final double Pi = 3.14; // 变量前加上final变成了常量
var ss = new StringBuilder(); //用var省略变量类型
x = x +100; // 第二次使用不需要再定义
int n = x;
System.out.println(x);
System.out.println("x = " + (x + n));
}
等号是赋值语句,变量可以重新赋值,基本类型包括:整数型、浮点型、布尔型、字符型
强制转型切不可超出范围
浮点数运算 浮点数常常无法精确表示,因此,浮点数运算会产生误差;判断两个浮点数之差的绝对值是否小于一个很小的数
布尔运算 字符和字符 数组类型
字符串 字符
public class Hello {
public static void main(String[] args){
// 向屏幕输出文本:
System.out.println("Hello, Sam!");
/* 多行注释开始
注释内容
注释结束
*/
char a = 'S'; // 字符类型,单引号只有一个字符
char qq = '中'; // 可以表示其他字符
String a11 = "ADSDCVs";
//转义字符
测试代码块
/**
* 可以用来自动创建文档的注释
*/
public class Hello {
public static void main(String[] args){
// 向屏幕输出文本:
System.out.println("Hello, Sam!");
/* 多行注释开始
注释内容
注释结束
*/
int x = 500; // 变量定义,表明类型
float f1 = 3.14f; // 对于浮点型的数据,要加上f后缀
float f2 = 3.14e5f; // 科学计数法表示的3.14x10^5
char a = 'S'; // 字符类型,单引号只有一个字符
char qq = '中'; // 可以表示其他字符
String a11 = "ADSDCVs"; // 包含几个字符串
final double Pi = 3.14; // 变量前加上final变成了常量
boolean age_1 = x == 500; // true
int[] ns = new int[6]; // 定义数组变量,数组一旦创建,不可改变
int[] ns1 = new int[] { 68, 79, 91, 85, 62, 99 };
int[] ns2 = { 68, 79, 91, 85, 62 };
var ss = new StringBuilder(); //用var省略变量类型
x = x +100; // 第二次使用不需要再定义
int n = x;
System.out.println(x);
System.out.println(age_1);
System.out.println("x = " + (x + n));
//运算
int i = (100+200)*(99-88); //3300
int x1 = 12345/67; // 184
int y = 123456 % 67; //取余运算使用%
// 溢出不会报错,会得到一个奇怪的结果
int a1 = n << 1; // 00000000 00000000 00000000 00001110 = 14
// 位运算 &,|,` 与或非
// 类型强制转换
short s = 12345;
int i1 = 12345;
int x2 = s + i1; // s自动转型为int
//short y= s + i1; // 编译错误!
String[] names = {"ABC", "XYZ", "zoo"};
String s0 = names[1];
names[1] = "cat";
System.out.println(names[1] + s0 + names[2]);
System.out.println(s0); // s是"XYZ"还是"cat"?
}
} // class定义结束
控制结构
输出、格式化输出、输入
import java.util.Scanner;
public class Process {
public static void main(String[] args){
// 输入和输出
System.out.println("hello"); //输出并换行
System.out.print("hello"); //输出不换行
System.out.println(" terminate"); //输出完之后换行
float a = 3.141f;
System.out.printf("%.2f\n", a); //格式化输出
// 输入
Scanner scanner = new Scanner(System.in);
System.out.print("Input your name: ");
String name = scanner.nextLine();
System.out.print("Input your age: ");
int age = scanner.nextInt(); //读取一行输入并获取整数
System.out.printf("My name is %s and I am %d\n", name, age);
}
}
格式化输出
If 语句
Switch 语句多选
int n = 20;
if (n>20){
System.out.println("标准");
} else if (n>10) {
System.out.println("好");
} else {
System.out.print("Bad");
}
int select = 1;
switch (select) {
case 1:
System.out.println("Selected 1");
break;
case 2:
System.out.println("Selected 2");
break;
case 3:
System.out.println("Selected 3");
break;
}
while 循环 – do while
// while 循环
int sum= 0; int n1 = 1;
while (n1<=50){
sum = sum + n1;
n1 = n1 + 1;
}
System.out.println(sum);
do {
sum = sum +n1;
System.out.print(sum + " " + n1 + " ");
n1 = n1 + 1;
} while(n1<=100);
System.out.println(sum);
for 循环 – break continue
int sum_for = 0;
for (int i=1; i<=50; i++){
sum_for = sum_for + i;
}
System.out.println(sum_for);
数组操作
遍历
int[] ns = {1, 4, 5, 7, 9};
for (int i=0; i<ns.length;i++){
int n = ns[i]; //数组的索引
System.out.print(n + " ");
}
for (int j : ns){ //直接取得数组元素
System.out.print(j + " ");
}
int[] ns1 = {1, 4, 5, 7, 9};
//用标准库打印数组内容
System.out.println(Arrays.toString(ns1));
排序
数组的内存不变,指向的数组内容改变
//数组排序——冒泡算法
int[] array = {25, 13, 44, 55,12, 64};
System.out.println(Arrays.toString(array));
//排序
for (int i=0; i<array.length-1; i++){
for (int j=0; j<array.length-1-i; j++){
if (array[j] > array[j+1]){
int temp= array[j];
array[j] = array [j+1];
array[j+1] = temp;
}
}
}
System.out.println(Arrays.toString(array));
多维数组
//多维数组
int[][] mu_array = {
{1,1,1,1},
{2,2,2,2},
{3,3,3,3}
};
for (int i=0; i<mu_array.length;i++){
// for (int j=0; j<mu_array.length; j++){
// System.out.print(mu_array[i][j] + " ");}
System.out.print(Arrays.toString(mu_array[i]));
}
System.out.print(ns.length + " " + Arrays.deepToString(mu_array));
面向对象编程 OOP
类、实例、方法
public class MainClass {
public static void main(String[] args){
City hn = new City();
hn.name = "Hunan";
hn.latitude = 32;
hn.longitude = 32;
System.out.println(hn.name);
System.out.print(" " + hn.name + " " + hn.longitude);
}
}
class City{
public String name;
public double latitude;
public double longitude;
}
// Hunan
// Hunan 32.0
public 定义的方法暴漏给外部,破坏封装性,并且直接操作,容易造成混乱
private 来拒绝外部访问,如需访问,可通过调用public method
修饰符 方法返回类型 方法名(方法参数列表) {
若干方法语句;
return 方法返回值;
}
定义private方法的理由是内部方法是可以调用private方法的。
this 隐含变量,始终指向当前实例 this.field访问
方法参数 可变参数 参数绑定 _ _
构造方法不同,会自动匹配构造方法
方法重载
实现方式:继承、多态
继承多态
继承树:class Student extends Person,定义Person的时候,没有写extends。在Java中,没有明确写extends的类,编译器会自动加上extends Object。所以,任何类,除了Object,都会继承自某个类。
子类无法访问父类的private字段或者private方法。例如,Student类就无法访问Person类的name和age字段,需要把private改为protected。用protected修饰的字段可以被子类访问