Strings in Java
Strings in Java are objects that represent a set of characters or a sequence of character values. In this article, let’s explain how string functions in Java work, along with examples. Explore types of Java applications and g ain expertise at SLA.
The ‘java.lang.String’ is the class that will be used in Java for creating a string object. There are two different ways to create string objects in Java.
- Using String Literal: String Literal will be created in Java using double quotes
Ex: String a = “Hello World”;
- Using the “New” keyword: Java String will be created using a “new” keyword.
Ex: String a = new String(“Hello World”)
One reference variable and two objects such as In String Pool and Heap will be created with the variables that refer to the object in the heap.
Java String Pool: It is the collection of strings that are stored in the heap memory. When an object is created, the String pool verifies whether the object is present already in the pool or not. If it is already there, the reference will be returned to the variable or it creates a new object and the relevant reference will be returned. String objects can be shared and are immutable. Here are the top 15 interesting facts about Java.
String s = “abc” is equivalent to char data[] = {a,b,c}; string s = new String (data);
There are some in-built methods in Java for the String class
Java String Methods
- Java String Length(): The Java String length() method explains the length of the string and it returns the count of the total number of characters available in the String. Learn about object methods in Java.
- Example
public class Sample
{
public static void main (String args[])
{
String a1 = “Hello”;
String a2 = “World”;
System.out.println(“The length of string is:” + a1.length());
System.out.println(“The length of string is:” + a2.length());
}
}
- Java String compareTo(): The Java String CompareTo() method is used to compare the given string with the present string. It is implemented by the String class for a comparable interface. Explore Java design patterns with best practices.
- Example
public class ExampleCompareTo
{
public static void main(String args[]
{
String a1 = “Welcome”;
String a2 = “Welcome”;
String a3 = “Velcome”;
String a = “Yellow”;
System.out.println(a1.compareTo(a2));
System.out.println(a1.compareTo(a3));
System.out.println(a1.compareTo(a4));
}
}
Output
0 (both are equal)
1 (“v” is one time greater than “w”
-2 (“y” is 2 times lower than “w”
- Java String concat() is the method used to combine the strings with the end of one string to another string and returns the combined string. It is like updating a string with another. Java memory management is a crucial thing that includes garbage collection and memory optimization techniques.
- Example
public class ExampleConcat
{
public static void main (String args[])
{
String a1 = ”Welcome to”
a1 = a1.concat(“SLA”);
System.out.println(a1);
}
}
Output
Welcome to SLA
- Java String IsEmpty is the method used to check whether the string consists of anything or not. If the string is empty, then it returns true or false. Learn about Java performance tuning, which helps optimize the performance of Java applications.
- Example
public class ExampleIsEmpty
{
public static void main(String args[])
{
String a1 = “ “;
String a2 = “SLA”;
System.out.println(a1.isEmpty());
System.out.println(a2.isEmpty());
}
}
Output
True
False
- Java String Trim() is a method used for removing leading and trailing spaces. It verifies the Unicode value of space characters before and after the string. If it is available, then remove the spaces and return the removed string. Explore Java security that protects Java applications from vulnerabilities and attacks.
- Example
public class ExampleTrim
{
public static void main (String args[])
{
String a1 = “ SLA”;
System.out.println(a1+”Welcomes You”);
System.out.println(a1.trim()+”Welcomes You”);
}
}
Output
SLA Welcomes You
SLA Welcomes You
Suggested Article: Latest IT salary in India for freshers
- Java String toLowerCase() is used to convert all the characters of a string into lowercase.
- Example
public class ExampleLowerCase
{
public static void main (String args[])
{
String a1 = “WELCOME to SLA”;
String a1lwr = a1.toLowerCase();
System.out.println(“s1lwr”);
}
}
Output
WELCOME to SLA
Also Read: Importance and Benefits of Android
- The Java String toUpper() method is used to convert all the characters of the given string into uppercase letters.
- Example
public class ExampleUpperCase
{
public static void main(String args[])
{
String a1 = “Hello world”;
String a1upr = a1.toUpperCase();
System.out.println(a1upr);
}
}
Output
HELLO WORLD
- Java String ValueOf() is the method used to convert various types of input values into a string. It can be used to convert int, long, boolean, character, float, double, and object data types to string data types and even char arrays. Following are the syntaxes for converting them to strings. Explore our spoken English classes in Chennai.
- public static String valueOf(boolean b)
- public static String valueOf(char c)
- public static String valueOf(char[] c)
- public static String valueOf(int i)
- public static String valueOf(long l)
- public static String valueOf(float f)
- public static String valueOf(double d)
- public static String valueOf(Object o)
Example
public class ExampleValueOf
{
public static void main(String args[])
{
int num = 10;
String a1 = String.valueOf(num);
System.out.println(a1+15);
}
}
Output
1015
- Java String replace() is the method used to return a string after replacing the existing value with new characters. It will be done in two ways in Java. Explore the top 10 software courses for high-paying jobs.
- public class FirstExampleReplace
{
public static void main(String args[])
{
String s1=”Hi SLA”;
String rplc = s1.replace(‘Hi’,’Jo’);
System.out.println(rplc);
} }
Output
Jo SLA
- public class SecondExampleReplace
{
public static void main(String args[])
{
String a1 = “Hello to Learners”;
String rplcstr = a1.replace(“Learners”, “Professionals”);
System.out.println(rplcstr);
}
}
Output
Hello to Professionals
- Java String contains() is the method used to search the sequence of characters in the string. If the sequence is present, it returns true, or else it returns false. Here is the compact guide to adding chat features to any web or mobile apps.
- Example
class ExampleContains
{
public static void main(String args[])
{
String n = “SLA Welcomes All Freshers”;
System.out.println(n.contains(“SLA”));
System.out.println(n.contains(“Professionals”));
System.out.println(n.contains(“All”));
}
}
Output
True
False
True
- Java String equals() is the method used to compare the two given strings based on the content of the strings or representation. If characters match on both sides, it returns true, or else it returns false. Explore the future of software testing.
- Example
public class ExampleEquals
{
public static void main(String args[])
{
String a1 = “Welcome”;
String a2 = “Welcome”;
String a3 = “Good”;
System.out.println(a1.equals(a2));
System.out.println(a1.equals(a3));
}
}
Output
True
False
- Java String equalIgnoreCase() is the method used to compare two strings based on the content but omits to check the case. If any character matches, it returns true; otherwise, it returns false. Learn the benefits of AWS training in Chennai that advance your IT career.
- Example
public class ExampleEqualsIgnoreCase
{
public static void main(String args[])
{
String a1 = “Welcome”;
String a2 = “WELCOME”;
String a3 = “Good”;
System.out.println(a1.equalsIgnoreCase(a2));
System.out.println(a1.equalsIgnoreCase(a3));
}
}
Output
True
False
- Java String toCharArray() is the method used to convert a string into a character array by calculating the length of the given string, which includes spaces for creating an array of char types with the same values. Unlocking career opportunities by learning why AWS training in Chennai is a smart choice.
- Example
public class ExampleCharArray
{
public static void main(String args[])
{
String a1 =”Hello all professionals”;
char[] ch = al.toCharArray();
for (int i = 0; i<ch.length; i++)
{
System.out.println(ch[i]);
}
}
}
Output
Hello all professionals
- Java String getBytes() is the method used to return the sequence of bytes or the byte array of the string. Enhance your career by learning mobile app testing best practices.
- Example
public class ExampleGetBytes
{
public static void main(String args[])
{
String a1 = “XYZ”;
byte[] b = a1.getBytes();
for (int i = 0; i<b.length; i++)
{
System.out.println(b[i]);
}
}
}
Output
88
89
90
- Java String endsWith() is the method used to verify if the string ends with the given suffix. It returns true if it ends; otherwise, it returns false. Learn Java servlets to build web applications using Java.
- Example
public class ExampleEndsWith
{
public static void main(String args[])
{
String a1 = “Welcome All”;
System.out.println(a1.endsWith(“l”));
System.out.println(a1.endsWith(“All”));
System.out.println(a1.endsWith(“You”));
}
}
Output
True
True
False
End Note
We hope this blog helps you understand the string concepts in Java. Learn practically by enrolling in our Java training in Chennai at the SLA Institute.