String s1 = "abc"; s1 += "d"; System.out.println(s1); // "abcd" // 内存中有"abc","abcd"两个对象,s1从指向"abc",改变指向,指向了"abcd"。
String s1 = "abc"; String s2 = "abc"; // 内存中只有一个"abc"对象被创建,同时被s1和s2共享。
如何证明s1和s2的内存地址是一样的?
用Hashcode来测试似乎是不可行的,以下是hashcode的简单解释。
1) 在散列集合中只实现插入对象通过hashcode值不能确定对象对象是否存在,hashcode值相同,还需判断对象equals确定对象是否存在。
2.)在散列集合存储无重复对象需重写hashcode和equals方法,hashcode方法是为了检索对象范围。equals才是判断对象是否相等的方法。所以相同对象hashcode一定相同,相同hashcode的对象不一定相等。
3) 存储在散列集合中的对象被改变后,在集合中被忽略无法remove,内存无法释放回收,大量积累会造成内存溢出
原文链接: https://blog.csdn.net/lin1094201572/article/details/81282596.
//例如: String str = "abc"; //相当于: char data[] = {'a', 'b', 'c'}; String str = new String(data); // String底层是靠字符数组实现的。
public static void main(String[] args) { String str = "abc"; char data[] = {'a', 'b', 'c'}; String str1 = new String(data); System.out.println(str.equals(str1));//true }
public static void main(String[] args) { // 无参构造 String str = new String(); // 通过字符数组构造 char chars[] = {'a', 'b', 'c'}; String str2 = new String(chars); System.out.println(str2);//abc // 通过字节数组构造 这里的数字表示的是ASCII码表中的值 byte bytes[] = { 97, 98, 99 }; String str3 = new String(bytes); System.out.println(str3);//abc }
public static void main(String[] args) { // 创建字符串对象 String s1 = "hello"; String s2 = "hello"; String s3 = "HELLO"; // boolean equals(Object obj):比较字符串的内容是否相同 System.out.println(s1.equals(s2)); // true System.out.println(s1.equals(s3)); // false System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); //boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写 System.out.println(s1.equalsIgnoreCase(s2)); // true System.out.println(s1.equalsIgnoreCase(s3)); // true System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); }
public static void main(String[] args) { //创建字符串对象 String s = "helloworld"; // int length():获取字符串的长度,其实也就是字符个数 System.out.println(s.length());//10 System.out.println("‐‐‐‐‐‐‐‐"); // String concat (String str):将将指定的字符串连接到该字符串的末尾. String s2 = s.concat("**hello itheima"); System.out.println(s2);// helloworld**hello itheima // char charAt(int index):获取指定索引处的字符 System.out.println(s.charAt(0));//h System.out.println(s.charAt(1));//e System.out.println("‐‐‐‐‐‐‐‐"); // int indexOf(String str):获取str在字符串对象中第一次出现的索引,没有返回‐1 System.out.println(s.indexOf("l"));//2 System.out.println(s.indexOf("owo"));//4 System.out.println(s.indexOf("ak"));//-1 System.out.println("‐‐‐‐‐‐‐‐"); // String substring(int start):从start开始截取字符串到字符串结尾 System.out.println(s.substring(0));//helloworld System.out.println(s.substring(5));//world System.out.println("‐‐‐‐‐‐‐‐"); // String substring(int start,int end):从start到end截取字符串。含start,不含end。 System.out.println(s.substring(0, s.length()));//helloworld System.out.println(s.substring(3,8));//loworl }
public static void main(String[] args) { //创建字符串对象 String s = "abcde"; // char[] toCharArray():把字符串转换为字符数组 char[] chs = s.toCharArray(); for(int x = 0; x < chs.length; x++) { System.out.print(chs[x] + " ");//a b c d e } // byte[] getBytes ():把字符串转换为字节数组 byte[] bytes = s.getBytes(); for(int x = 0; x < bytes.length; x++) { System.out.print(bytes[x] + " ");//97 98 99 100 101 } // 替换字母it为大写IT String str = "itcast itheima"; String replace = str.replace("it", "IT"); System.out.println(replace); // ITcast ITheima }
public static void main(String[] args) { //创建字符串对象 String s = "aa bb cc"; String[] strArray = s.split(" ");//按空格进行切片 System.out.println(Arrays.toString(strArray));//[aa, bb, cc] for(int x = 0; x < strArray.length; x++) { System.out.print(strArray[x] + " "); // aa bb cc } }
static 数据类型 变量名;
static int numberID;
修饰符 static 返回值类型 方法名 (参数列表){ // 执行语句 }
public class String_8_static { //静态的成员变量 private static int number = 5; //成员变量 private int num = 10; public static void main(String[] args) { //使用静态的成员变量 System.out.println(number);//5 //可以直接调用本类中的静态方法 System.out.println(add(1,2));//8 //调用非静态方法 System.out.println(new String_8_static().add1(1,2));//3 //调用成员变量 System.out.println(new String_8_static().num);//10 } //静态方法 public static int add(int number1,int number2){ // 不可以在静态方法中使用非静态的成员变量 //java: 无法从静态上下文中引用非静态 变量 num /* System.out.println(num);*/ // 可以在静态方法中调用静态方法 int sub = sub(1, 2); System.out.println(sub); return sub; } //静态方法 public static int sub(int number1,int number2){ // 可以在静态方法中使用静态的成员变量 return number1 + number2 + number; } //成员方法 public int add1(int a,int b){ return a + b; } }
public class ClassName{ //静态代码块可以有多个按照从上至下的顺序依次执行 static { // 执行语句 } static { // 执行语句 } }
public static void main(String[] args) { // 定义int 数组 int[] arr = {2,34,35,4,657,8,69,9}; // 打印数组,输出地址值 System.out.println(arr); // [I@2ac1fdc4 // 数组内容转为字符串 String s = Arrays.toString(arr); // 打印字符串,输出内容 System.out.println(s); // [2, 34, 35, 4, 657, 8, 69, 9] }
public static void main(String[] args) { // 定义int 数组 int[] arr = {24, 7, 5, 48, 4, 46, 35, 11, 6, 2}; System.out.println("排序前:"+ Arrays.toString(arr)); // 排序前:[24, 7, 5, 48, 4, 46, 35, 11, 6,2] // 升序排序 Arrays.sort(arr); System.out.println("排序后:"+ Arrays.toString(arr));// 排序后:[2, 4, 5, 6, 7, 11, 24, 35, 46,48] }
double d1 = Math.abs(‐5); //d1的值为5 double d2 = Math.abs(5); //d2的值为5
double d1 = Math.ceil(3.3); //d1的值为 4.0 double d2 = Math.ceil(‐3.3); //d2的值为 ‐3.0 double d3 = Math.ceil(5.1); //d3的值为 6.0
double d1 = Math.floor(3.3); //d1的值为3.0 double d2 = Math.floor(‐3.3); //d2的值为‐4.0 double d3 = Math.floor(5.1); //d3的值为 5.0
long d1 = Math.round(5.5); //d1的值为6.0 long d2 = Math.round(5.4); //d2的值为5.0