Strings
TNS India Foundation | ‹#›
Partners in Economic Transformation
Recap
● Collection- It is a group of objects.
● List - Ordered and allow duplicate elements.
● Set - Unordered and doesn’t allow duplicate elements.
● Queue - It follows FIFO.
● Map - Represents key-value pairs.
TNS India Foundation | ‹#›
Partners in Economic Transformation
Quiz
[Link] is the output of the following code?
ArrayList<String> list = new ArrayList<>();
[Link]("Java");
[Link]("Python");
[Link]([Link](0));
A Java
B Python
C True
D Compilation Error
TNS India Foundation | ‹#›
Partners in Economic Transformation
Quiz
[Link] is the output of the following code?
ArrayList<String> list = new ArrayList<>();
[Link]("Java");
[Link]("Python");
[Link]([Link](0));
A Java
B Python
C True
D Compilation Error
TNS India Foundation | ‹#›
Partners in Economic Transformation
Quiz
Q2. Which of the following interfaces provides methods like peek() and poll()?
A Set
B List
C Queue
D Map
TNS India Foundation | ‹#›
Partners in Economic Transformation
Quiz
Q2. Which of the following interfaces provides methods like peek() and poll()?
A Set
B List
C Queue
D Map
TNS India Foundation | ‹#›
Partners in Economic Transformation
Learning Objective
● Formulation of strings
● Utilization of String class techniques
● Exploration of the String Constant Pool Area
● Introduction to the StringBuffer
● Construction of the StringBuffer class
● Application of StringBuffer methods
● Differentiating between StringBuffer and String characteristics
TNS India Foundation | ‹#›
Partners in Economic Transformation
Strings
● To represent group of characters or character array enclosed within the double
quotes.
Eg:
String str=“Accenture”;
char [ ] str={‘a’,’c’,’c’,’e’,’n’,’t’,’u’,’r’,’e’};
TNS India Foundation | ‹#›
Partners in Economic Transformation
Creating a strings
String s1= “ Capgemini”; //Primitive Type
String s2= new String(“Capgemini”); //Object Type
char data[]= {‘ C‘, ’a ‘ ,’p’ ,’g’};
String s3= new String(data);
String s4= new String(s1); //Reference type
TNS India Foundation | ‹#›
Partners in Economic Transformation
String class methods
TNS India Foundation | ‹#›
Partners in Economic Transformation
String class methods
TNS India Foundation | ‹#›
Partners in Economic Transformation
String constant pool area (SCP)
TNS India Foundation | ‹#›
Partners in Economic Transformation
Hands on Coding
TNS India Foundation | ‹#›
Partners in Economic Transformation
StringBuffer class
● StringBuffer class creates of flexible length which can be modified.
● It is mutable,so they can be changeable.
● It is declared as final
TNS India Foundation | ‹#›
Partners in Economic Transformation
Create StringBuffer class
StringBuffer sb1 = new StringBuffer();
StringBuffer sb2 = new StringBuffer(int capacity);
StringBuffer sb3= new String(String str);
Eg: StringBuffer sb1= new StringBuffer();
StringBuffer sb2= new StringBuffer(5);
StringBuffer sb3= new StringBuffer(“Capgemini”);
TNS India Foundation | ‹#›
Partners in Economic Transformation
StringBuffer class methods
TNS India Foundation | ‹#›
Partners in Economic Transformation
Difference between StringBuffer and String
TNS India Foundation | ‹#›
Partners in Economic Transformation
Hands on Coding
TNS India Foundation | ‹#›
Partners in Economic Transformation
Quiz
[Link] is the purpose of charAt() method of a string in java?
A To add the character to the string
B To remove the character of the string
To get the character at a specific index
C
D To get the length of the string
TNS India Foundation | ‹#›
Partners in Economic Transformation
Quiz
[Link] is the purpose of charAt() method of a string in java?
A To add the character to the string
B To remove the character of the string
To get the character at a specific index
C
D To get the length of the string
TNS India Foundation | ‹#›
Partners in Economic Transformation
Quiz
Q2. What is the result of the expression “[Link](1,4)” ?
A “Hel”
B “H”
C “ell”
D “llo”
TNS India Foundation | ‹#›
Partners in Economic Transformation
Quiz
Q2. What is the result of the expression “[Link](1,4)” ?
A “Hel”
B “H”
C “ell”
D “llo”
TNS India Foundation | ‹#›
Partners in Economic Transformation
Summary
● String - an object that represents a number of character values
● StringBuffer - It is used to create modifiable strings.
● String pool - It is the special area in the heap memory to store string objects.
TNS India Foundation | ‹#›
Partners in Economic Transformation
Assignment Question
Problem Statement:
Jim is a little scientist. He has planned to design software to track all his works. A part of
this requirement, he needs to move all the uppercase letters to end of sentence and print
remaining letter without changing the order.
He wants your help to complete the task. Write a program to move all the uppercase letters
to the end of the string.
Input Format
The first line of the input contains the string
Output Format
The output prints the modified string.
TNS India Foundation | ‹#›
Partners in Economic Transformation
Assignment Question
Sample Input 1
Welcome to Java Training
Sample Output 1
elcome to ava rainingWJT
Sample Input 2
HAI How Are You?
Sample Output 2
ow re ou?HAIHAY
TNS India Foundation | ‹#›
Partners in Economic Transformation
Let’s Reflect -
1. What is your major learning from today’s session?
2. How are you going to use this learning in your journey on the job?
TNS India Foundation | ‹#›
Partners in Economic Transformation