Sunday, 28 June 2020

Java Strings and String Methods

In languages like C and C++ strings are character arrays and end with a null character. The null character is used to process the string and it is useful in many situations. The sequences of characters are stored in an array. But in Java, the string is an object that represents a sequence of characters. The java.lang.String class is used to create string class objects.

Things to note about Java strings:
  1. Java strings are objects of String class.
  2. Java strings are stored on a separate memory location called "String constant pool".
  3. There are also null-terminated strings in java, but they are extensively used in Internet applications.
  4. There are two types of java strings: Mutable and immutable.

How to Declare strings in Hava?

There are two ways to create a String object: 

1. By string literal
2. By new keyword 

1.Java String literal is created by using double quotes. 

For Example: String s="welcome"; 

Each time you create a string literal, the JVM checks the "string constant pool" first. If the string already exists in the pool, a reference to the pooled instance is returned. If the string doesn't exist in the pool, a new string instance is created and placed in the pool. 

For example String s1="Welcome"; 

String s2="Welcome";//It doesn't create a new instance  

In the above example, only one object will be created. Firstly, JVM will not find any string object with the value "Welcome" in a string constant pool that is why it will create a new object. After that it will find the string with the value "Welcome" in the pool, it will not create a new object but will return the reference to the same instance. 
string constant pool is a separate block of memory where the strings are stored by JVM.
2. String s=new String("Welcome"); 
In such a case, JVM will create a new string object in normal (non-pool) heap memory, and the literal "Welcome" will be placed in the string constant pool. The variable s will refer to the object in a heap (non pool).

Java String class methods: 

The String class defines a number of methods that allow us to accomplish a variety of string manipulation tasks.
a) Java String length(): The Java String length() method tells the length of the string. It returns the count of total number of characters present in the String.
b) Java String compareTo(): The Java String compareTo() method compares the given string with the current string. It either returns a positive number, negative number or 0.
c) Java String concat(): The Java String concat() method combines a specific string at the end of another string and ultimately returns a combined string. It is like appending another string. 

d) Java String Trim(): The java string trim() method removes white spaces at the beginning and ending.

e) Java String toLowerCase(): The java string toLowerCase() method converts all the characters of the String to lower case

f) Java String toUpper(): The Java String toUpperCase() method converts all the characters of the String to upper case

g) Java String ValueOf(): This method converts different types of values into a string.

h) Java String replace(‘x’,’y’): Replace all appearances of X with Y

i) Java String equals(): The Java String equals() method compares the two given strings on the basis of content of the string.

j)Java ChartAt(n): Gives the nth character of the string.  


Java Example Program for all string methods 
import java.util.Scanner;
 public class Alphabetical_Order
 {
 public static void main(String[] args)
 {
 int n;
 String temp;
 Scanner s = new Scanner(System.in);
 System.out.print("Enter number of names you want to enter:");
 n = s.nextInt();
 String names[ ] = new String[n];
 Scanner s1 = new Scanner(System.in);
 System.out.println("Enter all the names:");
 for(int i = 0; i < n; i++)
 {
 names[i] = s1.nextLine();
 }
 for (int i = 0; i < n; i++) 
  {
  for (int j = i + 1; j < n; j++) 
   {
 if (names[i].compareTo(names[j])>0)
 {
 temp = names[i];
 names[i] = names[j];
 names[j] = temp;
 }
 }
 }
 System.out.print("Names in Sorted Order:");
 for (int i = 0; i < n - 1; i++)
 {
 System.out.print(names[i] + ",");
 }
 System.out.print(names[n - 1]);
 }
 }

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top