Java String

By: Emiley J Viewed: 153702 times  Printer Friendly Format    


The literal meaning of the word String means 'something that is twisted together to form a thin length'. The verbr String means 'hang something so that it streches in a long line'. In computer programming languages, String means a datatype that is used to store some characters. A Java String is a stream of characters grouped together serially. An example String is a word "Hello" or a phrase "Hello World".

A String can be a NULL character, a single character or a word or a phrase, or an email or an entire book. Almost all computer languages store Strings similarly and represented between quotes and are a sequence of characters (Char). For example, in Java you can create a String as below

String str="Hello";

A Java String is stored in memory as below. The chars in a String are represented by index numbers. The left most character index is 0.

H e l l o
0 1 2 3 4

In the above String "Hello", the first character H is at index 0 and e is at index 1 and so on.

Since the index of a String always starts at 0, the length of a String is always 1 more than the index of the last character. In Java a String is a Class that is in the Java basic package java.lang. Therefore, when you want to use a String in your Java program, you don't need to import specifically any particular package. Since a Java String is a Class, it comes with many useful methods that you can use to manipulate Java Strings.

Java Manipulation

A very useful and a commonly used method is length(). Quite often when you deal with Java Strings you will need to know its length. In other words you want to know how many characters are there in the String. You can do this by calling its length() method as below.

String a = "Hello";
int len = a.length();  // len is 5

Here is a list of useful sample programs that will help you in handling Java Strings in your programs



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial