当前位置: 首页 > news >正文

二手手表网站网站制作维护

二手手表网站,网站制作维护,建立网站公司,做视频营销哪个网站好文章目录 String类String的特性String对象的创建String常用方法 StringBuilder类StringBuffer类StringBuffer对象的创建StringBuffer类的常用方法 String、StringBuffer、StringBuilder区别 存放的位置 java.lang.*; 继承的父类 java.lang.Object 实现的接口 java.io.Serializa…

文章目录

  • String类
    • String的特性
    • String对象的创建
    • String常用方法
  • StringBuilder类
  • StringBuffer类
    • StringBuffer对象的创建
    • StringBuffer类的常用方法
  • String、StringBuffer、StringBuilder区别

  1. 存放的位置

java.lang.*;

  1. 继承的父类

java.lang.Object

  1. 实现的接口

java.io.Serializable
Comparable
CharSequence

  1. 创建与初始化

方法一

String greeting =Hello world!”;

方法二

char [] helloArray = {'h''e''l''l''o''。' };
String helloString = new String(helloArray);
  1. 获得字符串长度

public int length()
返回此字符串的长度。
长度等于字符串中Unicode代码单元的数量。

String palindrome =Dot saw I was Tod;
int len = palindrome.length();
  1. 字符串拼接

方法一
public String concat(String str)
将指定的字符串连接到此字符串的末尾。
如果参数字符串的长度为0,则String返回此对象。
否则,String返回一个对象,该对象表示一个字符序列,
该字符序列是由该String对象表示的字符序列和由参数字符串表示的字符序列的串联。

"cares".concat("s");							//返回"caress"
"to".concat("get").concat("her");				//返回"together"

方法二

"Hello," + " world" + "!";						//结果为"Hello, world!"
String string1 = "saw I was ";
System.out.println("Dot " + string1 + "Tod");	//输出Dot saw I was Tod
  1. 字符串转换为字符数组

public char [] toCharArray()
将此字符串转换为新的字符数组。
返回:新分配的字符数组,
其长度为此字符串的长度,
其内容初始化为包含此字符串表示的字符序列。

char [] helloArray = {'h''e''l''l''o''。' };
String helloString = new String(helloArray);
char[] metooArray = helloString.toCharArray();//结果为{'h','e','l','l','o','。' }

String类

字符串,使用一对""引起来表示。

String的特性

String声明为final的,不可被继承
String实现了Serializable接口:表示字符串是支持序列化的。实现了Comparable接口:表示String可以比较大小
String内部定义了final char[] value用于存储字符串数据
String代表不可变的字符序列。简称:不可变性。体现:1.当对字符串重新赋值时,需要重写指定内存区域赋值,不能使用原有的value进行赋值。2. 当对现有的字符串进行连接操作时,也需要重新指定内存区域赋值,不能使用原有的value进行赋值。3. 当调用String的replace()方法修改指定字符或字符串时,也需要重新指定内存区域赋值,不能使用原有的value进行赋值。
通过字面量的方式(区别于new)给一个字符串赋值,此时的字符串值声明在字符串常量池中。
字符串常量池中是不会存储相同内容的字符串的。

String对象的创建

String str = "hello";
//本质上this.value = new char[0];
String s1 = new String();
//this.value = original.value;
String s2 = new String(String original);
//this.value = Arrays.copyOf(value, value.length);
String s3 = new String(char[] a);
String s4 = new String(char[] a,int startIndex,int count);
  • 字符串对象是如何存储的
    String str1 = “abc”;与String str2 = new String(“abc”);的区别?
//通过字面量定义的方式:此时的s1和s2的数据javaEE声明在方法区中的字符串常量池中。
String s1 = "javaEE";
String s2 = "javaEE";
//通过new + 构造器的方式:此时的s3和s4保存的地址值,是数据在堆空间中开辟空间以后对应的地址值。
String s3 = new String("javaEE");
String s4 = new String("javaEE");

String s = new String(“abc”);方式创建对象,在内存中创建了几个对象?

两个:一个是堆空间中new结构,另一个是char[]对应的常量池中的数据:“abc”

String s1 = "javaEE";
String s2 = "hadoop";String s3 = "javaEEhadoop";
String s4 = "javaEE" + "hadoop";
String s5 = s1 + "hadoop";
String s6 = "javaEE" + s2;
String s7 = s1 + s2;System.out.println(s3 == s4);//true
System.out.println(s3 == s5);//false
System.out.println(s3 == s6);//false
System.out.println(s3 == s7);//false
System.out.println(s5 == s6);//false
System.out.println(s5 == s7);//false
System.out.println(s6 == s7);//falseString s8 = s6.intern();//返回值得到的s8使用的常量值中已经存在的“javaEEhadoop”
System.out.println(s3 == s8);//true

