备案 控制台
开发者社区 开发与运维 文章 正文

编写高质量代码改善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

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.

                               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

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

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.

二哥聊RPA
目录
相关文章
梦回故国楼台梦
|
17小时前
|
算法 Java 程序员
Java多态:不只是代码,更是思想的碰撞!
【6月更文挑战第17天】Java的多态性展示了编程的哲学,通过抽象基类(如`AudioFile`、`Shape`、`Product`)和重写方法实现。案例中,音乐播放器利用多态统一处理不同音频格式,绘图软件优雅地绘制各种形状,电商系统灵活管理商品信息。多态简化代码,增强可扩展性,连接技术与设计,体现代码的灵活性和艺术性。
梦回故国楼台梦
12 0
梦回故国楼台梦
|
1天前
|
安全 Java
Java 面向对象之旅:封装——让代码更加“接地气”的秘诀。
【6月更文挑战第16天】**Java面向对象的封装秘籍:**将数据和操作打包成类,如`Student`和`Car`,隐藏内部详情,只通过`get/set`方法交互。封装提升代码清晰度,便于管理和保护安全性,就像整理工具箱,让每个功能一目了然,操作自如。
梦回故国楼台梦
13 4
梦回故国楼台梦
|
1天前
|
安全 Java 数据安全/隐私保护
一探 Java 封装究竟:为何它让代码更加“高大上”?
【6月更文挑战第16天】Java中的封装如城堡般迷人,它确保数据安全(如`UserInfo`类的私有属性),增强代码结构(如`Order`类的操作封装),并提升复用性与扩展性(如`Shape`类的抽象设计)。封装是打造高质量、易维护代码的关键,让代码既安全又高效。
梦回故国楼台梦
20 7
梦回故国楼台梦
|
2天前
|
安全 Java
Java 面向对象之旅:在封装的港湾中,找到代码的安宁。
【6月更文挑战第15天】封装是Java面向对象的核心,它保护了类的内部数据,如`Book`和`Student`类中的属性。通过设定私有成员和公共方法,代码更有序,防止直接访问导致的混乱。封装提供了一种控制数据交互的方式,确保安全,如`setGpa()`方法验证输入。这使得代码结构清晰,如同港湾中的船只,井然有序,赋予编程过程美感和安全感。在面向对象的旅程中,封装是我们的庇护所,助力我们构建更美好的程序世界。
梦回故国楼台梦
11 1
梦回故国楼台梦
|
2天前
|
算法 Java
Java关键字与保留字:如何正确使用,让你的代码“飞”起来!
【6月更文挑战第15天】Java编程中,关键字如"class"、"int"用于特定语法,保留字可能未来成为关键字。理解其含义和用法至关重要,避免用作标识符以防止未来冲突。正确使用如"for"控制循环,优化代码能提升效率,使程序运行更流畅。避免保留字,如"goto"、"const",查阅文档确保合规性。通过代码优化,让程序效率更高,代码飞行在技术的云端。
梦回故国楼台梦
24 11
梦回故国楼台梦
|
3天前
|
Java 编译器 程序员
【实战攻略】Java高手教你如何灵活运用if-else和switch,提升代码效率!
【6月更文挑战第14天】本文探讨了Java中if-else和switch语句的巧妙运用,通过示例展示了如何提升代码效率和可读性。通过使用Map重构if-else结构,使代码更简洁易维护;利用switch处理枚举类型,实现清晰的代码结构。在性能方面,switch在选项少时占优,而现代JIT编译器优化后的if-else适用于大规模字符串比较。理解并灵活运用这两种控制结构,能助你在Java编程中写出高效、易读的代码。
梦回故国楼台梦
6 0
梦回故国楼台梦
|
3天前
|
算法 Java 程序员
【程序员必看!】掌握这招if-else,你的Java代码将瞬间高大上!
【6月更文挑战第14天】本文介绍了if-else语句在程序设计中的重要性,通过示例展示其基本语法和复杂用法,如嵌套和else-if链。强调了避免过度嵌套、逻辑运算符的使用、保持一致性及添加注释等提升代码质量的技巧。学习并实践这些技巧,将使你的Java代码更优雅、高效,助你成为编程艺术家。
梦回故国楼台梦
6 0
JavaPub
|
3天前
|
Java
ElasticSearch启动报错 java version is an early-access build ,only use release builds【已解决】
ElasticSearch启动报错 java version is an early-access build ,only use release builds【已解决】
JavaPub
8 0
JavaPub
|
4天前
|
Java
启动Java 程序脚本 版本二
启动Java 程序脚本 版本二
JavaPub
12 0
JavaPub
|
4天前
Failed to bind properties under ‘logging.level‘ to java.util.Map java.lang.String, java.lang.String
Failed to bind properties under ‘logging.level‘ to java.util.Map java.lang.String, java.lang.String
JavaPub
4 0

热门文章

