Class 10 ICSE - Java

Basics of String Class

In java string is a class , having lot of different methods performed on string like valueOf(),replace(),length(),etc.To access these methods we create instance of this class.


This String class is available in java.lang package and to use this package we write

import java.lang.*;

This package is the default package ,so we dont need to explicitly write in the program,it is imported in the java program by default.

These String objects are immutable(cannot modified on same location).That is when we modify this class object as:

str="World" .

Then new instance will be created,the more the number of times the user will modify this object ,the more new instance will be created and consume the heap memory.

String is also a non-primitive datatype.It is a sequence of characters or we can say array of characters that is enclosed in a double quotes.

Ways for creating Java String Objects



There are basically 2 ways for creating string objects:
  1. By String literal
  2. By new keyword

String literal



In this technique, to initialize the string object we use datatype String.Here string is formed by array of characters which is enclosed inside the double quotes.

Syntax of this String literal techniques is:


After that Java compiler will create object of string class and assign value to it in double qoutes as

String a=new String("Aman");
Whenever we create any String object,it is stored in a String const pool,this is special type of memory for Strings which is located inside heap memory

New keyword



In this technique to create or initialize a Sring object we use new keyword.In this technique,string object are created in same manner as you create object of other classes

Syntax of this new keyword techniques is:



Whenever we create any String object using this technique,it is stored in Normal (non-pool) heap memory.

Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com