编写高质量代码改善java程序的151个建议——[52-57]String !about String How to use them?
Do u knw the String Object ? If u do some projects,u can see the String is used usually. A object is created by the key word : new.Therefore , we can create a String Obejct by :“
String str3 = new String("Jeff"); ” .
Here, in my word,using the String direct value for the assignment is a better way.
for example:
public class String01 public static void main(String[] args) String str1 = "Jeff"; String str2 = "Jeff"; String str3 = new String("Jeff"); String str4 = str3.intern(); boolean b1 = (str1 == str2); boolean b2 = (str1 == str3); boolean b3 = (str1 == str4); System.out.println("b1:"+b1+" "+"b2:"+b2+" "+"b3:"+b3+" "); #outputs: b1:true b2:false b3:true
b1:true b2:false
u will think ,thats why they r different.
As we all kno , the operator“==”show whether two objects’address references
are the same. Java designed a String Pool for storing all the String
used to avoid there are to many String Objects created in a system. So
String str3 = new String("Jeff"); is creating a object in java heap memory not the String Pool.
intern() is a method of String. we can see from the jdk help doc.
public String intern() Returns a canonical representation for the string object. A pool of strings, initially empty, is maintained privately by the class String. When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.
All in all, using String str = "Jeff"; u dont mind the Thread-security or Garbage collection mechanism.String is a nice man , treat it as a little boy pelasse.
Look at the pic:
String , StringBuffer ,StringBuilder implement the CharSequence.But they are different.
String
String Object is a non-variable . it can not be changed and in the memory when u create it.
substring() method creates a new String Object and links the reference of it to str1. But when “str.substring(0)”,str1 and str both link to the “abc”by the JVM.
StringBuffer StringBuilder
they are very similar and they r variables of the sequence of characters.Only different, the StringBuffer has the methods which are synchronized where necessary. String buffers are safe for use by multiple threads. Different from String, if z refers to a string buffer object whose current contents are "start", then the method call z.append("le") would cause the string buffer to contain "startle", whereasz.insert(4, "le") would alter the string buffer to contain "starlet".
All in all:
String can be used for the constants.
StringBuffer can be used for some operating methods in multithreaded environment.like XML analyze,the parameters of HTTP analyze etc.
StringBuilder can be used for HQL/SQL splice, JSON package etc.
Easy Time:Pay attention to the address of String
what we can see from the result-values.why ? how ? they did.
Because the JAVA handling mechanism to the operator “+”. when there is a string in the expression, all the expression data will change itself to the String class.if the data is an Object, it will call its toString method.
So,String str1 = 1 + 2 + "apples" just like String str1 = (1 + 2) + "apples" .thats all.
Complex string manipulation using regular expressions
二哥聊RPA 公号「二哥聊RPA」。关注我,一起洞察 RPA ◎ RPA创业者 ◎ 现明堂红木商业顾问,为十月结晶、可啦啦、鑫蜂维等多家企业提供 RPA 咨询服务 ◎ 希望帮助大家在 RPA 上做一些力所能及的事情
相关文章
- JAVA-一个简单的JSP程序(HelloWorld)
- Java程序优化的一些最佳实践(转)
- JAVA Eclipse开发Android程序如何自定义图标
- Java通过JNI调用C++程序
- 编写高质量代码改善java程序的151个建议——[1-3]基础?亦是基础
- Java实现空瓶换汽水
- java实现第五届蓝桥杯猜字母
- Java实现 蓝桥杯VIP 算法训练 统计字符次数
- 编写高质量代码改善java程序的151个建议——[1-3]基础?亦是基础
- 编写高质量代码改善java程序的151个建议——导航开篇
- java 结束程序进程 代码
- 编写高质量代码:改善Java程序的151个建议 --[106~117]
- 编写高质量代码:改善Java程序的151个建议 --[78~92]
- Spark之wordcount程序(Java Scala)
- 编写高质量代码:改善Java程序的151个建议 --[106~117]
- 编写高质量代码:改善Java程序的151个建议 --[98~105]
- 编写高质量代码:改善Java程序的151个建议 --[65~78]
- Eclipse开发Java程序入门,HelloWord
- 用Eclipse调试Java程序的一些小技巧
- 【java】Java连接mysql数据库及mysql驱动jar包下载和使用
- Java开发中程序和代码性能优化
- java-信息安全(十二)-数字证书、CA证书【Java证书体系实现】
- Java开发技术之成为高级java工程师必须学习的三个技术
- java.lang.OutOfMemoryError: Java heap space
- 【java】Java 集合框架
- 【java】Java线程池实现原理及业务中的实践
- JAVA程序对应不同的部署环境针对配置文件如何管理