1. Core Java
Debasish Pratihari
String:
Strings are sequence of character
In java strings are object of
String,StringBuffer or StringBuilder class.
Strings in Java are 16-bit Unicode.
The Java language provides special support for
the string concatenation operator ( + ).
All String representation classes of java are final
Type of String :
Immutable
Mutable
They are read-only, once created can’t
be changed
The String class implements immutable
character Strings.
All string literals in Java programs are
immutable strings
Are dynamic character string. i.e they
can be changed unlike mutable string.
StringBuffer and StringBuilder class
in java implements Mutable String.
Example String :
System.out.println("Lakshya");
String place = "Bhubaneswar";
System.out.println("Lakshya " + place);
String c = "Lakshya".substring(3,6);
String d = place.substring(3, 6);
String name= new String(“Debasish”);
String s1 = new String();
String s3 = new String( name );
char lan[] = { 'j', 'a', 'v',’a’ };
String s4 = new String( lan );
Lecture/core/String/07
Page #1
feel the Technology…
2. Core Java
Debasish Pratihari
String Literal Pool :
String allocation, like all object allocation, proves costly in
both time and memory. The JVM performs some trickery
while instantiating string literals to increase performance
and decrease memory overhead. To cut down the number
of String objects created in the JVM, the String class
keeps a pool of strings.
Each time your code create a string literal, the JVM
checks the string literal pool first. If the string already
exists in the pool, a reference to the pooled instance
returns. If the string does not exist in the pool, a new
String object instantiates, then is placed in the pool. Java
can make this optimization since strings are immutable
and can be shared without fear of data corruption.
String concatenation:
Java Strings can be concatenated (joined) using
the + and += operators to create new Strings.
Every time an operation modifies a String
object, a new read-only String object is created.
String language = "Java";
String course = "Introduction to " + language;
course += ", @ Lakshya";
Comparing String :
Java provides a variety of methods to compare String
objects.
public int compareTo( String str )
public int compareToIgnoreCase( String str )
public boolean equals( Object obj )
public boolean equalsIgnoreCase( String str )
Use of the == operator
Lecture/core/String/07
Page #2
feel the Technology…
3. Core Java
Debasish Pratihari
Useful String methods :
public
public
public
public
public
public
public
public
char charAt( int index )
String concat( String str )
int length()
String toUpperCase()
String toLowerCase()
String trim()
String substring(int beginIndex)
static String valueOf( xxx b )
String representation of Objects :
All Java objects implement the method toString() which
returns a String object that best "describes" that object.
you do not even need to explicitly call toString(). The
println() method has a version that takes an Object as its
argument. This version will implicitly call toString() for
you and print the result.
Color color = new Color( 255, 0, 0 );
String colorStr = color.toString();
System.out.println( colorStr );
This will printout:
java.awt.Color[r=255,g=0,b=0]
String Interning:
It ensures that no two objects encapsulates the
same string.
This means that if two string variables reference
strings that are identical, the reference must be
identical.
The intern() method will check a string
reference against all the String objects currently
in existence.
If it already exists, the current object will
discarded, and reference to the existing object
will be given.
Lecture/core/String/07
Page #3
feel the Technology…
4. Core Java
Debasish Pratihari
Creating Mutable String :
StringBuffer str= new StringBuffer(“Exlore java
@ Lakshya”);
StringBuffer duplicate= new StringBuffer(str);
StringBuffer str= new StringBuffer(20);
Manipulating StringBuffer Object:
25%
Setting a Length
o Str.setLength(30);
Adding to existing String
o str.apppend(“new string”);
Finding Position of a substring
o str.lastIndexOf(“new”);
Replacing substring
o str.replace(0,4, “java”);
Things to Remember:
A String object stores a fixed character string
that can’t be changed.
StringBuffer and StringBuilder objects can store
a string of character that you can modify.
Number of character present in a string can be
found using length().
A String object can be created from StringBuffer
by using toString().
Lecture/core/String/07
Page #4
feel the Technology…