Assignment :5 - Sairab/Learning-_JAVA GitHub Wiki

What is string ?

String is a data type. But in java basically it is an object that repersents the sequence of char values.It is immutable object in java it means that it can not be changed after creation in string but a new instance is created.

In two way string is created in java:

1: using double quotes

String = “Pakistan”

2: using new keyword

String s = new String(Pakistan)

String Example:

public class StringExample{

public static void main(String [] abc){

String s1 = “Pak”;

String s2 = new String(“Army”);

System.out.println(s1);

System.out.println(s2);

} }

Some functions of java String are

Int length():
Use to return the length of string

char charAt(int index):
return char value for particular index

boolean contians(Charsequence s):
return true or flas

String toLowerCase():
return string in lower case

String toUpperCase():
return string in upper case

What is Immutable?

If an object can not change its state is called immutable after that it is constructed.It is particularly useful in concurrent application.Since they cannot change state .

1: Do not provide setter methods methods that modify fields or objects .

2: All fields of immutable class should be final.

3: Object must be properly construct.

What is Mutable?

Mutable object fields can be changed after it is created.

For Example:

public class change{

public static void main (String abc[]) {

String s1 = “Hello World”;

System.out.println(s1);

String s1 = s1.toUpperCase();

System.out.println(); } }