MainMenu

Home Core Java Overview Maven Tutorials

Saturday, 14 January 2017

String in Java with example

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.

String Example:Below example have most type of string creation method :

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 :
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 characte 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 Arrises , whay java string is immutable ??????
Answer : immutable means unmodifiable 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 ; 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
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'));
}
}

Tags :-

What is string in java?

Example of string in java?

why string is immutable in java?

2 comments:

  1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students.

    ReplyDelete
  2. I am happy to find this post very useful for me, as it contains lot of information. I always prefer to read the quality content and this thing I found in you post.

    tutuapp apk
    instagram sign up

    ReplyDelete