**** Java Certification Training: https://www.edureka.co/java-j2ee-soa-training ****
This Edureka tutorial on “Java Strings” will talk about one of the most important classes of Java i.e String class. It will also talk about the various methods and interfaces implemented by the String class. Through this tutorial you will learn the following topics:
Java Strings
String Pool
Creating a String
String Methods
Check out our Java Tutorial blog series: https://goo.gl/osrGrS
Check out our complete Youtube playlist here: https://goo.gl/gMFLx3
Follow us to never miss an update in the future.
Instagram: https://www.instagram.com/edureka_learning/
Facebook: https://www.facebook.com/edurekaIN/
Twitter: https://twitter.com/edurekain
LinkedIn: https://www.linkedin.com/company/edureka
3. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Java String
Object 01
String is a Java
object
Characters 02
Class 03
Immutable 04
Represents a sequence
of characters
java.lang.String class is used to
create and manipulate strings
A string is immutable
in nature
4. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
The quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dog
The quick fox jumps The quick lazy dog
Java Strings
The quick brown fox jumps over the lazy dogSearch
Create Substring
Create new strings The quick brown fox jumps over the lazy dog
With Strings in Java you can perform various operations, some of which are:
6. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Java Strings
java.lang.String
ComparableSerializable CharSequence
public final class String
extends Object
implements Serializable, Comparable<String>, CharSequence
7. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Comparable interface is used for ordering the objects of any user-defined class. This interface is
found in java.lang.package and contains only one method named compareTo(Object)
A CharSequence interface is a readable sequence of characters. This interface provides uniform,
read-only access to various kind of character sequences
Serializable is a marker interface that contains no data member or method. It is used to “mark”
the java classes so that objects of these classes may get a specific capability
Java Strings
Comparable
Serializable
CharSequence
8. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
java,lang.StringBuffer is a peer class
of String that represents growable and
writable character sequences
java.lang.StringBuilder is identical to
StringBuffer except for one important
difference that it is not synchronized, which
means it is not thread safe
java.lang.String class is used to create string
objects and provides a list of methods to
create and manipulate Strings
Java Strings
CharSequence
String StringBuffer StringBuilder
10. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
String Pool
Java String pool refers to collection of Strings which
are stored in heap memory
As String objects are immutable in nature the
concept of String Pool came into the picture
String Pool helps in saving space for Java Runtime
Heap Memory
String Pool
12. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Literal
new
Keyword
Creating a String – Using Literal
Java String literal is created by using double quotes
String str = “Edureka”;
Heap Memory
String Pool
Edureka
str
str1
String str2 = “Welcome”;str2
Welcome String str1 = “Edureka”;
Before creating a String literal first looks for String with same value in the String pool, if found it
returns the reference else it creates a new String in the pool & returns the reference
13. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Literal
new
Keyword
Creating a String – Using new Keyword
Heap Memory
String Pool
Edureka
Welcome
str
str1
str2
Edureka str3
String object created using “new” keyword it always create a new object in heap memory
String str = new String (“Edureka”);
15. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
String Methods
boolean equals(Object another) Checks the equality of string with the given object
equalsIgnoreCase() Compares another string without matching the case
length() Returns a strings length
charAt(i) Returns a character at a index ‘i’
toUpperCase() Returns the string in uppercase
toLowerCase() Returns the string in lowercase
replace(oldVal, newVal) Replaces all occurrences of the specified char value with the given value
trim() Removes the white spaces from the beginning and ending of string
contains("value") Checks for the matching sequence of char value and returns true/false
toCharArray() Converts a string to a new character array
IsEmpty() Checks whether the string is empty or not
endsWith() Checks if the string ends with the specified suffix
concat() Concatenates two strings