加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java String Class Example--reference

发布时间:2020-12-14 06:17:39 所属栏目:Java 来源:网络整理
导读:reference:http://examples.javacodegeeks.com/core-java/lang/string/java-string-class-example/ 1. Introduction In this example we are going to discuss about the basic characteristics of?.? String ?is probably one of the most used types in J
</table>

"Hello World"?is called a?String?literal. In a Java program,everything between two double quotes is a?String?literal. Literals are implemented as instances of String class. As you can see,you can conveniently initialize a?String?Object like a primitive type,e.g?int i = 0;.

There is no need to do:

1? </tr></table>

There is a difference between these two initialization methods,although the result is the same : A?String?with value “Hello World”. But more on that in just a bit.

For now,here is a simple?main?with the most important?String?API methods:

StringClassExample.java

001? </tr></table>

002</tr></table>

003?? </tr></table>

004</tr></table>

005??? </tr></table>

006 </tr></table>

007 </tr></table>

008 </tr></table>

009</tr></table>

010 </tr></table>

011? </tr></table>

012? </tr></table>

013 </tr></table>

014</tr></table>

015 </tr></table>

016 </tr></table>

017? </tr></table>

018 </tr></table>

019</tr></table>

020 </tr></table>

021 </tr></table>

022</tr></table>

023 </tr></table>

024 </tr></table>

025 </tr></table>

026</tr></table>

027 </tr></table>

028 </tr></table>

029 </tr></table>

030</tr></table>

031 </tr></table>

032 </tr></table>

033</tr></table>

034 </tr></table>

035 </tr></table>

036 </tr></table>

037</tr></table>

038 </tr></table>

039? </tr></table>

040 </tr></table>

041</tr></table>

042 </tr></table>

043 </tr></table>

044 </tr></table>

045</tr></table>

046 </tr></table>

047 </tr></table>

048 </tr></table>

049</tr></table>

050 </tr></table>

051 </tr></table>

052 </tr></table>

053 </tr></table>

054</tr></table>

055 </tr></table>

056? </tr></table>

057 </tr></table>

058</tr></table>

059 </tr></table>

060 </tr></table>

061</tr></table>

062 </tr></table>

063 </tr></table>

064</tr></table>

065 </tr></table>

066 </tr></table>

067 </tr></table>

068</tr></table>

069 </tr></table>

070 </tr></table>

071 </tr></table>

072 </tr></table>

073</tr></table>

074 </tr></table>

075 </tr></table>

076 </tr></table>

077</tr></table>

078 </tr></table>

079</tr></table>

080 </tr></table>

081 </tr></table>

082 </tr></table>

083</tr></table>

084 </tr></table>

085 </tr></table>

086 </tr></table>

087 </tr></table>

088</tr></table>

089 </tr></table>

090? </tr></table>

091 </tr></table>

092 </tr></table>

093</tr></table>

094 </tr></table>

095 </tr></table>

096</tr></table>

097 </tr></table>

098 </tr></table>

099? </tr></table>

100</tr></table>

101 </tr></table>

102 </tr></table>

103</tr></table>

104 </tr></table>

105</tr></table>

106</tr></table>

This is the output of the above program:

str1:Hello World
str2:Hello
str3:Hello World
11
Length: 7
Substring :llo Worl
Literal Substring :bcd
Char array : [l,l,o]
Index of 'W':6
Index of 'orld':7
LAST Index of 'l':9
LAST Index of 'l':3
Character at position 5: 
abcefafa
ABCASIPASC
Hell0 W0rld
    Java
Java
Java,is,great
Java,great,J,a,v,i,s,Contains :true
Equals :false
Equals ignore case:true
Compare:-23
false
true

From the above program is clear that Java designers decided to treat Strings somewhat differently from other Objects. For example you can initialize them like a primitive,e.g?String a="abc"?and you can concatenate two strings using?+?operator,like you would add twoints?(looks like overloading + operator in C++).

The?comparison attempts?section of the code might seem a little fuzzy,but it will get clear in the next section. What you should take away from it now,is that you SHOULD NEVER try to compare the contents of Strings using?==?operator. You are only comparing reference equality,not content equality. You MUST use?equals?or?equalsIgnoreCase.

3. Other characteristics of String objects

String?objects are immutable. This means that once a?String?is created,its contents cannot be changed. In the above example,every time we attempt to change its contents,e.g when concatenating,a new?String?object is created representing the result. Additionally,String class is final,so you cannot override its behavior.

Immutability was mostly chosen for security reasons and for performance. It also means that two different thread can share the same String and manipulate it as they want,not having to synchronize anything,because every time they make a change in the original string,a new one is created,while the old one remains untouched.

Now let’s see this :

1
2
3
4?
5
6
7

This outputs:

true
false

Literals are stored in a special place in memory,called a?String pool,of course in the form of?String?Objects. In that pool,a?Stringobject with value “abc” is only created and stored once. Any other?String?that gets the value “abc” (statically – hard coded) will reference the same?String?object. So,every time you create a?String?using a literal,the system will search that pool and checks if the value of the literal exists in an object of the pool. If it does,it sends back the reference to that matching object,if not it creates a new Object and stores it in the pool. So,?String?references,initialized with the same literals,will point to the same?String?object. This technique was used to save precious memory,as it shares as much common data as possible.

Now,you can also see another reason why Strings are immutable. Imagine thread A creating a local string “abc” and then a second thread B creating his own local string “abc”. These two threads will share the same String object… If String was mutable,then if A changed the string,the change would affect thread B,but in a meaningless (put catastrophic) way.

When creating a?String?using?new,you explicitly create a brand new object in the heap. This is also the case for non hard codedString?initialization,for example,if you are reading input?Strings?from a source. These?String?Objects will not be stored in the pool. Imagine that you create an application that has to hold addresses for users living in Greece. There are four million people living in Athens,so consider the massive waste of space should you store four million String objects with value “Athens”. In order to pool those non hard coded?Strings,there is an API method called?intern,and can be used like so:

01
02
03
04?
05
06
07
08
09
10

This will now output:

true
false
true

When calling intern,the system follows the same procedure as if we did?s3 = "abc",but without using literals.

But be careful. Before Java 7,this pool was located in a special place in the?,called PermGen. PermGen is of fixed size,and can only hold a limited amount of string literals. So,interning should be used with ease. From Java 7 onward,the pool will be stored in the normal heap,like any other object (making them eligible for garbage collection),in a form of a hashmap and you can adjust its size using?-XX:StringTableSize?option. You could create your own String pool for that matter,but don’t bother.

This is only one of the aspects that Java creators changed in the String class. Even more radical changes ware made,including the internal String representation (it now has two less static fields).

Download the Eclipse Project

This was an example of Java String Class. You can download the Eclipse Project of this example here :?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

reference:http://examples.javacodegeeks.com/core-java/lang/string/java-string-class-example/

1. Introduction

In this example we are going to discuss about the basic characteristics of?.?String?is probably one of the most used types in Java programs. That’s why Java provides a number of API methods that make?String?manipulation easy and efficient,straight out of the box.?Strings?are so important that even in the latest Java releases (including 7 and 8),several changes have been made to its class methods and its internal representation,improving it even further in terms of performance and security.??

2. String Class basic methods

A?String?is simply a sequence of characters. As a matter of fact,a?String?Object is backed by a?char?array. Consequently,it is not null terminated,like in C/C++.

Here is how you can create a?String

1 </tr>