Java常见类-学习小结

8次阅读

共计 2775 个字符,预计需要花费 7 分钟才能阅读完成。

常见类

包装类

主动装箱(int->Integer): Integer.valueOf(i)

主动拆箱(Integer->int): integer.intValue()

当包装类类和根本类型比拟的时候,会将包装类主动拆箱,所以比拟后果总是 True。

Integer 中有一个缓存,在主动装箱的时候 Integer i = 1,调用 valueOf 办法将根本数据类型转化为 Integer 对象,如果这个数字i >= IntegerCache.low && i <= IntegerCache.high 个别是 -128~127,则会从缓存的 cache 数组中取出对象而不创立新的对象,否则调用new Integer()

// Integer 源码
public static Integer valueOf(int i) {if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}

如果新建了 Integer 对象,那么雷同数值对应的 Integer 对象也会不同。即i1 == i2 返回 False,否则返回 True,因为都是取了 cache 中的对象,没有新建,提高效率(不设置更大范畴,优化内存,只设置罕用的)。

Double 中就没有 cache 对象数组。

String

String str = "abc"字符串放在常量池,在 1.7 以前在办法区,1.7 之后在堆空间。和 String str = new String("abc") 的地址不同,后者是指向对象。

String 为 finally 对象,不能被继承。

String 为不可变对象,指的是 String 类内的 private final char[] value 数组是惟一对应的,value 不能扭转援用的数组,然而 value 利用数组的值能够通过反射进行拜访和批改。

equals()

对象地址雷同返回 True,否则取每个字符串的 value 赋值给字符串数组,而后逐位字符比拟。

charAt(i)

间接返回value[i]

intern()

如果常量池外面蕴含 str 中的字符串值(用 equals 判断),则间接返回这个常量地址。

String a = "abc";
String b = new String("abc");
print(a == b); // False 一个指向常量池,一个指向对象
print(a == b.intern()); // True intern()找到常量池中 abc 的地址

StringBuffer & StringBuilder

StringBuilder: 效率高

StringBuffer: 线程平安(synchronized)

日期格式化

Date date = new Date();
// pattern 中,yyyy 代表年保留四位数字,其余同理,如果让月日等信息保留两位数的格局就要写两个
// 字母代表的意思能够到官网查 api
DateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(dataFormat.format(date));

在定义好 dataFormat 对象当前,它曾经晓得你的日期格局,就能够应用 date = dataFormat.parse("2020-1-1 10:19:21"); 来失去 date 对象,与 format 作用相同。

Calendar 用法:

// 用 dateFormat 结构出 date 对象
DateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dataFormat.parse("2020-1-1 10:19:21");
// Calendar 用法:不能间接 new,因为其构造方法为 protected,要应用 getInstance
Calendar calendar = Calendar.getInstance();
calendar.setTime(date); // 将 date 对象传入,否则展现的是以后工夫
// get()的参数为 int,在 Calendar 中有对应各个值对应的常量序号
System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.MONTH));
System.out.println(calendar.get(Calendar.DATE));

枚举类

须要应用一组常量时,倡议应用枚举类。

用法示例

public class EnumTest
{public static void main(String[] args)
   {System.out.println("间接用常量名拜访:" + Size.SMALL);
      Scanner in = new Scanner(System.in);
      System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE)");
      String input = in.next().toUpperCase(); // 疏忽大小写(全副转大写)Size size = Enum.valueOf(Size.class, input); // 依据输出的值,通过反射获取枚举类型中的常量
      System.out.println("size=" + size);
      System.out.println("abbreviation=" + size.getAbbreviation());
      if (size == Size.EXTRA_LARGE) // 比拟时,用 ==,而不须要用 equals
         System.out.println("Good job--you paid attention to the _.");      
   }
}

enum Size
{SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");

   private Size(String abbreviation) {this.abbreviation = abbreviation;} // 只能由编译器调用
   public String getAbbreviation() { return abbreviation;}

   private String abbreviation;
}
Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) SMALL
间接用常量名拜访:SMALL
size=SMALL
abbreviation=S

如果写了 abbreviation 参数,如 SMALL(“S”)这种模式,就肯定要定义构造方法 private Size(String abbreviation),否则会报错。

正文完
 0