Study Anytime Any Where We Will Open The Knowledge For You Anish Sir 24x7 Learning solution It Is Time to join JOB
23 October 2017

Study Anytime Any Where

23 October 2017

We Will Open The Knowledge For You

23 October 2017

Anish Sir 24x7 Learning solution

23 October 2017

It Is Time to join JOB

C Programming

C++ Programming

JAVA Programming

Android Language

Android-logo.png

.Net Programming

net-logo-b55c217213ae49991589ed43d7ed58a7.png

c# Programming

photo_1389636992_quiz_image_temp.png

Asp .Net

asplogo-square.png

PHP Programming

58481791cef1014c0b5e4994.png

Search Your Topic

config
Anish Sir

Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

Website URL: http://toshiyas.in
Monday, 24 July 2017 04:20

Java StringBuffer class

Java StringBuffer class is used to create mutable (modifiable) string. The StringBuffer class in java is same as String class except it is mutable i.e. it can be changed.

Note: Java StringBuffer class is thread-safe i.e. multiple threads cannot access it simultaneously. So it is safe and will result in an order.

Important Constructors of StringBuffer class

Constructor Description
StringBuffer() creates an empty string buffer with the initial capacity of 16.
StringBuffer(String str) creates a string buffer with the specified string.
StringBuffer(int capacity) creates an empty string buffer with the specified capacity as length.

Important methods of StringBuffer class

Modifier and Type Method Description
public synchronized StringBuffer append(String s) is used to append the specified string with this string. The append() method is overloaded like append(char), append(boolean), append(int), append(float), append(double) etc.
public synchronized StringBuffer insert(int offset, String s) is used to insert the specified string with this string at the specified position. The insert() method is overloaded like insert(int, char), insert(int, boolean), insert(int, int), insert(int, float), insert(int, double) etc.
public synchronized StringBuffer replace(int startIndex, int endIndex, String str) is used to replace the string from specified startIndex and endIndex.
public synchronized StringBuffer delete(int startIndex, int endIndex) is used to delete the string from specified startIndex and endIndex.
public synchronized StringBuffer reverse() is used to reverse the string.
public int capacity() is used to return the current capacity.
public void ensureCapacity(int minimumCapacity) is used to ensure the capacity at least equal to the given minimum.
public char charAt(int index) is used to return the character at the specified position.
public int length() is used to return the length of the string i.e. total number of characters.
public String substring(int beginIndex) is used to return the substring from the specified beginIndex.
public String substring(int beginIndex, int endIndex) is used to return the substring from the specified beginIndex and endIndex.

What is mutable string

A string that can be modified or changed is known as mutable string. StringBuffer and StringBuilder classes are used for creating mutable string.

1) StringBuffer append() method

The append() method concatenates the given argument with this string.

 
  1. class StringBufferExample{  
  2. public static void main(String args[]){  
  3. StringBuffer sb=new StringBuffer("Hello ");  
  4. sb.append("Java");//now original string is changed  
  5. System.out.println(sb);//prints Hello Java  
  6. }  
  7. }  

2) StringBuffer insert() method

The insert() method inserts the given string with this string at the given position.

 
  1. class StringBufferExample2{  
  2. public static void main(String args[]){  
  3. StringBuffer sb=new StringBuffer("Hello ");  
  4. sb.insert(1,"Java");//now original string is changed  
  5. System.out.println(sb);//prints HJavaello  
  6. }  
  7. }  

3) StringBuffer replace() method

The replace() method replaces the given string from the specified beginIndex and endIndex.

 
  1. class StringBufferExample3{  
  2. public static void main(String args[]){  
  3. StringBuffer sb=new StringBuffer("Hello");  
  4. sb.replace(1,3,"Java");  
  5. System.out.println(sb);//prints HJavalo  
  6. }  
  7. }  

4) StringBuffer delete() method

The delete() method of StringBuffer class deletes the string from the specified beginIndex to endIndex.

 
  1. class StringBufferExample4{  
  2. public static void main(String args[]){  
  3. StringBuffer sb=new StringBuffer("Hello");  
  4. sb.delete(1,3);  
  5. System.out.println(sb);//prints Hlo  
  6. }  
  7. }  

5) StringBuffer reverse() method

The reverse() method of StringBuilder class reverses the current string.

 
  1. class StringBufferExample5{  
  2. public static void main(String args[]){  
  3. StringBuffer sb=new StringBuffer("Hello");  
  4. sb.reverse();  
  5. System.out.println(sb);//prints olleH  
  6. }  
  7. }  

