0% found this document useful (0 votes)
1 views2 pages

Strings Java

The document explains the concept of Strings in Java, highlighting that they are immutable sequences of characters defined in the java.lang package. It provides examples of string initialization, various string methods such as length(), charAt(), compareTo(), substring(), isEmpty(), toUpperCase(), toLowerCase(), and trim(). The output of the program demonstrates the usage of these methods with a sample string 'Ram'.

Uploaded by

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

Strings Java

The document explains the concept of Strings in Java, highlighting that they are immutable sequences of characters defined in the java.lang package. It provides examples of string initialization, various string methods such as length(), charAt(), compareTo(), substring(), isEmpty(), toUpperCase(), toLowerCase(), and trim(). The output of the program demonstrates the usage of these methods with a sample string 'Ram'.

Uploaded by

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

Strings in java :

●​ String in java is a class which is present in java.lang package.


●​ String is defined as Sequence of characters.

/* Strings in java are immutable


that means the String once defined can't be modified , if we want
any modification we need to use other String
*/
class Strings
{
public static void main(String[] args)
{
String name1 = "Ram"; // initialisation using literals
String name2 = new String("Ram"); // Initialisation with new
keyword
String fullName = name1 + " " + name2;
System.out.println("Full Name : "+fullName);

// length() function returns length of string


System.out.println("Length of full name : "+fullName.length());
// length of string

// chaeAt(index) function returns character present at specific


index
System.out.println("CHaracter at index 4 in full_name :
"+fullName.charAt(4));

/* CompareTo() function compares one string to another ,


returns 0 if both equal
return +ve value if str1 > str2
returns -ve value if str1 < str2
*/
if(name1.compareTo(name2)==0)
{
System.out.println(name1 +" is equals to "+name2);
}
// substring(starting index,ending index) functiion returns the
characters between given indexes

System.out.println("Substring in given String :


"+fullName.substring(0, 3));
/* isEmpty() function used to check that the String is empty
or not
if string is empty ist returns true , if string is not
empty it returns false
*/
if(name1.isEmpty()==false)
{
System.out.println("String is not empty");
}

// toUppercase() & toLowercase() fuctions used to convert the


given string in uppercase & lowercase
System.out.println("Uppercase : "+fullName.toUpperCase());
System.out.println("Lowercase : "+fullName.toLowerCase());

// trim() function erases whitespace from left & right side of


string
System.out.println("Trim : "+fullName.trim());
}

Output : -
Full Name : Ram Ram
Length of full name : 7
CHaracter at index 4 in full_name : R
Ram is equals to Ram
Substring in given String : Ram
String is not empty
Uppercase : RAM RAM
Lowercase : ram ram
Trim : Ram Ram

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy