By: Emiley J in Java Tutorials on 2012-11-27
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
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.
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.
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
- String Concatenation using Java
- String Array in Java
- Palindrome String in Java
- concat(), replace(), and trim() Strings in Java
- String Class Constructors in Java
- String Conversion and toString() in Java
- Count number of vowels, consonants and digits in a String in Java
- indexOf() and lastIndexOf() in Java
- How to use equals() and equalsIgnoreCase() in Java
- Converting Numbers to and from Strings using Java
- Using toLowerCase() and toUpperCase() in Java
- valueOf() in Java
- Using StringTokenizer in Java
- Using substring() in Java
- compareTo() in Java
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in Java )
Latest Articles (in Java)
- Data Science
- React Native
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews