Copyright

String Declaration in Java

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Java Integer: Max Value & Min Value

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Declaring a String in Java
  • 0:26 Declare a String Literal
  • 1:01 Creating a New Instance
  • 1:59 Checking for Equality
  • 3:12 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
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.

A string in Java is useful for holding text and numeric data. This lesson will cover the different methods used to declare a string variable in Java, providing working code examples.

Declaring a String in Java

A string in Java is a string of values (text and/or numbers). It's also a String, with a capital S, meaning it's an object. Therefore, both lines of code are equally valid. They create two different objects, and even though the values are the same we'll see why this is OK. Let's explore each option and what's happening in Java.


String employee1 = "Jane Austen"
String employee2 = new String("Jane Austen");


Declare a String Literal

The first option creates a literal string, meaning that employee1 is now 'Jane Austen.' When you create a literal string, Java looks in memory to see if there are other 'Jane Austen' strings, and if not, it creates a new one. If that string already exists, then it just re-uses that value from memory. For example, if you created an employee783 and set its value to 'Jane Austen', Java would share the literal string already in memory. This space in memory is called the constant pool. Let's look at the code again. The variable employee1 is set to a literal value.


String employee1 = "Jane Austen";


Creating a New Instance

The string declaration for employee2 still invokes the String class, but Java does this automatically, creating a reference to that String object behind the scenes. We can take another peek at the code, which creates a new instance of the String class.


String employee2 = "Jane Austen"


When you create an instance of the String class, you bypass this memory look-up. Instead, you create an instance of the String class. The variable employee2 can be 'Jane Austen'; so could employee352! They're separate objects. Therefore, if you create another instance of the String class, call it employee783, and give it a value of 'Jane Austen,' Java creates another instance; it doesn't draw from strings already in memory.

Finally, since this is a new instance of a class, you don't really need to provide a value for the string. In fact, you don't need one for the literal declaration, either. However, it's good practice to declare the strings as you need them. Therefore, it's important to give a value to a string when it's declared.

Checking for Equality

Here's the interesting part. We have two employees, employee1 and employee2. They're equal, right? Well, let's find out. We can add the following code to check if these two objects are the same:


String employee1 = new String("Jane Austen");
String employee2 = "Jane Austen";
if(employee1 == employee2) {
  System.out.println("equal");
} else {
  System.out.println("Not Equal");
}


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