[Java Plasterer] Java Components 4:Java String,How to use them?

Reprint it anywhere u want.

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

 

Written In The Font

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:

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

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.

?


1

2

3

4

5

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.

 

How to use the String , StringBuffer,StringBuilder

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.

for example:

?


1

2

3

4

5

6

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.

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

for example:

?


1

2

3

4

5

6

7

8

9

10

11

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.

 

Write to Reader

Thank u!

时间: 2024-07-29 07:34:32

[Java Plasterer] Java Components 4:Java String,How to use them?的相关文章

[Java 泥水匠] Java Components 之一:Java String (肯定有你不懂的)

1.2 你好 String         怀着初次见你的心情,泥瓦匠和你一起打开JDK1.7文档.我最近想写一些关于JDK1.7的理解,都知道JDK8出来了,新特性我准备下阶段有空学习.         泥瓦匠想说,阅读E文文档有利于体会原汁原味.但毕竟国内大牛翻译的很不错了,咱们不加评判,喜欢哪种自己挑.能抓老鼠,能解决实际项目,适应业务环境的就是你学到了.请看下面的小例子: 清单1.1 String abc = "abc"; char data[] = {'a','b','c'}

[Java Plasterer] Java Components 3:Java Enum

Written In The Font   When we to set some constants for projects, we always use 'public static final'to set Int or String constants.Or sometimes,we can also set the paramters in properties.When the project starts,we can get the properties to use them

JAVA学习(六):JAVA中的继承及其常见问题分析

JAVA中的继承及其常见问题分析 1.JAVA中继承的定义 JAVA中,类的继承是通过扩展其他类而形成新类来实现的,原来的类称为父类(Super Class)或基类,新的类称为原来类的子类或派生类.在子类中,不仅包含了父类的属性和方法,还可以增加新的属性和方法,从而使得父类的基本特征可被所有子类对象共享. 注:类的继承并不改变类成员的访问权限,也就是说,如果父类的成员是公有的.被保护的或默认的,它的子类仍具有相应的这些特性. /**********************************

JAVA学习(九):JAVA多线程编程

本文详细解释JAVA多线程编程,首先对进程和线程做了区别,其次介绍线程的两种实现方式,即继承Thread类和实现Runnable接口,然后讨论了线程常用的方法和优先级,最后介绍了线程的同步和死锁以及线程的生命周期.   1.进程与线程的区别与联系 进程:是应用程序的运行实例,是应用程序的一次动态执行.进程是由进程控制块.程序段和数据段3部分组成的.进程是操作系统进行资源分配的单位. 线程:是进程中的一个实体,其本身依靠程序进行运行,是程序中的顺序控制流,只能使用分配给程序的资源和环境.线程是被系

JAVA学习(四):Java流程控制语句(顺序结构、if条件语句、switch条件语句、循环语句与跳转语句)

Java流程控制语句 本博文将对JAVA中组成各种结构的控制语句进行重点介绍,主要内容是区别于C/C++中的流程控制语句的重点解析. 1.顺序语句 顺序结构中的语句可分为:表达式语句.空语句和复合语句三大类. 注: (1).空语句主要用来作为空循环体,其语法格式如下: : //其实就是一个分号 执行一个空语句,就是将控制转到该语句的结束点.这样,如果空语句是可以到达的,则空语句的结束点也是可以到达的. (2).复合语句又称为语句块,若语句块是空的,则控制转到语句块的结束点. 2.if条件语句 J

JAVA学习(一):Java介绍及其平台、开发环境的配置与搭建

Java介绍及其平台.开发环境的配置与搭建 1.Java的介绍 Java是一种面向对象的编程语言,具有跨平台.可移植.分布式.简单.可扩展等诸多特性.Java可以进行桌面应用.Web应用.分布式系统及嵌入式等应用程序的开发. Java包含3个不同版本,即J2ME/J2EE/J2SE(微缩版/企业版/标准版). 2.JDK的下载与安装.配置 (1).JDK下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html

java scoket http请求-求教:java socket编程接受HTTP请求出问题

问题描述 求教:java socket编程接受HTTP请求出问题 自己用Java scoket模拟编写接受http请求服务器,在浏览器中输入1次url地址,浏览器却会向服务器发送多次相同请求,这是什么原因? 以下是eclipse控制台打印的结果,可以看到浏览器发送了多次 /1234567890 请求,浏览器中地址栏只输入1次 http://localhost:8080/1234567890 eclipse控制台打印的结果: 初始化线程池,线程池中拥有10个线程可使用 初始化 base-param

面向Java开发人员的Ajax:Java对象序列化(1)

ajax|java对象 本文我们讨论 Ajax 开发的基础知识,但是将侧重于许多 Java Web 开发人员最关心的问题:为客户机生成数据. 多数 Java 开发人员已经把模型-视图-控制器(MVC)模式应用在他们的 Web 应用程序上.在传统的 Web 应用程序中,视图组件由 JSP 或者其他表示技术(例如 Velocity 模板)构成. 这些表示组件动态地生成全新的 HTML 页面,替代用户以前正在查看的页面,从而更新用户界面.但是,在 Java Web 应用程序使用 Ajax UI 的情况

JAVA学习(三):Java基础语法(变量、常量、数据类型、运算符与数据类型转换)

Java基础语法(变量.常量.数据类型.运算符与数据类型转换) 1.变量 Java中,用户可以通过指定数据类型和标识符来声明变量,其基本语法为: DataType identifier;  或  DataType identifier = value; 其中,DataType是变量类型,如int/string/char/double/boolean等:identifier是变量名称,即标识符:value就是声明变量的值. 注: a.标识符由数字0-9.大小写字母.下划线.美元符号.人民币符号以及