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


原创地址:   http://www.cnblogs.com/Alandre/  (泥沙砖瓦浆木匠),需要转载的,保留下! Thanks

Although the world is full of suffering , it is full also of the overcoming of it.  -Hellen Keller   

相信自己看得懂就看得懂了,相信自己能写下去,我就开始写了.其实也简单—泥沙砖瓦浆木匠

Written In The Font

Three pieces[52-3]:

         52.Suggestion:Use the String direct value for the assignment [推荐使用String直接量赋值]

         54. How to use the String , StringBuffer,StringBuilder               [正确的使用String , StringBuffer,StringBuilder ]

         55.Easy Time:Pay attention to the address of String                   [注意字符串的位子]

         57.Complex string manipulation using regular expressions      [复杂字符串操作使用正则表达式]

     

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.

                                  image

 

Write to Reader

based on image.

Thank u!

道可道也,非恒道也。名可名也,非恒名也。无名,万物之始也;有名,万物之母也。故恒无欲也,以观其眇;恒有欲也,以观其所徼。两者同出,异名同谓。玄之又玄,众眇之门。
程序员泥瓦匠
关注 关注
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
三个“清洁代码”技巧将使您的开发团队效率提 50%
禅与计算机程序设计艺术
10-19 9344
如果我必须从软件工程最佳实践的圣经“清洁代码”中选择一点,我们就是作者。阅读与写作的时间比例远远超过 10:1。再读一遍。慢慢地。作为编写代码的一部分,我们不断地审查旧代码。由于我们花费大量时间阅读旧代码,因此对我们和我们的团队来说,投入一点使其简单易懂可能是个好主意。尽管我建议阅读整本书,但我发现以下三个想法很容易获胜,它们可以极大地提团队的生产力、效率,最重要的是——减少挫败感:使用好名字...
C++ How to Program, 10th Edition
01-06
The early objects approach gets readers thinking about objects immediately–allowing them to more thoroughly master the concepts. Emphasis is placed on achieving program clarity and building well-...
现代大学英语精读第二版(第四册)学习笔记(原文及全文翻译)——9A - A String of Beads(一串珍珠项链)
预见未来to50的专栏
11-01 6264
Unit 9B - A String of Beads A String of Beads W.Somerset Maugham "What a bit of luck that I'm placed next to you," said Laura, as we sat down to dinner. "For me," I replied politely. "That remains to be seen. I particularly wanted to have the chance o
Java岗大厂面试百日冲刺 - 日积月累,每日三题【Day2】 —— Redis篇1
陈哈哈的菜园子
06-02 2万+
百日闭关修炼,每日三道频面试题。一起冲进大厂!看什么看?快上车!今天是Reds,香
Java ——guava-libraries
半夏陌殇Earth
08-27 3321
Java 8 jar包 链接:https://pan.baidu.com/s/1bpYIPcz 密码:0ob7  Google前日开源了其内部Java项目所用的Java库,并取名为Guava库(guava是番石榴的意思)。Guava库旨在提供核心JDK 1.6 API所没有的常用功能。Guava包括三个包: ◆com.google.common.primitives(原始值)
如何将字符串转换成Java中的int类型?
dearbaba_8520的博客
05-20 1万+
How can I convert a String to an int in Java? 如何将字符串转换成Java中的int类型? My String contains only numbers
Java - Guide to Java Reflection
热门推荐
了解➔熟悉➔掌握➔精通
10-15 3万+
分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net 1. Overview In this article we will be exploring java reflection, which allows us to inspect or/and modify runtime attribute...
String.intern in Java 6, 7 and 8 – string pooling (在Java 6,7和8中的String.intern - 字符串常量池化)
FIRE_TRAY的Android之路
04-30 4519
String.intern in Java 6, 7 and 8 – string pooling (在Java 6,7和8中的String.intern - 字符串常量池化)原文链接:http://java-performance.info/string-intern-in-java-6-7-8关键点 java 6中字符串常量池存储于永久代区中,由于此区域大小固定,因此String.intern的
编写可读性代码的艺术
The magic of fingertips
07-16 1万+
原文地址: http://www.cnblogs.com/greeenplace/articles/4667830.html PDF文件下载地址: http://download.csdn.net/detail/aradin/8440247 译者序 在做IT的公司里,尤其是软件开发部门,一般不会要求工程师衣着正式。在我工作过的一些环境相对宽松的公司里,很多程序员的衣着连得
Rust的String和str之间有什么区别?
xfxf996的博客
06-16 3291
Why does Rust have String and str ? 为什么Rust会有String和str ? What are the differences between String a
javacv-platform-1.3.3-src
08-12
To learn how to use the API, since documentation currently lacks, please refer to the Sample Usage section below as well as the sample programs, including two for Android (FacePreview.java and Record...
Git-2.21.0-64-bit.zip
10-22
* Auto-detect how to tell HP-UX aCC where to use dynamically linked libraries from at runtime. * "git mergetool" and its tests now spawn fewer subprocesses. * Dev support update to help tracing ...
php.ini-development
05-08
sets foo to the string 'None' ; If you use constants in your value, and these constants belong to a ; dynamically loaded extension (either a PHP extension or a Zend extension), ; you may only use ...
Java邮件开发Fundamentals of the JavaMail API
01-15
protocols will help you understand how to use the JavaMail API. While the API is designed to be protocol agnostic, you can't overcome the limitations of the underlying protocols. If a capability isn...
毕业设计MATLAB_执行一维相同大小矩阵的QR分解.zip
05-27
毕业设计matlab
ipython-7.9.0.tar.gz
最新发布
05-27
Python库是一组预先编写代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以效率、质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
debugpy-1.0.0b3-cp37-cp37m-manylinux2010_x86_64.whl
05-27
Python库是一组预先编写代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以效率、质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
libaacs-devel-0.10.0-1.mga8.i586.rpm
05-27
rpm -i xx.rpm 只要报错遇到aacs的可以看看架构是否一致
Java编写程序,求1!-2!+3!-4!+5!-6!
11-18
以下是Java编写程序,求1!-2!+3!-4!+5!-6!的代码: ```java public class FactorialSum { public static void main(String[] args) { int n = 6; int sum = 0; int sign = 1; int factorial = 1; for (int i =...

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
写文章

热门文章

  • 代码整洁 vs 代码肮脏 99733
  • Spring Boot 2.0 WebFlux 上手系列课程:快速入门(一) 30951
  • Spring Boot Dubbo applications.properties 配置清单 14104
  • 项目ITP(一) 二维码 12250
  • Spring Boot 整合 Thymeleaf 完整 Web 案例 10375

分类专栏

  • Java 70篇
  • Java - 基础 6篇
  • Java - 容器和泛型 5篇
  • Java Web - 项目经验 6篇
  • 读书笔记-《The C Programming Language》
  • JavaEE 网络Http相关 4篇
  • Java - 文件 I/O 7篇
  • Java - 文件 I/O源码 3篇
  • Java EE Servlet & JSP 1篇
  • Java 设计模式 2篇
  • SpringBoot 24篇
  • 工作贴 14篇
  • TensorFlow
  • Machine Learning 1篇
  • Spring Data 1篇

最新评论

  • GPTs Hunter 是什么?

    m0_59698348: 这家好像停止更新了?我看gptsapp.io的数量已经40w了,这家才10多万

  • GPT Zero 是什么?

    CSDN-Ada助手: 恭喜你这篇博客进入【CSDN每天值得看】榜单,全部的排名请看 https://bbs.csdn.net/topics/617807594。

  • GPTs Hunter 是什么?

    爱折腾的黄馒头: 可以理解为Gpts的第三方应用市场?

  • Midjourney:一步一步教你如何使用 AI 绘画 MJ

    2301_77922902: 付款必须用信用卡吗,换借记卡可以吗

您愿意向朋友推荐“博客详情页”吗?

  • 强烈不推荐
  • 不推荐
  • 一般般
  • 推荐
  • 强烈推荐
提交

最新文章

  • Elevate Your Lead Generation Game with Maps Scraper AI
  • 让创意在幻觉中肆虐: 认识Illusion Diffusion AI
  • AI 图像自动补全 Uncrop 工具介绍
2024年5篇
2023年38篇
2022年8篇
2021年5篇
2020年38篇
2019年55篇
2018年3篇
2017年18篇
2016年15篇
2015年35篇
2014年76篇

目录

目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值

哆哆女性网心理学书籍梦的解析周公解梦梦见看社火商丘市男性医院上海松江新房铁臂阿童木游戏二维码营销推广的败走汉津口读后感黄姓起名好听又顺口畜牧行业个体户起名大全永城市属于起一个商标名是商标品牌起名字吗姓石男孩起名两个字成都网站建设公司推荐seo班培训班冯姓起名骆驼祥子读后感1000字服务网站制作公司深爱SEOseo是什么行业发展甘姓起名有意义哪里算命的好武汉营销型网站制作建设公司起名大全起名字大全配送公司池州网站优化网站建设个人免费周易生辰八字my电影天堂厦门网站ui设计淀粉肠小王子日销售额涨超10倍罗斯否认插足凯特王妃婚姻不负春光新的一天从800个哈欠开始有个姐真把千机伞做出来了国产伟哥去年销售近13亿充个话费竟沦为间接洗钱工具重庆警方辟谣“男子杀人焚尸”男子给前妻转账 现任妻子起诉要回春分繁花正当时呼北高速交通事故已致14人死亡杨洋拄拐现身医院月嫂回应掌掴婴儿是在赶虫子男孩疑遭霸凌 家长讨说法被踢出群因自嘲式简历走红的教授更新简介网友建议重庆地铁不准乘客携带菜筐清明节放假3天调休1天郑州一火锅店爆改成麻辣烫店19岁小伙救下5人后溺亡 多方发声两大学生合买彩票中奖一人不认账张家界的山上“长”满了韩国人?单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#青海通报栏杆断裂小学生跌落住进ICU代拍被何赛飞拿着魔杖追着打315晚会后胖东来又人满为患了当地回应沈阳致3死车祸车主疑毒驾武汉大学樱花即将进入盛花期张立群任西安交通大学校长为江西彩礼“减负”的“试婚人”网友洛杉矶偶遇贾玲倪萍分享减重40斤方法男孩8年未见母亲被告知被遗忘小米汽车超级工厂正式揭幕周杰伦一审败诉网易特朗普谈“凯特王妃P图照”考生莫言也上北大硕士复试名单了妈妈回应孩子在校撞护栏坠楼恒大被罚41.75亿到底怎么缴男子持台球杆殴打2名女店员被抓校方回应护栏损坏小学生课间坠楼外国人感慨凌晨的中国很安全火箭最近9战8胜1负王树国3次鞠躬告别西交大师生房客欠租失踪 房东直发愁萧美琴窜访捷克 外交部回应山西省委原副书记商黎光被逮捕阿根廷将发行1万与2万面值的纸币英国王室又一合照被质疑P图男子被猫抓伤后确诊“猫抓病”

哆哆女性网 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化