Chinaunix首页 | 论坛 | 博客
  • 博客访问: 268051
  • 博文数量: 113
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1044
  • 用 户 组: 普通用户
  • 注册时间: 2015-02-15 16:09
文章分类

全部博文(113)

文章存档

2016年(5)

2015年(108)

我的朋友

分类: Java

2015-09-04 03:20:39

方法,类,对象,三者的关系
hello.java

点击(此处)折叠或打开

  1. //public这个类是公共的,class表示这是一个类
  2. //并且hello 为类名,必须和文件名一致
  3. public class hello {

  4.     /**
  5.      * @param args the command line arguments
  6.      */
  7.     //main主函数,相当于函数的入口
  8.     public static void main(String[] args) {
  9.         //运行命令
  10.         //编译用 javac hello.java
  11.         //执行java 程序 java hello
  12.             System.out.println ("hello ");
  13.         // TODO code application logic here
  14.     }
  15.     
  16. }
java的变量类型,有;
1. byte型 ,short型,int型,long  ,float ,double,
2.char   string型,。boolean型

点击(此处)折叠或打开

  1. /*使用强制类型转换,防止数据溢出*/
  2. public class DataDemo03 {
  3.     public static void main(String[] args) {
  4.     int max = Integer.MAX_VALUE;     // 得到整型的最大值
  5.     System.out.println("整型的最大值:" + max);// 输出最大值
  6.     System.out.println(“整型最大值 + 1:+ (max + 1));//
  7.                                               大值加1
  8.     System.out.println(“整型最大值 + 2:+ (max + 2L));//
  9.                               最大值加2,变为long型
  10.     System.out.println(“整型最大值 + 2:+ ((long)max+ 2)); // 强制转换为long型
  11.     }
  12. }
#################
在Java中也提供了左移“<<”及右移“>>”两种操作。
左移操作是将运算数的二进制码整体左移指定位数,左移之后的空位使用0来填充
右移操作“>>”是将运算数的二进制码整体右移,右移之后空出来的位置以符号位填充。如果是整数使用“0”填充,如果是负数使用“1”填充。
####################

点击(此处)折叠或打开

  1. public class OperatorDemo19 {
  2.     public static void main(String[] args) {
  3.         int x = 3 ;    
  4.         // 3的二进制数据:00000000 00000000 00000000 00000011
  5.         int y = -3 ;    
  6.         // -3的二进制数据:11111111 11111111 11111111 11111101
  7. System.out.println(x + "右移2位之后的内容:" + (x >>> 2));
  8. System.out.println(y + "右移2位之后的内容:" + (y >>> 2));
  9.     }
  10. }
3.switch语句的使用

点击(此处)折叠或打开

  1. public class SwitchDemo{
  2.     public static void main(String args[]){
  3.         char c='a';
  4.         switch(c){
  5.             case 'b':System.out.println('b');
  6.             break;
  7.             case 'c':System.out.println('c');
  8.             break;
  9.             case 'a':System.out.println('a');
  10.             break;
  11.             default:System.out.println('d');
  12.         }            
  13.     }
  14. }
4.break  跳出循环,而 continue 则是忽略本次循环,执行下一次循环。
5.return  直接回到方法的开头。
6.取得数组长度

点击(此处)折叠或打开

  1. /*数组名称.length 返回一个int型数据*/
  2. public class ArrayDemo03 {
  3.     public static void main(String[] args) {
  4.         int score[] = new int[3];                        // 声明并实例化数组
  5.         System.out.println("数组长度为" + score.length);    // 求出数组长度
  6.     }
  7. }
数组的静态初始化

点击(此处)折叠或打开

  1. public class ArrayDemo04 {
  2.     public static void main(String[] args) {
  3.         int score[] = {91,92,93,94,95,96};    
  4.                              // 使用静态初始化声明数组
  5.         for (int x = 0; x < score.length; x++) {    
  6.                                  // 循环输出
  7.         System.out.println("score["+x+"] = " + score[x]) ;
  8.         }
  9.     }
  10. }
7.对整型数组按照由小到大的顺序进行排列

点击(此处)折叠或打开

  1. public class ArrayDemo06 {
  2.     public static void main(String[] args) {
  3.     int score[] = { 67, 89, 87, 69, 90, 100, 75, 90 };    
  4.                                             // 声明数组
  5.     for (int i = 1; i < score.length; i++) {// 循环判断
  6.             for (int j = 0; j < score.length; j++) {
  7.             if (score[i] < score[j]) {    // 交换位置                
  8.                     int temp = score[i];
  9.                     score[i] = score[j];
  10.                     score[j] = temp;
  11.                 }
  12.             }
  13.         }
  14.      for (int i = 0; i < score.length; i++) {// 数组输出
  15.             System.out.print(score[i] + "\t");
  16.         }
  17.     }
  18. }

8.二维数组的定义及使用

