package demo211126;
import org.junit.jupiter.api.Test;
class TestA {
// 自动装箱=基本数据类型->对应包装类的引用类型
// 自动拆箱=对应包装类的引用类型->基本数据类型
// Integer中有个静态的内部类IntegerCache(整数缓存区)
// 里面有个cache[],也就是Integer常量池
// 常量池的大小范围为一个字节范围(-128,127)
// 1-当赋值在这个区间的时候,不会创建新的Integer对象
// 而是从缓存中获取经创建好的Integer对象
// 2-超出这个范围,相当于 new Integer来创建Integer对象
public static void main(String[] args) {
int i = 10;// 栈内存
int i0 = 10;
Integer in1 = 10;// 在直接赋值的时候会精选自动装箱(装包)
Integer in2 = 10;
Integer in3 = new Integer(10);
Integer in4 = new Integer(10);
Integer in5 = 199;// Integer in5 = new Integer(199);
Integer in6 = 199;// Integer in6 = new Integer(199);
System.out.println(i == i0);// true
System.out.println(i == in1);// true//自动拆箱
System.out.println(i == in3);// true
System.out.println(in1 == in2);// true//引用相同,地址一样
System.out.println(in1 == in3);// false
System.out.println(in3 == in4);// false
System.out.println(in5 == in6);// false
Byte b;
Integer i00;
Short s1;
Long l1;
// Float/Double
Character c1;// [0,127)
}
输出结果:
@Test
public void test() {
Byte a = 123;
Byte c = a;
System.out.println(c == a);// true
a++;
// byte a0 =123;
// a0++;
// a = a0;
System.out.println(c == a);// false
Byte b1 = 100;
testB(b1);
System.out.println(b1);// 100
}
private void testB(Byte b2) {
b2++;
}
输出结果:
@Test
public void f1() {// 类型转换(与字符串)
// int--->String
int a = 1;
String s1 = a + "";
String s2 = String.valueOf(a);
//将 int 变量 i 转换成字符串
String s3 = Integer.toString(a);
String s = "1 ";
// String--->int
int a1 = Integer.parseInt(s.trim());
Integer a2 = Integer.valueOf(s);
int a3 = a2.intValue();
Integer a4 = new Integer(a3);
}
}
package demo211126;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.junit.jupiter.api.Test;
public class TestB {
// 时间日期1.7/1.8
@Test
public void fDate() {
Date d1 = new Date();
System.out.println(d1);// 系统当前时间
// Fri Nov 26 09:48:55 CST 2021
System.out.println(d1.toLocaleString());// 即将过时的方法---国内日常时间格式
// 2021-11-26 9:53:53
java.sql.Date d2 = new java.sql.Date(1000000);// 标准的全文件名
long l;// 时间的毫秒数
java.util.Date d3 = new java.util.Date();
}
public void fDate(Date d1, Date d2) {
long time1 = d1.getTime();// 时间的毫秒数
long time2 = d2.getTime();
d1.after(d2);
d1.before(d2);
System.out.println(time1 > time2);
System.out.println((time1 - time2) / 1000 / 60 / 60); 秒 / 分 / 小时
}
@Test
public void fcal() {
Calendar cal = Calendar.getInstance();// Calendar日历
System.out.println(cal);
Date d1 = cal.getTime();
long ti = cal.getTimeInMillis();
cal.set(2021, 12, 26, 10, 5, 0);
System.out.println(cal.getTime().toLocaleString());
// 2022-1-26 10:05:00---会自动校准时间
int year = cal.get(Calendar.YEAR);
System.out.println(year);// 2020
System.out.println(cal.get(Calendar.MONTH));// 0
System.out.println(cal.get(Calendar.DAY_OF_MONTH));// 26
System.out.println(cal.get(Calendar.HOUR_OF_DAY));// 10
System.out.println(cal.get(Calendar.MINUTE));// 5
System.out.println(cal.get(Calendar.SECOND));// 00
System.out.println(cal.get(Calendar.MILLISECOND));// 毫秒
cal.set(Calendar.YEAR, 2020);
cal.add(Calendar.DAY_OF_MONTH, -1);
System.out.println(cal.getTime().toLocaleString());// 2020-1-25 10:05:00
}
@Test
public void f1() {
Date d1 = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datestring = sdf.format(d1);
// 将时间值格式化为时间字符串,返回格式化的时间字符串。
System.out.println(datestring);// 2021-11-26 19:41:00
try {
Date d2 = sdf.parse(datestring);
} catch (ParseException e) {
e.printStackTrace();
}
}
// 时间日期的字符串-比较
@Test
public void f2() {
String s0 = "2021-11-26 10::22:00";
String s1 = "2021-11-27 10::22:00";
String s2 = "2021-11-26 20::22:00";
System.out.println(s0.compareTo(s1));// -1
System.out.println(s0.compareTo(s2));// -1
System.out.println(s1.compareTo(s1));// 0
System.out.println("17:00:00".compareTo(s0.substring(11, 19)));// 7
//substring截取字符串
}
}
package demo211126;
import java.math.BigDecimal;
import org.junit.Test;
public class TestC {
// float double
public static void main(String[] args) {
BigDecimal b1 = new BigDecimal(1.1);
System.out.println(b1);// 1.100000000000000088817841970012523233890533447265625
BigDecimal b2 = new BigDecimal("1.1");
System.out.println(b2);// 1.1
BigDecimal b3 = new BigDecimal("0.5");
System.out.println(b3);// 0.5
System.out.println(b2.add(b3));// 1.6
System.out.println(b2.subtract(b3));// 0.6
System.out.println(b2.multiply(b3));// 0.55
System.out.println(b2.divide(b3, 4, BigDecimal.ROUND_HALF_UP));// 2.2000
// BigDecimal.ROUND_HALF_UP四舍五入
}
@Test
public void test() {
BigDecimal b2 = new BigDecimal("1.1");
int i1 = b2.intValue();
System.out.println(i1);// 1
double d1 = b2.doublevalue();
System.out.println(d1);// 1.1
b2 = b2.setScale(2);
System.out.println(b2);// 1.10
}
//实例变量与局部变量
String a1[];// 实例变量,有默认值
public void temp() {
Integer[] q;// 局部变量,没有默认值
System.out.println(a1);
// System.out.println(q);
}
}
package demo211126;
import java.util.Scanner;
import org.junit.Test;
public class TestD {
// 递归:自身方法中调用自身方法+结束条件+执行的功能
// 公有的+非静态的+无参的+默认构造方法可用
// 输入整数n,输出1到n的整数
@Test
public void test() {
TestD Num = new TestD();
Scanner sc = new Scanner(System.in);
System.out.println("输入num");
int num = Integer.valueOf(sc.nextLine());
Num.y0(num);// 方法一
System.out.println();
Num.y1(num);// 方法二
System.out.println();
}
// 方法一
public void y0(int n) {
if (n == 0)
return;
y0(n - 1);
System.out.print(n + "t");
}
// 方法二
public int y1(int n) {
if (n > 0) {
System.out.print(y1(n - 1) + 1 + "t");
}
return n;
}
// 9*9乘法表(for循环实现)
@Test
public void test99() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + (i * j) + " ");
}
System.out.println();
}
}
//9*9乘法表(递归实现)
@Test
public void ninenine() {
TestD td = new TestD();
td.test9(1);
}
public void test9(int n) {
// 输出每行多少个
m9(n, n);
// 输出多少行
System.out.println();
if (n == 9)
return;
test9(n + 1);
}
public void m9(int k, int n) {
if (k == 0)
return;
m9(k - 1, n);
System.out.print(k + "*" + n + "=" + (k * n) + " ");
}
//n的阶乘
@Test
public void testM() {
digui jc = new digui();
System.out.println(testN(5));
}
public long testN(int n) {
return n < 2 ? 1 : n * testN(n - 1);
}