6) StringBuffer capacity() method

The capacity() method of StringBuffer class returns the current capacity of the buffer. The default capacity of the buffer is 16. If the number of character increases from its current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34.

 
  1. class StringBufferExample6{  
  2. public static void main(String args[]){  
  3. StringBuffer sb=new StringBuffer();  
  4. System.out.println(sb.capacity());//default 16  
  5. sb.append("Hello");  
  6. System.out.println(sb.capacity());//now 16  
  7. sb.append("java is my favourite language");  
  8. System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2  
  9. }  
  10. }  

7) StringBuffer ensureCapacity() method

The ensureCapacity() method of StringBuffer class ensures that the given capacity is the minimum to the current capacity. If it is greater than the current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34.

 
  1. class StringBufferExample7{  
  2. public static void main(String args[]){  
  3. StringBuffer sb=new StringBuffer();  
  4. System.out.println(sb.capacity());//default 16  
  5. sb.append("Hello");  
  6. System.out.println(sb.capacity());//now 16  
  7. sb.append("java is my favourite language");  
  8. System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2  
  9. sb.ensureCapacity(10);//now no change  
  10. System.out.println(sb.capacity());//now 34  
  11. sb.ensureCapacity(50);//now (34*2)+2  
  12. System.out.println(sb.capacity());//now 70  
  13. }  
  14. }  

 

 

Monday, 24 July 2017 04:17

Java String class methods

The java.lang.String class provides a lot of methods to work on string. By the help of these methods, we can perform operations on string such as trimming, concatenating, converting, comparing, replacing strings etc.

Java String is a powerful concept because everything is treated as a string if you submit any form in window based, web based or mobile application.

Let's see the important methods of String class.

Java String toUpperCase() and toLowerCase() method

The java string toUpperCase() method converts this string into uppercase letter and string toLowerCase() method into lowercase letter.

 
  1. String s="Sachin";  
  2. System.out.println(s.toUpperCase());//SACHIN  
  3. System.out.println(s.toLowerCase());//sachin  
  4. System.out.println(s);//Sachin(no change in original)  
SACHIN
sachin
Sachin

Java String trim() method

The string trim() method eliminates white spaces before and after string.

 
  1. String s="  Sachin  ";  
  2. System.out.println(s);//  Sachin    
  3. System.out.println(s.trim());//Sachin  
  Sachin  
Sachin

Java String startsWith() and endsWith() method

 
  1. String s="Sachin";  
  2.  System.out.println(s.startsWith("Sa"));//true  
  3.  System.out.println(s.endsWith("n"));//true  
true
true

Java String charAt() method

The string charAt() method returns a character at specified index.

 
  1. String s="Sachin";  
  2. System.out.println(s.charAt(0));//S  
  3. System.out.println(s.charAt(3));//h  
S
h

Java String length() method

The string length() method returns length of the string.

 
  1. String s="Sachin";  
  2. System.out.println(s.length());//6  
6

Java String intern() method

A pool of strings, initially empty, is maintained privately by the class String.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

 
  1. String s=new String("Sachin");  
  2. String s2=s.intern();  
  3. System.out.println(s2);//Sachin  
Sachin

Java String valueOf() method

The string valueOf() method coverts given type such as int, long, float, double, boolean, char and char array into string.

 
  1. int a=10;  
  2. String s=String.valueOf(a);  
  3. System.out.println(s+10);  

Output:

1010

Java String replace() method

The string replace() method replaces all occurrence of first sequence of character with second sequence of character.

 
  1. String s1="Java is a programming language. Java is a platform. Java is an Island.";    
  2. String replaceString=s1.replace("Java","Kava");//replaces all occurrences of "Java" to "Kava"    
  3. System.out.println(replaceString);    

Output:

Kava is a programming language. Kava is a platform. Kava is an Island.

 

Monday, 24 July 2017 04:11

Substring in Java

A part of string is called substring. In other words, substring is a subset of another string. In case of substring startIndex is inclusive and endIndex is exclusive.

Note: Index starts from 0.

You can get substring from the given string object by one of the two methods:

  1. public String substring(int startIndex): This method returns new String object containing the substring of the given string from specified startIndex (inclusive).
  2. public String substring(int startIndex, int endIndex): This method returns new String object containing the substring of the given string from specified startIndex to endIndex.

