Java String
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
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
- 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
Add Comment
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.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Read a file having a list of telnet commands and execute them one by one using Java
Open a .docx file and show content in a TextArea using Java
Step by Step guide to setup freetts for Java
Of Object, equals (), == and hashCode ()
Using the AWS SDK for Java in Eclipse
DateFormat sample program in Java
concurrent.Flow instead of Observable class in Java
Calculator application in Java
Sending Email from Java application (using gmail)
Comments