java基础知识-字符串与数组
原创
©著作权归作者所有:来自51CTO博客作者富坚老贼的原创作品,请联系作者获取转载授权,否则将追究法律责任
字符串
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.动态初始化
2.静态初始化
int[] a=new int[]{3,6,9};
简写
3.获取与写入数据
根据索引下标
//读取
int i = a[0];
//写入
a[1] = 10;
4.长度
数组的长度同一个地址不能改变
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]);
}