In case of string:

  • startIndex: inclusive
  • endIndex: exclusive

Let's understand the startIndex and endIndex by the code given below.

 
  1. String s="hello";  
  2. System.out.println(s.substring(0,2));//he  

In the above substring, 0 points to h but 2 points to e (because end index is exclusive).

Example of java substring

 
  1. public class TestSubstring{  
  2.  public static void main(String args[]){  
  3.    String s="SachinTendulkar";  
  4.    System.out.println(s.substring(6));//Tendulkar  
  5.    System.out.println(s.substring(0,6));//Sachin  
  6.  }  
  7. }  
Tendulkar
Sachin
Monday, 24 July 2017 04:09

String Concatenation in Java

In java, string concatenation forms a new string that is the combination of multiple strings. There are two ways to concat string in java:

  1. By + (string concatenation) operator
  2. By concat() method

1) String Concatenation by + (string concatenation) operator

Java string concatenation operator (+) is used to add strings. For Example:

 
  1. class TestStringConcatenation1{  
  2.  public static void main(String args[]){  
  3.    String s="Sachin"+" Tendulkar";  
  4.    System.out.println(s);//Sachin Tendulkar  
  5.  }  
  6. }  
Output:Sachin Tendulkar

The Java compiler transforms above code to this:

 
  1. String s=(new StringBuilder()).append("Sachin").append(" Tendulkar).toString();  

In java, String concatenation is implemented through the StringBuilder (or StringBuffer) class and its append method. String concatenation operator produces a new string by appending the second operand onto the end of the first operand. The string concatenation operator can concat not only string but primitive values also. For Example:

 
  1. class TestStringConcatenation2{  
  2.  public static void main(String args[]){  
  3.    String s=50+30+"Sachin"+40+40;  
  4.    System.out.println(s);//80Sachin4040  
  5.  }  
  6. }  
80Sachin4040

Note: After a string literal, all the + will be treated as string concatenation operator.

2) String Concatenation by concat() method

The String concat() method concatenates the specified string to the end of current string. Syntax:

 
  1. public String concat(String another)  

Let's see the example of String concat() method.

 
  1. class TestStringConcatenation3{  
  2.  public static void main(String args[]){  
  3.    String s1="Sachin ";  
  4.    String s2="Tendulkar";  
  5.    String s3=s1.concat(s2);  
  6.    System.out.println(s3);//Sachin Tendulkar  
  7.   }  
  8. }  
Sachin Tendulkar
Monday, 24 July 2017 04:06

Java String compare

We can compare string in java on the basis of content and reference.

It is used in authentication (by equals() method), sorting (by compareTo() method), reference matching (by == operator) etc.

There are three ways to compare string in java:

  1. By equals() method
  2. By = = operator
  3. By compareTo() method

1) String compare by equals() method

The String equals() method compares the original content of the string. It compares values of string for equality. String class provides two methods:

  • public boolean equals(Object another) compares this string to the specified object.
  • public boolean equalsIgnoreCase(String another) compares this String to another string, ignoring case.
 
  1. class Teststringcomparison1{  
  2.  public static void main(String args[]){  
  3.    String s1="Sachin";  
  4.    String s2="Sachin";  
  5.    String s3=new String("Sachin");  
  6.    String s4="Saurav";  
  7.    System.out.println(s1.equals(s2));//true  
  8.    System.out.println(s1.equals(s3));//true  
  9.    System.out.println(s1.equals(s4));//false  
  10.  }  
  11. }  
Output:true
       true
       false
 
  1. class Teststringcomparison2{  
  2.  public static void main(String args[]){  
  3.    String s1="Sachin";  
  4.    String s2="SACHIN";  
  5.   
  6.    System.out.println(s1.equals(s2));//false  
  7.    System.out.println(s1.equalsIgnoreCase(s3));//true  
  8.  }  
  9. }  
Output:false
       true
Monday, 24 July 2017 04:04

Immutable String in Java

In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable.

Once string object is created its data or state can't be changed but a new string object is created.

Let's try to understand the immutability concept by the example given below:

 
  1. class Testimmutablestring{  
  2.  public static void main(String args[]){  
  3.    String s="Sachin";  
  4.    s.concat(" Tendulkar");//concat() method appends the string at the end  
  5.    System.out.println(s);//will print Sachin because strings are immutable objects  
  6.  }  
  7. }  

Test it Now

Output:Sachin

