Java String Class
Java String Class
This article is part of a tutorial.
Tutorial Index page - Java Data Modelling
Introduction
The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.
Class Name : String
Package Name : java.lang
API Doc Location : http://java.sun.com/javase/6/docs/api/java/lang/String.html
Things to know about the String class
- This class represents a sequence of characters (char). characters (deliberately not using a capital C) represented by the 'char' keyword is one of the 8 primitive data types pre-defined in the Java programming Java.
For example 'a' , 'j', 'a' and 'v' are just characters and are represented by the primitive char data type. If we want to order these characters as 'j', 'a', 'v', 'a' to form a word 'java' and want to hold these ordered characters, String is the class to be used.
- The String class is a blueprint for creating String objects. These String objects have a state which is the current set of ordered characters it holds. For the example above, the state is 'j', 'a', 'v', 'a'. The String objects have a defined behaviour using which they interact with other objects and values. For example, a String object - 'j', 'a', 'v', 'a' can be appended (concatenate) with another String object - 'w', 'o', 'r', 'l', 'd' to form a resulting String object - 'j', 'a', 'v', 'a' 'w', 'o', 'r', 'l', 'd'.
- A String object can be defined/created without using any constructor provided by the String class. String objects can be defined by a string literal which is a series of characters enclosed by double quotes. The string literal for the set of characters 'j', 'a', 'v', 'a' is "java". There fore we can define/create a String object by just doing
String javaWord = "java";
This is possible because behing the scenes, the Java compiler will construct a String object using the default constructor for every string literal it encounters in the code.
- The String class is immutable. An immutable object is an object whose state cannot be modified after it is created. As said before the state of a String object is the ordered set of characters it holds. If we apply a trim function (<Stirng>.trim()) to a String object which has some trailing white-space characters
String javaWord = "java ";
we would generally expect the state (value) of the String object - javaWord to be over-written or change to a value that has no leading or trailing whitespaces. Instead of this, the String class being mutable will create a brand new object with the state (value) "java" in it. This also means that the state of the first object which had the trailing spaces in it will never change at all. An references pointing to this String object will always be valid and constant throughout the runtime irrespective of what operations are carried over on that String object.
This is a very important behaviour to be kept in mind while dealing with Strings as using String objects carelessly in an application would bloat the application with a lots of object creations and may cause a noticible performance impact to the application.
- The String class has been around since Java version 1.1 (released on February 19, 1997). Currently at version Java 1.6 the String class has...
- 15 Constructors out of which 2 are deprecated.
- 13 Public Static Methods
- 52 Public Methods out of which 1 method is deprecated.
Feedback or Questions?
We welcome feedback and questions and will try our best to attend to it as quickly as possible!
Please note that you would have to register before you can post in our forums and this is purely to guard us from the spam-bots. Be assured that we do not send spam mails and our website registration only takes minutes.
