Java从零开始 基础(一)环境和语法
三大版本:SE(个人)、ME、EE(企业)
JDK:java开发者工具,包含JRE和JVM
JRE:java运行环境,包含JVM
JVM:java虚拟机(解释器)
环境相关
配置
1、jdk 8下载与安装
https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
卸载:删除安装目录、注释环境变量
2、环境变量配置
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH:.
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
3、验证安装
java
javac
java -version
HelloWorld
新建一个文件:Hello.java,写入内容
public class HelloWorld {
public static void main(String[] args) {
System.out.println("HelloWorld");
}
}
执行命令
javac Hello.java # 编译成class文件
java Hello # 执行代码
编译型、解释型语言
编译型:需要将代码翻译成其他可执行文件,如c++、java;
解释型:代码可直接执行,如python;
编译型相对来说性能较好;
IDEA代码补充
public class Demo_01 {
// psvm
public static void main(String[] args) {
int[] arrays = {1,2,3,4,5}
// sout
System.out.println();
// 100.for
for (int i = 0; i < 100; i++) {}
// arrays.for
for (int array: arrays) {}
// new Demo_01(); + option+Enter
Demo_01 demo_01 = new Demo_01();
}
}
基础语法
注释、标识符、关键字
// 单行注释 command + /
/*
多行注释 option + command + /
*/
/**
* 文档注释
*/
标识符(命令规则)
1、不能用关键字命名
2、开头只能是英文、$ 或下划线 _
3、不能出现$ _ 之外的其他符号
4、大小写敏感
// java关键字:
abstract、assert、boolean、break、byte、case、catch、char、class、continue、default、do、double、else、enum、extends、final、finally、float、for、if、implements、import、int、interface、instanceof、long、native、new、package、private、protected、public、return、short、static、strictfp、super、switch、synchronized、this、throw、throws、transient、try、void、volatile、while
数据类型
java是强类型语言,变量的定义必须先制定类型
分类
基本类型:
byte:1个字节 2^8 = -128bit~127bit
short:2个字节 2^16= -32768~32767
int:4个字节 2^32=-2147483648~2147483647
long: 8个字节 2^64
float :4个字节
double: 8个字节
char:2个字节
boolean:1位 bit,True/False
注:1字节=8位
引用类型:
类、接口、数组
定义
public class Demo_01 {
public static void main(String[] args) {
int num = 1000000;
long num1 = 10000000000000L; // long 类型要在后面加L
double num2 = 3.1415926535;
char name = '中';
System.out.println(teacher);
System.out.println(num);
}
}
进制
// 二进制 0b 八进制0 十六进制 0x
public class Demo_01 {
public static void main(String[] args) {
int i = 10;
int j = 0b10000000;
int k = 010;
int l = 0x10;
System.out.println(i);
System.out.println(j);
System.out.println(k);
System.out.println(l);
}
}
类型转换
public class Demo_01 {
public static void main(String[] args) {
int i = 10;
byte j = (byte)i; // 强制转换 高->低
int k = j; // 自动转换 低->高
System.out.println(j);
}
}
// 强制转换注意内存溢出和精度问题
变量、常量
// 变量
public class Demo_01 {
static double salary = 2500; // 类变量
int name; // 实例变量
public static void main(String[] args) {
int i, j, k; // 局部变量
Demo_01 Demo = new Demo_01(); // 实例化类
int name = Demo.name; // 调用实例变量
double a = salary; // 调用类变量
}
}
/*
类变量:定义在类里面 方法外面,加上static关键字
实例变量:定义在类里面 方法外面,实例化类后调用
局部变量:定义在某方法里面,仅供该方法使用
*/
// 常量 使用final关键字
public class Demo_01 {
static final double PI = 3.14;
public static void main(String[] args) {
System.out.println(PI);
}
}
运算符
太简单不写了
public class Demo_01 {
public static void main(String[] args) {
int a=0;
int b = a++; // 先赋值再自增
System.out.println(a); // 1
System.out.println(b); // 0
}
}
public class Demo_01 {
public static void main(String[] args) {
int a=0;
int b = ++a; // 先自增再赋值
System.out.println(a); // 1
System.out.println(b); // 1
}
}
包机制
跟python的package差不多
一般用域名倒置来建包,如:com. baidu.www
java文件的第一句应该是: package xxx.xxx.xxx
导入第三方包:import xxx.xxx.xxx
javaDoc文档注释
/**
* @author LinXin
* @version 1.0.0
* @since 1.8.0
*/
public class Demo_01 {
/**
* @throws Exception
* @param a 形参
* @return a
*/
public String test(String a) throws Exception{
return a;
}
}
// javadoc -encdeing UTF-8 -charset UTF-8 xxx.java
// 生成帮助文档
流程控制
用户交互Scanner
package:java.util.Scanner 获取用户输入
语法:Scanner s = new Scanner(System.in);
api:next() nextLine() hasNext hasNextLine() hasNextInt ...
import java.util.Scanner;
public class Demo_01 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
if (s.hasNext()) { // 如果有输入
String str = s.next(); // 接收输入,以空格为结束
System.out.println(str);
}
s.close(); // 释放对象
}
}
public class Demo_01 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
if (s.hasNextLine()) { // 如果有输入
String str = s.nextLine(); // 接收输入,以回车为结束
System.out.println(str);
}
s.close(); // 释放对象
}
}
public class Demo_01 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
if (s.hasNextInt()) { // 如果有输入整数
int str = s.nextInt(); // 接收输入,以回车为结束
System.out.println(str);
} else{
System.out.println("请输入整数");
}
s.close(); // 释放对象
}
}
顺序结构
从上到下执行
选择结构
import java.util.Scanner;
public class Demo_01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
// equals 判断字符串是否相等
// 单选择结构
if (s.equals("hello")){
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
}
public class Demo_01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
// 双选择结构
if (score >= 60) {
System.out.println("及格");
} else {
System.out.println("不及格");
}
System.out.println("End");
scanner.close();
}
}
public class Demo_01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
// 多选择结构
if (score < 60) {
System.out.println("不及格");
} else if (score < 70) {
System.out.println("及格");
} else if (score < 80) {
System.out.println("一般");
} else if (score < 90) {
System.out.println("良");
} else if (score < 100) {
System.out.println("优");
} else if (score == 100) {
System.out.println("满分");
} else {
System.out.println("输入错误");
}
System.out.println("End");
scanner.close();
}
}
public class Demo_01 {
public static void main(String[] args) {
char grade = 'c';
// switch多选择结构 匹配一个具体的值
// 不加break会出现case穿透现象
switch (grade){
case 'a':
System.out.println("优");
break;
case 'b':
System.out.println("良");
break;
case 'c':
System.out.println("一般");
break;
default:
System.out.println("输入错误");
}
System.out.println("End");
}
}
循环结构
public class Demo_01 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
while (i < 10) { // while 循环 先判断再执行
i++;
sum = sum + i;
}
System.out.println(sum);
}
}
public class Demo_01 {
public static void main(String[] args) {
int i = 0;
int sum = 0;
do { // do while 循环 先执行后判断 所以至少执行一次
i++;
sum = sum + i;
} while (i < 10);
System.out.println(sum);
}
}
public class Demo_01 {
public static void main(String[] args) {
int sum = 0;
for (int i=1; i<=10; i++){ // for 循环
sum = sum + i;
}
System.out.println(sum);
}
}
public class Demo_01 {
public static void main(String[] args) {
int[] list = {1, 2, 3, 4, 5}; // 定义数组
for (int x:list){ // 遍历数组
System.out.println(x);
}
}
}
break continue
- break 中止循环
- continue 跳过本次循环(此次循环不执行continue之后的语句)
方法
设计原则:一个方法完成一个功能
方法的定义
public class Demo_01 {
// main方法
public static void main(String[] args) {
int sum = sum(1, 2); // 调用方法
System.out.println(sum);
}
// 自定义类方法(static), 类方法在该类中可直接调用
public static int sum(int a, int b) { // 返回1个int对象,接收2个int参数
return a + b;
}
}
方法的重载
在同一类下,方法名必须相同 且 形参必须不同(类型、数量、顺序)的方法。作用是传不同的实参,调不同的方法
public class Demo_01 {
// main方法
public static void main(String[] args) {
double sum = sum(1, 2); // 此时调用是第二个sum方法
System.out.println(sum);
}
public static int sum(int a, int b) {
return a + b;
}
public static double sum(double a, double b) {
return a + b;
}
}
命令行参数
public class Demo_01 {
// main方法的args就是接收命令行参数的
public static void main(String[] args) {
for (int i=0; i< args.length; i++){
System.out.println(args[i]);
}
}
}
可变参数
在方法的声明中,指定参数参数类型后加一个 ... , 一个方法中只能指定一个可变参数,且必须是方法的最后一个参数
public class Demo_01 {
// main方法
public static void main(String[] args) {
Demo_01 demo = new Demo_01(); // 调用实例方法
demo.test('a', 2, 3, 4); // 传参格式
}
public void test(char a, int... number) { // 实际接收的是一个数组
for (int i = 0; i < number.length; i++) {
System.out.println(number[i]);
}
}
}
递归
自己调用自己,结构包含2个部分:
- 递归头:结束递归条件
- 递归体:调用自身
public class Demo_01 {
// main方法
public static void main(String[] args) {
Demo_01 demo_01 = new Demo_01();
int f = demo_01.f(5);
System.out.println(f);
}
public int f(int num) {
if (num == 1) { // 递归结束条件
return 1;
} else {
return num * f(num - 1); // 递归调用自身
}
}
}
数组
数组概述
- 相同类型元素的有序集合,可通过下标访问
- 数组中可以存放引用类型(对象)
- 数组一旦创建,长度不可改变(静态顺序表)
声明和创建
- 静态初始化:声明的时候赋值
- 动态初始化:声明之后赋值
public class Demo_01 {
public static void main(String[] args) {
// 动态初始化:声明变量、开辟空间、赋值
char[] chars = new char[10];
for (int i = 0; i < chars.length; i++) {
chars[i] = '你'; // 给数组赋值
}
System.out.println(chars);
// 静态初始化:声明一个int类型的数组并开辟空间、赋值
int[] nums = {1, 2, 3, 4, 5};
for (int x:nums){
System.out.println(x);
}
}
}
内存分析
- 堆:存放new出来的对象的值(new的时候开辟);可以被所有线程共享,不会存放别的对象引用;
- 栈:存放声明出来的变量名(声明的时候开辟),和指向堆内存的指针(new的时候写入);
- 方法区:
数组使用
public class Demo_01 {
public static void main(String[] args) {
int[] arrays = {1, 2, 3, 4, 5};
int[] reversed = reversed(arrays);
printArray(reversed);
}
public static int[] reversed(int[] arrays) {
// 反转数组
int[] res = new int[arrays.length];
for (int i = 0, j = res.length - 1; i < arrays.length; i++, j--) {
res[j] = arrays[i];
}
return res;
}
public static void printArray(int[] arrays) {
// 打印数组
for (int i = 0; i < arrays.length; i++) {
System.out.print(arrays[i] + " ");
}
}
}
多维数组
以二维数组为例,int[2] [3] 可以看作2行3列的矩阵
public class Demo_01 {
public static void main(String[] args) {
int[][] array = new int[3][4];
int[] a = {1, 2, 3, 0};
int[] b = {4, 5, 6, 0};
int[] c = {7, 8, 9, 0};
array[0] = a;
array[1] = b;
array[2] = c;
// array = {{1, 2, 3, 0}, {4, 5, 6, 0}, {7, 8, 9, 0}}
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]);
}
System.out.println();
}
}
}
Arrays类
package:java.lang.Arrays
文档:https://tool.oschina.net/apidocs/apidoc?api=jdk-zh
import java.util.Arrays;
public class Demo_01 {
public static void main(String[] args) {
int[] a = {1, 5, 4, 3, 2};
System.out.println(a); // 打印hashcode,类似: [I@5cad8086
System.out.println(Arrays.toString(a)); // 打印整个数组 相当于python的 print([1, 2, 3, 4, 5])
Arrays.sort(a); // 正序
System.out.println(Arrays.toString(a));
Arrays.fill(a, 1,3,0); // 将下标为[1,3)之间的元素值替换为0
System.out.println(Arrays.toString(a));
}
}
两个算法
冒泡排序
import java.util.Arrays;
public class Demo_01 {
public static void main(String[] args) {
int[] array = {1,32,13,321,3,213,45,35,34,45,6546,54,7,6};
bubblingSort(array);
}
// 倒序
public static void bubblingSort(int[] array) {
int temp;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length - i - 1; j++) {
if (array[j + 1] > array[j]){
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
System.out.println(Arrays.toString(array));
}
}
稀疏数组
当一个数组大部分元素为0(没有值),或值是同样的时,可以用稀疏数组
处理方式:
- 记录数组一共有几行几列,有多少不同值;
- 把具有不同值的元素的行、列、值记录在一个小规模数组中
import java.util.Arrays;
public class Demo_01 {
public static void main(String[] args) {
int[][] array_1 = new int[9][9];
array_1[0][0] = 1;
array_1[1][1] = 2;
array_1[2][2] = 3;
// 输出原始数组
for (int i = 0; i < array_1.length; i++) {
System.out.println(Arrays.toString(array_1[i]));
}
// 计算出需要放到稀疏矩阵的元素个数
int sum = 0;
for (int i = 0; i < array_1.length; i++) {
for (int j = 0; j < array_1[i].length; j++) {
if (array_1[i][j] != 0) {
sum++;
}
}
}
System.out.println("======================================");
// 定义稀疏矩阵
int[][] array_2 = new int[sum + 1][3]; // sum行,3列 每行存一个元素信息,包括:行、列、值
array_2[0][0] = 9; // 第0行存放矩阵信息,分别为: 原始行数、原始列数、存放元素个数
array_2[0][1] = 9;
array_2[0][2] = sum;
// 将元素存放到稀疏矩阵
int count = 1;
for (int i = 0; i < array_1.length; i++) {
for (int j = 0; j < array_1[i].length; j++) {
if (array_1[i][j] != 0) {
array_2[count][0] = i;
array_2[count][1] = j;
array_2[count][2] = array_1[i][j];
count++;
}
}
}
// 输出稀疏矩阵
for (int i = 0; i < array_2.length; i++) {
System.out.println(Arrays.toString(array_2[i]));
}
System.out.println("=======================================");
//还原稀疏矩阵
int[][] array_3 = new int[array_2[0][0]][array_2[0][1]];
for (int i = 0; i < array_2.length - 1; i++) {
array_3[array_2[i + 1][0]][array_2[i + 1][1]] = array_2[i + 1][2];
}
// 输出原始矩阵
for (int i = 0; i < array_3.length; i++) {
System.out.println(Arrays.toString(array_3[i]));
}
}
}

浙公网安备 33010602011771号