Java基础知识整理(一)

来源:互联网 发布:nginx http2配置 编辑:程序博客网 时间:2024/05/21 11:16

1.print和println的区别:利用print输出结果后不换行,而println换行。

 System.out.println("你好,"+        "很高兴认识你");//这个自动换行        System.out.print("不换行");//这个不换行        System.out.println("换行");        System.out.println("看!!");

2.格式控制符:%d-int;%c-char;%f-float;%s-字符串;%md-m列的int型;%m.nf-输出的float型数据占m列,小数点保留n位。

 System.out.printf("%d,%f",12,23.78);

3.判断一个数是否在数组中:

         int start=0,end,middle;        int a[]={12,45,67,89,123,-45};        int t=0;        int N=a.length;        for(int i=0;i<N;i++) {            for(int j=i+1;j<N;j++) {                if (a[j]<a[i]) {                     t=a[j];                    a[j]=a[i];                    a[i]=t;                }            }        }        Scanner scanner=new Scanner(System.in);        System.out.println("输入整数,程序判断该整数是否在数组中:");        int number=scanner.nextInt();        int count=0;        end=N;        middle=(start+end)/2;        while(number!=a[middle]) {            if(number>a[middle])                start=middle;            else if (number<a[middle])                end=middle;            middle=(start+end)/2;            count++;            if(count>N/2)                break;        }            if(count>N/2)                System.out.printf("%d不在数组中.\n",number);            else                System.out.printf("%d在数组中.\n",number);

4.逻辑运算符与位运算符的比较:

        int x,y;        x=1;        //逻辑运算符短路求值        System.out.println(((y=1)==0)&&((x=6)==6));        System.out.println(x);        //位运算符将所有值计算之后再算结果        x=1;        System.out.println( ((y=1)==0)&((x=6)==6));        System.out.println(x);

5.查看数据类型的取值范围:

        /*        System.out.println("byte取值范围:"+Byte.MIN_VALUE+"至"+Byte.MAX_VALUE);        System.out.println("short取值范围:"+Short.MIN_VALUE+"至"+Short.MAX_VALUE);        System.out.println("int取值范围:"+Integer.MIN_VALUE+"至"+Integer.MAX_VALUE);        System.out.println("long取值范围:"+Long.MIN_VALUE+"至"+Long.MAX_VALUE);        System.out.println("float取值范围:"+Float.MIN_VALUE+"至"+Float.MAX_VALUE);        System.out.println("double取值范围:"+Double.MIN_VALUE+"至"+Double.MAX_VALUE);        */

6.查看数组的长度:

 /*        long[] a={1,2,3,4};        long[] b={100,200,300,400};        b=a;        System.out.println("数组b的长度:"+b.length);        System.out.println("b[0]="+b[0]);        */
 /*        int[] a={10,20,30,40},b[]={{1,2},{4,5,6,7}};        b[0]=a;        b[0][1]=b[1][3];        System.out.println(b[0][3]);        System.out.println(a[1]);        */
输出结果:40 7(中间有换行)

7.查看汉字在Unicode表中的位置

 /*        char ni='你';        char wo='我';        char ta='他';        System.out.println("你、我、他的位置分别是:");        System.out.println((int)ni);        System.out.println((int)wo);        System.out.println((int)ta);        */

8.利用异或运算进行加密和解密

 /*        char a1='十',a2='点',a3='进',a4='攻';        char secret='A';        a1=(char)(a1^secret);        a2=(char)(a2^secret);        a3=(char)(a3^secret);        a4=(char)(a4^secret);        System.out.println("密文:"+a1+a2+a3+a4);        a1=(char)(a1^secret);        a2=(char)(a2^secret);        a3=(char)(a3^secret);        a4=(char)(a4^secret);        System.out.println("原文:"+a1+a2+a3+a4);        */

9.for语句与数组:使用for语句的传统方式和改进方式遍历数组

 /*        int a[]={1,2,3,4};        char b[]={'a','b','c','d'};        for(int n=0;n<a.length;n++) {//传统方式            System.out.println(a[n]);        }        for(int n=0;n<b.length;n++) {//传统方式            System.out.println(b[n]);        }        for(int i:a) {              //改进方式            System.out.println(i);        }        for(char ch:b) {            //改进方式            System.out.println(ch);        }        */

9.switch语句的执行

 char c='\0';        for(int i=1;i<=4;i++) {            switch(i) {            case 1: c='J';                    System.out.print(c);            case 2: c='e';                    System.out.print(c);                    break;            case 3: c='p';                    System.out.print(c);            default:System.out.print("好");            }        }
 输出结果:Jeep好好

10. 自增和自减运算符

 int x=1,y=6;        while(y-->0) {            x--;            //System.out.println("x="+x+",y="+y);        }        System.out.print("x="+x+",y="+y);
输出结果x=-5,y=-1

代码来源:《Java2使用教程》(第四版)耿祥义 张跃平

0 0