最最基础部分
保留关键字:goto、const
增加关键字:assert、enum
public class SimpleDemo02{// 声明一个类,类名称的命名规范:所有单词的首字母大写
       public static void main(String args[]){ // 主方法
              double x = 30.3 ;    // 声明小数
              float y = 30.3f ;      // 声明小数
       }
};
最大加1成最小
最小减1成最大
public class SimpleDemo{     // 声明一个类,类名称的命名规范:所有单词的首字母大写
       public static void main(String args[]){ // 主方法
              int max = Integer.MAX_VALUE    ;      // 求出整型的最大值
              int min = Integer.MIN_VALUE ;    // 求出整型的最小值
              System.out.println("int的最大值:" + max) ;
              System.out.println("int的最大值 + 1:" + (max+1)) ;
              System.out.println("int的最小值:" + min) ;
              System.out.println("int的最小值 - 1:" + (min-1)) ;
       }
}
数据类型的转换:
public class SimpleDemo03{// 声明一个类,类名称的命名规范:所有单词的首字母大写
       public static void main(String args[]){ // 主方法
              char c1 = 'a' ;// 使用”'“括起来表示字符
              int x1 = c1 ;    // 将char变为int型
              x1++ ;                   // 自增
              char c2 = (char)x1 ;      // 将int --> char,进行强制转换
              System.out.println(c2) ;
       }
}
char→int
byte→short→int→long
int→float→double
反过来,需进行强制转换
转换时如碰到的是字串符,则所有类型都向字串符转换
public class SimpleDemo04{// 声明一个类,类名称的命名规范:所有单词的首字母大写
       public static void main(String args[]){ // 主方法
              String str = "hello " ;      // 表示字符串
              int i = 10 ;
              System.out.println(str + (i + 20)) ;
              System.out.println(1l + 11) ;
       }
}
位操作:指使用二进制代码完成的数据操作
&按位与
|按位或
^异或(相同为0,不同为1)
~取反
<<左位移(有符号)
>>右位移(有符号)
>>>无符号右位移
public class SimpleDemo05{// 声明一个类,类名称的命名规范:所有单词的首字母大写
       public static void main(String args[]){ // 主方法
              int x = 3 ;
              int y = 6 ;
              System.out.println(x & y) ;
              System.out.println(x | y) ;
              System.out.println(x ^ y) ;
       }
}
3的二进制代码:00000000 00000000 00000000 00000011
6的二进制代码:00000000 00000000 00000000 00000110
反码:反码所有的位按位取反,整数的反码就是其本身,负数的反码是用补码的形式表示出来,再进行取反操作,补码=反码+1
public class SimpleDemo06{// 声明一个类,类名称的命名规范:所有单词的首字母大写
       public static void main(String args[]){ // 主方法
              int x = -3 ;
              System.out.println(~x) ;
       }
}
-3的反码是2
3的反码是-4
public class SimpleDemo07{// 声明一个类,类名称的命名规范:所有单词的首字母大写
       public static void main(String args[]){ // 主方法
              int x = 3 ;
              System.out.println(x<<2) ;    // 左移两位
       }
}
public class SimpleDemo08{// 声明一个类,类名称的命名规范:所有单词的首字母大写
       public static void main(String args[]){ // 主方法
              int x = 3 ;
              int y = -3 ;
              System.out.println(x>>2) ;    // 右移两位
              System.out.println(y>>2) ;    // 右移两位
       }
}
public class SimpleDemo09{// 声明一个类,类名称的命名规范:所有单词的首字母大写
       public static void main(String args[]){ // 主方法
              int x = 3 ;
              int y = -3 ;
              System.out.println(x>>>2) ; // 右移两位
              System.out.println(y>>>2) ; // 右移两位
       }
}
循环控制
分支结构:if、if…else、if…else if…else
循环结构:while、do…whlie、for
循环控制:break、continue
数组
1.       声明时直接开辟内存空间
数据类型数组名称[]=new 数据类型[长度];
或 数据类型 [] 数组名称=new 数据类型[长度];
2.分步声明
声明数组:数据类型数组名称[]=null;
实例化数组:数组名称=new 数据类型[长度];
public class ArrayDemo01{
       public static void main(String args[]){
              int i[] = null ;
              // i = new int[10] ; // 开辟了10个空间大小的数组
              System.out.print("数组开辟之后的内容:") ;
              for(int x=0;x<i.length;x++){
                     System.out.print(i[x] + "、") ;
              }
              i[0] = 30 ;       // 为第一个元素赋值
              i[9] = 60 ;       // 为最后一个元素赋值
              System.out.print(""n数组赋值之后的内容:") ;
              for(int x=0;x<i.length;x++){
                     System.out.print(i[x] + "、") ;
              }
       }
}
为数组的元素赋值,需通过下标的形式进行访问
静态初始化:
public class ArrayDemo02{
       public static void main(String args[]){
              int i[] = {1,2,3,4,6,7} ; // 此时属于静态初始化
              System.out.print("数组开辟之后的内容:") ;
              for(int x=0;x<i.length;x++){
                     System.out.print(i[x] + "、") ;
              }
       }
}
二维数组
public class ArrayDemo03{
       public static void main(String args[]){
              int i[][] = {{1,2},{2,3,4},{3,4,5,6,7}} ;      // 此时属于静态初始化
              System.out.print("数组开辟之后的内容:") ;
              for(int x=0;x<i.length;x++){
                     for(int y=0;y<i[x].length;y++){
                            System.out.print(i[x][y] + "、") ;
                     }
                     System.out.println("") ;
              }
       }
}
主方法之外的方法:可重复调用的代码段
public class MethodDemo01{
       public static void main(String args[]){
              print() ;          // 调用方法
              print() ;          // 调用方法
              print() ;          // 调用方法
       }
       public static void print(){
              System.out.println("Hello World!!!") ;
       }
}
public class MethodDemo02{
       public static void main(String args[]){
              System.out.println(add(10,20)) ;
              System.out.println(add(30,30)) ;
       }
       public static int add(int x ,int y){
              int temp = x + y ; 
              return temp ;   // 将计算结果返回
       }
}
此内容包含方法的重载
指过程中调用的方法名称相同,但是参数的类型或个数不同(同样方法也被声明多个,但方法都相同)
public class MethodDemo03{
       public static void main(String args[]){
              System.out.println(add(10,20)) ;
              System.out.println(add(30,30)) ;
              System.out.println(add(30,30,30)) ;
              System.out.println(add(30.03f,30.01f)) ;
       }
       public static int add(int x ,int y){
              int temp = x + y ; 
              return temp ;   // 将计算结果返回
       }
       public static int add(int x ,int y,int z){
              int temp = x + y + z ; 
              return temp ;   // 将计算结果返回
       }
       public static float add(float x ,float y){
              float temp = x + y ; 
              return temp ;   // 将计算结果返回
       }
}
public class MethodDemo04{
       public static void main(String args[]){
              System.out.println(1) ;
              System.out.println("hello") ;
              System.out.println(1.1) ;
              System.out.println('c') ;
       }
}
System.out.println()可接受任意形式的参数,属于方法的重载
但要注意
public class MethodDemo05{
       public static void main(String args[]){
       }
       public static int add(int x ,int y){
              int temp = x + y ; 
              return temp ;   // 将计算结果返回
       }
       public static float add(int x ,int y){
              float temp = x + y ; 
              return temp ;   // 将计算结果返回
       }
}
不是方法的重载,因为此情况下虽然返回值不同,但参数的类型和个数相同
return的使用:使用return结束一个方法的操作,当执行到return时直接返回到方法调用处继续执行
public class MethodDemo06{
       public static void main(String args[]){
              fun(10) ;
              fun(3) ;
       }
       public static void fun(int x){
              System.out.println("进入方法。") ;
              if(x==3){
                     return ;// 返回方法的被调用处
              }
              System.out.println("结束方法。") ;
       }
}
递归调用:自己调用自己
public class MethodDemo07{
       public static void main(String args[]){
              int sum = 0 ;
              for(int i=0;i<=100;i++){
                     sum += i ;      // 累加操作
              }
              System.out.println("计算结果:" + sum) ;
       }
}
但递归操作通常要有明确的截止条件,否则会出现异常
public class MethodDemo08{
       public static void main(String args[]){
              int sum = 0 ;
              sum = fun(100) ;
              System.out.println("计算结果:" + sum) ;
       }
       public static int fun(int temp){
              if(temp==1){
                     return 1 ;
              }else{
                     return temp + fun(temp-1) ;
              }
       }
}
方法与数组
方法中对数组所作的一切修改,最终都会被保留下来
public class MethodArrayDemo01{
       public static void main(String args[]){
             int temp[] = {1,3,5,7,9} ;      // 声明数组
              fun(temp) ;
              print(temp) ;
       }
       public static void fun(int []x){
              x[0] = 6 ;       // 修改第一个元素
       }
       public static void print(int[] x){
              for(int i=0;i<x.length;i++){
                     System.out.print(x[i] + "、") ;
              }
       }
}
使用方法可以返回一个数组,只要在返回值类型上加入数组类型即可
public class MethodArrayDemo02{
       public static void main(String args[]){
             int temp[] = fun() ;// 声明数组
              print(temp) ;
       }
       public static int[] fun(){
              int x[] = {1,3,5,7,9} ;
              return x ;// 返回数组
       }
       public static void print(int[] x){
              for(int i=0;i<x.length;i++){
                     System.out.print(x[i] + "、") ;
              }
       }
}
排序操作
public class MethodArrayDemo03{
       public static void main(String args[]){
             int temp[] = fun() ;// 声明数组
              java.util.Arrays.sort(temp) ;      // 进行排序操作
              print(temp) ;
       }
       public static int[] fun(){
              int x[] = {23,1,5,3,24,3,56,4,3,1} ;
              return x ;// 返回数组
       }
       public static void print(int[] x){
              for(int i=0;i<x.length;i++){
                     System.out.print(x[i] + "、") ;
              }
       }
}
拷贝操作
public class MethodArrayDemo04{
       public static void main(String args[]){
             int t1[] = {1,2,3,4,5,6,7,8,9} ;
              int t2[] = {11,22,33,44,55,66,77,88,99} ;
              // 源数组名称下标目标数组下标拷贝长度
              System.arraycopy(t2,0,t1,3,3) ; // 数组拷贝
              print(t1) ;
       }
       public static void print(int[] x){
              for(int i=0;i<x.length;i++){
                     System.out.print(x[i] + "、") ;
              }
       }
}
新特性对数组的支持
foreach
for(数据类型变量:数组){
   //操作
}
public class MethodArrayDemo05{
       public static void main(String args[]){
             int t1[] = {1,2,3,4,5,6,7,8,9} ;
              for(int x:t1){
                     System.out.print(x + "、") ;
              }
       }
}
可变参数
声明方法参数时,参数个数不固定,则使用也不固定
public static 返回值类型数组名称(数据类型…参数名称)
public class MethodArrayDemo06{
       public static void main(String args[]){
              int temp[] = {2,4,6,8} ;
             fun() ;            // 没有参数
             fun(1) ;   // 一个参数
             fun(1,3,5,7,9) ;      // 一个参数
              fun(temp) ;
       }
       public static void fun(int ... arg){
              for(int x:arg){
                     System.out.print(x + "、") ;
              }
              System.out.println() ;
       }
}
对象的创建及使用
类通过属性建立模型,而对象将模型实例化,方法则是完成任务使用的便利工具
类名对象名称=null;    //声明对象
对象名称=new 类名();    //实例化对象
或直接实例化   类名对象名称=new 类名();
class Person{
       String name    ;      // 表示人的姓名
       int age ;          // 表示人的年龄
       public void tell(){   // 定义说话的方法
              System.out.println("姓名:" + name + ",年龄:" + age) ;
       }
};
public class OODemo02{
       public static void main(String args[]){
              Person per = new Person() ; // 产生实例化对象
       }
}
访问类中的属性和方法
访问属性:对象名称.属性名=值
访问方法:对象名称.方法()
class Person{
       String name    ;      // 表示人的姓名
       int age ;          // 表示人的年龄
       public void tell(){   // 定义说话的方法
              System.out.println("姓名:" + name + ",年龄:" + age) ;
       }
};
public class OODemo03{
       public static void main(String args[]){
              Person per = new Person() ; // 产生实例化对象
              per.name = "张三" ;              // 为名字赋值
              per.age = 30 ;                // 为年龄赋值
              per.tell() ;                     // 调用方法
       }
}
引用传递
将一个堆内存空间的使用权交给其他对象,相当于为一个堆内存空间起了一个别名
class Person{
       String name    ;      // 表示人的姓名
       int age ;          // 表示人的年龄
       public void tell(){   // 定义说话的方法
              System.out.println("姓名:" + name + ",年龄:" + age) ;
       }
};
public class OODemo06{
       public static void main(String args[]){
              Person per1 = null ;       // 声明对象
              Person per2 = null ;       // 声明对象
              per1 = new Person() ;    // 实例化对象
              per2 = per1 ;                 // 引用传递
              per1.name = "张三" ;            // 为名字赋值
              per1.age = 30 ;                     // 为年龄赋值
              per2.age = 33 ;
              per1.tell() ;                   // 调用方法
              per2.tell() ;
       }
}
class Person{
       String name    ;      // 表示人的姓名
       int age ;          // 表示人的年龄
       public void tell(){   // 定义说话的方法
              System.out.println("姓名:" + name + ",年龄:" + age) ;
       }
};
public class OODemo07{
       public static void main(String args[]){
              Person per1 = null ;       // 声明对象
              Person per2 = null ;       // 声明对象
              per1 = new Person() ;    // 实例化对象
              per2 = new Person() ;    // 实例化对象
              per2.name = "李四" ;     
              per2 = per1 ;                 // 引用传递
              per1.name = "张三" ;            // 为名字赋值
              per1.age = 30 ;                     // 为年龄赋值
              per2.age = 33 ;
              per1.tell() ;                   // 调用方法
              per2.tell() ;
       }
}
一个栈内存只能引用一个堆内存,但一个堆内存中可以被多个栈内存所指向
封装属性:private 数据类型属性名称=默认值;
封装方法:private 返回值类型|void 方法名称(参数列表)
class Person{
       private String name;      // 表示人的姓名
       private int age ;             // 表示人的年龄
       public void tell(){   // 定义说话的方法
              System.out.println("姓名:" + name + ",年龄:" + age) ;
       }
};
public class OODemo09{
       public static void main(String args[]){
              Person per = new Person() ; // 实例化对象
              per.name = "张三" ;              // 为name属性赋值
              per.age = -30;
              per.tell() ;
       }
}
无法执行
固有:只要属性就必须封装,被封装的属性通过setter及getter方法设置和取得
class Person{
       private String name;      // 表示人的姓名
       private int age ;             // 表示人的年龄
       public void tell(){   // 定义说话的方法
              System.out.println("姓名:" + this.getName() + ",年龄:" + this.getAge()) ;
       }
       public void setName(String n){
              name = n ;
       }
       public void setAge(int a){
              if(a>=0&&a<=200){
                     age = a ;
              }
       }
       public String getName(){
              return name ;
       }
       public int getAge(){
              return age ;
       }
};
public class OODemo10{
       public static void main(String args[]){
              Person per = new Person() ; // 实例化对象
              per.setName("张三") ;           // 为name属性赋值
              per.setAge(-30);
              per.tell() ;
       }
}
this.方法()→调用本类方法
类的图形,power designer工具
构造方法,为类中属性进行初始化,每一个类中都存在一个构造方法
构造方法必与类名一致,定义时不能有返回值声明,也不能在中用return返回一个内容
class Person{
       private String name;      // 表示人的姓名
       private int age ;             // 表示人的年龄
       public Person(){            // 定义了一个构造方法
              System.out.println("******************") ;
       }
       public void tell(){   // 定义说话的方法
              System.out.println("姓名:" + this.getName() + ",年龄:" + this.getAge()) ;
       }
       public void setName(String n){
              name = n ;
       }
       public void setAge(int a){
              if(a>=0&&a<=200){
                     age = a ;
              }
       }
       public String getName(){
              return name ;
       }
       public int getAge(){
              return age ;
       }
};
public class OODemo11{
       public static void main(String args[]){
              Person per = null ;
              per = new Person() ;     // 实例化对象
              /*
              per.setName("张三") ;           // 为name属性赋值
              per.setAge(30);
              per.tell() ;
       */
       }
}
class Person{
       private String name;      // 表示人的姓名
       private int age ;             // 表示人的年龄
       public Person(){}
       public Person(String n){
              this.setName(n) ;
       }
       public Person(String n,int a){              // 定义了一个构造方法
              this.setName(n) ;    // 调用setName()方法
              this.setAge(a) ;              // 调用setAge()方法
              System.out.println("******************") ;
       }
       public void tell(){   // 定义说话的方法
              System.out.println("姓名:" + this.getName() + ",年龄:" + this.getAge()) ;
       }
       public void setName(String n){
              name = n ;
       }
       public void setAge(int a){
              if(a>=0&&a<=200){
                     age = a ;
              }
       }
       public String getName(){
              return name ;
       }
       public int getAge(){
              return age ;
       }
};
public class OODemo12{
       public static void main(String args[]){
              Person per = null ;
              per = new Person("张三",-30) ;    // 实例化对象
              per.tell() ;
       }
}
匿名对象,只使用一次的对象
class Person{
       private String name;      // 表示人的姓名
       private int age ;             // 表示人的年龄
       public Person(){}
       public Person(String n){
              this.setName(n) ;
       }
       public Person(String n,int a){              // 定义了一个构造方法
              this.setName(n) ;    // 调用setName()方法
              this.setAge(a) ;              // 调用setAge()方法
              System.out.println("******************") ;
       }
       public void tell(){   // 定义说话的方法
              System.out.println("姓名:" + this.getName() + ",年龄:" + this.getAge()) ;
       }
       public void setName(String n){
              name = n ;
       }
       public void setAge(int a){
              if(a>=0&&a<=200){
                     age = a ;
              }
       }
       public String getName(){
              return name ;
       }
       public int getAge(){
              return age ;
       }
};
public class OODemo13{
       public static void main(String args[]){
              new Person("张三",-30).tell() ;
       }
}
使用
class Student{
       private String name ;
       private int age ;
       private float english ;
       private float computer ;
       private float math ;
       public Student(){}
       public Student(String n,int a,float e,float c,float m){
              this.setName(n) ;
              this.setAge(a) ;
              this.setEnglish(e) ;
              this.setComputer(c) ;
              this.setMath(m) ;
       }
       public float sum(){
              return english + computer + math ;
       }
       public float avg(){
              return this.sum() / 3 ;
       }
       public float max(){
              float max = computer>math?computer:math ;
              max = max>english?max:english ;
              return max ;
       }
       public float min(){
              float min = computer<math?computer:math ;
              min = min<english?min:english ;
              return min ;
       }
       public String getInfo(){
              return      "学生信息: "n" +
                            ""t|- 姓名:" + this.getName() + ""n" +
                            ""t|- 年龄:" + this.getAge() + ""n" +
                            ""t|- 数学成绩:" + this.getMath() + ""n" +
                            ""t|- 英语成绩:" + this.getEnglish() + ""n" +
                            ""t|- 计算机成绩:" + this.getComputer() ;
       }
       public void setName(String n){
              name = n ;
       }
       public void setAge(int a){
              age = a ;
       }
       public void setEnglish(float e){
              english = e ;
       }
       public void setComputer(float c){
              computer = c ;
       }
       public void setMath(float m){
              math = m ;
       }
       public String getName(){
              return name ;
       }
       public int getAge(){
              return age ;
       }
       public float getEnglish(){
              return english ;
       }
       public float getComputer(){
              return computer ;
       }
       public float getMath(){
              return math ;
       }
}
public class ExecDemo{
       public static void main(String args[]){
              Student stu = new Student("张三",30,89.0f,91.0f,87.0f) ;
              System.out.println("总分:" + stu.sum()) ;
              System.out.println("平均分:" + stu.avg()) ;
              System.out.println("最高分:" + stu.max()) ;
              System.out.println("最低分:" + stu.min()) ;
              System.out.println(stu.getInfo()) ;
       }
}
class Address {
       private String national ;
       private String province ;
       private String city ;
       private String street ;
       private String zipcode ;
       public Address(){}
       public Address(String n,String p,String c,String s,String z){
              this.setNational(n) ;
              this.setProvince(p) ;
              this.setCity(c) ;
              this.setStreet(s) ;
              this.setZipcode(z) ;
       }
       public String getInfo(){
              return      "地址信息:" + ""n" +
                            ""t|- 国家:" + this.getNational() + ""n" +
                            ""t|- 省份:" + this.getProvince() + ""n" +
                            ""t|- 城市:" + this.getCity() + ""n" +
                            ""t|- 街道:" + this.getStreet() + ""n" +
                            ""t|- 邮编:" + this.getZipcode() ;                          
       }
       public void setNational(String n){
              national = n ; 
       }
       public void setProvince(String p){
              province = p ;
       }
       public void setCity(String c){
              city = c ;
       }
       public void setStreet(String s){
              street = s ;
       }
       public void setZipcode(String z){
              zipcode = z ;
       }
       public String getNational(){
              return national ;
       }
       public String getProvince(){
              return province ;
       }
       public String getCity(){
              return city ;
       }
       public String getStreet(){
              return street ;
       }
       public String getZipcode(){
              return zipcode ;
       }
};
public class AddressDemo{
       public static void main(String args[]){
              Address add = new Address("中国","北京","北京市","西城区","100088") ;
              System.out.println(add.getInfo()) ;
       }
}