0% found this document useful (0 votes)
19 views3 pages

Class 5

Java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Class 5

Java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Date: 04-05-2024

___________________

Non primitive data type:


-------------------------
-> We can have classes and objects here

String:
--------
-> String is a class present in java.lang.*
-> Using the object we can store the group of character or sequence of character
-> String class contains number of methods. Using these methods we can perform
multiple operation like removal of character, add character, split, etc...
-> String is immutable

There are two ways to create String object:


--------------------------------------------
-> Using new keyword
-> Using string literal

1). new keyword:


-------------
-> Whenever a programmer creating an object using the new keyword the value of the
string will be stored on the heap memory and as well as String constant pool
-> If a programmer try to create a multiple string object for each object it will
create a seperate instance of memory allocation

Syntax for creating string object:


-----------------------------------
String str = new String("welcome to session");
String str2 = new String("welcome to session");

heap -------------------------------> String Constant Pool


| |
welcome to session -- str welcome to session
welcome to session -- str2

class Test{
public static void main(String[] args){
String s = new String("welcome to session");
System.out.println(s);
String str2 = new String("welcome to session");

//to compare two strings '==' operator is used

int a=10;
int b=10;
char ch='a';

}
}

O/P:
-----
welcome to session
true
true
true
c.e
false

2). String Literal:


-----------------
-> Whenever a programmer creates an object using the String literal the object
value will be stored only in the String Constant Pool

Syntax for creating string object:


-----------------------------------

String str = "welcome to session";


String str1 = "welcome to session";

heap---------------> String Constant Pool


|
-------------------------
| welcome to session | --> str, str1
-------------------------

class Test{
public static void main(String[] args){
String s = "welcome to session";
String str1 = "welcome to session";
System.out.println(s);
System.out.println(s == str1);
}
}

o/p:
-----
welcome to session
true

Difference between string literal and new keyword:


---------------------------------------------------
eg:
----
class Test{
public static void main(String[] args){
String s = "welcome to session";
String str2 = "welcome";
String str3 = s;

String ss = new String("welcome to session");


String ss2 = new String("welcome");
String ss3 = ss;
System.out.println(ss == ss2);
System.out.println(ss == s);
System.out.println(str2 == str3);
System.out.println(ss2 == ss3);
System.out.println(ss3 == ss);
System.out.println(s == str3);
}
}

o/p:
-----
false
false
false
false
true
true

Methods in the string:


-----------------------
charAt(int index)
indexOf(char a)
indexOf(String s)
split(String s)
length()
trim()
concat(String s)
substring(int startindex, int lastindex-1)

Comparison of 'concat()' method and '+' :


------------------------------------------
class Test{
public static void main(String[] args){
String s = "welcome to session";
int a = 10;
int b = 20;
String s2 = a+b+s;
String s3 = s+a+b;
System.out.println(s);
System.out.println(s2);
System.out.println(s3);

s.concat(" hello everyone"); // no reference


System.out.println(s); // will print welcome to session

s = s.concat(" hello everyone"); // s as a reference


System.out.println(s); // will print welcome to session hello everyone

String str = "hello"+null;


System.out.println(str);

String ob = null;
//ob.length(); --------------> c.e

//str = str.concat(null); -------------------> c.e Null Pointer Exception


}
}

o/p:
-------
welcome to session
30welcome to session
welcome to session1020
welcome to session
welcome to session hello everyone
hellonull
___________________________________________________________

You might also like