Java String tutorial with easy explanation

What do you mean by string in java ?

Strings are a sequence of characters,but in a java strings are treated as objects

It is terminated with the help of null character ie ‘\0’.

It is the sequence of array of character.

Syntax:

<String> str_variable=”string_name”;

Example

String name=”hello”;

Or

Ch ch[ ]={‘h’,’e’,’l’,’l’,’o’};

String str=new String(ch);

How can create string  in java

1.By the string literal

2.By the new keyword

1.By the string literal

It is created with the help of double quotation ie “ “.

Example:

String name=”hello”;

2.By the new keyword

It is created with the help of new keyword.

Example:

String name=new String(“hello”);

Note:

String objects are stored in the special memory known as string constant pool.

Example that how to declare string in both ways

public class Test
{
Public static void main(String[ ] args)
{
//declare string using string literal

String name=”hello”;

//printing the hello
System.out.println(“string one=”+name);

//declare string using new keyword
String new_name=new String(“hello world”);

//printing hello world
System.out.println(“string two=”+new_name);

//by array of character
Char ch[ ]={‘h’,’e’,’l’,’l’,’o’,’p’};
String array_name=new String(ch);

//printing the array of character
System.out.println(“string three=”+array_name);
}
}

Output:

string one=hello

string two=hello world

string three=hellop

Some  string methods are given below:

1.length( )=It returns the length of string.

2.charAt( )=It returns the character at a specific index.

3. compareTo(string s)=It compare the two strings.

4.append( )=It is used to insert any string before any string.

5.isEmpty( )=it checks if string is empty and return boolean value.

Immutable string in java

String immutable means unchangeable or unmodifiable ie once string is  created it can not be changed but new string object can be created.

Let’s try to understand with the help of example.

Import java.io.*;
class Test
{
Public static void main(String[ ] args)
{
String s=”placement”;
s.concat(“preparation”);

//Here s still referring “placement” because string is immutable
System.out.println(s);
}
}

Output

Placement

Why only prints “placement”.Let’s try to understand step by step

Here we can see that the JVM  creates a string “placement” and refer object s.

Secondly again  JVM create the string “preparation” but does not refer to it.so second string “preparation” is not concat with the   “placement” string.

Therefore,Here reference ‘s’ refers to the original string ie ‘placement’.

But explicitly we can assign it into the reference variables.

class Test
{
Public static void main(String[ ] arg)
{
String s=”placement”;
s=s.contact(“preparation”);//Here s points the placement preparation
System.out.println(s);
}
}

Output:

Placement preparation

Java string comparison

In java we can compare the string on the basis of content and references.

Here content means real string and references means how we have defined the string 

ie  literal or new keyword.

There are three ways to compare the string

1.equals( ) methods

2.== operator

3.compareTo(  ) method

1.equals( ) methods

equals( ) methods compare the string on the basis of original content.  

Example:

class Test
{
Public static void main(String[ ] args)
{
String s=”placement”;
String s1=new String(“placement”);
String s2=”preparation”;
String s3=”placement”;
System.out.println(s.equals(s1));
System.out.println(s.equals(s2));
System.out.println(s.edquals(s3));
}
}

Output:

True

false

true

2.== operator

== operator compare the string on the basis of reference(means how string is created with the help of string literal or new keyword)  not the original value/content.

If reference value is same the it return true value otherwise false.

Try to understand with the help of example:

class Test
{
Public static void main(String[ ] args)
{
String s1=”placement”;
String s2=new String(“placement”);
String s3=”placement”;
System.out.print(s1==s2);
System.out.println(s1==s3);
}
}

Output:

false

true

3.compareTo( ) method

CompareTo( ) method returns the integer value in the following cases:

1.If str1>str2 return 1

2.if str1<str2 return -1

3.str1==str2 return 0

Let’s try to understand with the help of example

class Test
{
Public static void main(String[ ] args)
{
String s1=”hello”;
String s2=”placement”;
String s3=”hello”;
System.out.println(s1.compareTo(s3));
System.out.println(s2.compareTo(s3));
System.out.println(s3.compareTo(s1));
}
}

Output:
0
1
-1

StringBuffer( ) &StringBuilder( )

StringBuffer( )

String buffer is a mutable means modifiable or changeable.

It is a thread-safe and synchronised.

StringBuilder( )

String builder is also mutable means modifiable or changeable.

It is not thread-safe and non-synchronised.

It is faster than the string buffer.

Example of StringBuffer( )

class Test
{
Public static void main(String[ ] args)
{
StringBuffer obj=new StringBuffer(“placement”);
obj.append(“preparation”);
System.out.println(obj);
}
}

Output:

Placementpreparation

Example of StringBuilder( )

class Test
{
Public static void main(String[ ] args)
{
StringBuilder obj=new StringBuilder(“placement”);
obj.append(“preparation”);
System.out.println(obj);
}
}

Output:

Placementpreparation

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *