java基础知识-字符串与数组
字符串
String s和 new String 的区别
String s1="hello";
String s2= new String("hello");
String s1 = "hello"; 先在内存中找是不是有"hello" 这个对象,
如果有,就让s1指向那个"hello".如果内存里没有"hello",就创建一个新的对象保存"hello".
String s2=new String ("hello") 就是不管内存里是不是已经有"hello"这个对象,
都新建一个对象保存"hello".
数组
1.动态初始化
int[] a=new int[30];
2.静态初始化
int[] a=new int[]{3,6,9};
简写
int[] a={3,6,9};
3.获取与写入数据
根据索引下标
//读取 int i = a[0];
//写入 a[1] = 10;
4.长度
//计算长度 int i = a.length;
数组的长度同一个地址不能改变
int[] a = {3, 6, 9}; //这会使a指向另一个起始地址 a= new int[]{1, 3};
5.遍历
for (int i = 0; i < a.length; i++) { System.out.println(a[i]); }