String in Java with example
For Video Tutorial : Move on Youtube Channel
Note : Select the playlist as per your need & move with number sequence
Video for this tutorial : CLICK HERE
Strings are a sequence of characters but in java Strings are objects.
A Java string is a sequence of characters that exist as an object of the class java. lang
Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.
package csc;
public class csc_string1
{
public static void main(String args[])
{
String s1 = "chandan";
String s2 = "chandan";
String s3 = new String("chandan");
String s4 = new String("chandan");
String s5 = new String(s1);
char arr[] = {'h','e','l','1','0','w', 'o','r', 'l', 'd'};
String s6 = new String(arr);
String s7 = new String(arr, 0, 4);
System.out.println(s1==s2);
System.out.println(s3==s4);
System.out.println(s5);
System.out.println(s6);
System.out.println(s7);
int x = s3.length();
System.out.println(x);
}
}
Output :public class csc_string1
{
public static void main(String args[])
{
String s1 = "chandan";
String s2 = "chandan";
String s3 = new String("chandan");
String s4 = new String("chandan");
String s5 = new String(s1);
char arr[] = {'h','e','l','1','0','w', 'o','r', 'l', 'd'};
String s6 = new String(arr);
String s7 = new String(arr, 0, 4);
System.out.println(s1==s2);
System.out.println(s3==s4);
System.out.println(s5);
System.out.println(s6);
System.out.println(s7);
int x = s3.length();
System.out.println(x);
}
}
true
false
chandan
hel10world
hel1
7
String In Java :
String is a sequence of characters & in java , string is an object that represents a sequence of characters.
or STRING is character array.
The java.lang.String class is used to create string object.
In java, string is an immutable object which means it is constant and can not be changed once it has been created.
Now Question comes that , why java string is immutable ??????
Answer : immutable means unmodified or unchangeable.
better will be understood by example :-
public class chandan
{
public static void main(String args[])
{
String a = "chandan";
a.concat("singh"); // concat() method appends the string at the end
System,out.println(a);
}
}
output ; {
public static void main(String args[])
{
String a = "chandan";
a.concat("singh"); // concat() method appends the string at the end
System,out.println(a);
}
}
chandan
__________ s -----> |"chandan" |
----------- "chandan singh"
Two objects will be created but a reference variable still refer to "chandan" not to "chandan singh".
public class chandan
{
public static void main(String args[])
{
String a = "chandan";
a = a.concat("singh"); // concat() method appends the string at the end
System,out.println(a);
}
}
Output : Chandan Singh{
public static void main(String args[])
{
String a = "chandan";
a = a.concat("singh"); // concat() method appends the string at the end
System,out.println(a);
}
}
Why we don't use "new" keyword for string to create them?
Answer :-
1) Strings are immutable --> no need to use new keyword.
2) We would want to use new because it allows us to create a separate
object that references a seprate value.
3) When working with Strings this is not needed because the original String will
not be modified because Strings are immutable.
How to create string object?
There are two ways to create String object :
1). By string literal
2). By new keyword
1). String Literal : String a = "welcome at iamchandan.com"; // this will not create new instance
2). By new keyword
String a = new String("welcome at iamchandan.com");
Example :
public class LN_string
{
public static void main(String args[])
{
String LN = "Hello iamchandan";
System.out.println(LN);
int stringlength = LN.length();
System.out.println(stringlength);
String lowercase = LN.toLowerCase();
System.out.println(lowercase);
String uppercase = LN.toUpperCase();
System.out.println(uppercase);
String replace = LN.replace('e', 'x');
System.out.println(LN.indexof('a'));
}
}
{
public static void main(String args[])
{
String LN = "Hello iamchandan";
System.out.println(LN);
int stringlength = LN.length();
System.out.println(stringlength);
String lowercase = LN.toLowerCase();
System.out.println(lowercase);
String uppercase = LN.toUpperCase();
System.out.println(uppercase);
String replace = LN.replace('e', 'x');
System.out.println(LN.indexof('a'));
}
}
String Builder and String Buffer in JAVA
package com.lang.beginner;
public class ConsTest
{
public static void main(String args[])
{
String str = String.join("--", "Chandan", "Singh");
System.out.println(str);
StringBuffer sbf = new StringBuffer("way2testing");
sbf.append(".com");
System.out.println(sbf);
StringBuilder sbd = new StringBuilder("Software testing tutorials");
sbd.append("way2testing");
System.out.println(sbd);
long stime = System.currentTimeMillis();
StringBuffer sbf2 = new StringBuffer("way2testing");
StringBuilder sbd2 = new StringBuilder("way2testing");
for(int i = 0; i<10000; i++)
{
sbf2.append(".com");
}
System.out.println("Time taken by String buffer == " + (System.currentTimeMillis()- stime) + " " + "ms");
long stime2 = System.currentTimeMillis();
for(int i = 0; i<10000; i++)
{
sbd2.append(".com");
}
System.out.println("Time taken by String builder == " + (System.currentTimeMillis()- stime2) + " " + "ms");
}
}
public class ConsTest
{
public static void main(String args[])
{
String str = String.join("--", "Chandan", "Singh");
System.out.println(str);
StringBuffer sbf = new StringBuffer("way2testing");
sbf.append(".com");
System.out.println(sbf);
StringBuilder sbd = new StringBuilder("Software testing tutorials");
sbd.append("way2testing");
System.out.println(sbd);
long stime = System.currentTimeMillis();
StringBuffer sbf2 = new StringBuffer("way2testing");
StringBuilder sbd2 = new StringBuilder("way2testing");
for(int i = 0; i<10000; i++)
{
sbf2.append(".com");
}
System.out.println("Time taken by String buffer == " + (System.currentTimeMillis()- stime) + " " + "ms");
long stime2 = System.currentTimeMillis();
for(int i = 0; i<10000; i++)
{
sbd2.append(".com");
}
System.out.println("Time taken by String builder == " + (System.currentTimeMillis()- stime2) + " " + "ms");
}
}
Difference between String builder and string buffer
StringBuffer | StringBuilder |
---|---|
String buffer is synchronized i.e. thread safe, means two threads can not call the methods of stringbuffer simultaneously | StringBuilder is non-synchrnized i.e. not thread safe means two threads can call the methods of stringbuilder simultaneously |
String buffer is less efficient than string builder | String builder is more efficient than stringbuffer |
String buffer was introduced in java 1.0 | String builder was introduced in java 1.5 |
Tags :-
No comments:
Post a Comment