String类 对String在内存存储方面的理解: 第一:字符串一旦创建不可变。 第二:双引号括起来的字符串存储在字符串常量池中。 第三:字符串的比较必须使用equals方法。 第四:String已经重写了toString()和equals()方法。
String的构造方法 String s = "abc";
String s = new String("abc");
String s = new String(byte数组);
String s = new String(byte数组, 起始下标, 长度);
String s = new String(char数组);
String s = new String(char数组, 起始下标, 长度);
String类常用的21个方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 public class StringTest05 { public static void main (String[] args) { char c = "中国人" .charAt(1 ); System.out.println(c); int result = "abc" .compareTo("abc" ); System.out.println(result); int result2 = "abcd" .compareTo("abce" ); System.out.println(result2); int result3 = "abce" .compareTo("abcd" ); System.out.println(result3); System.out.println("xyz" .compareTo("yxz" )); System.out.println("HelloWorld.java" .contains(".java" )); System.out.println("http://www.baidu.com" .contains("https://" )); System.out.println("test.txt" .endsWith(".java" )); System.out.println("test.txt" .endsWith(".txt" )); System.out.println("fdsajklfhdkjlsahfjkdsahjklfdss" .endsWith("ss" )); System.out.println("abc" .equals("abc" )); System.out.println("ABc" .equalsIgnoreCase("abC" )); byte [] bytes = "abcdef" .getBytes(); for (int i = 0 ; i < bytes.length; i++){ System.out.println(bytes[i]); } System.out.println("oraclejavac++.netc#phppythonjavaoraclec++" .indexOf("java" )); String s = "a" ; System.out.println(s.isEmpty()); System.out.println("abc" .length()); System.out.println("" .length()); System.out.println("oraclejavac++javac#phpjavapython" .lastIndexOf("java" )); String newString = "http://www.baidu.com" .replace("http://" , "https://" ); System.out.println(newString); String newString2 = "name=zhangsan&password=123&age=20" .replace("=" , ":" ); System.out.println(newString2); String[] ymd = "1980-10-11" .split("-" ); for (int i = 0 ; i < ymd.length; i++){ System.out.println(ymd[i]); } String param = "name=zhangsan&password=123&age=20" ; String[] params = param.split("&" ); for (int i = 0 ; i <params.length; i++){ System.out.println(params[i]); } System.out.println("http://www.baidu.com" .startsWith("http" )); System.out.println("http://www.baidu.com" .startsWith("https" )); System.out.println("http://www.baidu.com" .substring(7 )); System.out.println("http://www.baidu.com" .substring(7 , 10 )); char [] chars = "我是中国人" .toCharArray(); for (int i = 0 ; i < chars.length; i++){ System.out.println(chars[i]); } System.out.println("ABCDefKXyz" .toLowerCase()); System.out.println("ABCDefKXyz" .toUpperCase()); System.out.println(" hello world " .trim()); String s1 = String.valueOf(new Customer ()); System.out.println(s1); System.out.println(100 ); System.out.println(3.14 ); System.out.println(true ); Object obj = new Object (); System.out.println(obj); System.out.println(new Customer ()); } }class Customer { @Override public String toString () { return "我是一个VIP客户!!!!" ; } }
StringBuffer/StringBuilder 1、StringBuffer/StringBuilder可以看做可变长度字符串。 2、StringBuffer/StringBuilder初始化容量16. 3、StringBuffer/StringBuilder是完成字符串拼接操作的,方法名:append 4、StringBuffer是线程安全的。StringBuilder是非线程安全的。 5、频繁进行字符串拼接不建议使用“+”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 public class StringBufferTest02 { public static void main (String[] args) { StringBuffer stringBuffer = new StringBuffer (); stringBuffer.append("a" ); stringBuffer.append("b" ); stringBuffer.append("d" ); stringBuffer.append(3.14 ); stringBuffer.append(true ); stringBuffer.append(100L ); System.out.println(stringBuffer.toString()); StringBuffer sb = new StringBuffer (100 ); sb.append("hello" ); sb.append("world" ); sb.append("hello" ); sb.append("kitty" ); System.out.println(sb); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public class StringBufferTest04 { public static void main (String[] args) { String s = "abc" ; s = "xyz" ; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public class StringBuilderTest01 { public static void main (String[] args) { StringBuilder sb = new StringBuilder (); sb.append(100 ); sb.append(true ); sb.append("hello" ); sb.append("kitty" ); System.out.println(sb); } }
八种基本数据类型对应的包装类 八种包装类的类名是什么? 基本数据类型 包装类型
-------------------------------------
byte java.lang.Byte(父类Number)
short java.lang.Short(父类Number)
int java.lang.Integer(父类Number)
long java.lang.Long(父类Number)
float java.lang.Float(父类Number)
double java.lang.Double(父类Number)
boolean java.lang.Boolean(父类Object)
char java.lang.Character(父类Object)
以上八种包装类中,重点以java.lang.Integer为代表进行学习,其它的类型照葫芦画瓢就行。
什么是自动装箱和自动拆箱,代码怎么写? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 System.out.println("int的最大值:" + Integer.MAX_VALUE); System.out.println("int的最小值:" + Integer.MIN_VALUE); System.out.println("byte的最大值:" + Byte.MAX_VALUE); System.out.println("byte的最小值:" + Byte.MIN_VALUE);public class IntegerTest05 { public static void main (String[] args) { Integer x = 900 ; System.out.println(x); int y = x; System.out.println(y); Integer z = 1000 ; System.out.println(z + 1 ); Integer a = 1000 ; Integer b = 1000 ; System.out.println(a == b); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package com.bjpowernode.javase.integer;public class IntegerTest06 { public static void main (String[] args) { Integer a = 128 ; Integer b = 128 ; System.out.println(a == b); Integer x = 127 ; Integer y = 127 ; System.out.println(x == y); } }
Integer类常用方法 Integer.valueOf() Integer.parseInt(“123”) Integer.parseInt(“中文”) : NumberFormatException
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 import jdk.swing.interop.SwingInterOpUtils;public class IntegerTest07 { public static void main (String[] args) { Integer x = new Integer (1000 ); int y = x.intValue(); System.out.println(y); Integer a = new Integer ("123" ); int retValue = Integer.parseInt("123" ); System.out.println(retValue + 100 ); double retValue2 = Double.parseDouble("3.14" ); System.out.println(retValue2 + 1 ); float retValue3 = Float.parseFloat("1.0" ); System.out.println(retValue3 + 1 ); String binaryString = Integer.toBinaryString(3 ); System.out.println(binaryString); String hexString = Integer.toHexString(16 ); System.out.println(hexString); hexString = Integer.toHexString(17 ); System.out.println(hexString); String octalString = Integer.toOctalString(8 ); System.out.println(octalString); System.out.println(new Object ()); Integer i1 = Integer.valueOf(100 ); System.out.println(i1); Integer i2 = Integer.valueOf("100" ); System.out.println(i2); } }
Integer String int三种类型互相转换 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 public class IntegerTest08 { public static void main (String[] args) { int i1 = Integer.parseInt("100" ); System.out.println(i1 + 1 ); String s2 = i1 + "" ; System.out.println(s2 + 1 ); Integer x = 1000 ; int y = x; Integer k = Integer.valueOf("123" ); String e = String.valueOf(k); } }