Java String

By: Emiley J Emailed: 1728 times Printed: 2329 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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

Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment

Very good article on string , please also mentions the difference methods of creating string object .

View Tutorial          By: Subham at 2014-04-29 18:01:04

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Emiley J
Password must include both numeric and alphabetic characters - Magento
What is Hadoop?
Returning multiple values from a web service
Tomcat and httpd configured in port 8080 and 80
Java Webservices using Netbeans and Tomcat
Java WebService connected to Database
How to Deploy a Java Web Service
Call a webservice in Java
Java WebService - Create your first web service in Java
package javax.jws does not exist
Getting Started with Android
HTML5 Location - getCurrentPosition() in HTML5
HTML5 Canvas - Using Canvas in HTML5
HTML5 - Introduction
HTML5 Video - Handling video in HTML5

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
How to Send SMS using Java Program (full code sample included)
XML and Java - Parsing XML using Java Tutorial
Method Overriding in Java
Method Overloading (function overloading) in Java
The Basic Structure of a Simple Java program
FileReader and FileWriter example program in Java
What is Java?
Vector example in Java
Stack example in Java - push(), pop(), empty(), search()
for loop in java
How to use ArrayList in Java
Read from a COM port using Java program
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
arraycopy example in Java
Most Emailed Articles (in Java)
Vector in Java - Sample Program
Applet Notinited : Applet xxxxxxxxxxxx notinited
Basics of Exception Handling in Java
Tomcat and httpd configured in port 8080 and 80
Calculate average sale of the week in Java
LinkedList in Java
Taking the size of an Array at runtime & generate random numbers to populate the Array
Getting Started with Java
Configuring a DataSource in Tomcat
What is JasperReports?
java.lang.reflect package
instanceof operator example in Java
The Unary Operators example in Java
Operator Precedence in Java
toRadians() and toDegrees() Sample program in Java