字符串对象存储的位置因为name最初指向的是传智,后面只是调用name继续对字符进行拼接,但是字符串的指向是没有改变的。
创建字符串的两种常用的方法 两种创建方法的区别 底层原理这种通过""创建的对象在字符串常量池中存储,而且相同内容只会在其中存储一份。
package String_try; public class stringdemo { public static void main(String[] args) { // 方法1 String name = "我爱你中国"; System.out.println(name); // public String(char [] c);根据传入的字符串内容,来创建字符串对象 String s2 = new String("我是中国人"); System.out.println(s2); // public string(byte[] b) 根据字节数组内容,来创建字符串对象 char[] chars = {'a','b','中','国'}; String s3 = new String(chars); System.out.println(s3); // public String(byte[] b) 根据字节数组的内容,来创建对象 byte[] bytes = {97,98,99,65,66,67}; String s4 = new String(bytes); System.out.println(s4); // abcABC // 判断两个字符串类型是否相等 // 以“”方式给出的字符串对象,在字符串常量池中存储,而且相同内容只会在其中存储一份。 String ss1 = "abc"; String ss2 = "abc"; System.out.println(ss1 == ss2); // true // 通过构造器new对象,每new一次都会产生一个新对象,放在堆内存中。 char[] chars1 = {'a','b','c'}; String ss3 = new String(chars1); String ss4 = new String(chars1); System.out.println(ss3 == ss4); // false } }字符串可能遇到的面试题
package String_try; public class stringdemo2 { public static void main(String[] args) { // 实际上该代码创建了2个对象 System.out.println("------------------"); String s2 = new String("abc"); String s1 = "abc"; System.out.println(s1 == s2); // false // System.out.println("---------------------"); String s3 = "abc"; String s4 = "ab"; String s5 = s4 + "c"; System.out.println(s3 == s5); // false System.out.println("---------------------"); // Java存在编译优化机制,程序在编译时:“a”+ “b”+ “c”会直接转成"abc" String s6 = "abc"; String s7 = "a" + "b" + "c"; System.out.println(s6 == s7); //true } }字符串内容的比较
package String_try; import java.util.Scanner; public class stringdemo3 { public static void main(String[] args) { String loginname = "peihj"; String password = "123456"; Scanner sc = new Scanner(System.in); for (int i = 0; i < 3; i++) { System.out.println("请输入账号:"); String inputname = sc.next(); System.out.println("请输入密码:"); String inputpassword = sc.next(); if (inputname.equals(loginname)){ if(inputpassword.equals(password)){ System.out.println("登录成功请随意浏览----"); break; }else { System.out.println("密码错误"+";剩余的次数为:"+(2-i)); } }else { System.out.println("账号错误,请重新输入"+";剩余的次数为:"+(2-i)); } } } }String常用的API *** 作演示
package String_try; public class stringdemo4 { public static void main(String[] args) { // public int length() String name = "我爱你中国abcd"; System.out.println(name.length()); // 取长度 // public char charAt(int index) char c = name.charAt(0); System.out.println(c); System.out.println("--------------------------遍历字符串-----------------------"); for (int i = 0; i < name.length(); i++) { char c1 = name.charAt(i); System.out.println(c1); } System.out.println("--------------------将字符串中的每个字符转化成数组-----------------------"); char[] chars = name.toCharArray(); for (int i = 0; i < chars.length; i++) { char ch = chars[i]; System.out.println(ch); } System.out.println("-------------------截取字符串内容----------------"); // public string substring(int beginIndex,int endindex),截取内容(包前不包后) String name2 = "java是最好的编程语言"; String name3 = name2.substring(0,9); System.out.println(name3); // public string substring(int beginIndex,int endindex),截取内容(包前不包后) String rs = name2.substring(4); System.out.println(rs); System.out.println("------------------字符串替换--------------"); // public string replace(target,replacement) String names = "小明是最厉害的年轻人。"; String rs3 = names.replace("小明","**"); System.out.println(rs3); // public boolean contains(charsequence s) System.out.println(names.contains("小明")); // true System.out.println(names.contains("小红")); // false // public string[] split(string s) 按照特定的内容把字符串分割成字符串数组返回 System.out.println("----------------------------------"); String name4 = "小王,小红,小蓝"; String[] name5 = name4.split(","); for (int i = 0; i < name5.length; i++) { System.out.println("您选择了:"+name5[i]); } } }结果演示 案例1:开发验证码 源码
package String_try; import java.util.Scanner; import java.util.Random; public class checkcode { public static void main(String[] args) { String str1 = "asdfghjklzxcvbnmqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM0123456789"; Random r = new Random(); String code = ""; for (int i = 0; i < 5; i++) { int index = r.nextInt(str1.length()); // System.out.println(index); char N = str1.charAt(index); code += N; } System.out.println("验证码为:"+code); } }结果 案例2:模拟用户登录功能 代码演示
package String_try; import java.util.Scanner; public class stringdemo3 { public static void main(String[] args) { String loginname = "peihj"; String password = "123456"; Scanner sc = new Scanner(System.in); for (int i = 0; i < 3; i++) { System.out.println("请输入账号:"); String inputname = sc.next(); System.out.println("请输入密码:"); String inputpassword = sc.next(); if (inputname.equals(loginname)){ if(inputpassword.equals(password)){ System.out.println("登录成功请随意浏览----"); break; }else { System.out.println("密码错误"+";剩余的次数为:"+(2-i)); } }else { System.out.println("账号错误,请重新输入"+";剩余的次数为:"+(2-i)); } } } }结果演示 手机号屏蔽功能 代码演示
package String_try; import java.util.Scanner; public class replacephone { public static void main(String[] args) { System.out.println("请输入手机号码:"); Scanner sc = new Scanner(System.in); String tel = sc.next(); String before = tel.substring(0,3); String after = tel.substring(7); System.out.println(before + "****" + after); } }结果演示 参考
黑马程序员
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)