String in Java

in #utopian-io6 years ago (edited)

What Will I Learn?

This tutorial covers following topic

  • String Class in Java
  • Use String class methods in Java. Methods are concat() , toUpperCase() , toLowerCase() , trim() , startsWith() , endsWith() , replace() . charAt() , length() , valueOf(). Rest of the method will discuss in second part.

Requirements

-OS: Windows/Linux/mac
-Preinstalled Jvm
-Preinstalled Jdk
-Preinstalled Code editor.

Difficulty

You must have knowledge of simple java program writing

  • Intermediate

Tutorial Contents

In this tutorial I will tell you about String class and uses of String class methods

String Class

In java, String is a class which gives us a lot of methods to perform operation on string. Some of the methods are concat() , equals() , split() , length() etc.

java.lang.String is the name of String class.This call implements three interfaces. Serializable , compareble and CharSequence interface.

string.png

CharSequence interfaces represents sequence of charecter. It is implemented by three classes of String, StringBuilder , StringBuffer. Means we can handle string in java by using three classes.

charsequence.png

But in this tutorial I will show you handle string with String class.

Java String class is immutable which can not be change further. It means if we change something in string then a new instance is created. It simply means unchangeble. If a string object is created then we can not change or modify data and state but we can create new object.

Example-

class Civilstudy{
 public static void main(String args[]){
   String a="Steemit";
   a.concat("Decentralized");//concat() method appends the string at the end
   System.out.println(a);//will print Sachin because strings are immutable objects
 }
}
Output-
Steemit

BUILD SUCCESSFUL (total time: 0 seconds)

From the above example it is clear that here Steemit is not change but object is created with SteemitDecentralized. This is reason string is immutable.

How to create String objects-

There are two ways to create and object in Java.

  • String literals
  • New keywords.

String literals:-

This is created using double quote.

Example-

String a1="Steemit"
String a2="Steemit" //will not create new instance 

Every time when we create a string literal JVM checks and if that string value is already exist in pool then it will not create new object but will return the reference to the same instances.

New Keywords:-

Example-

String a=new String("Steemit");//creates two objects and one reference variable  
Jvm creates new string objects in normal heap memory. And string literals goes to constant pool. The varriable a refers to use object in heap.

Methods of String class-

Java provides many methods to perform operation on string. We will discuss here methods concat() , toUpperCase() , toLowerCase() , startsWith() , endsWith() , charAt() , valureOf() , replace() , trim() , length() .

Concatination-

In java there is two ways of concatination of string.

Using +operator-

Concatenation operator + is used in this process.

Example-

class Civilstudy{
 public static void main(String args[]){

String a="Steemit"+" Decentralized";
System.out.println(a);//Steemit Decentralized
}
}

Output-
 Steemit Decentralized

BUILD SUCCESSFUL (total time: 0 seconds)

Using concat() method-

This method concate one string to the end of other string.

Example-

class Civilstudy{
 public static void main(String args[]){

String a1="Steemit ";
String a2="Decentralized";

String a3=a1.concat(a2);

System.out.println(a3);//Steemit Decentralized
}
}

Output-
 Steemit Decentralized

BUILD SUCCESSFUL (total time: 0 seconds)

toUpperCase() method -

This method is used to convert all letters of string in uppercase.

Example-

public class Civilstudy{
 public static void main(String args[]){

String a="Steemit";
System.out.println(a.toUpperCase());//STEEMIT

System.out.println(a);//Steemit(no change in original)
}
}

Output-
STEEMIT

Steemit

BUILD SUCCESSFUL (total time: 0 seconds)

We can see it display the string in uppercase but it not convert it actual data value.


toLowerCase() method-

This method is used to convert all letters of string in lowercase.

Example-

public class Civilstudy{
 public static void main(String args[]){

String a="Steemit";

System.out.println(a.toLowerCase());//steemit
System.out.println(a);//Steemit(no change in original)
}
}

Output-
steemit

Steemit

BUILD SUCCESSFUL (total time: 0 seconds)

We can see it display the string in lowercase but it not convert it actual data value.

trim() method -

It is use to remove white spaces before and after any string.
Example-

public class Civilstudy{
 public static void main(String args[]){

String sa=" Steemit ";
System.out.println(a);// Steemit
System.out.println(a.trim());//Steemit
}
}

Output-
 Steemit

Steemit

BUILD SUCCESSFUL (total time: 0 seconds)

We can see it removes white spaces when we trim.


startsWith() method-

This method is boolean which returns true or false value. This method is used to check wether an string is starts with particuler substring or not.


endswith() method-

This method is also return true or false value.This method is used to check wether an string is ends with particuler substring or not.

Example-

public class Civilstudy{
 public static void main(String args[]){

String a="Steemit";
System.out.println(a.startsWith("St"));//true
System.out.println(s.endsWith("t"));//true
}
}

Output-
true
true

BUILD SUCCESSFUL (total time: 0 seconds)


charAt() method-

This method is used to find a perticuler letter at particuler index value.

Example-

public class Civilstudy{
 public static void main(String args[]){

String a="Steemit";
System.out.println(a.charAt(0));//chatrecter at index position 0
System.out.println(a.charAt(3));//chatrecter at index position 0
}
}

Output-
S

e

BUILD SUCCESSFUL (total time: 0 seconds)

length() method-

This is used for counting the length of string.
Example-

public class Civilstudy{
 public static void main(String args[]){

String a="Steemit";
System.out.println(a.length());//Counting of the length of string
}
}

Output-
6

BUILD SUCCESSFUL (total time: 0 seconds)


String ValueOf() method-

This method converts data type int, float , double , long , boolean ,char , chararray etc into string
Example-

public class Civilstudy{
 public static void main(String args[]){

int a=10;
String a1=String.valueOf(a);
System.out.println(a1+10);
}
}

Output-
1010

BUILD SUCCESSFUL (total time: 0 seconds)

Here we can see it treated as String because it concatenates the value instead add the value.


replace() method-

This method is used for replace the charecter with given charecter.
Example-

public class Civilstudy{
 public static void main(String args[])
{ 
String a1="Steemit is decentralized platform.";    
String aString=a1.replace("platform","website");//replaces all occurrences of "platform" to "website"    
System.out.println(aString);  
}
}
Output-
Steemit is decentralized website.

BUILD SUCCESSFUL (total time: 0 seconds)

Here we can see it replaces platform with website.

####Conclusion

In this tutorial we learn about String class and some of its methods.

What will next?

In the next tutorial we show the use of other importent method of String class which is not discuss in this tutorial.

Curriculum

This is first part of Java String.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

  • Your contribution is basic and not technical enough for approval.

You can contact us on Discord.
[utopian-moderator]

Coin Marketplace

STEEM 0.17
TRX 0.13
JST 0.027
BTC 60133.79
ETH 2686.90
USDT 1.00
SBD 2.49