Now it can be understood by the diagram given below. Here Sachin is not changed but a new object is created with sachintendulkar. That is why string is known as immutable.

Heap diagram

As you can see in the above figure that two objects are created but s reference variable still refers to "Sachin" not to "Sachin Tendulkar".

But if we explicitely assign it to the reference variable, it will refer to "Sachin Tendulkar" object.For example:

 
  1. class Testimmutablestring1{  
  2.  public static void main(String args[]){  
  3.    String s="Sachin";  
  4.    s=s.concat(" Tendulkar");  
  5.    System.out.println(s);  
  6.  }  
  7. }  

Test it Now

Output:Sachin Tendulkar

In such case, s points to the "Sachin Tendulkar". Please notice that still sachin object is not modified.


Why string objects are immutable in java?

Because java uses the concept of string literal.Suppose there are 5 reference variables,all referes to one object "sachin".If one reference variable changes the value of the object, it will be affected to all the reference variables. That is why string objects are immutable in java.
Monday, 24 July 2017 04:03

Java String equalsIgnoreCase()

The String equalsIgnoreCase() method compares the two given strings on the basis of content of the string irrespective of case of the string. It is like equals() method but doesn't check case. If any character is not matched, it returns false otherwise it returns true.


Signature

 
  1. public boolean equalsIgnoreCase(String str)  

Parameter

str : another string i.e. compared with this string.


Returns

It returns true if characters of both strings are equal ignoring case otherwise false.


Java String equalsIgnoreCase() method example

 
  1. public class EqualsIgnoreCaseExample{  
  2. public static void main(String args[]){  
  3. String s1="javatpoint";  
  4. String s2="javatpoint";  
  5. String s3="JAVATPOINT";  
  6. String s4="python";  
  7. System.out.println(s1.equalsIgnoreCase(s2));//true because content and case both are same  
  8. System.out.println(s1.equalsIgnoreCase(s3));//true because case is ignored  
  9. System.out.println(s1.equalsIgnoreCase(s4));//false because content is not same  
  10. }}  
true
true
false
Monday, 24 July 2017 04:00

Java String equals

The java string equals() method compares the two given strings based on the content of the string. If any character is not matched, it returns false. If all characters are matched, it returns true.

The String equals() method overrides the equals() method of Object class.


Signature

 
  1. public boolean equals(Object anotherObject)  

Parameter

anotherObject : another object i.e. compared with this string.


Returns

true if characters of both strings are equal otherwise false.


Overrides

equals() method of java Object class.


Java String equals() method example

 
  1. public class EqualsExample{  
  2. public static void main(String args[]){  
  3. String s1="javatpoint";  
  4. String s2="javatpoint";  
  5. String s3="JAVATPOINT";  
  6. String s4="python";  
  7. System.out.println(s1.equals(s2));//true because content and case is same  
  8. System.out.println(s1.equals(s3));//false because case is not same  
  9. System.out.println(s1.equals(s4));//false because content is not same  
  10. }}  
true
false
false
Monday, 24 July 2017 03:58

Java String endsWith

The java string endsWith() method checks if this string ends with given suffix. It returns true if this string ends with given suffix else returns false.


Signature

The syntax or signature of endsWith() method is given below.

 
  1. public boolean endsWith(String suffix)  

Parameter

suffix : Sequence of character


Returns

true or false


Java String endsWith() method example

 
  1. public class EndsWithExample{  
  2. public static void main(String args[]){  
  3. String s1="java by javatpoint";  
  4. System.out.println(s1.endsWith("t"));  
  5. System.out.println(s1.endsWith("point"));  
  6. }}  

Output:

true
true
Monday, 24 July 2017 03:57

Java String contains

The java string contains() method searches the sequence of characters in this string. It returns true if sequence of char values are found in this string otherwise returns false.


Signature

The signature of string contains() method is given below:

 
  1. public boolean contains(CharSequence sequence)  

Parameter

sequence : specifies the sequence of characters to be searched.


Returns

true if sequence of char value exists, otherwise false.


Throws

NullPointerException : if sequence is null.


Java String contains() method example

 
  1. class ContainsExample{  
  2. public static void main(String args[]){  
  3. String name="what do you know about me";  
  4. System.out.println(name.contains("do you know"));  
  5. System.out.println(name.contains("about"));  
  6. System.out.println(name.contains("hello"));  
  7. }}  
true
true
false
Page 7 of 31