Java String Class

This chapter explores the String class and demonstrates the methods this class has got with working examples.

Introduction

The StringThe String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Below are all the articles related to 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.

15 Constructors out of which 2 are deprecated.

13 Public Static Methods

52 Public Methods out of which 1 method is deprecated.

Use the links below to see other articles in the same category.
Tags: 
Java
Tags: 
Strings
This article is part of a book. Use the below links to navigate through the book.

Comments

this is very helpful for

this is very helpful for me.do post more such tuts like these
creatine

Post new comment

  • You can enable syntax highlighting of source code with the following tags: <code>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options

Java String Class | Our website now yours! - Currenlty Java focussed.

Java String Class

This chapter explores the String class and demonstrates the methods this class has got with working examples.

Introduction

The StringThe String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Below are all the articles related to 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.

15 Constructors out of which 2 are deprecated.

13 Public Static Methods

52 Public Methods out of which 1 method is deprecated.

Use the links below to see other articles in the same category.
Tags: 
Java
Tags: 
Strings
This article is part of a book. Use the below links to navigate through the book.

Comments

this is very helpful for

this is very helpful for me.do post more such tuts like these
creatine

Post new comment

  • You can enable syntax highlighting of source code with the following tags: <code>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options