You are on page 1of 3

Name : T K Santhosh

Roll No : 22Z433
Date : 27/11/2023

Experiment 11

Object Oriented Programming Laboratory

Aim : To write a java program to work with different string handling


methods and functions.

package string_handling;
import java.lang.String;

public class string_handling_practice {

public static void main(String[] args){


// Creating string with string literal.
/*
* REF : https://www.javatpoint.com/java-string
*
* Java stores the string in a special memory location
`String constant pool`.
* If the string you trying to create already exist in the
string pool. It
* won't be re-created instead it just point the variable
to the already
* existing object.
*/
String name = "santhosh";

// Creating string with the new keyword with list of char.


char[] letters = {'s', 'a', 'n'};
String new_name = new String(letters);

System.out.println("String literal : " + name);


System.out.println("String building : " + new_name);

// Creating string with new keyword.


Name : T K Santhosh
Roll No : 22Z433
Date : 27/11/2023
String my_name = new String("santhosh");
System.out.println("Id of -santhosh: " +
System.identityHashCode("santhosh"));
/*
* This will create a new object in the non-pool heap
memory and creates a string
* obejct santhosh in the pooled memory. And it has a
reference variable my_name
* which refers to the non-pooled memory.
*/

// If you notice carefully the id of the line 27 and id of


the 37 is identical.

String your_name = "santhosh";


System.out.println("Id of your_name : " +
System.identityHashCode(your_name));

// METHODS SUPPORTED BY THE STRING CLASS.


// 1. char charAt(int index)
String string = "I am santhosh";
System.out.println("The character at index 6 : " +
string.charAt(6));

// 2. int length()
System.out.println("Length of the string : " +
string.length());

// Formatting the string using format() method.


/* REF : Online for more.
* NullPointerException : if format is null.
* IllegalFormatException : if format is illegal or
incompatible.
*/

// 5. String substring(int beginIndex)


System.out.println("My name in the string is : " +
string.substring(5));
Name : T K Santhosh
Roll No : 22Z433
Date : 27/11/2023
// 6. boolean contains(CharSequence s)
System.out.println("The string contains santhosh : " +
string.contains("santhosh"));

// 10. boolean equals(object)


System.out.println("The string and santhosh are not equal
: " + string.equals("santhosh"));

// 11. boolean isEmpty()


System.out.println("The string is not empty : " +
string.isEmpty());

// 12. Concatenation
System.out.println("Concatenation : " + string.concat("I
am a SWE."));

// Replacing a sequence
System.out.println("Relacing a sequence : " +
string.replace("santhosh", "santhoshtk."));

}
}

santhoshtk@intense ~/Desktop/PSG-TECH/3rd-sem/java

String literal : santhosh


String building : san
Id of -santhosh: 2060468723
Id of your_name : 2060468723
The character at index 6 : a
Length of the string : 13
My name in the string is : santhosh
The string contains santhosh : true
The string and santhosh are not equal : false
The string is not empty : false
Concatenation : I am santhoshI am a SWE.
Relacing a sequence : I am santhoshtk.

You might also like