String class and its methods in Java

  • String Class
  • String Methods
  • Comparing Strings

String Class

public class Main {
public static void main(String[] args) {
String str = "apple";
System.out.println(str); // apple

str = "banana";
System.out.println(str); // banana

String name = new String("John");
System.out.println(name); // John
}
}
  • We can initialize a string directly using = operator or we can create using new keyword. The first version will use StringPool memory and the second option will create an object in the heap memory.
public class Main {
public static void main(String[] args) {
String str = "apple";
System.out.println(str); // apple

str.toUpperCase(); // original value will not get changed
System.out.println(str); // apple

str = str.toUpperCase(); // reassign new value
System.out.println(str); // APPLE
}
}
  • The string is an immutable class. This is the most important definition to remember. Immutable means that once created with some value we cannot change it. Just to be clear we can reassign the value of reference(as shown in the above example with str) but the original object that was created in the beginning will never get changed.
  • The string is thread-safe. All immutable classes are thread-safe because they are not modifiable.
  • If you are reassigning your string reference a great number of times in some infinite loop. Consider using StringBuilder instead and converting to String after the loop. Because every time you reassign your string you are creating a new object in the memory.
  • The string is the final class and we cannot extend the final classes.

String Methods

String str = "apple";
str = str.toUpperCase();
// it will return new string with all upper cases
System.out.println(str); // APPLE
String name = "John Doe";
name = name.toLowerCase();
// it will return new string with all lower cases
System.out.println(name); // john doe
String str = "apple";

// .length() method returns number of characters in the string int strLength = str.length();
System.out.println(strLength); // 5

str = "car "; // space is also the character System.out.println(str.length()); // 4
each char in the string has a position — index
//             012345678910  
String java = "Java is fun";
// Using substring we can get part of the string based on indexes
String javaFun = java.substring(8);
System.out.println(javaFun); // fun
System.out.println(java); // Java is fun
// 0123456789
String str = "SoftSkills";
str = str.substring(4);
System.out.println(str); // Skills

str = "my workspace";
str = str.substring(7);
System.out.println(str); // space

// 0123456789
String str1 = "SoftSkills";
str1 = str1.substring(0, 4);
System.out.println(str1); // Soft

String str2 = "Java is Cool!";
str2 = str2.substring(5, 7);
System.out.println(str2); // is

// 012345678
String str3 = "workspace";
str3 = str3.substring(0, 4);
System.out.println(str3); // work
//            01234567  
String str = "applpepp";
int index = str.indexOf("e");
System.out.println(index); // 4
System.out.println(str.indexOf("l")); // 3 System.out.println(str.indexOf("p")); // 1 System.out.println(str.lastIndexOf("p")); // 7 looking from back
//            0123  
String str = "home";
char ch = str.charAt(1);
System.out.println(ch); // o

// 012345
str = "Public";
char ch2 = str.charAt(5);
System.out.println(ch2); // c

// 0123456
str = "Pricing";
System.out.println(str.charAt(6)); // g
String city = "Alma-Ata"; 
System.out.println(city); // Alma-Ata
city = city.replace("-", " ");
System.out.println(city); // Alma Ata
String word = "Hello, World!";
word = word.replace("Hello", "Aloha");
System.out.println(word); // Aloha, World!
String str = "My name is John Doe";
String[] parts = str.split(" ");
for (String part : parts) {
System.out.println(part);
}
My
name
is
John
Doe
String topic = "Variables, assignment operator, arithmetic operator, input from terminal";

boolean isThere = topic.contains("input");
System.out.println(isThere); // true
System.out.println(topic.contains("java")); // false System.out.println(topic.contains("terminal")); // true

Comparing Strings

String str = "hello";
String str1 = "hello";
System.out.println(str == str1); // true
System.out.println(str.equals(str1)); // true
  • When we created str there are no string objects at all in the string pool so new string object with "hello" the value will get created.
  • When we created str1 there is one string object with the same value in the string pool already. It will just reuse the same object and made str1 point to it. That’s a reason == returns true — both references are pointing to the same object.
  • equals() compare the actual value of the string.
String str = new String("hello");
String str1 = new String("hello");
System.out.println(str == str1); // false
System.out.println(str.equals(str1)); // true
  • if we create a string with new keyword, it will not use a string pool and two separate objects got created. That’s why we have false when using == operator.
  • equals() compare the actual value of the string.

--

--

--

Software Developer, Java Instructor https://www.techleadacademy.io/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Stop Using Your Mouse At PyCharm

Scala Meetup in Berlin| Functional Programming

Project and Delivery Management in the context of changing software development paradigm

Project and Delivery Management in the context of changing software development paradigm. Image Source: https://en.wikipedia.org/wiki/Paradigm_shift#/media/File:Duck-Rabbit_illusion.jpg

5 Web Development Productivity Hacks You Need In Your Life

How I Accidentally Became a Developer

Testing your Flutter App

Utilize parameters with MySQL Stored Procedure by examples.

How to Configure Pytest in Pycharm for Django?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Beknazar

Beknazar

Software Developer, Java Instructor https://www.techleadacademy.io/

More from Medium

Pyramid Pattern of Numbers code in java

Java Encapsulation

Introduction to Sealed Classes

Polymorphism in a Nutshell