- Java中没unsigned关键字
- int Math.round(float a) long Math.round(double a)
- &&、||:短路操作 &、|:非短路操作
- 枚举类型必须放在放在函数之外定义
- Java String中的字符不能修改修改,String类对象为不可变字符串
-
String greeting = "hello"; System.out.println(greeting == "hello");//true System.out.println(greeting.substring(0,4) == "hell");//false System.out.println(greeting.substring(0,5) == "hello");//true System.out.println("he" + "l" == "hel");//true
substring源码:
public String substring(int beginIndex, int endIndex) { if (beginIndex < 0) { throw new StringIndexOutOfBoundsException(beginIndex); } if (endIndex > value.length) { throw new StringIndexOutOfBoundsException(endIndex); } int subLen = endIndex - beginIndex; if (subLen < 0) { throw new StringIndexOutOfBoundsException(subLen); } return ((beginIndex == 0) && (endIndex == value.length)) ? this : new String(value, beginIndex, subLen); }
在源码中,可以看出,如果需要返回的子串不是原字符串本身,就得返回一个新对象(此时该对象通过new关键字创建,存在于堆之上);而字面量"hell"存在于栈之上,两者的地址不同;通过"+"得到的新对象也是存在于栈之上
- Java的八大基本数据类型:byte、short、int、long、float、double、char、boolean
- 对于Java八大基本数据类型来说,无论其变量值是通过字面量直接赋值而得还是通过关键字new而得,该对象都是存放于栈之上
char c = 'h';
char c1 = new Character('h');
System.out.println(c == c1);//true
- StringBuilder在单线程中比StringBuffer更具优势
- String、StringBuffer、StringBuilder内部都是使用字符数组保存变量的值
- BigInteger与BigDecimal之间无法进行运算
-
int[] s = {1,23,4};//相当于int[] s = new int[]{1,23,4} int[] a = {1,23,4};//相当于int[] a = new int[]{1,23,4} System.out.println(s == a);//false
- 栈中一般存放八大基本数据类型的对象(字面量)、通过字面量赋值的字符串以及引用变量,堆中一般存放通过关键字new而得到的非八大基本数据类型的对象
- Arrays.copyOf(original, newLength) //数组复制函数。第一个参数为原数组,第二个参数为新数组的长度 。在该函数的最内部实现中,会创建一个新的数组copy,用于保存新数组的元素,再返回copy,使得新数组与copy指向堆中的同一对象。若newLength大于original.length,则在新数组中,索引在original.length及之后的数组元素值为0或false
public static <T> T[] copyOf(T[] original, int newLength) {
return (T[]) copyOf(original, newLength, original.getClass());
}
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
- Java中提供了直接使用{}创建一维数组和二维数组的方法
int[][] aa = {{1,23,4},{4,5,6}};
int[] a1 = {1,2,3};
- Arrays.deepToString()
int[][] aa = {{1,23,4},{4,5,6}};
System.out.println(Arrays.deepToString(aa));//[[1, 23, 4], [4, 5, 6]]
- Java创建不规则数组
int[][] a = new int[5][];//此时a[i] = null,i=0,1,2,3,4
for (int i = 0; i < a.length; i++) {
a[i] = new int[i + 1];//此时,a[i]中的每个元素都是0
for(int j = 0;j <= i;j++) {
a[i][j] = j;
}
}
System.out.println(Arrays.deepToString(a));