public class SudentTest { public static void main(String[] args) { Student s = new Student("凯",1001); Student s2 = new Student("凯2",1001); System.out.println(s.equals(s2)); } }
public class integerdemo01 { public static void main(String[] args) { Integer it1 = new Integer(10); System.out.println("it1 = " + it1); int i = it1.intValue(); System.out.println(i); Integer integer = Integer.valueOf(i); Integer it2 = new Integer("20"); System.out.println("it2 = " + it2); } }
import java.time.LocalDate; public class LocalDateExample { public static void main(String[] args) { LocalDate today = LocalDate.now(); System.out.println("Today's date: " + today); } }
import java.time.LocalTime; public class LocalTimeExample { public static void main(String[] args) { LocalTime now = LocalTime.now(); System.out.println("Current time: " + now); } }
import java.time.LocalDateTime; public class LocalDateTimeExample { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); System.out.println("Current date and time: " + now); } }
import java.util.Calendar; import java.util.TimeZone; public class CalendarExample { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(TimeZone.getDefault()); System.out.println("Current date and time using Calendar: " + calendar.getTime()); } } 5.7 包装类(Wrapper)的使用 5.7.1 单元测试方法的使用 选中当前项目工程 --> 右键:build path --> add libraries -->JUnit 4 --> 下一步; 创建一个Java 类进行单元测试。此时的Java 类要求:①此类是公共的 ②此类提供一个公共的无参构造器; 此类中声明单元测试方法。此时的单元测试方法:方法的权限是public, 没有返回值,没有形参; 此单元测试方法上需要声明注解:@Test 并在单元测试类中调用:org.testng.annotations.Test; 声明好单元测试方法以后,就可以在方法体内测试代码; 写好代码后,左键双击单元测试方法名:右键 --> run as -->JUnit Test; 说明:如果执行结果无错误,则显示是一个绿色进度条,反之,错误即为红色进度条。 import org.testng.annotations.Test; public class JUnit { int num = 10; // 第一个单元测试方法 @Test public void testEquals(){ String s1 = "MM"; String s2 = "MM"; System.out.println(s1.equals(s2)); //ClassCastException 的异常 // Object obj = new String("GG"); // Date date = (Date)obj; System.out.println(num); show(); } public void show(){ num = 20; System.out.println("show()..."); } // 第二个单元测试方法 @Test public void testToString(){ String s2 = "MM"; System.out.println(s2.toString()); } } 1 5.7.2 包装类的使用 java 提供了8 种基本数据类型对应的包装类,使得基本数据类型的变 量具有类的特征。 包装类 父类为Number 基本数据类型 Boolean Character Byte Short Integer Long Float Double byte short int long float double boolean char 注意: 其中Byte、Short、Integer、Long、Float、Double 的父类是:Number 5.7.3 包装类与基本数据类型相互转换 import org.testng.annotations.Test; public class WrapperTest { //String 类型---> 基本数据类型、包装类, 调用包装类的parseXxx() @Test public void test5() { String str1 = "123"; // String str1 = "123a"; // 错误的情况,可能会报错 // int num1 = (int)str1; // Integer in1 = (Integer)str1; int num2 = Integer.parseInt(str1); System.out.println(num2 + 1); //124 String str2 = "true"; Boolean b1 = Boolean.parseBoolean(str2); System.out.println(b1); //true } // 基本数据类型、包装类---》String 类型, 调用String 重载的valueOf(Xxx xxx) @Test public void test4() { int num1 = 10; // 方式1: 连接运算 String str1 = num1 + ""; System.out.println(str1); // 方式2: 调用String 的valueOf(Xxx xxx) float f1 = 12.3f; String str2 = String.valueOf(f1); //"12.3" Double d1 = new Double(12.4); String str3 = String.valueOf(d1); System.out.println(str2); System.out.println(str3); //"12.4" } /* * JDK 5.0 新特性: 自动装箱与自动拆箱 */ @Test public void test3() { // int num1 = 10; // // 基本数据类型 --》 包装类的对象 // method(num1); //Object obj = num1 // 自动装箱: 基本数据类型 --》 包装类 int num2 = 10; Integer in1 = num2;// 自动装箱 boolean b1 = true; Boolean b2 = b1;// 自动装箱 // 自动拆箱:包装类 --》 基本数据类型 System.out.println(in1.toString()); int num3 = in1; } public void method(Object obj) { System.out.println(obj); } // 包装类 --》 基本数据类型: 调用包装类的xxxValue() @Test public void test2() { Integer in1 = new Integer(12); int i1 = in1.intValue(); System.out.println(i1 + 1); Float f1 = new Float(12.3f); float f2 = f1.floatValue(); System.out.println(f2 + 1); } // 基本数据类型--》包装类, 调用包装类的构造器 @Test public void test1() { int num1 = 10; // System.out.println(num1.toString()); Integer in1 = new Integer(num1); System.out.println(in1.toString()); Integer in2 = new Integer("123"); System.out.println(in2.toString()); // 报异常 // Integer in3 = new Integer("123abc"); // System.out.println(in3.toString()); Float f1 = new Float(12.3f); Float f2 = new Float("12.3"); System.out.println(f1); System.out.println(f2); Boolean b1 = new Boolean(true); Boolean b2 = new Boolean("true"); Boolean b3 = new Boolean("true123"); System.out.println(b3); //false Order order = new Order(); System.out.println(order.isMale); //false System.out.println(order.isFemale); //null } } class Order { boolean isMale; Boolean isFemale; }