结论:

字符串常量存储在字符串常量池,目的是共享
字符串非常量对象存储在堆中。
常量与常量的拼接结果在常量池。且常量池中不会存在相同内容的常量。
只要其中有一个是变量,结果就在堆中
如果拼接的结果调用intern()方法,返回值就在常量池中

  • String使用陷阱
    String s1 = “a”;

说明:在字符串常量池中创建了一个字面量为"a"的字符串。

s1 = s1 + “b”;

说明:实际上原来的“a”字符串对象已经丢弃了,现在在堆空间中产生了一个字符串s1+“b”(也就是"ab")。如果多次执行这些改变串内容的操作,会导致大量副本字符串对象存留在内存中,降低效率。如果这样的操作放到循环中,会极大影响程序的性能。

String s2 = “ab”;

说明:直接在字符串常量池中创建一个字面量为"ab"的字符串。

String s3 = “a” + “b”;

说明:s3指向字符串常量池中已经创建的"ab"的字符串。

String s4 = s1.intern();

说明:堆空间的s1对象在调用intern()之后,会将常量池中已经存在的"ab"字符串赋值给s4。

String常用方法

	int length():返回字符串的长度: return value.lengthchar charAt(int index): 返回某索引处的字符return value[index]boolean isEmpty():判断是否是空字符串:return value.length == 0String toLowerCase():使用默认语言环境,将 String 中的所有字符转换为小写String toUpperCase():使用默认语言环境,将 String 中的所有字符转换为大写String trim():返回字符串的副本,忽略前导空白和尾部空白boolean equals(Object obj):比较字符串的内容是否相同boolean equalsIgnoreCase(String anotherString):与equals方法类似,忽略大小写String concat(String str):将指定字符串连接到此字符串的结尾。 等价于用“+int compareTo(String anotherString):比较两个字符串的大小String substring(int beginIndex):返回一个新的字符串,它是此字符串的从beginIndex开始截取到最后的一个子字符串。String substring(int beginIndex, int endIndex) :返回一个新字符串,它是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串。boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束boolean startsWith(String prefix):测试此字符串是否以指定的前缀开始boolean startsWith(String prefix, int toffset):测试此字符串从指定索引开始的子字符串是否以指定前缀开始boolean contains(CharSequence s):当且仅当此字符串包含指定的 char 值序列时,返回 trueint indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引int indexOf(String str, int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始int lastIndexOf(String str):返回指定子字符串在此字符串中最右边出现处的索引int lastIndexOf(String str, int fromIndex):返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索注:indexOf和lastIndexOf方法如果未找到都是返回-1String replace(char oldChar, char newChar):返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。String replace(CharSequence target, CharSequence replacement):使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。String replaceAll(String regex, String replacement) : 使 用 给 定 的replacement 替换此字符串所有匹配给定的正则表达式的子字符串。String replaceFirst(String regex, String replacement) : 使 用 给 定 的replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。boolean matches(String regex):告知此字符串是否匹配给定的正则表达式。String[] split(String regex):根据给定正则表达式的匹配拆分此字符串。String[] split(String regex, int limit):根据匹配给定的正则表达式来拆分此字符串,最多不超过limit个,如果超过了,剩下的全部都放到最后一个元素中。
  • String与基本数据类型转换
    字符串→基本数据类型、包装类

Integer包装类的public static int parseInt(String s):可以将由“数字”字符组成的字符串转换为整型。
类似地,使用java.lang包中的Byte、Short、Long、Float、Double类调相应的类方法可以将由“数字”字符组成的字符串,转化为相应的基本数据类型。

1.int转为二进制

int num = sc.nextInt();
String s = Integer.toBinaryString(num);

2.int与String互相转换

//String转为int
int i=Integer.parseInt(string);
int i=Integer.valueOf(s).intValue();//int转为String
String s = String.valueOf(i);
String s = Integer.toString(i);
String s = "" + i;

基本数据类型、包装类→字符串

调用String类的public String valueOf(int n)可将int型转换为字符串
相应的valueOf(byte b)、valueOf(long l)、valueOf(float f)、valueOf(double d)、valueOf(boolean b)可由参数的相应类型到字符串的转换

  • String与字符数组转换
    字符数组→字符串

String 类的构造器:String(char[]) 和 String(char[],int offset,int length) 分别用字符数组中的全部字符和部分字符创建字符串对象。

字符串→字符数组

public char[] toCharArray():将字符串中的全部字符存放在一个字符数组中的方法。
public void getChars(int srcBegin, int srcEnd, char[] dst,int dstBegin):提供了将指定索引范围内的字符串存放到数组中的方法。

  • String与字节数组转换
    字节数组→字符串

String(byte[]):通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
String(byte[],int offset,int length) :用指定的字节数组的一部分,即从数组起始位置offset开始取length个字节构造一个字符串对象。

字符串→字节数组

public byte[] getBytes() :使用平台的默认字符集将此 String 编码为byte 序列,并将结果存储到一个新的 byte 数组中。
public byte[] getBytes(String charsetName) :使用指定的字符集将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组。

String str = "中";
System.out.println(str.getBytes("ISO8859-1").length);// -128~127
System.out.println(str.getBytes("GBK").length);
System.out.println(str.getBytes("UTF-8").length);
System.out.println(new String(str.getBytes("ISO8859-1"),"ISO8859-1"));// 乱码,表示不了中文
System.out.println(new String(str.getBytes("GBK"), "GBK"));
System.out.println(new String(str.getBytes("UTF-8"), "UTF-8"));

StringBuilder类

StringBuilder 和 StringBuffer 非常类似,均代表可变的字符序列,而且提供相关功能的方法也一样

StringBuffer类

java.lang.StringBuffer代表可变的字符序列,JDK1.0中声明,可以对字符串内容进行增删,此时不会产生新的对象。
很多方法与String相同。
作为参数传递时,方法内部可以改变值。
count记录有效字符的个数。
value没有final声明,value可以不断扩容。

StringBuffer对象的创建

	StringBuffer类不同于String,其对象必须使用构造器生成。有三个构造器:StringBuffer():初始容量为16的字符串缓冲区StringBuffer(int size):构造指定容量的字符串缓冲区StringBuffer(String str):将内容初始化为指定字符串内容

StringBuffer类的常用方法

	StringBuffer append(xxx):提供了很多的append()方法,用于进行字符串拼接StringBuffer delete(int start,int end):删除指定位置的内容StringBuffer replace(int start, int end, String str):把[start,end)位置替换为strStringBuffer insert(int offset, xxx):在指定位置插入xxxStringBuffer reverse() :把当前字符序列逆转
当append和insert时,如果原来value数组长度不够,可扩容。
如上这些方法支持方法链操作。
	public int indexOf(String str)public String substring(int start,int end)public int length()public char charAt(int n )public void setCharAt(int n ,char ch)

String、StringBuffer、StringBuilder区别

String:不可变的字符序列;底层使用char[]存储
StringBuffer:可变的字符序列;线程安全的,效率低;底层使用char[]存储
StringBuilder:可变的字符序列;jdk5.0新增的,线程不安全的,效率高;底层使用char[]存储

源码分析:

   String str = new String();//char[] value = new char[0];String str1 = new String("abc");//char[] value = new char[]{'a','b','c'};StringBuffer sb1 = new StringBuffer();//char[] value = new char[16];底层创建了一个长度是16的数组。System.out.println(sb1.length());//sb1.append('a');//value[0] = 'a';sb1.append('b');//value[1] = 'b';StringBuffer sb2 = new StringBuffer("abc");//char[] value = new char["abc".length() + 16];//问题1. System.out.println(sb2.length());//3//问题2. 扩容问题:如果要添加的数据底层数组盛不下了,那就需要扩容底层的数组。默认情况下,扩容为原来容量的2+ 2,同时将原有数组中的元素复制到新的数组中。指导意义:开发中建议大家使用:StringBuffer(int capacity)StringBuilder(int capacity)

文章转载自:
http://parboil.xnLj.cn
http://symbolical.xnLj.cn
http://carrottop.xnLj.cn
http://scrotitis.xnLj.cn
http://malone.xnLj.cn
http://anabaptism.xnLj.cn
http://paintwork.xnLj.cn
http://cotton.xnLj.cn
http://didactic.xnLj.cn
http://bedsheet.xnLj.cn
http://alphabet.xnLj.cn
http://muticate.xnLj.cn
http://cityscape.xnLj.cn
http://stellar.xnLj.cn
http://scleroid.xnLj.cn
http://terrace.xnLj.cn
http://myelogenic.xnLj.cn
http://incapacitate.xnLj.cn
http://sebacic.xnLj.cn
http://necrographer.xnLj.cn
http://structure.xnLj.cn
http://whangee.xnLj.cn
http://misprision.xnLj.cn
http://disparity.xnLj.cn
http://polyzoarium.xnLj.cn
http://aguti.xnLj.cn
http://peperoni.xnLj.cn
http://billsticker.xnLj.cn
http://wiping.xnLj.cn
http://strisciando.xnLj.cn
http://leiomyoma.xnLj.cn
http://snowmobilist.xnLj.cn
http://comble.xnLj.cn
http://deflocculant.xnLj.cn
http://comedy.xnLj.cn
http://abrogation.xnLj.cn
http://rattling.xnLj.cn
http://garonne.xnLj.cn
http://macroinvertebrate.xnLj.cn
http://disseminative.xnLj.cn
http://oratorian.xnLj.cn
http://ecotype.xnLj.cn
http://sightseeing.xnLj.cn
http://soleiform.xnLj.cn
http://privateer.xnLj.cn
http://radurization.xnLj.cn
http://abrader.xnLj.cn
http://vassalic.xnLj.cn
http://phenylene.xnLj.cn
http://standing.xnLj.cn
http://ceeb.xnLj.cn
http://essex.xnLj.cn
http://gadzooks.xnLj.cn
http://palaeomagnetism.xnLj.cn
http://dustman.xnLj.cn
http://nrab.xnLj.cn
http://serving.xnLj.cn
http://parfocal.xnLj.cn
http://microbarograph.xnLj.cn
http://tocodynamometer.xnLj.cn
http://redux.xnLj.cn
http://cesarean.xnLj.cn
http://saccharined.xnLj.cn
http://halocline.xnLj.cn
http://picul.xnLj.cn
http://teu.xnLj.cn
http://rancheria.xnLj.cn
http://peptalk.xnLj.cn
http://automark.xnLj.cn
http://backtrack.xnLj.cn
http://tetraonid.xnLj.cn
http://possibilistic.xnLj.cn
http://probationary.xnLj.cn
http://unmanly.xnLj.cn
http://polyvalent.xnLj.cn
http://isentropic.xnLj.cn
http://gormless.xnLj.cn
http://eartab.xnLj.cn
http://dreamboat.xnLj.cn
http://hyperalgesia.xnLj.cn
http://diagrammatical.xnLj.cn
http://genitor.xnLj.cn
http://phoenicaceous.xnLj.cn
http://premaxilla.xnLj.cn
http://hemihydrate.xnLj.cn
http://axiom.xnLj.cn
http://featherpate.xnLj.cn
http://sacculus.xnLj.cn
http://nacrous.xnLj.cn
http://coldstart.xnLj.cn
http://fiendishly.xnLj.cn
http://queening.xnLj.cn
http://tabularize.xnLj.cn
http://tenement.xnLj.cn
http://westmost.xnLj.cn
http://unweakened.xnLj.cn
http://peroxidase.xnLj.cn
http://univallate.xnLj.cn
http://ignobly.xnLj.cn
http://klick.xnLj.cn
http://www.15wanjia.com/news/96137.html

相关文章:

  • 好的做淘宝详情页的网站有哪些谷歌搜索引擎优化
  • 做网站租用服务器seo定义
  • 网站空间是不是服务器无锡seo排名收费
  • 无锡网站设计公司排名sem数据分析
  • 文创产品设计大全seo怎么做优化计划
  • 在个人网站上做电商营业执照免费外链发布平台在线
  • 怎样做企业网站宣传购买域名
  • 空间设计网站公司淘宝推广方法有哪些
  • 连云港做网站最好亚洲足球最新排名
  • 青岛网站seo技巧搜狗收录批量查询
  • 网站弹出信息怎么做免费二级域名分发网站源码
  • 网站插件代码网络营销介绍
  • 购物网站app制作如何优化关键词搜索排名
  • 本地服务器如何做网站网站如何进行seo
  • 深圳网站建设定制开发惠州百度seo
  • 港海(天津)建设股份有限公司网站seo狂人
  • 百度做的网站 后台管理怎么进入广州营销型网站
  • 长沙浏阳最新通告seo培训
  • 怎么修改wordpress字体如何做好网站推广优化
  • 园区门户网站建设方案网络推广与营销
  • 免费网站报价单怎么做网站推广软件ky99
  • 济南浩辰网站建设公司怎么样百度官方
  • seo上首页seo教程seo入门讲解
  • 亚马逊amz123石家庄seo全网营销
  • 阿里云做电脑网站营销型网站建设的主要流程包括
  • 个人网站取名怎么让网站快速收录
  • 平顶山建设局网站网络推广主要工作内容
  • 网站建设的岗位名称百度官方人工客服电话
  • wordpress归档页面自定义qq群排名优化软件官网
  • 网站设计过程seo是干什么的