关于对象、自动装箱拆箱

562 查看

== 是地址是否相等
equals

javaint a = 10;
        //将20放入Integer类型
        Integer b = 20;
        double c = b.doubleValue();
        //自动装箱
        Integer d = a;
        //包装类型也可以自动转换为基本类型
        int e = d;
        Integer count = 0;
        long time1 = System.currentTimeMillis();
        for(int i = 0; i < 100000000;i++) {
            count++;
        }
        long time2 = System.currentTimeMillis();
        int count1 = 0;
        long time3 = System.currentTimeMillis();
        for(int i = 0; i < 100000000;i++) {
            count1++;
        }
        long time4 = System.currentTimeMillis();
        System.out.printf("包装类型运算时间:%d\n", time2-time1);
        System.out.printf("基本类型运算时间:%d\n", time4-time3);
        //Integer age = null;
        //int baseType = age.intValue();
        Integer num1 = 100;
        Integer num2 = 100;
        System.out.println(num1==num2);
        Integer num3 = 200;
        Integer num4 = 200;
        System.out.println(num3==num4);

#

javaimport java.util.Scanner;
import java.util.Random;
public class Demo{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        System.out.println("sssss");
        int num = random.nextInt(26);
        String a = scanner.next();
        //char a = 'a';
        System.out.println(num+a);

    }
}

获取当前时间

javaimport java.util.Date;
public class Demo{
    public static void main(String[] args){
        Date date = new Date();
        System.out.println(date.toString());
    }
}

随机字符串

javaimport java.util.Scanner;
import java.util.Random;
import java.util.Date;
public class Demo{
    public static void main(String[] args){
        char ch = 'A';
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        System.out.print("字符串长度:");
        int len = scanner.nextInt();
        String str = "";
        for (int i = 0;i<len;i++){
            int num = random.nextInt(58);
            if (num >= 26 && num < 32){
                i--;
                continue;
            }
            char result = (char)(ch+num);
            str = str + result;
    }
        System.out.println(str);
}

}