zoukankan      html  css  js  c++  java
  • Java 基础(一)

    声明变量

    class demo{
        public static void main(String[] args){
            // 先声明,后赋值
            int a;
            a = 1;
    
            // 直接赋值
            int b = 2;
    
            // 多个声明
            int c,d = 4, e=5;
            c = 3;
        }
    }

    基本数据类型

    整数

    • byte      -128 ~ 127
    • short     -32768 ~ 32767
    • int         -2147483648 ~ 2147483647
    • long      -9223372036854775708 ~ 9223372036854775707

    如果提示该整数值过大,在后面加 L 即可。

    class demo{
        public static void main(String[] args){
            long l = 99999999999999L;
            System.out.print(l);
        }
    }

    浮点型

    • float
    • double

    浮点默认为:double,如果想转 float,在数值后加 F 。

    class demo{
        public static void main(String[] args){
            float f = 1.5F;
            System.out.print(f);
        }
    }

    布尔型

    • boolean

    字符

    ASCLL 表

    ascll值 ---> 控制字

    class demo{
        public static void main(String[] args){
            char a = 97;
            char b = 'b';
            System.out.println(a); // a
            System.out.println(b); // b
        }
    }

    控制字 ---> ascll值

    public class Demo {
        public static void main(String[] args){
            char c = 97;
            System.out.println(c); // a
    
            int i = Integer.valueOf(c);
            System.out.println(i); // 97
        }
    }

    字符串

    • String

    声明时,不能使用 ' ' 。 

    class demo{
        public static void main(String[] args){
            String str1 = "Hello World";
            String str2 = "Hello Java";
            System.out.print(str1);
            System.out.print(str2);
        }
    }

    任何类型与字符串拼接时,都会变成字符串类型。

    class demo{
        public static void main(String[] args){
            int i = 1;
            String result = "" + i;
            System.out.println(result);
        }
    }

    数据类型的方法:https://www.cnblogs.com/py-peng/p/13660571.html

    数据类型转换

    • 自动:内置帮你自动转,大的往小的转。
    • 强制:(强制类型)强制的值
    public class Demo {
        public static void main(String[] args){
            // 数据类型自动转换
            // int ---> double
            int a = 123;
            double b;
            b = a;
            System.out.println(b); // 123.0
    
            // 强制数据类型转换
            int c;
            double d = 321.2;
            c = (int)d;
            System.out.println(c); // 321
        }
    }

    算术运算符

    +    求和

    -     求差

    *     求积

    /     求商

    %   求余

    ++  递增

    --    递减

    如果 ++再前,则优先级为高,所以先运算。

    class demo{
        public static void main(String[] args){
            int a = 100, b, c;
            b = a++;
            a = 100;
            c = ++a;
            System.out.println(b); // 100 先赋值,后运算
            System.out.println(c); // 101 先运算,后赋值
        }
    }

    运算符

    赋值运算符

    =      直接赋值

    +=    求和后赋值

    -=     求差后赋值

    *=     求积后赋值

    /=     求商后赋值

    %=   求余后赋值

    关系运算符

    >      大于

    <      小于

    >=    大于等于

    <=    小于等于

    ==    等于

    !=     不等于

    逻辑运算符

    &&    与

    ||       或

    !        非

    三元运算符

    https://www.cnblogs.com/py-peng/p/12547668.html

    判断语句

    if-else

    语法:

    if (条件){
    
      逻辑1
    
    } else if (条件){
    
      逻辑2
    
    }else{
    
      逻辑3
    
    }

    实例:

    import java.util.Scanner; // 导入
    
    class Demo{
        public static void main(String[] args){
            System.out.print(">>>");
            Scanner input = new Scanner(System.in); // 创建 Scanner 对象
            int num = input.nextInt(); // 接收用户输入
            if (num >= 80){
                System.out.println("高分!");
            }else if(num >= 60){
                System.out.println("及格了!");
            }else{
                System.out.println("需继续努力!");
            }
        }
    }

    switch

    语法:

    switch (变量){
    
      case 值:
    
        逻辑1
    
        break
    
      default:
    
        逻辑2
    
        break
    
    }

    实例:

    import java.util.Scanner; // 导入
    
    class Demo{
        public static void main(String[] args){
            System.out.print(">>>");
            Scanner input = new Scanner(System.in); // 创建 Scanner 对象
            int num = input.nextInt(); // 接收用户输入
            switch (num){
                case 123:
                    System.out.println("天王盖地虎");
                    break;
                case 456:
                    System.out.println("小鸡炖蘑菇");
                    break;
                default: // 分支不存在
                    System.out.println("没有该暗号");
                    break;
            }
        }
    }

    循环语句

    while

    判断表达式是否成立,成立运行 while 语句中的代码。

    语法:

    while (进入条件){
      逻辑
    }

    实例:

    class Demo{
        public static void main(String[] args){
            int num = 1, result=0;
            while (num < 101){
                result += num;
                num++;
            }
            System.out.println(result);
        }
    }

    do-while

    选运行 do 中的代码,再去判断 while 表达式是否成立。

    语法:

    do {
    
    逻辑
    
    } while(进入条件)

    实例:

    class Demo{
        public static void main(String[] args){
            do{
                System.out.println("第一次进入,不管进入条件");
            }while (false);
        }
    }

    for

    语句:

    for (初始部分; 循环条件; 迭代部分){
    
      逻辑
    
    }

    实例:

    class Demo{
        public static void main(String[] args){
            int num = 0;
            for (int i=1; i<101; i++){
                num += i;
            }
            System.out.println(num); // 5050
        }
    }

    也可以提前定义,初始的部分。

    class Demo{
        public static void main(String[] args){
            int num = 0, i=1;
            for (; i<101; i++){
                num += i;
            }
            System.out.println(num); // 5050
        }
    }

    break与continue

    表示退出,折回。

    class Demo{
        public static void main(String[] args){
            int num = 10;
            while (true){
                num --;
                if (num == 0){
                    break; // 退出
                }else if (num != 1){
                    continue; // 折回
                }
                System.out.println("我只会执行一次!");
            }
        }
    }

    递归

    • 必须要有一个退出条件,不然死循环。
    • 递归能完成的 for 循环也可以完成。
    • 递归的效率会低,但代码简洁。

    实现阶乘

    class Demo{
        // 程序入口
        public static void main(String[] args){
            System.out.println(mul(10)); // 3628800
        }
    
        // 1 * 2 * 3 ... num
        public static int mul(int num){
            if (num == 1){
                return 1;
            } else {
                return mul(num-1) * num;
            }
    
        }
    }

    数组

    快速使用

    class Demo{
        public static void main(String [] args){
            String[] arr = new String[3]; // 该数组最大值为:3
            arr[0] = "a"; // 设值
            arr[1] = "b";
            arr[2] = "c";
            for (int i = 0; i < arr.length; i++) {
                String s = arr[i]; // 取值
                System.out.println(s);
            }
        }
    }

    创建方式

    class Demo{
        public static void main(String [] args){
            // 先声明,再分配空间
            int[] arr1;
            arr1 = new int[3];
    
            // 声明并分配空间
            int[] arr2 = new int[3];
    
            // 声明并赋值(1)
            int[] arr3 = new int[]{11,22,33};
            
            // 声明并赋值(2)
            int[] arr4 = {11,22,33}; // 不可换行
        }
    }

    默认值

    如果没给数组的传值,会使用以下默认值:

    • 整数:0
    • 小数:0.0
    • 字符:u0000
    • 布尔:false
    • 其它:null

    java基础(二)

    https://www.cnblogs.com/py-peng/p/13661587.html

  • 相关阅读:
    The "tsconfig.json" file must have compilerOptions.sourceMap set to true
    *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<WKWebViewConfiguration 0x1701bcd20> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the k
    Android Service
    关于phonegap-plugin-contentsync插件
    Ionic APP 热更新 之 产品发布状态下的热更新搭建,去local-dev-addon插件
    Ionic APP 热更新
    在懒加载的Ionic工程中使用 ionic2-auto-complete 组件:Can't bind to 'dataProvider' since it isn't a known property of 'ion-auto-complete'
    Template parse errors: The pipe 'translate' could not be found
    How to update Ionic cli and libraries
    第七章 Hyper-V 2012 R2 授权管理
  • 原文地址:https://www.cnblogs.com/py-peng/p/13653280.html
Copyright © 2011-2022 走看看