/** * 1字节: byte , boolean * 2字节: short , char * 4字节: int , float * 8字节: long , double * 注:1字节(byte)=8位(bits) * 1.编码与中文: * Unicode/GBK: 中文2字节 * UTF-8: 中文通常3字节,在拓展B区之后的是4字节 * 综上,中文字符在编码中占用的字节数一般是2-4个字节。 */ public static void testString() { System.out.println("Integer.MAX_VALUE: " + Integer.MAX_VALUE); char c = (char) Integer.MAX_VALUE; System.out.println("char.MAX_VALUE: " + (int) c); try { System.out.println("utf-8: " + "中".getBytes("utf-8").length); System.out.println("gbk: " + "中".getBytes("gbk").length); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } /** * 结果: Integer.MAX_VALUE: 2147483647 char.MAX_VALUE: 65535 utf-8: 3 gbk: 2 */
String:
public final class String implements java.io.Serializable, Comparable<String>, CharSequence { /** The value is used for character storage. */ private final char value[]; /** Cache the hash code for the string */ private int hash; // Default to 0 /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = -6849794470754667710L; ... }
关于字符串最大多长,字符串是一个char[],数组的最大长度int的值-2^31--2^31-1,所以最大长度是2147483647,又由于一个长度占两个字节,最大长度时一个字符串占用4G内存,所以在内存充足的情况下可以达到2^31-1的长度,内存较小的时候是会溢出的。
StringBuffer:
public final class StringBuffer extends AbstractStringBuilder implements java.io.Serializable, CharSequence { @Override public synchronized StringBuffer append(Object obj) { toStringCache = null; super.append(String.valueOf(obj)); return this; } }
StringBuilder:
public final class StringBuilder extends AbstractStringBuilder implements java.io.Serializable, CharSequence { @Override public StringBuilder append(Object obj) { return append(String.valueOf(obj)); } }
BigDecimal:(金融行业保存数据)
public BigDecimal add(BigDecimal augend) public BigDecimal subtract(BigDecimal subtrahend) public BigDecimal multiply(BigDecimal multiplicand) public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode)
Arrays:(package java.util)
public static void sort(int[] a) public static <T> void parallelSort(T[] a, Comparator<? super T> cmp) public static int binarySearch(long[] a, long key)
Comparable:(package java.lang)
public interface Comparable<T> { ... public int compareTo(T o); }
Comparator:(package java.util)
@FunctionalInterface public interface Comparator<T> { ..... int compare(T o1, T o2); }