面试题:
自动装箱:把基本数据类型会自动的变成其对应的包装类
自动拆箱:把包装类自动的变成其对象的基本数据类型
package Integer;public class Demo {public static void main(String[] args) {Integer i = 10;String binaryString = Integer.toBinaryString(i);String hexString = Integer.toHexString(i);String octalString = Integer.toOctalString(i);System.out.println("二进制:" + binaryString + "\n十六进制:" + hexString + "\n八进制:" + octalString);//4.将字符串类型的整数转成int类型的整数//强类型语言:每种数据在java中都有各自的数据类型//在计算的时候,如果不是同一种数据类型,是无法直接计算的。//字符串已经转换成整数类型int i1 = Integer.parseInt(binaryString);System.out.println(i1);//验证是否转换成功System.out.println(i1 + 1);//细节1://在类型转换的时候,括号中的参数只能是数字不能是其他,否则代码会报错//细节2://8种包装类当中,除了character都有对应的parsexxx的方法,进行类型转换String str ="true";boolean b=Boolean.parseBoolean(str);System.out.println(b);}
}
package Integer;import java.util.Scanner;public class Demo1 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入:");String s = sc.nextLine();//进行类型转换int i = Integer.parseInt(s);System.out.println(i);//验证System.out.println(i+1);}
}
package execrise;import javax.sound.sampled.SourceDataLine;
import java.util.ArrayList;
import java.util.Scanner;public class Demo2 {public static void main(String[] args) {ArrayList<Integer> list = new ArrayList<>();Scanner sc = new Scanner(System.in);int sum = 0;while (sum <= 200) {System.out.println("请输入整数:");String num1 = sc.nextLine();int num = Integer.parseInt(num1);if (num >= 1 && num <= 100) {list.add(num);sum += num;} else {System.out.print("输入有误,");}}System.out.print("集合中的数据为:");for (int i = 0; i < list.size(); i++) {System.out.print(list.get(i) + " ");}}
}
方法1:
package execrise;import java.util.Scanner;public class Demo3 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);String s;while (true) {System.out.println("请输入字符串");s = scanner.nextLine();if (judement(s)) {break;} else {System.out.print("输入错误,");}}char[] array = s.toCharArray();int number = 0;for (int i = 0; i < array.length; i++) {int i1 = array[i] - '0';number = number * 10 + i1;}System.out.println("字符串转换后的数字为:" + number);//验证System.out.println(number + 1);}public static boolean judement(String string) {char[] array = string.toCharArray();for (int i = 0; i < array.length; i++) {if (array[i] < '0' || array[i] > '9') {return false;}}if (string.length() < 1 || string.length() > 10) {return false;}if (string.startsWith("0")) {return false;}return true;}
}
方法2:
package execrise;import java.util.Scanner;public class Demo4 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("请输入十进制整数:");String s = scanner.nextLine();int number = Integer.parseInt(s);toBinary(number);}private static void toBinary(int number) {String s = "";while (number != 0) {int yu = number % 2;number = number / 2;s = yu + s;}System.out.println("十进制对应二进制为:" + s);}
}
JDK7:
package execrise;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;public class Demo5 {public static void main(String[] args) throws ParseException {Date date = new Date();//System.out.println(System.currentTimeMillis());String s = "2000-1-1";SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");Date parse = simpleDateFormat.parse(s);long l = date.getTime() - parse.getTime();System.out.println(l / 1000 / 60 / 60 / 24);}
}
JDK8
package execrise;import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;public class Demo6 {public static void main(String[] args) {LocalDateTime now = LocalDateTime.now();LocalDateTime localDateTime = LocalDateTime.of(1999, 2, 12, 4, 0, 0);System.out.println(ChronoUnit.DAYS.between(localDateTime, now));}
}
JDK7:
方法1:
package execrise;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;public class Demo7 {public static void main(String[] args) throws ParseException {String string1 = "2024-1-1";String string2 = "2025-1-1";SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");Date parse1 = sdf.parse(string1);Date parse2 = sdf.parse(string2);long l = parse2.getTime() - parse1.getTime();if (l / (1000 * 60 * 60 * 24) == 365) {System.out.println("不是闰年");} else {System.out.println("是闰年");}}
}
方法2:
JDK8:
方法1:
package execrise;import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;public class Demo8 {public static void main(String[] args) {LocalDateTime localDateTime = LocalDateTime.of(2025, 1, 1, 0, 0);LocalDateTime localDateTime1 = LocalDateTime.of(2024, 1, 1, 0, 0);if (ChronoUnit.DAYS.between(localDateTime1, localDateTime) == 365) {System.out.println("不是闰年");} else {System.out.println("是闰年");}}
}
方法2: