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

Strings_1.1

The document provides an overview of strings in Java, explaining their characteristics, creation methods, and various operations such as concatenation, comparison, and manipulation methods like length(), charAt(), toUpperCase(), and toLowerCase(). It includes code examples to illustrate these concepts and exercises for checking string properties like palindrome and length. Additionally, it highlights the differences between string literals and objects created with the new keyword.

Uploaded by

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

Strings_1.1

The document provides an overview of strings in Java, explaining their characteristics, creation methods, and various operations such as concatenation, comparison, and manipulation methods like length(), charAt(), toUpperCase(), and toLowerCase(). It includes code examples to illustrate these concepts and exercises for checking string properties like palindrome and length. Additionally, it highlights the differences between string literals and objects created with the new keyword.

Uploaded by

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

Strings

• Sequence of characters and not a primitive data type


• Example: Hello
• In java, strings are objects.
• The class “String” comes under java.lang package.
• Internally string is implemented as a character array.
• E.g:
H e l l 0
How to create a string?
Two ways to create a string:
1) String literal
2) Using new keyword
String literal:
String str = “Hello”
Using new Keyword:
String s = new String(“Hello”)
1 // Predict the output
2
3 class Main{
4 public static void main(String args[]){
5 String str = "Programming";
6 String s = new String("World");
7 System.out.println(str);
8 System.out.println(s);
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
String Literal - String str =
“Hello”
How it is working in the backend?

str Hello

String pool

Heap
String Literal - String str =
“Hello”
How it is working in the backend?
String str1 = “Hello”

str Hello

str1

String pool

Heap
new - String s = new
String(“Hello”)
How it is working in the backend?

str Hello

str1

s
String pool

Heap
new - String s = new
String(“Hello”)
How it is working in the backend?

str Hello

str1

s Hello

String pool

Heap
1 // Predict the output of the below code
2
3 class Main{
4 public static void main(String args[]){
5 String str = "Hello";
6 String str1 = "Hello";
7 String str2 = new String("Hello");
8 System.out.println(str == str1);
9 System.out.println(str == str2);
10 }
11 }
12
13
14
15
Output
true
false
1 // Predict the output of the below code
2
3 class Main{
4 public static void main(String args[]){
5 String str1 = "Hello";
6 String str2 = "Hello";
7 String s1 = new String("Hello");
8 String s2 = new String("Hello");
9 System.out.println(str1 == str2);
10 System.out.println(str2 == s1);
11 System.out.println(s1 == s2);
12 }
13 }
14
15
Output
true
false
false
1 // Predict the output of the below code
2
3 class Main{
4 public static void main(String args[]){
5 String s1 = new String("Noughat");
6 String s2 = new String("Noughat");
7 System.out.println(s1 == s2);
8 System.out.println(s1.equals(s2));
9 }
10 }
11
12
13
14
15
Output
false
true
Question 1
Write a Java code to check whether the input string matches with
the word “Dhoni”

Sample Sample
Input:
Kohli Output:
Not matching
1 // Solution
2
3 import java.util.Scanner;
4 class Main{
5 public static void main(String args[]){
6 Scanner in = new Scanner(System.in);
7 String str = "Dhoni";
8 String s = in.next();
9 if(str.equals(s)){
10 System.out.print("Matching");
11 }
12 else{
13 System.out.print("Not matching");
14 }
15 }
16 }
17
18
19
20
21
22
The length() method
The java String length() method returns the length of the string
The length of the string will be an integer
1 // Predict the output of the below code
2
3 class Main{
4 public static void main(String args[]){
5 String str = "Programming";
6 System.out.println(str.length());
7 String s = "Let's continue";
8 System.out.println(s.length());
9 }
10 }
11
12
13
14
15
Output
11
14
The charAt() method
The java String charAt() method returns a char value at the given
index number
The index number starts from 0 and goes up to (n-1), where n is
length of the string.
1 // Predict the output of the below code
2
3 class Main{
4 public static void main(String args[]){
5 String str = "Java";
6 char ch = str.charAt(2);
7 System.out.println(ch);
8 System.out.println(str.charAt(3));
9 System.out.println(str.charAt(4));
10 System.out.println(str.charAt(-1));
11 }
12 }
13
14
15
Output
v
a
Error
The toUpperCase() method
The java String toUpperCase() method converts all the lowercase
characters of a string into uppercase
It will not alter the already existing uppercase characters in the
string.
E.g: System.out.print(“You are on the right track”.toUpperCase());
Output: YOU ARE ON THE RIGHT TRACK
The toLowerCase() method
The java String toLowerCase() method converts all the uppercase
characters of a string into lowercase
It will not alter the already existing lowercase characters in the
string.
E.g: System.out.print(“YOU ARE ON THE RIGHT
TRACK”.toLowerCase());
Output: you are on the right track
The concat() method
The Java string concat() method allows you to join two strings.
This method returns a string with the value of the string passed into
the method is appended to the end of the string.
E.g:
String s1 = "Nice ";
String s2 = " Day";
System.out.println(s1.concat(s2));
Output: Nice Day
+ operator
+ operator is also used to concatenate strings
E.g:
System.out.println("You " + "are " + "the " + "best");
Output:
You are the best
1 // Predict the output
2
3 class Main{
4 public static void main(String[] args) {
5 String s = "Are", t = "you", u = "ready";
6 System.out.println(s + t + u);
7 System.out.println(s.concat(t));
8 }
9 }
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 class Main{
4 public static void main(String args[]){
5 String s = 50 + 50 + "error" + 50 + 50;
6 System.out.println(s);
7 }
8 }
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 class Main{
4 public static void main(String args[]){
5 String s = "Apple";
6 int a = 10;
7 System.out.println(s + a);
8 System.out.println(s.concat(a));
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 class Main{
4 public static void main(String args[]){
5 String s = "Apple";
6 String r = null;
7 System.out.println(s + r);
8 System.out.println(s.concat(r));
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
1 // Predict the output
2
3 class Main{
4 public static void main(String args[]){
5 String s = "Great", t = "";
6 String u = s.concat(t);
7 if(u == s){
8 System.out.println("Same");
9 }
10 else{
11 System.out.println("Not same");
12 }
13 String e = s + t;
14 if (e == s){
15 System.out.println("Same");
16 }
17 else{
18 System.out.println("Not same");
19 }
20 }
21 }
22
1 // Predict the output
2
3 class Main{
4 public static void main(String args[]){
5 String s = "Great", t = "H";
6 String u = s.concat(t);
7 if(u == s){
8 System.out.println("Same");
9 }
10 else{
11 System.out.println("Not same");
12 }
13 String e = s + t;
14 if (e == s){
15 System.out.println("Same");
16 }
17 else{
18 System.out.println("Not same");
19 }
20 }
21 }
22
Question 2
Write a Java program to accept two strings from user and perform
the following operations:
1) Find the length of both input strings
2) Concatenation of two strings
3) Convert the first string into uppercase

Sample Sample
Input:
Hello Output:
5 10
Developers Hello Developers
HELLO
1 // Solution
2
3 import java.util.Scanner;
4 class Main{
5 public static void main(String args[]){
6 Scanner in = new Scanner(System.in);
7 String str1 = in.nextLine();
8 String str2 = in.nextLine();
9 System.out.println(str1.length() + " " + str2.length());
10 System.out.println(str1.concat(str2));
11 System.out.print(str1.toUpperCase());
12 }
13 }
14
15
16
17
18
19
20
21
22
Question 3
Write a Java code to check whether the given string is palindrome
or not.
If the given string is a palindrome, then print “Yes”. Otherwise,
print “No”.

Sample Sample
Input:
spacecaps Output:
Yes
1 // Solution
2
3 import java.util.Scanner;
4 class Main{
5 public static void main(String args[]) {
6 Scanner scan = new Scanner(System.in);
7 String str = scan.nextLine();
8 int str_len = str.length();
9 int end = str_len - 1;
10 int front = 0;
11 boolean is_palindrome = true;
12 while(front < end){
13 if(str.charAt(front) != str.charAt(end))
14 {
15 is_palindrome = false;
16 break;
17 }
18 front++;
19 end--;
20 }
21
22
1 // Solution
2
3 if(is_palindrome == true){
4 System.out.println("Yes");
5 }
6 else{
7 System.out.println("No");
8 }
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
THANK YOU

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