java基础语法

基础语法

Demo03

进制运算

public class Demo03 {
   public static void main(String[] args) {
       int i = 10;
       int j = 010;
       int k = 0x10;

       System.out.printf("%d %d %d",i,j,k);    //10 8 16
       System.out.println();

       float f = 0.1f; //离散 有限 大约 接近但不等于 最好避免使用浮点型进行比较
       double b = 0.1;

       System.out.println(f == b); //false

       float f1 = 321543513f;
       float f2 = f1 + 1;
       System.out.println(f1 == f2);   //true

       char r = (char)20013;
       System.out.println(r);  //中
  }
}

 

 

Demo06

位运算

/**
* @version 1.0.0
* @time 2022/7/1 15:43
* @Author SmallWatermelon
*/
public class Demo06 {
   public static void main(String[] args) {

       /*
        * 位运算:
        * A = 0011 1100
        * B = 0000 1101
        *
        * A&B = 0000 1100
        * A|b = 0011 1101
        * A^B = 0011 0001
        * ~B = 1111 0010
        *
        * 2*8 = 16 2*2*2*2
        * 2 << 3 (效率极高,与底层接触)
        *
        * 0000 0001 1
        * 0000 0010 2
        * 0000 0011 3
        * 0000 0100 4
        * 0000 1000 8
        * 0001 0000 16
        * */

       System.out.println(2 << 3);
  }
}

 

Demo07

字符串连接符

/**
* @version 1.0.0
* @time 2022/7/1 16:07
* @Author SmallWatermelon
*/
public class Demo07 {
   public static void main(String[] args) {

       int a = 10;
       int b = 20;

       System.out.println("" + a + b); //1020
       System.out.println(a + b + ""); //30
  }
}

 

包机制 & JavaDoc

包机制

www.baidu.com

包应该生成为: com.baidu.www

package com.swm.base;

/**
* @time 2022/7/1 16:20
* @Author SmallWatermelon
* @since 1.8
*/
public class Doc {

   String name;

   /**
    *
    * @param name
    * @return
    * @throws Exception
    */
   
   // 两个斜杠加*即可生成上面注释
   public String test(String name) throws Exception{
       return name;
  }
}

##

JavaDoc 生成方法

进入文件夹内输入cmd 以下命令

javadoc -encoding UTF-8 -charset UTF-8 需要生成的类

 

 

 

递归

package com.swm.method;

/**
* @version 1.0
* @time 2022/7/2 10:45
* @Author SmallWatermelon
* @since 1.8
*/

//递归
//数据较小的时候使用
public class Demo06 {
   public static void main(String[] args) {
       Demo06 demo06 = new Demo06();
       int f = demo06.f(5);    //f = 120
       System.out.println("f = " + f);
  }

   public static int f(int i) {

       if(i == 1) {
           return 1 ;
      }else {
           return i * f(i - 1);
      }
  }
}

 

 

多维数组

package com.swm.array;

/**
* @version 1.0
* @time 2022/7/3 13:50
* @Author SmallWatermelon
* @since 1.8
*/
public class Demo08 {

   public static void main(String[] args) {

       int[][] ints = new int[11][11];

       ints[1][2] = 1;
       ints[2][3] = 2;

       for (int[] i:ints
            ) {
           for (int k:i
                ) {
               System.out.print(k + "\t");
          }
           System.out.println();
      }

       System.out.println("=============");

       int sum = 0;
       for (int i = 0; i < 11; i++) {
           for (int j = 0; j < 11; j++) {
               if (ints[i][j] != 0)
                   sum++;
          }
      }
       System.out.println("sum = " + sum);

       //稀疏数组
       int[][] ints1 = new int[sum + 1][3];

       ints1[0][0] = 11;
       ints1[0][1] = 11;
       ints1[0][2] = sum;

       int count = 0;

       for (int i = 0; i < ints.length; i++) {
           for (int j = 0; j < ints[i].length; j++) {
               if (ints[i][j] != 0) {
                   count++;
                   ints1[count][0] = i;
                   ints1[count][1] = j;
                   ints1[count][2] = ints[i][j];
              }
          }
      }

       for (int[] i:ints1
            ) {
           for (int k:i
                ) {
               System.out.print(k + "\t");
          }
           System.out.println();
      }
  }
   /*0 0 0 0 0 0 0 0 0 0 0
           0 0 1 0 0 0 0 0 0 0 0
           0 0 0 2 0 0 0 0 0 0 0
           0 0 0 0 0 0 0 0 0 0 0
           0 0 0 0 0 0 0 0 0 0 0
           0 0 0 0 0 0 0 0 0 0 0
           0 0 0 0 0 0 0 0 0 0 0
           0 0 0 0 0 0 0 0 0 0 0
           0 0 0 0 0 0 0 0 0 0 0
           0 0 0 0 0 0 0 0 0 0 0
           0 0 0 0 0 0 0 0 0 0 0
           =============
   sum = 2
           11 11 2
           1 2 1
           2 3 2*/
   
}

 

Static

package com.swm.oop.Demo07;

/**
* @version 1.0
* @time 2022/7/4 10:20
* @Author SmallWatermelon
* @since 1.8
*/
public class Person {

  {
       System.out.println("匿名代码块");
  }

   static {//只会在类加载的时候执行一次,属于类方法
       System.out.println("静态代码块");
  }

   public Person() {
       System.out.println("构造方法");
  }

   public static void main(String[] args) {
       /*Person person = */new Person();
       System.out.println("========");
       new Person();
       //静态代码块
       //匿名代码块
       //构造方法
       //========
       //匿名代码块
       //构造方法
  }
}

 

链接地址:

java基础

 
posted @ 2022-07-04 13:46  SmallWatermelon  阅读(105)  评论(0)    收藏  举报