最新文章

  • 1
    后浪拍前浪-覆写父类方法 | 带你学《Java面向对象编程》之三十九
  • 2
    【Java入门提高篇】Day8 Java内部类——匿名内部类
  • 3
    Java设计模式之代理模式
  • 4
    排查Java高CPU占用原因
  • 5
    第三章 AOP 通过Java API创建增强
  • 6
    【Java数据结构的实现】之系列二栈的介绍
  • 7
    java中迭代器应用
  • 8
    Java NIO -- 通道 Channel
  • 9
    2013编程之美资格赛之树上的三角形(Java实现)
  • 10
    Java数字图像处理基础知识 - 必读
  • 1
    js开发:请解释什么是ES6的模板字符串(template string),并给出一个示例。
    22
  • 2
    【Java】String类常用方法总结
    24
  • 3
    CMake String函数:如何巧妙地在cmake中操作字符串
    235
  • 4
    C++ std::string类的使用
    24
  • 5
    深入C#中的String类
    14
  • 6
    Java StringBuffer 类
    8
  • 7
    Java String split()方法详细教程
    33
  • 8
    【C++】——string的功能介绍及使用
    52
  • 9
    String类及相应的字符串操作方法
    75
  • 10
    Python系列(16)—— string类型转float类型
    43
  • 相关课程

    更多
  • Java面试疑难点解析 - 面试技巧及语言基础
  • Java面试疑难点解析 - Java Web开发
  • Java编程入门
  • Java面向对象编程
  • Java高级编程
  • 相关电子书

    更多
  • Spring Cloud Alibaba - 重新定义 Java Cloud-Native
  • The Reactive Cloud Native Arch
  • JAVA开发手册1.5.0
  • 相关实验场景

    更多
  • 基于ECS搭建Java Web开发环境
  • 使用Aliyun Java Intializr进行项目开发
  • 搭建Java Web开发环境
  • 部署基于Dragonwell的Java运行环境
  • 阿里云平台上进行Java程序的编译与运行
  • 下一篇
    2024年阿里云免费云服务器及学生云服务器申请教程参考

    深圳SEO优化公司迪庆关键词排名包年推广报价德阳网站建设哪家好济源建站公司青岛网站优化软件哪家好菏泽SEO按天计费价格广州seo辽阳百姓网标王推广价格天水SEO按天扣费公司平顶山英文网站建设哪家好内江百度关键词包年推广公司阜新SEO按天收费哪家好济宁网站排名优化推荐铜仁百度网站优化排名公司鹤岗百度竞价平湖营销型网站建设坂田网站优化按天扣费推荐文山百度标王价格抚顺网站优化按天收费报价资阳百姓网标王推广哪家好龙华建站价格白城网站优化按天计费菏泽seo优化多少钱观澜seo网站推广哪家好晋城百度网站优化龙岩网站关键词优化哪家好徐州SEO按天扣费推荐平湖关键词按天计费公司随州关键词按天计费哪家好晋城关键词按天计费公司玉林网站优化按天收费价格歼20紧急升空逼退外机英媒称团队夜以继日筹划王妃复出草木蔓发 春山在望成都发生巨响 当地回应60岁老人炒菠菜未焯水致肾病恶化男子涉嫌走私被判11年却一天牢没坐劳斯莱斯右转逼停直行车网传落水者说“没让你救”系谣言广东通报13岁男孩性侵女童不予立案贵州小伙回应在美国卖三蹦子火了淀粉肠小王子日销售额涨超10倍有个姐真把千机伞做出来了近3万元金手镯仅含足金十克呼北高速交通事故已致14人死亡杨洋拄拐现身医院国产伟哥去年销售近13亿男子给前妻转账 现任妻子起诉要回新基金只募集到26元还是员工自购男孩疑遭霸凌 家长讨说法被踢出群充个话费竟沦为间接洗钱工具新的一天从800个哈欠开始单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#中国投资客涌入日本东京买房两大学生合买彩票中奖一人不认账新加坡主帅:唯一目标击败中国队月嫂回应掌掴婴儿是在赶虫子19岁小伙救下5人后溺亡 多方发声清明节放假3天调休1天张家界的山上“长”满了韩国人?开封王婆为何火了主播靠辱骂母亲走红被批捕封号代拍被何赛飞拿着魔杖追着打阿根廷将发行1万与2万面值的纸币库克现身上海为江西彩礼“减负”的“试婚人”因自嘲式简历走红的教授更新简介殡仪馆花卉高于市场价3倍还重复用网友称在豆瓣酱里吃出老鼠头315晚会后胖东来又人满为患了网友建议重庆地铁不准乘客携带菜筐特朗普谈“凯特王妃P图照”罗斯否认插足凯特王妃婚姻青海通报栏杆断裂小学生跌落住进ICU恒大被罚41.75亿到底怎么缴湖南一县政协主席疑涉刑案被控制茶百道就改标签日期致歉王树国3次鞠躬告别西交大师生张立群任西安交通大学校长杨倩无缘巴黎奥运

    深圳SEO优化公司 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化