三大版本:SE(我的)、ME、EE(企业)html
JDK:java开发者工具,包含JRE和JVM
JRE:java运行环境,包含JVM
JVM:java虚拟机(解释器)java
一、jdk 8下载与安装python
https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
卸载:删除安装目录、注释环境变量c++
二、环境变量配置算法
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
三、验证安装api
java
javac
java -version数组
新建一个文件:Hello.java,写入内容oracle
public class HelloWorld { public static void main(String[] args) { System.out.println("HelloWorld"); } }
执行命令ide
javac Hello.java # 编译成class文件
java Hello # 执行代码工具
编译型:须要将代码翻译成其余可执行文件,如c++、java;
解释型:代码可直接执行,如python;
编译型相对来讲性能较好;
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 + / */ /** * 文档注释 */
标识符(命令规则)
一、不能用关键字命名
二、开头只能是英文、$ 或下划线 _
三、不能出现$ _ 以外的其余符号
四、大小写敏感
// 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:1个字节
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
/** * @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 // 生成帮助文档
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); } } }
设计原则:一个方法完成一个功能
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); } } }
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(); } } }
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])); } } }