保安做网站,关联网站有那些,做货运网站找哪家好,网页游戏宣传片排行榜1、基本数据类型包装类#xff1a; 测试代码1#xff1a;
package apitest.com;
//使用Integer类的不同方法处理整数。
//将字符串转换为整数#xff08;parseInt#xff09;和Integer对象#xff08;valueOf#xff09;#xff0c;
//将整数转换回字符串#xff08;…1、基本数据类型包装类 测试代码1
package apitest.com;
//使用Integer类的不同方法处理整数。
//将字符串转换为整数parseInt和Integer对象valueOf
//将整数转换回字符串toString以及转换为十六进制toHexString、八进制toOctalString和二进制toBinaryString字符串。
//使用bitCount、highestOneBit和lowestOneBit方法处理整数的位。
//使用Integer对象的compareTo方法来比较两个Integer对象。
public class IntegerMethodsDemo { public static void main(String[] args) { // 字符串转整数 String str 123; int numFromString Integer.parseInt(str); System.out.println(parseInt: numFromString); // 字符串转Integer对象 Integer intObj Integer.valueOf(str); System.out.println(valueOf(String): intObj); // int转Integer对象 Integer intObj2 Integer.valueOf(456); System.out.println(valueOf(int): intObj2); // 整数转字符串 String numAsString Integer.toString(numFromString); System.out.println(toString: numAsString); // 整数转十六进制字符串 String hexString Integer.toHexString(numFromString); System.out.println(toHexString: hexString); // 整数转八进制字符串 String octalString Integer.toOctalString(numFromString); System.out.println(toOctalString: octalString); // 整数转二进制字符串 String binaryString Integer.toBinaryString(numFromString); System.out.println(toBinaryString: binaryString); // 位操作方法 int numBits 9; // 二进制: 1001 System.out.println(bitCount: Integer.bitCount(numBits)); System.out.println(highestOneBit: Integer.highestOneBit(numBits)); System.out.println(lowestOneBit: Integer.lowestOneBit(numBits)); // Integer对象比较 Integer int1 100; Integer int2 200; System.out.println(compareTo: int1.compareTo(int2)); // 输出: -1 }
}
运行结果如下 测试代码2
package apitest.com;
//创建一个IntAndStringConversion类包含三个静态方法intToString、stringToInt和hexStringToInt。
//将int转换为StringString转换为int将十六进制String转换为int。
public class IntAndStringConversion { public static void main(String[] args) { // int 到 String 的转换 int number 123; String strFromInt intToString(number); System.out.println(int to String: strFromInt); // String 到 int 的转换 String str 456; try { int numFromString stringToInt(str); System.out.println(String to int: numFromString); } catch (NumberFormatException e) { System.out.println(Error: The string cannot be parsed as an integer.); } // 十六进制字符串到 int 的转换 String hexStr 1C8; try { int hexNum hexStringToInt(hexStr); System.out.println(Hex String to int: hexNum); } catch (NumberFormatException e) { System.out.println(Error: The hex string cannot be parsed as an integer.); } } // int 到 String 的转换 public static String intToString(int number) { return String.valueOf(number); } // String 到 int 的转换处理异常 public static int stringToInt(String str) throws NumberFormatException { return Integer.parseInt(str); } //非数字字符的字符串转换为整数时会抛出异常。// 十六进制字符串到 int 的转换处理异常 public static int hexStringToInt(String hexStr) throws NumberFormatException { return Integer.parseInt(hexStr, 16); }
}
运行结果如下 2、字符串中的数字排序
package apitest.com;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
//字符串中数字排序是通过正则表达式或遍历字符串来提取所有的数字
//然后将这些数字存储到某种集合如ListInteger中接着对这个集合进行排序
//最后将排序后的数字重新组合成字符串。
public class SortNumbersInString {public static String sortNumbersInString(String input) {ListInteger numbers new ArrayList();StringBuilder sb new StringBuilder();for (char c : input.toCharArray()) {if (Character.isDigit(c)) {numbers.add(Integer.parseInt(String.valueOf(c)));} else {sb.append(c);}}Collections.sort(numbers, Comparator.naturalOrder());for (int num : numbers) {sb.append(num);}return sb.toString();}public static void main(String[] args) {String input 327810hello82world975java310;String sortedString sortNumbersInString(input);System.out.println(Sorted string: sortedString);}
}
运行结果如下 3、自动装箱和自动拆箱
package apitest.com;
public class AutoBoxingAndUnboxing { //自动装箱将基本数据类型自动转换为对应的包装类对象。//自动拆箱将包装类对象自动转换为对应的基本数据类型。 //性能影响自动装箱和自动拆箱可能会导致性能下降因为每次装箱和拆箱时都会创建新的对象对于装箱或执行类型转换对于拆箱。//在性能敏感的应用中应当避免不必要的装箱和拆箱操作。//空指针异常自动拆箱一个null的包装类对象时会抛出NullPointerException。需要特别注意处理可能为null的包装类对象。//缓存机制自动装箱的某些基本数据类型比如Integer、Short、Byte、Character和Boolean在-128到127之间的Integer值有特殊的缓存机制。//这个范围内的Integer对象会被缓存相同的值不会重复创建新的对象。但这并不影响自动拆箱可能导致的NullPointerException。public static void main(String[] args) { // 自动装箱 Integer integerObject 5; // int - Integer // 使用integerObject像使用Integer对象一样 System.out.println(Integer value: integerObject); // 自动拆箱 int intValue integerObject; // Integer - int // 现在intValue是一个基本数据类型int System.out.println(int value: intValue); // 自动拆箱可能导致NullPointerException Integer nullableInteger null; try { int willThrowNPE nullableInteger; // 尝试自动拆箱 } catch (NullPointerException e) { System.out.println(Caught NullPointerException: Cannot unbox null value.); } // 避免自动拆箱导致的NullPointerException int safeIntValue nullableInteger ! null ? nullableInteger : 0; System.out.println(Safe int value: safeIntValue); }
}
运行结果如下 4、Date类
测试代码1
package apitest.com;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; public class DateTimeExample { public static void main(String[] args) { // 打印当前日期 printCurrentDate(); // 打印当前日期和时间并格式化为字符串 printCurrentDateTimeFormatted(); } // 打印当前日期 public static void printCurrentDate() { LocalDate currentDate LocalDate.now(); System.out.println(当前日期: currentDate); } // 打印当前日期和时间并格式化为字符串 public static void printCurrentDateTimeFormatted() { LocalDateTime currentTime LocalDateTime.now(); DateTimeFormatter formatter DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss); String formattedDateTime currentTime.format(formatter); System.out.println(当前日期和时间格式化: formattedDateTime); }
}
测试代码2
package apitest.com;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; public class DateFormatExamples { public static void main(String[] args) { // 创建一个LocalDate实例 LocalDate date LocalDate.now(); // 创建一个LocalDateTime实例 LocalDateTime dateTime LocalDateTime.now(); // 定义并打印不同的日期格式 DateTimeFormatter formatter1 DateTimeFormatter.ofPattern(yyyy-MM-dd); System.out.println(格式1: date.format(formatter1)); // 例如2023-10-05 DateTimeFormatter formatter2 DateTimeFormatter.ofPattern(dd/MM/yyyy); System.out.println(格式2: date.format(formatter2)); // 例如05/10/2023 DateTimeFormatter formatter3 DateTimeFormatter.ofPattern(EEE, d MMM yyyy); System.out.println(格式3: date.format(formatter3)); // 例如Thu, 5 Oct 2023 // 定义并打印不同的日期时间格式 DateTimeFormatter formatter4 DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss); System.out.println(格式4: dateTime.format(formatter4)); // 例如2023-10-05 14:30:45 DateTimeFormatter formatter5 DateTimeFormatter.ofPattern(dd/MM/yyyy HH:mm a); System.out.println(格式5: dateTime.format(formatter5)); // 例如05/10/2023 02:30 PM取决于当前时间 DateTimeFormatter formatter6 DateTimeFormatter.ISO_LOCAL_DATE_TIME; System.out.println(ISO格式: dateTime.format(formatter6)); // 例如2023-10-05T14:30:45 }
}
运行结果如下 测试代码3
package apitest.com;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Locale; public class DateParseExamples { public static void main(String[] args) { String dateString1 2023-10-05; String dateString2 05/10/2023; String dateString3 Thu, 5 Oct 2023; String dateTimeString 2023-10-05 14:30:45; try { DateTimeFormatter formatter1 DateTimeFormatter.ofPattern(yyyy-MM-dd); LocalDate date1 LocalDate.parse(dateString1, formatter1); System.out.println(解析的日期1: date1); DateTimeFormatter formatter2 DateTimeFormatter.ofPattern(dd/MM/yyyy); LocalDate date2 LocalDate.parse(dateString2, formatter2); System.out.println(解析的日期2: date2); // 使用 DateTimeFormatter 解析包含星期几的日期字符串 DateTimeFormatter formatter3 DateTimeFormatter.ofPattern(EEE, d MMM yyyy, Locale.ENGLISH); LocalDate date3 LocalDate.parse(dateString3, formatter3); System.out.println(解析的日期3: date3); DateTimeFormatter formatter4 DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss); LocalDateTime dateTime LocalDateTime.parse(dateTimeString, formatter4); System.out.println(解析的日期时间: dateTime); } catch (DateTimeParseException e) { System.err.println(日期时间解析错误: e.getMessage()); } }
}
运行结果如下 5、 Calendar类
package apitest.com;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class CalendarExample { public static void main(String[] args) { // 获取默认时区和语言环境的Calendar实例 Calendar calendar Calendar.getInstance(); // 显示当前日期和时间 System.out.println(当前日期和时间: calendar.getTime()); // 获取并打印年份、月份注意月份是从0开始的即0代表1月、日期 int year calendar.get(Calendar.YEAR); int month calendar.get(Calendar.MONTH) 1; // 月份加1以便按常规方式显示 int day calendar.get(Calendar.DAY_OF_MONTH); System.out.println(年: year , 月: month , 日: day); // 设置日期为2023年10月1日 calendar.set(Calendar.YEAR, 2023); calendar.set(Calendar.MONTH, Calendar.OCTOBER); // 直接使用Calendar的常量 calendar.set(Calendar.DAY_OF_MONTH, 1); System.out.println(设置后的日期: calendar.getTime()); // 给日期加上10天 calendar.add(Calendar.DAY_OF_MONTH, 10); System.out.println(加10天后的日期: calendar.getTime()); // 使用GregorianCalendar创建特定日期 GregorianCalendar gregorianCalendar new GregorianCalendar(2022, Calendar.DECEMBER, 25); System.out.println(特定的日期: gregorianCalendar.getTime()); }
}
运行结果如下