Java Strings and String Buffer
•
Strings represent a sequence of characters in
Java
•
String and StringBuffer are both classes
that operate on strings.
•
The object of the String class is of fixed
length.
•
The object of the StringBuffer class is
growable.
Example 01:
package String;
/*A string is a sequence of characters.
* In Java, String objects are immutable, meaning a constant cannot be changed once created.
* String is slow and consumes more memory when we concatenate too many strings because every time it creates a new instance.
* String class overrides the equals() method of Object class. So you can compare the contents of two strings by the equals() method.*/
public class stringsConcept {
public static void main(String args[])
{
//String in Java
String a="Programming in Java";
String b="Java is simple";
System.out.println("A : "+a);
System.out.println("B : "+b);
System.out.println("A HashCode "+a.hashCode());
System.out.println("B HashCode "+b.hashCode());
System.out.println("Equals : "+a.equals(b));
System.out.println("Equals Ignore Case: "+a.equalsIgnoreCase(b));
System.out.println("Length: "+a.length());
System.out.println("CharAt: "+a.charAt(0));
System.out.println("Uppercase: "+a.toUpperCase());
System.out.println("Lowercase: "+a.toLowerCase());
System.out.println("Replace: "+a.replace("Programming in Java"," Oracle - Java"));
System.out.println("Contains : " + a.contains("Java"));
System.out.println("Empty : " + a.isEmpty());
System.out.println("EndWith : " + a.endsWith("va"));
System.out.println("StartWith : " + a.startsWith("Pro"));
System.out.println("Substring : " + a.substring(5));
System.out.println("Substring : " + a.substring(0, 5));
char[] carray = a.toCharArray();
for(char c : carray){
System.out.print(c+ " ");
}
String c=" Mentor ";
System.out.println("Length: "+c.length());
System.out.println("C:"+c);
System.out.println("C Trim :"+c.trim());
System.out.println("C Trim Length:"+c.trim().length());
}
}
//A part of a String is called a substring. In other words,
//substring is a subset of another String.
//public String substring(int startIndex)
//public String substring(int startIndex, int endIndex)
Example: 02
package String;
public class stringBuffer_stringBuilder {
public static void main(String args[])
{
//StringBuffer & StringBuilder in Java
StringBuilder buffer =new StringBuilder("Kishore");
System.out.println(buffer);
buffer.append(" ragav");
System.out.println(buffer);
buffer.insert(10," Computer");
System.out.println(buffer);
buffer.replace(9,11,"@@@");
System.out.println(buffer);
buffer.delete(9,11);
System.out.println(buffer);
buffer.reverse();
System.out.println(buffer);
System.out.println(buffer.charAt(2));
System.out.println(buffer.length());
System.out.println(buffer.substring(0));
System.out.println(buffer.substring(0,5));
buffer.setCharAt(0,'@');
System.out.println(buffer);
StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2
}
}
/*StringBuffer
The StringBuffer and StringBuilder classes are suitable for both assembling and modifying strings;
i.e. they provide methods for replacing and removing characters as well as adding them in various.
Java StringBuilder class is used to create mutable (modifiable) strings.
Key Points:
Used to create a mutable (modifiable) string.
Mutable: Which can be changed?
Is thread-safe i.e. multiple threads cannot access it simultaneously.
Methods:
public synchronized StringBuffer append ( String s )
public synchronized StringBuffer insert ( int offset, String s )
public synchronized StringBuffer replace ( int startIndex, int endIndex, String str )
public synchronized StringBuffer delete ( int startIndex, int endIndex )
public synchronized StringBuffer reverse ()
StringBuilder
Java StringBuilder class is used to create mutable (modifiable) strings.
The Java StringBuilder class is the same as the StringBuffer class except that it is non-synchronized.
The StringBuffer and StringBuilder classes are suitable for both assembling and modifying strings;
i.e. they provide methods for replacing and removing characters as well as adding them in various.*/
0 Comments