Assignments/Article By Gowsika Yogaraj

String in Java

Student Assignment

String in Java

Published on Nov 06, 2022

String in Java

  • It is a collection or sequence of characters.
  • In Java String is represented using double quotes.

E.g.:

String var = “Welcome to Java”

  • In Java, Strings are not primitive types like int, char, etc.
  • All String Variables are Instances of the String Class.

Java String Operations

Java String provides various methods to perform different operations on the Strings.

Get the Length of the String

E.g.  String var = “Welcome to Java”

         Int length = var.length();

Join Two Java Strings

String first = “Welcome”

String second =” To Java”

String joinedString=first.concat(second);

Compare Two Strings

The String can be compared in java using the equals() method.

String first =” Java Programming”;

String second= “Java Programming”;

String third= “Python Programming”;

boolean result1=first.equals(second);

boolean result2=first.equals(third);

Java Strings are Immutable

In Java, Strings are immutable. This means, once we create a string, we cannot change that string.

E.g. Create a string

String example=”Hello”;

example=example.concat(“World”);

Here we are using concat () method to add string world to the previous string Hello.

  • JVM takes the first string “Hello”
  • Creates a new string by adding “World” to the first string
  • Assign the new string “Hello World” to the example variable
  • The first string “Hello” remains unchanged.

Creating Strings using the new keyword

The strings in java are objects, we can create strings using the new keyword.

E.g.  String name= new String(“Java Programming”);

String Pool

In Java, the JVM maintains a string pool to store all strings inside the memory. The String pool helps in reusing the strings.

  1. While creating strings using string literals,
  2. String var=” Java”;

Here we directly provide the value of the string. Hence the compiler first checks the string pool to see if the string already exists.

If the String already exists, the new string is not created. Instead, the new reference points to the already existing sting in the memory.

If the string does not exist, a new string will be created.

Methods of Java String

Besides those mentioned above, there are various string methods present in Java. Here are some of those methods:
Methods Description
contains() checks whether the string contains a substring
substring() returns the substring of the string
join() join the given strings using the delimiter
replace() replaces the specified old character with the specified new character
replaceAll() replaces all substrings matching the regex pattern
replaceFirst() replace the first matching substring
charAt() returns the character present in the specified location
getBytes() converts the string to an array of bytes
indexOf() returns the position of the specified character in the string
compareTo() compares two strings in the dictionary order
compareToIgnoreCase() compares two strings ignoring case differences
trim() removes any leading and trailing whitespaces
format() returns a formatted string
split() breaks the string into an array of strings
toLowerCase() converts the string to lowercase
toUpperCase() converts the string to uppercase
valueOf() returns the string representation of the specified argument
toCharArray() converts the string to a char array
matches() checks whether the string matches the given regex
startsWith() checks if the string begins with the given string
endsWith() checks if the string ends with the given string
isEmpty() checks whether a string is empty of not
intern()  returns the canonical representation of the string
contentEquals() checks whether the string is equal to charSequence
hashCode() returns a hash code for the string
subSequence() returns a subsequence from the string

Student Assignment

The views expressed in this document are those of the author and do not necessarily reflect the position of the London School of Emerging Technology. View the detailed policy Disclaimer for Student and Personal Websites

Our Latest Blog

Java Programming

Java Basics for Beginners: A Step-by-Step Guide to Mastering Java Programming

Introduction to Java programming Java, a versatile and extensively utilised programming language, is popular among...
Read More
Entity Relationship Diagrams (ERD)

Entity Relationship Diagrams (ERD): A Comprehensive Guide

Introduction to Entity Relationship Diagrams (ERD) Entity Relationship Diagrams (ERD) are powerful tools used in...
Read More
Full Stack Java Development

From Novice to Pro: Navigating the World of Full Stack Java Development for Beginners

Introduction to Full Stack Java Development Full-stack Java Development} is a comprehensive approach to creating...
Read More