编写高质量代码改善java程序的151个建议——[52-57]String !about String How to use them?

简介:

Suggestion:Use the String direct value for the assignment

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
AI 代码解读

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.
AI 代码解读

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.

                               image

How to use the String , StringBuffer,StringBuilder

Look at the pic:
                               image

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.

for example:

String str  = "abc";
    String str1 = str.substring(1);
        
    System.out.println("str1" + str1);

#outputs:
bc
AI 代码解读

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.

                                    image

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.

                            image

 

Easy Time:Pay attention to the address of String

for example:

public static void main(String[] args)
{
    String str1 = 1 + 2 + "apples";
    String str2 = "apples" + 1 + 2;
    
    System.out.println(str1);
    System.out.println(str2);
}
#outputs:
3apples
apples12
AI 代码解读

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

just reading!! the part , i will write in the future.

目录
打赏
0
0
0
0
171
分享
相关文章
java实现队列数据结构代码详解
本文详细解析了Java中队列数据结构的实现,包括队列的基本概念、应用场景及代码实现。队列是一种遵循“先进先出”原则的线性结构,支持在队尾插入和队头删除操作。文章介绍了顺序队列与链式队列,并重点分析了循环队列的实现方式以解决溢出问题。通过具体代码示例(如`enqueue`入队和`dequeue`出队),展示了队列的操作逻辑,帮助读者深入理解其工作机制。
接替此文【下篇-服务端+后台管理】优雅草蜻蜓z系统JAVA版暗影版为例-【蜻蜓z系列通用】-2025年全新项目整合搭建方式-这是独立吃透代码以后首次改变-独立PC版本vue版搭建教程-优雅草卓伊凡
接替此文【下篇-服务端+后台管理】优雅草蜻蜓z系统JAVA版暗影版为例-【蜻蜓z系列通用】-2025年全新项目整合搭建方式-这是独立吃透代码以后首次改变-独立PC版本vue版搭建教程-优雅草卓伊凡
225 96
接替此文【下篇-服务端+后台管理】优雅草蜻蜓z系统JAVA版暗影版为例-【蜻蜓z系列通用】-2025年全新项目整合搭建方式-这是独立吃透代码以后首次改变-独立PC版本vue版搭建教程-优雅草卓伊凡
C++ String揭秘:写高效代码的关键
在C++编程中,字符串操作是不可避免的一部分。从简单的字符串拼接到复杂的文本处理,C++的string类为开发者提供了一种更高效、灵活且安全的方式来管理和操作字符串。本文将从基础操作入手,逐步揭开C++ string类的奥秘,帮助你深入理解其内部机制,并学会如何在实际开发中充分发挥其性能和优势。
JVM实战—1.Java代码的运行原理
本文介绍了Java代码的运行机制、JVM类加载机制、JVM内存区域及其作用、垃圾回收机制,并汇总了一些常见问题。
JVM实战—1.Java代码的运行原理
|
1月前
|
Java代码结构解析:类、方法、主函数(1分钟解剖室)
### Java代码结构简介 掌握Java代码结构如同拥有程序世界的建筑蓝图,类、方法和主函数构成“黄金三角”。类是独立的容器,承载成员变量和方法;方法实现特定功能,参数控制输入环境;主函数是程序入口。常见错误包括类名与文件名不匹配、忘记static修饰符和花括号未闭合。通过实战案例学习电商系统、游戏角色控制和物联网设备监控,理解类的作用、方法类型和主函数任务,避免典型错误,逐步提升编程能力。 **脑图速记法**:类如太空站,方法即舱段;main是发射台,static不能换;文件名对仗,括号要成双;参数是坐标,void不返航。
82 5
课时8:Java程序基本概念(标识符与关键字)
课时8介绍Java程序中的标识符与关键字。标识符由字母、数字、下划线和美元符号组成,不能以数字开头且不能使用Java保留字。建议使用有意义的命名,如student_name、age。关键字是特殊标记,如蓝色字体所示。未使用的关键字有goto、const;特殊单词null、true、false不算关键字。JDK1.4后新增assert,JDK1.5后新增enum。
课时7:Java程序基本概念(注释)
课时7介绍了Java程序中的注释。编程语言有其语法和语义,注释有助于理解代码需求,防止断档。Java支持三类注释:单行(//)、多行(/* */)和文档注释(/** */)。注释不会被编译器编译。范例中展示了如何在代码中使用注释,并强调了注释对项目文档管理的重要性。
【YashanDB知识库】Java程序调用存储过程,在提取clob时报YAS-00004
【YashanDB知识库】Java程序调用存储过程,在提取clob时报YAS-00004
课时146:使用JDT开发Java程序
在 Eclipse 之中提供有 JDT环境可以实现java 程序的开发,下面就通过一些功能进行演示。 项目开发流程
课时5:第一个Java程序
课时5介绍了编写第一个Java程序的步骤,包括创建Hello.java文件、编写“Hello World”代码、编译和运行程序。主要内容有:1) 新建并编辑Hello.java;2) 编译Java源文件生成.class文件;3) 通过命令行解释执行Java程序;4) 解释主方法的作用及信息输出操作。本课强调了类定义、文件命名规则和基本程序结构的重要性,并建议初学者使用记事本编写代码以熟悉基础语法。

热门文章

最新文章