In this article String in Java we give the information about String is an object representing a sequence of characters.

String in Java: 

String is a series of characters or group of characters, double quotes ” ” are used to define the string in Java. String class is used to define string in Java. String is a non-primitive(i.e., there is size is not fixed) data types because it references a memory location where data is stored in the heap memory (or String Constant Pool) i.e., it references to a memory where an object is actually placed. And thus the variable of a non-primitive data types is also called reference data types or object reference variable. This object reference variable lives on the stack memory and the object to which it points always lives on the heap memory.

String is an object representing a sequence of characters. An array of characters functions similarly to a Java string. We use java.lang.String class to create a String object.

There are many java string methods used to perform different operations on strings, such as split(), compare(), equals(), substring(), concat(), replace(), intern(), compareTo( ) etc.

class StringTest

{

  public static void main(String[] args)

  {

    String name = “Rajveer”;

    System.out.println(name);

  }

}

Output

javac StringTest.java

java StringTest

Rajveer

To create a String, there are three classes-

  1. String
  2. StringBuffer
  3. StringBuilder

How to Create a String Object in Java

There are two ways to create a string object:

  1. Using String Literal
  2. Using New Keyword

1. Using String Literal

Java string literals are created and represented using double quotes, and all characters are appended between these quotations. This method makes Java more memory efficient because new objects are not created in this method.

Every time we create a string literal, the Java Virtual Machine (JVM) checks the string constant pool. If a string exists in the pool, it references the pooled instance. However, if the string does not exist in the pool, it creates a new String instance and places it in the pool.

Example

String s=”hello”;

2. Using New Keyword

Another way to create a Java string is to use the new keyword. Just as a new keyword is used to create an instance of any other class, it also works to create an instance of the String class.

The primary difference between the two methods using string literals and using the new keyword is that when a string is created using the latter method, the JVM creates a new object of the string in heap memory (non-pool). And the pool is kept in the string constant.

We can create String class object by – String s=new String();

It will create an IMMUTABLE object.

Example

String s = new String(“hello”);

Regardless of whether the same valued strings exist in heap memory or not, string objects created using the new keyword are placed in heap memory, i.e., outside the string constant pool.

Syntax

String stringName = new String(“string_value”);

public class MyClass {

  public static void main(String[] args) {

    String text = “Hello World”;

    System.out.println(text);

  }

}

// Result: Hello World

Note:

  • Using New Keyword you can create a two object one in Heap memory and other in String Constant Pool
  • Using String Literal you can create only one object in String Constant Pool
  • The String object present in SCP are not applicable for Garbage Collector because a reference variable internally is maintained by JVM.

Java String Methods

As we know that Java Strings is actually an object, which contains a lot of methods, such as length(), toUpperCase(), toLowerCase(), indexOf() etc.

  1. length() : To find the length of the String.
  2. toUpperCase() : To convert String to UPPERCASE
  3. toLowerCase() : To convert String to lowercase.
  4. indexOf(): To find the position of any character in the String.

public class MyClass {

  public static void main(String[] args) {

    String text = “HELLO World”;

    System.out.println(text.length());

    System.out.println(text.toUpperCase());

    System.out.println(text.toLowerCase());

    System.out.println(text.indexOf(‘W’));

  }

}

 11

HELLO WORLD

hello world

6

String Concatenation

Joining two or more strings is called Concatenation, Concatenation is done by the two methods given below.

  1. By using + operator.
  2. By using concet() method.

public class MyClass {

  public static void main(String[] args) {

    String text1 = “Ram”;

    String text2 = “Singh”;

    System.out.println(text1 + text2);

    System.out.println(text1 + ” ” + text2);

    System.out.println(text1.concat(text2));

    System.out.println(text1 + ” “.concat(text2));

  }

}

OUTPUT:

RamSingh

Ram Singh

RamSingh

Ram Singh

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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