赞
踩
String类表示字符串。 Java程序中的所有字符串文字(例如"abc" )都实现为此类的实例(对象)。
字符串是不变的; 它们的值在创建后无法更改。
String str = "abc";
//相当于:
char data[] = {'a', 'b', 'c'};
String str = new Strin(data);
因为String对象是不可变的,所以可以共享它们,两个字符串内容如果完全相同,则占用同一块内存地址
String text1 = "123";
String text2 = "123";
System.out.println(text1 == text2);
//true
String text3 = new String("123");
System.out.println(text1 == text3);
//false, new开辟新的内存空间,生成了两个对象
源码表明字符串可以看作常量:
public final class String
方法区(Method Area),又称永久代(Permanent Generation)(永久存放在内存中,只要程序不结束),又称非堆区(Non-Heap space)方法区,常称PermGen.
方法区是被所有线程共享.
但是 实例变量 存在 堆内存中,和方法区无关。
以上仅仅是逻辑上的定义。在HotSpot中,方法区仅仅指示逻辑上的独立,实际上还是在java堆中,方法区物理上属于java堆区中的一部分,而永久区(Permanent Gereration)就是方法区的实现。
一个JVM实例只存在一个堆内存,堆内存的大小是可以调节的。类加载器读取了类文件后,需要把类,方法,常变量放到堆内存中,保存所有引用类型的真实信息,以方便执行器执行。
堆 在逻辑上分为三部分(Perm):
//StringBuilder是线程不安全的实现,StringBuffer是线程安全的实现,两者使用方法类似,使用append追加,在使用toString()转换成字符串
StringBuilder sb = new StringBuilder();
sb.append("1");
sb.append("2");
sb.append("3");
String text = sb.toString();
System.out.println(text);
使用 + 拼接字符串造成的内存浪费如图所示:
public char charAt(int index)
String str1 = "qwertyuio";
char c1 = str1.charAt(3);
System.out.println(c1);
//'r'
public int codePointAt(int index)
https://blog.csdn.net/xjp_xujiping/article/details/50210533
String str1 = "qwertyuio";
int n1 = str1.codePointAt(3);
System.out.println(n1);
//114
String str1 = "锄禾日当午";
int a1 = str1.codePointAt(3);
System.out.println(a1);
//24403,char可以表示中文字符
public int codePointBefore(int index)
public int codePointCount(int beginIndex, int endIndex)
public int compareTo(String anotherString)
String str1 = "qwertyuio";
String str2 = "123456789";
int n1 = str1.compareTo(str2);
System.out.println(n1);
//64 q->113, 1->49, 'q'-'1'=64
String str1 = "qw";
String str2 = "qwer";
int n1 = str1.compareTo(str2);
System.out.println(n1);
//-2 str1.length() - str2.length() = -2
public int compareToIgnoreCase(String str)
String str1 = "qw";
String str2 = "QW";
int n1 = str1.compareToIgnoreCase(str2);
System.out.println(n1);
//0
public String concat(String str)
String str1 = "qw";
String str2 = "QW";
String str3 = str1.concat(str2);
System.out.println(str3);
//qwQW
public boolean contains(CharSequence s)
String str1 = "qw45fgfgfxc";
String str2 = "5f";
boolean b1 = str1.contains(str2);
System.out.println(b1);
//true
public boolean endsWith(String suffix)
String str1 = "qw45fgfgfxc";
boolean b1 = str1.endsWith("c");
System.out.println(b1);
//true
public boolean equals(Object anObject)
String str1 = "12345";
String str2 = "12345";
boolean b1 = str1.equals(str2);
System.out.println(b1);
//true
public boolean equalsIgnoreCase(String anotherString)
String str1 = "AqWqq";
String str2 = "aqwqq";
boolean b1 = str1.equalsIgnoreCase(str2);
System.out.println(b1);
public byte[] getBytes()
String str1 = "AqWqq";
String str2 = "aqwqq";
byte[] arr = str1.getBytes(StandardCharsets.UTF_8);
System.out.println(Arrays.toString(arr));
//[65, 113, 87, 113, 113]
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
String str1 = "AqWqq";
char[] c1 = {'a', 'c', 'd', 'w', 'a'};
str1.getChars(1, 4, c1, 0);
System.out.println(Arrays.toString(c1));
//[q, W, q, w, a]
public int indexOf(int ch)
String str1 = "AqWqq";
int a = str1.indexOf('W');
System.out.println(a);
//2
public int indexOf(int ch, int fromIndex)
public int indexOf(String str)
public int lastIndexOf(int ch)
public String intern()
详情跳转至
public boolean isBlank()
String str1 = " ";
System.out.println(str1.isBlank());
//true
public boolean isEmpty()
String str1 = " ";
System.out.println(str1.isEmpty());
//false
public boolean matches(String regex)
正则表达式 https://www.runoob.com/regexp/regexp-syntax.html
public boolean regionMatches(int toffset, String other, int ooffset, int len)
regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
String str1 = "wewewewe";
String str2 = "aaweweweaa";
System.out.println(str1.regionMatches(2, str2, 2, 4));
//true
public String repeat(int count)
String str1 = "abc";
System.out.println(str1.repeat(4));
//abcabcabcabc
public String replace(CharSequence target, CharSequence replacement)
String str2 = "aaaweweweaaa";
System.out.println(str2.replace("aa", "bs"));
//bsawewewebsa
public String replaceAll(String regex, String replacement)
public String replaceFirst(String regex, String replacement)
public String[] split(String regex)
public String[] split(String regex, int limit)
public boolean startsWith(String prefix)
String str2 = "aaaweweweaaa";
System.out.println(str2.startsWith("aaa"));
//true
public String strip()
String str2 = " aaawewe weaaa ";
System.out.println(str2.strip());
//aaawewe weaaa
public String stripLeading()
public String stripTrailing()
public CharSequence subSequence(int beginIndex, int endIndex)
String str2 = "锄禾日当午";
System.out.println(str2.subSequence(2, 5));
//日当午
public String substring(int beginIndex)
public String substring(int beginIndex, int endIndex)
public char[] toCharArray()
String str2 = "锄禾日当午";
char[] c1 = str2.toCharArray();
System.out.println(Arrays.toString(c1));
//[锄, 禾, 日, 当, 午]
public String toLowerCase()
String str2 = "锄禾日当午";
System.out.println(str2.toLowerCase(Locale.ROOT));
String str1 = "HELLO";
System.out.println(str1.toLowerCase(Locale.ROOT));
//锄禾日当午
//hello
public String toUpperCase()
public String toUpperCase(Locale locale)
public String trim()
String str2 = " 锄禾日当午 ";
System.out.println(str2.trim());
//锄禾日当午
public static String valueOf(double d)
double d = 12.133;
String str1 = String.valueOf(d);
System.out.println(str1);
//12.133
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。