点击(此处)折叠或打开

  1. public class ArrayDemo08 {
  2.     public static void main(String[] args) {
  3.         int score[][] = new int[4][3];    // 声明并实例化二维数组
  4.         score[0][1] = 30 ;            // 为数组中的部分内容赋值
  5.         score[1][0] = 31 ;            // 为数组中的部分内容赋值
  6.         score[2][2] = 32 ;            // 为数组中的部分内容赋值
  7.         score[3][1] = 33 ;            // 为数组中的部分内容赋值
  8.         score[1][1] = 30 ;            // 为数组中的部分内容赋值
  9.         for (int i = 0; i < score.length; i++) {    // 外层循环行
  10.             for(int j=0;j<score[i].length;j++){// 内层循环列
  11.                 System.out.print(score[i][j] + "\t");
  12.             }
  13.             System.out.println("") ;        // 换行
  14.         }
  15.     }
  16. }
9.1使用Java类库完成数组的排序操作

点击(此处)折叠或打开

  1. public class ArrayRefDemo04 {
  2.     public static void main(String[] args) {
  3.         int score[] = { 67, 89, 87, 69, 90, 100, 75, 90 };    
  4.                                                // 定义整型数组
  5.         int age[] = { 31, 30, 18, 17, 8, 9, 1, 39 };    // 定义整型数组
  6.         java.util.Arrays.sort(score);    // 使用JAVA提供的排序操作
  7.         print(score);                // 输出数组
  8.         System.out.println("\n-----------------------------");
  9.         java.util.Arrays.sort(age);    // 使用JAVA提供的排序操作
  10.         print(age);
  11.     }
  12.     public static void print(int temp[]) {    // 数组输出
  13.         for (int i = 0; i < temp.length; i++) {
  14.             System.out.print(temp[i] + "\t");
  15.         }
  16.     }
  17. }


9.使用静态初始化声明一个二维数组

点击(此处)折叠或打开

  1. public class ArrayDemo09 {
  2.     public static void main(String[] args) {
  3.         // 静态初始化一个二维数组,每行的数组元素个数不一样
  4.         int score[][] = { { 67, 61 }, { 78, 89, 83 }, { 99, 100, 98, 66, 95 } };
  5.         for (int i = 0; i < score.length; i++) {    // 外层循环输出行
  6.             for (int j = 0; j < score[i].length; j++) {
  7.                                                 // 内存循环输出列
  8.                 System.out.print(score[i][j] + "\t");
  9.                                                 // 输出每一个元素
  10.             }
  11.             System.out.println("");            // 换行
  12.         }
  13.     }
  14. }

10.定义一个方法,在主方法中进行调用

点击(此处)折叠或打开

  1. public class MethodDemo01 {
  2.     public static void main(String[] args) {
  3.         printInfo() ;            // 调用printInfo()方法
  4.         printInfo() ;            // 调用printInfo()方法
  5.         printInfo() ;            // 调用printInfo()方法
  6.         System.out.println("Hello World!") ;
  7.     }
  8.     // 此处由于此方法是由main方法直接调用所以一定要加上public
  9.                                                   static
  10.     public static void printInfo() {    // 此处方法没有返回值
  11.         char c[] = {'H','e','l','l','o',
  12.                 ',','L','X','H'};// 定义一个字符数组
  13.         for (int x = 0; x < c.length; x++) {// 循环输出
  14.             System.out.print(c[x]) ;
  15.         }
  16.         System.out.println("") ;         // 换行
  17.     }
  18. }

11.方法的重载

点击(此处)折叠或打开

  1. public class MethodDemo03 {
  2.     public static void main(String[] args) {
  3.         int one = add(10, 20);    // 调用有两个参数的整型加法
  4.         int two = add(10, 20, 30);    // 调用有三个参数的整型加法
  5.         float three = add(10.3f, 13.3f);// 调用有两个参数的浮点型加法
  6.         System.out.println("add(int x, int y)的计算结果:" + one) ;
  7.         System.out.println("add(int x, int y, int z)的计算结果:"+two);
  8.         System.out.println("add(float x, float y)的计算结果:"+three);
  9.     }
  10.     public static int add(int x, int y) {    // 定义add方法,完成两个整数相加
  11.         int temp = 0;            // 定义局部变量
  12.         temp = x + y;            // 执行加法计算
  13.         return temp;            // 返回计算结果
  14.     }
  15.     public static int add(int x, int y, int z) {// 定义add方法,完成三个整数相加
  16.         int temp = 0;            // 定义局部变量
  17.         temp = x + y + z;            // 执行加法操作
  18.         return temp;            // 返回计算结果
  19.     }
  20.     public static float add(float x, float y) {    // 定义add方法,完成两个浮点数相加
  21.         float temp = 0;            // 定义局部变量
  22.         temp = x + y;            // 执行加法操作
  23.         return temp;            // 返回计算结果
  24.     }
  25. }
12.使用Java类库中的方法完成数组拷贝操作

点击(此处)折叠或打开

  1. public class ArrayCopyDemo02 {
  2.     public static void main(String args[]) {
  3.         int i1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };// 源数组
  4.         int i2[] = { 11, 22, 33, 44, 55, 66, 77, 88, 99 };//
  5.                                                        标数组
  6.         System.arraycopy(i1, 3, i2, 1, 3);// JAVA对数组拷贝的支持
  7.         print(i2);
  8.     }
  9.     public static void print(int temp[]) {    // 输出数组
  10.         for (int i = 0; i < temp.length; i++) {
  11.             System.out.print(temp[i] + "\t");
  12.         }
  13.     }
  14. }








阅读(1125) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~