Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Difference Between String, StringBuilder And StringBuffer Classes in Java

Difference Between String, StringBuilder And StringBuffer Classes in Java

Here we will explain the difference between String , StringBuilder and StringBuffer . As you will find that there are minor differences between the above mentioned classes.
String
  • String is immutable( once created cannot be changed ) object.
  • The object created as a String is stored in the Constant String Pool
  • Every immutable object in Java is thread safe, that implies String is also thread safe.
  • String cannot be used by two threads simultaneously.
  • String once assigned cannot be changed.
Example
String  demo = " hello " ;
// The above object is stored in constant string pool and its value cannot be modified.
demo = "Bye" ;     //new "Bye" string is created in constant pool and referenced by the demo variable 
// "hello" string still exists in string constant pool and its value is not overridden but we lost reference to the "hello" string
StringBuffer 
  • StringBuffer is mutable means one can change the value of the object .
  • The object created through StringBuffer is stored in the heap . - StringBuffer has the same methods as the StringBuilder , but each method in StringBuffer is synchronized that is StringBuffer is thread safe.
Due to this it does not allow two threads to simultaneously access the same method. Each method can be accessed by one thread at a time.
But being thread safe has disadvantages too as the performance of the StringBuffer hits due to thread safe propert . Thus StringBuilder is faster than the StringBuffer when calling the same methods of each class.
String Buffer can be converted to the string by using toString() method.
Example:
StringBuffer demo1 = new StringBuffer("Hello") ;
// The above object stored in heap and its value can be changed.
demo1=new StringBuffer("Bye")
demo1=demo1.append("See You");
If you print demo1 you will get "HelloByeSee You"
StringBuilder 
  • StringBuilder is same as the StringBuffer , i.e. the object is stored in heap and it can also be modified.
  • The main difference between the StringBuffer and StringBuilder is that StringBuilder is also not thread safe. It have all the methods of StringBuffer - StringBuilder is fast as it is not thread safe.
String
StringBuffer
StringBuilder
Storage
Constant String Pool
Heap
Heap
Mutable
No
Yes
Yes
Thread Safe
Yes
Yes
No
Performance
Fast
Slow
Fast
Hope you guys like it. Let me now in comments if you guys have any doubts. Happy Learning !!!

Explain Strings in Java

Explain Strings  in Java

In this we are going to learn about Strings in Java

  • The String class is immutable (constant), i.e. Strings in java, once created and initialized, cannot be changed.
  • The String is a final class, no other class can extend it, and you cannot change the state of the string.
  • String values cannot be compare with '==', for string value comparision, use equals() method.
  • String class supports various methods, including comparing strings, extracting substrings, searching characters & substrings, converting into either lower case or upper case, etc.
  • The string examples given below describes more on string functionlaity.

The follwoing example expalin's about different way's of string initilization


package com.onlinecodegeek.javase;
public class StringInitializationEx {
public static void main(String a[]){
String str1 = "This is a string object";
String str2 = new String("this is also string object");
char[] c = {'a','b','c','d'};
String str4 = new String(c);
String str5 = str1+" This is another String object";
System.out.println("str1"+str1);
System.out.println("str2"+str2);
System.out.println("str4"+str4);
System.out.println("str5"+str5);
}
}
Output
str1This is a string object
str2this is also string object
str4abcd
str5This is a string object This is another String object

 

How to find largest word in a given string using Java with O(n)

How to find largest word in a given string using Java with O(n)

package com.onlinecodegeek.java;

public class FindLargetWord {

public static void main(String args[]) {
String s = " Welcome to Java World";
String[] wordsArray = s.split(" ");
int maxsize = 0;
String maxWord = "";
for (int i = 0; i < wordsArray.length; i++) {
if (wordsArray[i].length() > maxsize) {
maxWord = wordsArray[i];
maxsize = wordsArray[i].length();
}

}
System.out.println("Max sized word is " + maxWord + " with size " + maxsize);
}
}

 

Thulasiram

How to find Length of Last Word in long String



How to find Length of Last Word Length in long String
SOURCE : LastWordLength.java
INPUT
String s = "i love this country and and also it seems that we have the correctness of the somengdfer hereIam   ";
OUTPUT
7


Sandeep Kumar D

Hi, I have written and developed this post so that most of people will be benefited. I'm committed to provide easy and in-depth tutorials on various technologies.I hope it will help you a lot.
- Sandeep Kumar D

Follow Me @Google+