StringBuilder sb = new StringBuilder(1024); for (int i = 0; i < 1000; i++) { sb.append(','); sb.append(i); } String s = sb.toString(); System.out.println(s);
// 输出 // 0,1,2,3...999
此外,StringBuilder还可以进行链式操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
publicclassMain{ publicstaticvoidmain(String[] args){ var sb = new StringBuilder(1024); sb.append("Mr") .append("Bob") .append("!") .insert(0, "Hello, "); System.out.println(sb.toString()); } }
// 输出 // Hello, Mr Bob!
支持链式操作的关键可以从StringBuilder源码看出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
public StringBuilder append(String str){ super.append(str); returnthis; } public AbstractStringBuilder append(String str){ if (str == null) { return appendNull(); } int len = str.length(); ensureCapacityInternal(count + len); putStringAt(count, str); count += len; returnthis; }
//向上转型为Number Number num = new Integer(100); // 获取byte, int, long, float, double; byte b = num.byteValue(); int i = num.intValue(); long l = num.longValue(); float f = num.floatValue(); double d = num.doubleValue();
无符号整型
1 2 3 4 5 6 7 8 9
publicclassMain{ publicstaticvoidmain(){ byte x = -1; byte x = 127; System.out.println(Byte.toUnsignedInt(x)); // 255 System.out.println(Byte.toUnsignedInt(x)); // 127 } }
JavaBean
定义
JavaBean是一种特殊但较为常见的class,它符合下列要求:
有若干个private实例字段;
通过public方法来读写实例字段;
且读写方法符合如下命名规范:
1 2 3 4 5
//读方法 public Type getXyz() //写方法 publicvoid Type setXyz()
// 输出 /** age public int Person.getAge() public void Person.setAge(int) class public final native java.lang.Class java.lang.Object.getClass() null name public java.lang.String Person.getName() public void Person.setName(java.lang.String) **/
枚举类
直接上🌰:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
publicclassMain{ publicstaticvoidmain(){ Weekday day = Weekday.SUN; if (day == Weekday.SUN || day == Weekday.SAT) { System.out.println("Work at home"); } else { System.out.println("Work an office!"); } } }
publicclassMain{ publicstaticvoidmain(String[] args){ Weekday day = Weekday.SUN; if (day.dayValue == 6 || day.dayValue == 7){ System.out.println("Today is " + day + ". Work at home!"); } else { System.out.println("Today is " + day + ". Work at office!"); } } }
publicclassMain{ publicstaticvoidmain(String[] args){ Weekday day = Weekday.SUN; switch(day) { case MON: case TUE: case WED: case THU: case FRI: System.out.println("Today is " + day + ". Work at office!"); break; case SAT: case SUN: System.out.println("Today is " + day + ". Work at home!"); break; default: thrownew RuntimeException("cannot process " + day); } } }
enumWeekday{ MON, TUE, WED, THU, FRI, SAT, SUN; }
BigInteger
BigDecimal
定义
1 2 3
BigDecimal bd = new BigDecimal("123.4567"); System.out.println(bd.mutiply(bd)); // 15241.55677489
获取小数位数
1 2 3 4 5 6 7
BigDecimal d1 = new BigDecimal("123.45"); BigDecimal d2 = new BigDecimal("123.4500"); BigDecimal d3 = new BigDecimal("1234500"); System.out.println(d1.scale()); // 2,两位小数 System.out.println(d2.scale()); // 4 System.out.println(d3.scale()); // 0
Random r = new Random(); r.nextInt(); // 2071575453,每次都不一样 r.nextInt(10); // 5,生成一个[0,10)之间的int r.nextLong(); // 8811649292570369305,每次都不一样 r.nextFloat(); // 0.54335...生成一个[0,1)之间的float r.nextDouble(); // 0.3716...生成一个[0,1)之间的double
如果我们在创建Random实例时指定一个种子,就会得到完全确定的随机数序列:
1 2 3 4 5 6 7 8 9 10
import java.util.Random; publicclassMain{ publicstaticvoidmain(String[] args){ Random r = new Random(12345); for (int i = 0; i < 10; i++) { System.out.println(r.nextInt(100)); } } }