一、 练习课
1、比较字符:
- ==: 比较的是两个字符串的地址
- equals(): 比较的是两个字符串的内容
- 示例
1 package 第七天; 2 3 public class 练习 { 4 public static void main(String[] args) { 5 String str1 = "abc"; 6 String str2 = "abc"; 7 8 System.out.println(str1.equals(str2)); 9 System.out.println(str1 == str2); 10 11 String str3 = new String("abc"); 12 System.out.println(str1.equals(str3)); 13 System.out.println(str1 == str3); 14 } 15 }
- 经验:字符调用equals方法进行比较时,将字符串常量写在前面。
1 package 第七天; 2 3 public class 练习 { 4 public static void main(String[] args) { 5 String str = null; 6 boolean equals = str.equals("AA"); //java.lang.NullPointerException 7 System.out.println(equals); 8 } 9 }
2、案例:
用户登录:如果用户输入的用户名和密码分别为zhangsan、123456;则登录成功,否则,登录失败。
1 package 第七天; 2 3 import java.util.Scanner; 4 5 public class 用户登录 { 6 public static void main(String[] args) { 7 Scanner scanner = new Scanner(System.in); 8 System.out.println("Input your name: "); 9 String name = scanner.next(); 10 System.out.println("Input your pastword: "); 11 String password = scanner.next(); 12 if("zhangsan".equals(name)&&"123456".equals(password)) { 13 System.out.println("success"); 14 }else { 15 System.err.println("failes"); 16 } 17 } 18 19 }
二、 知识点
1 package 第七天; 2 3 import 第七天.User; 4 5 public class User{ 6 7 8 private int id; 9 private String name; 10 private boolean gender; 11 private byte age; 12 13 public User setId(int id) { 14 this.id = id; 15 return this; 16 } 17 public User setName(String name) { 18 this.name = name; 19 return this; 20 } 21 public User setAge(byte age) { 22 this.age = age; 23 return this; 24 } 25 public User setGender(boolean gender) { 26 this.gender = gender; 27 return this; 28 } 29 30 public static void main(String[] args) { 31 User user = new User(); 32 user.setId(3).setGender(false).setAge((byte )21).setName("ykl"); 33 34 } 35 }
1、 字符串
1 package 第七天; 2 3 import java.io.ByteArrayInputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 7 public class StreamFromString { 8 static String src = "尤可丽我爱你鸭~~"; 9 public static void main(String[] args) throws IOException{ 10 byte[] buff = new byte[1000024]; 11 //从字符串获取字节写入流 12 InputStream is = new ByteArrayInputStream(src.getBytes()); 13 int len = -1; 14 while(-1 != (len = is.read(buff))) { 15 //将字节数组转换为字符串 16 String res = new String(buff, 0, len); 17 System.out.println(res); 18 } 19 } 20 }
+:字符串连接符
示例:
1 package 第七天; 2 3 //import java.io.ByteArrayInputStream; 4 //import java.io.IOException; 5 //import java.io.InputStream; 6 7 public class StreamFromString { 8 public static void main(String[] args) { 9 String str1 = "abc"; 10 String str2 = "1234"; 11 String str = str1 + str2; 12 System.out.println(str); 13 } 14 }
- 注意:String定义出来的是字符串常量,一单定义之后,就不可改变了。
- StringBuffer/StringBuilder:字符串变量
- 可变的字符串
- StringBuffer 慢、安全
- StringBuilder 快、不安全
1 package 第七天; 2 3 public class 可变字符串比较 { 4 public static void main(String[] args) { 5 StringBuilder sb = new StringBuilder(); 6 sb.append("aaa"); 7 sb.append("bbb"); 8 sb.append("ccc"); 9 System.out.println(sb); //aaabbbccc 10 11 sb.insert(3, "1234"); //第三位后面加入1234 12 System.out.println(sb); //aaa1234bbbccc 13 14 sb.insert(4, true); //第四位后面加入"ture" 15 System.out.println(sb); //aaa1true234bbbccc 16 17 sb.delete(3, 6); 18 System.out.println(sb); //aaaue234bbbccc 19 20 sb.deleteCharAt(8); //删除特定位 21 System.out.println(sb); //aaaue234bbccc 22 23 int length = sb.length(); //统计长度 24 System.out.println(length); //13 25 26 sb.reverse(); //反转顺序 27 System.out.println(sb); //cccbb432euaaa 28 29 StringBuffer stringBuffer = new StringBuffer(); 30 stringBuffer.append("aa").append("bb").append("cc"); 31 System.out.println(stringBuffer); //aabbcc 32 } 33 }
2、 产生随机字符:
UUID:产生随机字符串使用
示例:
1 package 第七天; 2 3 import java.util.UUID; 4 5 public class 产生随机字符串 { 6 public static void main(String[] args) { 7 String string = UUID.randomUUID().toString(); 8 System.out.println(string); //e3049ad7-df42-48aa-8aa8-2b82e4bb1a38 9 System.out.println(string.length()); //36 10 } 11 }