Java String object instruction

来源:互联网 发布:ruby 创建windows窗口 编辑:程序博客网 时间:2024/05/06 10:21

Java strings are objects designed to represent a sequence of characters. Because character strings are commonly used in programs, Java supports the ability to declare String constants and perform concatenation of Strings directly without requiring access to methods of the String class. This additional support provides for Java Strings allows programmers to use Strings in a similar manner as other common programming languages.

 

Create a String

 

The simplest method to create a String object is to enclose the string literal in quotes and assign the value to a String object. Creation of a String through assignment does not require the use of the new operator, for example:

String str=”abc”;

String language=”java”;

 

Alternatively, String objects can be created through constructors. The following constructors are supported:

Public String()

Constructs a new String that contains the same sequence of characters as the specified String argument.

 

Public String(char[] value)

Constructs a new String containing the same sequence of characters contained in the character array argument.

 

Java String is immutable object. For an immutable object you cannot modify any of its attribute’s values. Once you have created a java String object it cannot be modified to some other object or a different String. References to a java String instance is mutable. There are multiple ways to make an object immutable. Simple and straight forward at is to make all the attributes of that class as final. Java String has all attributes marked as final except hash field.

We all know java String is immutable but do we know why Java String is immutable? Main reason behind it is for better performance. Creating a copy of existing java String is easier as here is no need to create a new instance but can be easily created by pointing to already existing String. This save s valuable primary memory. Using String as a key for Hashtable guaranttes that there will be no need to for re-hashing because of object change. Using Java String in multithreaded environment is safe by itself and we need to take any precautionary measures.

 

String is class it is used to holding the array of characters. The difference between String and StringBuffers is String is immutable where as StringBuffer is mutable. Means we can not change the value of the String.

Actually in Java, Strings are handling in Pool format.

For example:

String str1=”xyz”;

This string(str1) will be stored into memory in particular address. When we defining new String with same array of characters like

String str2=”xyz”;

Now JVM will check in String Pool where there is same characters are available or not. If two Strings are match the JVM will refer str1 address to str2. Now the str1 and str2 referring the same characters in same memory location. This is a good idea for increasing memory efficiency.

When we change the str1 characters, the changes will be reflected to str2. Because both str1 and str2 variables are referring the same memory location. For avoiding this we are keeping String as immutable. However we can use StringBuffer if you want to do modifications in the string.

publicstaticvoid main(String[] args){

      String str1="abc";

      String str2="abc";

      System.out.println(str1);

      System.out.println(str2);

      System.out.println(str1.hashCode());

      System.out.println(str2.hashCode());

      System.out.println(str1.equals(str2));

      System.out.println(str1==str2);

      System.out.println(str1.hashCode()==str2.hashCode());

      str1="xyz";

      System.out.println(str1);

      System.out.println(str2);

      System.out.println(str1.hashCode());

      System.out.println(str2.hashCode());

      System.out.println(str1.equals(str2));

      System.out.println(str1==str2);

      System.out.println(str1.hashCode()==str2.hashCode());

}

Output:

abc

abc

96354

96354

true

true

true

xyz

abc

119193

96354

false

false

false

 

Differences between String and StringBuffer in Java

Main difference between String and StringBuffer is String is immutable while StringBuffer is mutable means tou can modify a StringBuffer object once you created it without creating any new object. This mutable property makes StringBuffer an ideal choice for dealing with Strings in Java. You can convert a StringBuffer into String by its toString() method.

原创粉丝点击