Copyright

Java: String Data Type

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

In Java, strings are not chased by cats. A string is a sequence, or string, or single characters. This lesson will define the string data type, and provide some examples of its use.

String

Like a string of pearls, a Java string is a sequence of single characters. The char data type represents a single character. The following figure shows the construction of a string hello that consists of the characters Hello World.


Java String Object


A string is also a class, which means that it has its own methods for working on strings. As we examine the syntax of using a Java string, we'll explore these methods in detail.

Syntax and Examples

Let's look at the syntax of declaring a string. Because a string is a class, when you declare a string, you are creating an instance of that class. Therefore, the keyword new is used to create a new instance. However, because the string is so heavily used in Java, this isn't required. In this lesson, we will use the full syntax in order to reinforce the object-oriented nature of Java.

Both declarations below are valid. We will use the first for all examples, however the second works also.

String hello = new String("Hello World!");

Since the string is used so heavily, this is also acceptable:

String myString = "Hey World";

For most text-type data being stored, you don't need to do anything more than simply wrap the string value in double quotes. However, there are times when you need to include other characters in the string value; characters that might be reserved. Let's look at how Java handles special characters in the String data type/class.

Special Characters

Even though we can create a string with any type of character, there are some special characters that need special handling. For example, what if you need to include a double-quote, a tab, or carriage return in the string? Java, like most computer languages, there are special codes called escape codes. They are:


Java String Escape Chars


As used in the code, we have:

String hello = new String("Hello \n World!");
System.out.println(hello);

In the previous code, we've added a return, and the output looks like:


Java Escape Char Output


Concatenation

Yes, you can add strings together. Java is smart enough to know that if you use the + operator between strings, you mean to combine their values. Instead of addition, the + acts to concatenate, that is join together, the string sequences. Let's join the Hello World string to It's a Nice Day!

String hello = new String("Hello World!");
String niceDay = new String("It's a nice day!");
String output = hello + " " + niceDay;

The output will be Hello World! It's a nice day! You'll notice we used the concatenate to add a space between the exclamation point and the next word; otherwise the text would have run together.

String Methods

As we mentioned before, the Java string data type is also a class, and so it has its own methods. These options include finding string length, converting to upper or lower case, trimming text, or replacing text within the string. Let's take a look at some of these and show some code samples. In order to invoke the method, type the name of the variable followed by a period, and the method name.

String Length

This is perhaps the most-used method, because it can be used within other string manipulation routines. Once you know the length of the string, you can take out pieces of the string, or even try to enforce certain limitations on them. The method for finding the length is the length() method:

String hello = new String("Hello World!");
int len = hello.length();
System.out.println(len); //12

In the previous example, an integer, len, was defined to return the length of the string. When the code runs, it will display a 12, since there are 12 characters in the string, counting the space.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support