一、编写第一个java程序
1 public class Hello 2 { 3 pubic static void main(String args[]) 4 { 5 System.out.println("Hello World"); 6 } 7 }
二、简单的java范例
1 public class TestJava 2 { 3 public static void main(String[] args) //java操作的一个简单范例,输出和乘方 4 { 5 int num = 10; //定义整型变量 6 num = 30; //修改变量内容 7 System.out.println("num的内容是:" + num); //输出变量内容 8 System.out.println("num * num = " + num * num ); //输出乘方 9 } 10 } 11 /* 12 程序运行结果: 13 num的内容是:30 14 num * num = 900 15 */
1 class DataDemo02 2 { 3 public static void main(String[] args) 4 { 5 int max = Integer.MAX_VALUE; //得到整型的最大值 6 System.out.println("整型的最大值:" + max); //输出最大值 7 System.out.println("整型的最大值 + 1:" + (max + 1)); //最大值加1 8 System.out.println("整型的最大值 + 2:" + (max + 2)); //最大值加2 9 } 10 } 11 /* 12 输出结果是: 13 整型的最大值:2147483647 14 整型的最大值 + 1:-2147483648 15 整型的最大值 + 2:-2147483647 16 */
1 class DataDemo02 2 { 3 public static void main(String[] args) 4 { 5 int max = Integer.MAX_VALUE; //得到整型的最大值 6 System.out.println("整型的最大值:" + max); //输出最大值 7 System.out.println("整型的最大值 + 1:" + (max + 1)); //最大值加1 8 System.out.println("整型的最大值 + 2:" + (max + 2L)); //最大值加2,变为long型 9 System.out.println("整型的最大值 + 2:" + ((long)max + 2)); //强制转换为long型 10 } 11 } 12 /* 13 输出结果是: 14 整型的最大值:2147483647 15 整型的最大值 + 1:-2147483648 16 整型的最大值 + 2:2147483647 17 整型的最大值 + 2:2147483647 18 */
1 public class DataDemo04 2 { 3 public static void main(String[] args) 4 { 5 char ch1 = 'a'; //定义字符 6 char ch2 = 97; //定义字符,整型转字符 7 System.out.println("ch1 = " + ch1); //打印输出 8 System.out.println("ch2 = " + ch2); //打印输出 9 } 10 } 11 /* 12 输出内容: 13 ch1 = a 14 ch2 = a 15 */
1 public class DataDemo06 2 { 3 public static void main(String[] args) 4 { 5 float num = 3.0f; //定义float型变量 6 System.out.println("两个小数相乘:" + num * num); //计算两数相乘 7 } 8 } 9 10 /* 11 输出内容: 12 两个小数相乘:9.0 13 */
1 public class DataDemo07 2 { 3 public static void main(String[] args) 4 { 5 boolean flag = true; //定义布尔型变量 6 System.out.println("flag = " + flag); //打印输出 7 } 8 } 9 /* 10 输出内容: 11 flag = true 12 */
1 public class DataDemo08 2 { 3 public static void main(String[] args) 4 { 5 int x = 30; //定义整型变量 6 int float = 22.19f; //定义浮点型变量 7 System.out.println("x/y = " + (x/y)); //除法操作 8 System.out.println("10/3.5 = " + (10/3.5)); //直接使用常量进行除法 9 System.out.println("10/3 = " + (10/3)); //直接使用常量进行除法 10 } 11 } 12 /* 13 输出内容: 14 x/y = 1.3519603 15 10/3.5 = 2.857142857142857 16 10/3 = 3 17 */
1 public class DataDemo09 2 { 3 public static void main(String[] args) 4 { 5 String str = "lixinghua "; //定义字符变量 6 int x = 30; //定义整型变量 7 str = str + x; //改变字符串变量内容 8 System.out.println("str = " + str); //打印输出 9 } 10 } 11 /* 12 输出内容: 13 str = lixinghua 30 14 */
1 public class DataDemo11 2 { 3 public static void main(String[] args) 4 { 5 float f = 30.3f; //定义浮点型变量 6 int x = (int) f; //强制转换为int型 7 System.out.println("x = " + x); //输出转型之后的值 8 System.out.println("10/3 = " + ((float)10/3)); //常量计算使用强制类型转换 9 } 10 } 11 /* 12 输出内容: 13 x = 30 14 10 / 3 = 3.3333333 15 */
1 public class ForNestedDemo 2 { 3 public static void main(String[] args) 4 { 5 for(int i = 1; i <=9; i++) //第一层循环 6 { 7 for(int j = 1; j <= i; j++) //第二层循环 8 { 9 System.out.println(i + "*" + j + "=" + (i * j) + " "); 10 } 11 System.out.printf(" "); //换行 12 } 13 } 14 }
1 public class ArrayDemo01 2 { 3 public static void main(String[] args) 4 { 5 int score[] = null; 6 7 score = new int[3]; 8 System.out.println("score[0] = " + score[0]); 9 System.out.println("score[1] = " + score[1]); 10 System.out.println("score[2] = " + score[2]); 11 for(int x=0; x<3; x++) 12 { 13 System.out.println("score["+x+"] = "+score[x]); 14 } 15 } 16 }
1 public class ArrayDemo05 2 { 3 public static void main(String[] args) 4 { 5 int score[] = {67,89,87,69,90,100,30,50}; //静态初始化数组 6 int max = 0; //定义变量保存最大值 7 int min = 0; //定义变量保存最小值 8 max = min = score[0]; //把第一个元素的内容赋值给max和min 9 10 for(int x=0; x<score.length; x++) //循环求出最大和最小 11 { 12 if(score[x]>max) //依次判断后续元素是否比max大 13 { 14 max = score[x]; span class= //如果大,则修改max内容 15 } 16 if(score[x]<min) //一次判断后续元素是否比min小 17 { 18 min = score[x]; //如果小,则修改min内容 19 } 20 } 21 System.out.println("最高成绩:" + max); //输出最大值 22 System.out.println("最低成绩: " + min); //输出最小值 23 } 24 }
1 public class ArrayDemo06 2 { 3 public static void main(String[] args) 4 { 5 int score[] = {67,89,87,69,90,100,75,90}; // 声明数组 6 for(int i=1; i<score.length; i++) //循环判断 7 { 8 for(int j=0; j<score.length; j++) //交换位置 9 { 10 if(score[i]<score[j]) 11 { int temp = score[i]; 12 score[i] = score[j]; 13 score[j] = temp; 14 } 15 } 16 } 17 for(int i=0; i<score.length; i++) //数组输出 18 System.out.println(score[i] + " "); 19 } 20 }
1 class A 2 { 3 public int i = 10; 4 protected int j = 20; 5 6 public void e(int i, int j) 7 { 8 this.i = i; 9 this.j = j; 10 } 11 public void f() 12 { 13 System.out.printf("%d",this.i); 14 } 15 16 public void g() 17 { 18 f(); 19 System.out.printf("%d",this.j); 20 } 21 } 22 23 class D 24 { 25 public static void main(String[] args) 26 { 27 A aa1 = new A(); 28 aa1.e(50,60); 29 aa1.g(); 30 } 31 }
1 class StaticDemo 2 { 3 static int a = 42; 4 static int b = 99; 5 6 static void callme() 7 { 8 System.out.println("a = " + a); 9 } 10 } 11 12 class StaticByName 13 { 14 public static void main(String args[]) 15 { 16 StaticDemo.callme(); 17 System.out.println("b = " + StaticDemo.b); 18 } 19 } 20 21 /* 22 输出内容是: 23 a = 42 24 b = 99 25 26 static成员是不能被其所在class创建的实例访问的。 27 28 如果不加static修饰的成员是对象成员,也就是归每个对象所有的。 29 30 加static修饰的成员是类成员,就是可以由一个类直接调用,为所有对象共有的 31 32 33 */
1 class A 2 { 3 public int i = 10; 4 private static A aa =new A(); // aa 是否是A对象的属性 5 6 private A() 7 { 8 } 9 public static A getA() //static 一定不能省 如果省略就只能通过对象去调用它了 10 { 11 return aa; 12 } 13 } 14 15 public class D 16 { 17 public static void main(String[] args) 18 { 19 A aa1 = A.getA(); 20 A aa2 = A.getA(); 21 22 aa1.i = 99; 23 System.out.printf("%d ",aa2.i); 24 } 25 }
1 class A 2 { 3 private int i; 4 private static int count = 0; 5 6 public A() 7 { 8 ++count; 9 } 10 11 public A(int i) 12 { 13 this.i = i; 14 ++count; 15 } 16 17 public static int getcount() 18 { 19 //return 返回A对象的个数 20 return count; 21 } 22 23 } 24 class D 25 { 26 public static void main(String[] args) 27 { 28 A aa = new A(); 29 A bb = new A(); 30 System.out.printf("%d",aa.getcount()); 31 } 32 }
1 class Human 2 { 3 public String name = "晓得"; 4 public int age = 30; 5 } 6 7 class Student extends Human 8 { 9 public double score = 66.6; 10 } 11 12 class Graduate extends Student 13 { 14 public int c = 100; 15 } 16 17 class E 18 { 19 public static void main(String[] args) 20 { 21 Graduate gd = new Graduate(); 22 System.out.printf("%s %d %f",gd.name,gd.age,gd.score); 23 } 24 } 25 /* 26 一个新类从已有的类那里获得其已有的属性和方法,这中现象叫类的继承 27 这个新类别称为子类,也叫派生类,已有的哪个类叫做父类,也叫做基类 28 继承的好处 29 代码得到极大的重用 30 形成一种类的层次体系结构,为多态创造条件 31 */
1 class A 2 { 3 public int i,j; 4 5 public A(int i,int j) 6 { 7 this.i = i; 8 this.j = j; 9 } 10 } 11 12 class B extends A 13 { 14 public int k; 15 16 public B(int i, int j, int k) 17 { 18 super(i,j); 19 this.k = k; 20 } 21 22 public void g() 23 { 24 System.out.printf("%d %d %d",i,j,k); 25 } 26 } 27 28 29 30 class E 31 { 32 public static void main(String[] args) 33 { 34 B aa = new B(3,3,4); 35 aa.g(); 36 } 37 } 38 39 /* 40 总结: 41 1、每个子类构造方法的第一条语句,都是隐含地调用super(),如果父类没有这种形式的 42 构造函数,那么在编译的时候就会报错。 43 44 2、如果显示的写出super();语句,则必须保证该语句是第一条语句,否则会出错 45 46 3、super();如果不写,则编译器会自动添加,所以此时如果父类没有无参的构造函数就会出错 47 48 4、既可以显示写super();前提是父类必须有无参的构造函数 49 也可以显示写super(实参); 前提是父类必须有带参的构造函数 50 51 5、调用父类的构造函数的语句必须借助于super,不能直接写父类的类名,这与C++不同, 52 */
1 /* 2 2008年6月6日8:42:07 3 super的使用 4 */ 5 6 class A 7 { 8 A() 9 { 10 System.out.println("AAAA"); 11 } 12 13 A(int i) 14 { 15 } 16 } 17 18 class B extends A 19 { 20 B() 21 { 22 super(2); //如果把该语句注释掉的化,则编译器默认的是自动隐藏调用super(); 但如果父类没有无参的构造函数,则会报错 23 //一个子类的构造函数中只能出现一个 super(....) 24 System.out.println("BBBB"); 25 } 26 } 27 28 class C extends B 29 { 30 C() 31 { 32 //int k = 10; //如果该语句生效 则会出错,因为会导致super()语句不是构造函数的第一条语句 33 34 super(); //35行 每个子类构造方法的第一条语句,都是隐含地调用super(),如果父类没有无参的构造函数,那么在编译的时候就会报错。 35 //super();语句可以写,也可以不写,不写的化,系统会自动调用的 36 //如果显示写出来的话,编译器要求该语句前面不能加任何语句,也就是说该语句必须保证是第一条语句 37 // super()也可以改为super(2); 但前提是父类必须有带一个参数的构造函数,否则也会报错 38 //如果把35行改为 super(2); 编译时就会报错! 39 40 System.out.println("CCCC"); 41 } 42 } 43 44 class TestSuper_1 45 { 46 public static void main(String[] args) 47 { 48 C cc = new C(); 49 50 //System.out.println("Hello World!"); 51 } 52 } 53 /* 54 在j2sdk1.4.2_16中的运行结果是: 55 ----------------------- 56 BBBB 57 CCCC 58 ----------------------- 59 60 总结: 61 1、每个子类构造方法的第一条语句,都是隐含地调用super(),如果父类没有这种形式的 62 构造函数,那么在编译的时候就会报错。 63 64 2、如果显示的写出super();语句,则必须保证该语句是第一条语句,否则会出错 65 66 3、super();如果不写,则编译器会自动添加,所以此时如果父类没有无参的构造函数就会出错 67 68 4、既可以显示写super();前提是父类必须有无参的构造函数 69 也可以显示写super(实参); 前提是父类必须有带参的构造函数 70 71 5、调用父类的构造函数的语句必须借助于super,不能直接写父类的类名,这与C++不同, 72 */
1 class Human 2 { 3 private String name; 4 private int age; 5 6 public Human() 7 { 8 } 9 10 public Human(String name, int age) 11 { 12 this.name = name; 13 this.age = age; 14 } 15 16 public void setName(String name) 17 { 18 this.name = name; 19 } 20 21 public void setAge(int age) 22 { 23 this.age = age; 24 } 25 26 public String getInfor() 27 { 28 String strInf = name + ": " + age; 29 return strInf; 30 } 31 } 32 33 class Student extends Human 34 { 35 public String school; 36 37 public Student() 38 { 39 } 40 41 public Student(String name, int age, String school) 42 { 43 super(name, age); 44 // this.name = name; //error 因为name时私有的 45 // this.age = age; //error 同上 46 this.school = school; 47 } 48 49 public void setSchool(String school) 50 { 51 this.school = school; 52 } 53 54 public String getInfor() 55 { 56 //String strInf = name + ": " + age + ": " + school; //error 因为age是私有的 57 String strInf = super.getInfor() + ": " + school; 58 return strInf; 59 } 60 } 61 62 public class TestStudent1 63 { 64 public static void main(String[] args) 65 { 66 Student st1 = new Student("张三", 22, "瑞德"); 67 System.out.printf("%s ", st1.getInfor()); 68 } 69 }
1 class A 2 { 3 public void f() 4 { 5 System.out.printf("AAAA "); 6 } 7 } 8 9 class B extends A 10 { 11 public void f() 12 { 13 System.out.printf("BBBB "); 14 } 15 } 16 17 class C extends B 18 { 19 public void f() 20 { 21 System.out.printf("CCCC "); 22 } 23 } 24 25 public class F 26 { 27 public static void g(A aa) 28 { 29 aa.f(); 30 } 31 public static void main(String[] args) 32 { 33 A aa = new A(); 34 B bb = new B(); 35 C cc = new C(); 36 37 g(aa); 38 g(bb); 39 g(cc); 40 } 41 } 42 /* 43 多态注意事项 44 子类对象可以直接赋给父类引用,但父类对象在任何情况下 45 都不可以直接赋给子类引用,因为子类是父类的一种,但父类 46 不是子类的一种,或者讲“子类可以当作父类看待,但父类不可以 47 当做子类看待”,“狗可以当做动物看待,但动物不可以当做狗来看待” 48 49 通过父类引用只能访问子类从父类继承过来的成员 50 通过父类引用不能方位子类对象所特有的成员 51 父类引用永远不可能直接赋给子类引用 52 只有在父类引用本身指向的就是一个子类对象时,才可以把父类引用 53 强制转化为子类引用 54 55 其他情况下不允许把父类引用强制转化为子类引用,否则运行时会出错 56 */
1 interface It 2 { 3 public abstract void f(); //不能带有方法体( 抽象类中允许) 4 } 5 6 interface It2 7 { 8 final int i = 20; // 用final 修饰 表示i 是最后一次赋值 9 } 10 11 final class A implements It,It2 // 用final修饰 表示该类不能再被继承 implements 表示连接接口 12 { 13 14 public void f() //一个类要想实现某接口中的方法时,必须得在方法返回值前加public 15 { 16 System.out.printf("i = %d",i); 17 } 18 } 19 20 class F 21 { 22 public static void main(String[] args) 23 { 24 A aa = new A(); 25 aa.f(); 26 } 27 } 28 /* 29 1. 接口中定义的属性必须是public static final的,而接口中定义的方法则必须 30 是public abstract的,一次这些修饰符可以部分或全部省略。 31 32 2. 接口中定义的属性的值在实现类中不能被更改 33 34 3. 参见TestInter_1.java 35 36 4. 一个类只能实现某个接口, 不能继承某个接口 37 38 5. 但接口可以继承接口 39 40 6. 接口不但可以继承接口,而且可以继承多个接口,即接口允许多继承 41 42 7. 如果一个类只实现一个接口的部分方法,则该类必须的声明为抽象类 43 44 8. 一个类可以在继承一个父类的同时实现一个或多个接口,但extends 45 关键字必须的在implements之前 46 TestInter_2.java 47 48 9. 不可以new接口对象,但可以定义一个接口引用类型的变量,并将其指 49 向实现接口的对象,达到多态的目的! 50 */
1 class A 2 { 3 int divide(int a ,int b) 4 { 5 int m ; 6 //System.out.printf("123456"); 7 m = a/b; 8 //System.out.printf("123456"); 9 return m; 10 } 11 } 12 13 class B 14 { 15 public static void main(String[] args) 16 { 17 A aa = new A(); 18 19 try 20 { 21 aa.divide(6,0); 22 } 23 catch(ArithmeticException e) //e用来接收23行抛出的异常对象 24 { 25 e.printStackTrace(); //可以简单理解为输出该异常的具体信息 26 System.out.printf("除零错误,你的程序出错啦!除数不能为零。"); 27 } 28 System.out.printf("是不是啊"); 29 } 30 }