JAVA 1 2 3 4 5 public class Hello { public static void main (String[] args) { System.out.print("armandhe" ) } }
数据类型 基本数据类型 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 String st="armandhe" ; byte by=1 ;short sh=127 ;int in=65535 ;long lo=2111111L ;char ch='a' ; float fl=0.4f ;double dou=45.35 ;boolean bo=true ;str1=new String ("armanghe" ); str1=new String ("armanghe" ); str1 === str2 String str3="armandhe" ; String str4="armandhe" str3===str4
数据类型转换 只能转换能转换的类型
转换优先级 1 2 byte short char --> int --> long --> float --> double
低到高会发生自动类型转换,高到低需要进行强制转换
强制类型转换 1 2 3 4 5 6 int in=43 ;byte by=(int )in; int in1=2343444 ;int in2=22222222 ;long in3=in1*(long )in2;
变量 值可以变化的量。变量是内存中的一段空间,不存储值,只存储值得地址。java是强类型得语言,每个变量都必须指定类型,变量声明或者定义后必须以分号结尾。
变量作用域 类变量 1 2 3 4 5 6 7 8 9 10 11 public class Hello { static int i; static int j=10 ; public static void main (String[] args) { System.out.print(i); System.out.print(j)l } public void test () { }
实例变量 定义在类里面的变量,必须通过类引用
1 2 3 4 5 6 7 8 9 10 11 12 13 public class Hello { int i; int j=10 ; public static void main (String[] args) { Hello hel = new Hello ; System.out.print(hel.i); System.out.print(hel.j); } public void test () { } }
局部变量 定义在方法中的变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class Hello { public static void main (String[] args) { Hello hel = new Hello ; System.out.print(hel.i); System.out.print(hel.j); test.i } public void test () { int i; System.out.print(i); } }
常量 值固定的量
1 2 3 4 5 6 public class Hello { static final double PI=3.1415926 ; public static void main (String[] args) { System.out.print(PI) } }
算数运算符 1 2 3 4 5 6 7 + - * / % ++ -- = < > == <= >= != && || ! & | ^ ~ >> << >>> ? : += -+ *= /=
算数运算时,若运算数种不含有long类型,那么结果类型必是int类型,如果long类型存在,那么结果必为long。
流程控制 Scanner对象 next方式 1 2 3 4 5 6 7 8 9 10 11 import java.util.scanner;Scanner s = new Scanner (System.in);if (s.hasNext()){ String str = s.next(); System.out.println(str); } s.close();
nextline方式 1 2 3 4 5 6 7 8 import java.util.scanner;Scanner s = new Scanner (System.in);if (s.hasNextLine()){ String str = s.nextline(); System.out.println(str); } s.close();
其他用法 1 2 3 4 5 6 7 8 9 10 11 import java.util.scanner;int i=0 ;Scanner s = new Scanner (System.in);System.out.println("请输入整数:" ) if (s.hasNextInt()){ i=s.**nextInt()**; System.out.println("整数数据:" + i); }else { System.out.println("输入的不是整数" ) } s.close();
例:求和 1 2 3 4 5 6 7 8 9 10 11 12 13 14 import java.util.scanner;double sum;int m=1 ;Scanner s = new Scanner (System.in);while (s.hasNextDouble()){ double x = s.nextDouble(); x.equals("hello" ) System.out.println("你输入了第" +m+"个数据" ); sum += x; m += 1 ; } System.out.println("这些数字的总和为:" +sum+";" ); System.out.println("这些数字的平均值为:" +sum/m+";" );
顺序结构
选择结构 单选择结构与普通多选择结构 1 2 3 4 5 6 7 if (condition){ } else if (condition){ } else { }
switch多选择结构 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 26 27 28 29 switch (expression){ case value: case value: . . . default : } switch (expression){ case value: break ; case value: break ; . . . default : }
循环结构 while 1 2 3 4 5 while (condition){ }
do while 1 2 3 do { }while (condition);
for 1 2 3 4 5 6 7 8 9 for (int i=1 ; i<100 ;i++){ }
例:九九乘法表 1 2 3 4 5 6 7 8 9 10 public class test { public static void main (String[] args) { for (int i=1 ;i<=9 ;i++){ for (int j=1 ;j<=i;i++){ System.out.print(i+"x" +j+"=" +(i*j)+"\t" ); } System.out.println(); } } }
例:打印等腰三角形 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class test { public static void main (String[] args) { for int i = 1 ;i<=5 ; i++{ for (int j=5 ;j>=i;j--){ System.out.print(" " ); } for (int j=1 ;j<=i;j++){ System.out.print("*" ); } for (int j=1 ;j<i;j++){ System.out.print(" " ); } System.out.println(); } } }
增强型for 主要用于遍历数组与集合
1 2 3 4 5 6 7 8 public class test { public static void main (String[] args) { int [] num={10 ,20 ,30 }; for (int x:num){ System.out.println(x); } } }
方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class Test { public static void main (String[] args) { int sum=add(1333 ,3222 ); System.out.print(sum); Test su = new Test ; int sum2=su.sub(5 ,6 ); System.out.print(sum2); } public static int add (int a,int b) { return a+b; } public int substraction (int a,int b) { } }
重载
方法名称必须相同
参数列表必须不相同(个数不相同,类型不相同,排列顺序不相同)
可变传参 1 2 3 4 5 6 7 8 9 10 11 12 13 public class Test { public static void main (String[] args) { int a=1 ; int b = 3 ; int c = 4 int d = 5 ; arg(a,b,c,d); } public static int arg (int a;int b;int ... c) { } }
递归 方法自己调用自己,递归必须要有基例,即程序的出口,不然会发生栈溢出。
以阶乘为例
1 2 3 4 5 6 7 8 9 10 11 public class Test { public static void main (String[] args) { } public static int test (int n) { if (n==1 ){ return 1 ; }else { return n*test(n-1 ); } } }
数组 基本使用 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 int [] num = {1 ,2 ,3 ,4 ,5 ,5 ,6 }; int num[] = {55 ,1 ,3 ,4 ,5 ,6 ,}; int [] nums; nums = new int [100 ]; nums[0 ]=1 ; nums[1 ]=34 ; System.out.print(nums[1 ]); System.out.print(nums.length); for (int i:nums){ System.out.print(nums[i-1 ]); } for (int i=0 ;i<nums.lenth;i++){ System.out.print(nums[i]); } public static int [] reverse(int [] arrays){ int [] result = new int [arrays.length]; for (int i=0 ,j=result.length-1 ;i<arrays.length;i++,j--){ result[i]=arrays[j]; } return result; }
多维数组 1 2 3 4 5 6 7 8 public class Test { public static void main (String[] args) { int [][] nums={{1 ,2 },{1 ,4 },{45 ,56 }}; System.out.pringln(nums[1 ][0 ]); } }
Arrays对象 1 2 3 4 5 6 7 8 9 10 import java.utils.Arrays;public class Test { public static void main (String[] args) { int [] a={1 ,434 ,5 ,63 ,4223 ,56 ,8 }; System.out.print(a); System.out.print(Arrays.toString(a)) Arrays.sort(a) System.out.print(a) } }
冒泡排序 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import java.utils.Arrays;public class Test { public static void main (String[] args) { } public static int [] sor(int [] arrays){ int k=0 ; for (i=0 ;i<arrays.length-1 ;i++){ int flag=0 ; for (j=0 ;j < arrays.length-1 -i;j++){ if (arrays[j]>arrays[j+1 ]){ k = arrays[j+1 ]; arrays[j+1 ]=arrays[j]; arrays[j]=k; flag +=1 ; } } if (flag=0 ){ break ; } } return arrays; } }
稀疏数组
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 public class Test { public static void main (String[] args) { } public static int [] sparseArray(int [] arr){ int sum=0 ; int max=0 ; for (int i=0 ;i<arr.length){ for (int j=0 ;j<arr[i].length;j++) if (arr[i][j] !=0 ){ sum++; } if ((arr[i].length>arr[i+1 ].length) && i<(arr.length-2 )){ max=arr[i].length; } } } int [][] arrSpar = new int [sum+1 ][3 ]; arrSpar[0 ][0 ]=arr.length; arrSpar[0 ][1 ]=max; arrSpar[0 ][2 ]=sum; int count=0 ; for (int k=0 ;k<arr.length;k++){ for (int l=0 ;l<arr[k].length;l++){ if (arr[k][l] !=0 ){ count++; arrSpar[count][0 ]=k; arrSpar[count][1 ]=l; arrSpar[count][2 ]=arr[k][l]; } } } return arr2; } public static int [] restoreSparseArray(int [][] arrspar){ int [][] arr = new int [arrspar[0 ][0 ]][arrspar[0 ][1 ]]; for (int h=1 ;h<arrspar.length;h++){ arr[arrspar[h][0 ]][arrspar[h][1 ]]=arrspar[h][2 ]; } return arr; } }
内存分析
面向对象编程 静态方法 -有static关键字,不需要通过对象调用
动态方法 -无static关键字,需要通过对象调用
形参 -函数定义的时候指定的参数,作为占位符使用
实参 -函数调用的时候,实际传入的参数
值传递 -将值复制一份进行传递,函数内部改变参数的值,不会应用外部变量的值。 JAVA中均为值传递
引用传递 -传递值得内存地址,改变函数中的值将同步改变外部该变量的值
同一个文件中可以有多个class
,但只能有一个public class
**构造器:**必须与类名同名,不能有返回值,且没有void。
属性封装,类继承,方法重写、多态
无参构造器
1 2 3 4 5 6 7 8 public class Test { public Test () { } public static void main (String[] args) { } }
有参构造器
1 2 3 4 5 6 public class Test { public Test () {} public Test (String name) { } }
继承 1 2 3 public class Student **extends** Persion{ }
重写与this、super、多态 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 public class Parent { public Parent () { System.out.println("父类无参构造方法" ); } protected String name="woshinibaba" ; public static void main (String[] args) {} public void run () { System.out.print("father" ); } } public class Son extends Parent { public Son () { System.out.println("字类无参构造方法" ) } private String name="armandhe" ; public static void main (String[] args) {} @Override public void run () { System.out.print("son" ); } public void test (String name) { this .run(); run(); super .run(); System.out.pintln(name); System.out.println(this .name); System.out.println(super .name); } } public class Application { public static void main (String[] args) { Parent parent = new Parent ; Son son = new Son ; Parent test = new Student ; ** parent.run(); son.run(); son.test("hjx" ); test.run(); test. } }
Instanceof instanceof 判断两个类之间有没有父子关系
1 2 3 4 5 Object => Person => Student ===object Student object = new Student ;Person object = new Student ;Object object = new Student ; object instanceof Student
类型转换 1 2 3 Person obj new Student ; Student student = (Student)obj Person person = student;
代码块
导入包 1 2 3 4 5 import static java.lang.Math.random System.out.print(random()); publib final void class Test {
抽象类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public abstract class Action { public void doSomething () ; public void doanotherthing () { } } public abstract run extends Action { @override public boid dosomething () { } }
接口 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public interface UserServer { public static void run () ; } public interface TimeServer { public static void time () ; } public class UserServerImpl implements UserServer ,TimeServer{ @Override public void run () { } @Override public void time () { } }