SlideShare a Scribd company logo
Java & JEE Fast Track Training
Session 5 – Handling Strings in Java
Page 1Classification: Restricted
Agenda
• Memory Allocation & Garbage Collection
• Strings in Java
Memory Allocation & Garbage
Collection
Page 3Classification: Restricted
Memory Allocation in Java and Garbage Collection
• Java Heap Space
• Used by Java runtime to allocate memory to Objects and JRE classes.
• Any new Object is always created in Heap Space.
• Garbage Collection runs on the heap memory to free the memory used by objects that
doesn’t have any reference.
• All instance and class variables are also stored in the heap.
• Java Stack Memory
• Used for execution of a thread.
• Store method specific values, and “references” to Objects being used in the method.
• Stack memory is LIFO (Last-In-First-Out)
• Whenever a method is invoked, a new block is created in the stack memory for the
method to hold local primitive values and reference to other objects in the method. As
soon as method ends, the block becomes unused and become available for next method.
• Stack memory size is very less compared to Heap memory.
Page 4Classification: Restricted
Memory Allocation in Java and Garbage Collection
Page 5Classification: Restricted
Memory Allocation in Java and Garbage Collection
Strings in Java
Page 7Classification: Restricted
Creating String in Java
There are two ways to create a String in Java
• String literal
• Using “new” keyword
Does it make any difference? Well, yes!
Page 8Classification: Restricted
String Pool Concept in Java (String Interning)
 String Intern Pool maintained in Java Heap Space
Page 9Classification: Restricted
String Pool Concept in Java (String Interning)
• String is immutable in Java
• All Strings are stored in String Pool (also called String Intern Pool) allocated
within Java Heap Space
• It is implementation of String Interning Concept.
• String interning is a method of storing only one copy of each distinct string
value, which must be immutable.
• Interning strings makes some string processing tasks more time- or space-
efficient at the cost of requiring more time when the string is created or
interned.
• The distinct values are stored in a string intern pool.
• String pool is an example of Flyweight Design Pattern.
• Using new operator, we force String class to create a new String object in
heap space.
Page 10Classification: Restricted
Discussion: How many Strings are getting created here?
Page 11Classification: Restricted
java.lang.String API – Important methods
Page 12Classification: Restricted
java.lang.String API – Examples
Page 13Classification: Restricted
Converting String to numbers and vice versa
• String to Number
int i = Integer.parseInt(str);
Integer i = Integer.valueOf(str);
double d = Double.parseDouble(str);
Double d = Double.valueOf(str);
Note: Both throw NumberFormatException If the String is not valid for conversion
• String to Boolean
boolean b = Boolean.parseBoolean(str);
• Any Type to String
String s = String.valueOf(value);
Page 14Classification: Restricted
String vs StringBuffer/StringBuilder
Prefer StringBuffer or StringBuilder when performing multiple
concatenations in your code.
Page 15Classification: Restricted
Exercise: What is the output?
public static String someMethod( String str) //”Hello “
{
if( str == null)
return null;
int len = str.length();
for( ; len > 0; len--)
{
if( ! Character.isWhitespace( str.charAt( len - 1)))
break;
}
return str.substring( 0, len);
}
Page 16Classification: Restricted
Exercise
• Write a program to input 2 strings – string1 and string2, and concatenate
the first 5 characters of string1 with last 5 characters of string2.
Page 17Classification: Restricted
Exercise
• Write a program to input 3 strings and check if at least any two strings are
equal.
Page 18Classification: Restricted
Exercise
• Write a program to sort an array of Strings in ascending order.
Page 19Classification: Restricted
Topics to be covered in next session
• Practice session
• Object Oriented Programming (OOPs) Concepts
Page 20Classification: Restricted
Thank You!

More Related Content

PPSX
Strings in Java
PPSX
Arrays in Java
PPTX
Session 04 - Arrays in Java
PPTX
Session 14 - Object Class
PPSX
Object Class
PPTX
Session 10 - OOP with Java - Abstract Classes and Interfaces
PPSX
Practice Session
PPTX
Session 18 - Review Session and Attending Java Interviews
Strings in Java
Arrays in Java
Session 04 - Arrays in Java
Session 14 - Object Class
Object Class
Session 10 - OOP with Java - Abstract Classes and Interfaces
Practice Session
Session 18 - Review Session and Attending Java Interviews

What's hot (14)

PPTX
Session 17 - Collections - Lists, Sets
PPTX
Session 06 - Practice Session
PPTX
OOPS features using Objective C
PPTX
Session 03 - Elements of Java Language
PPT
10. Introduction to Datastructure
PPT
8. String
PPT
12. Stack
PPT
3. Data types and Variables
PPTX
Session 01 - Introduction to Java
PPTX
Basics of oops concept
PPTX
Java 103 intro to java data structures
ODP
Best practices in Java
PPT
Standard Template Library
PPT
Basic concepts of oops
Session 17 - Collections - Lists, Sets
Session 06 - Practice Session
OOPS features using Objective C
Session 03 - Elements of Java Language
10. Introduction to Datastructure
8. String
12. Stack
3. Data types and Variables
Session 01 - Introduction to Java
Basics of oops concept
Java 103 intro to java data structures
Best practices in Java
Standard Template Library
Basic concepts of oops
Ad

Similar to Session 05 - Strings in Java (20)

PPTX
Day_5.1.pptx
PPTX
Java Strings.pptxJava Strings.pptxJava Strings.pptx
PPT
Java™ (OOP) - Chapter 9: "Strings and Text I/O"
PPTX
Java Strings
PPTX
Java String
PPT
PPT
JavaYDL9
PPT
String handling session 5
PDF
Java - Strings Concepts
PPTX
Introduction to Java Strings, By Kavita Ganesan
PPTX
More about java strings - Immutability and String Pool
DOCX
Java R20 - UNIT-5.docx
PDF
Java R20 - UNIT-5.pdf
PPTX
What-is-String-in-Java.pptx
PPTX
PPT
Strings in javamnjn ijnjun oinoin oinoi .ppt
PDF
Java Presentation on the topic of string
PPT
String and string manipulation
PDF
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Day_5.1.pptx
Java Strings.pptxJava Strings.pptxJava Strings.pptx
Java™ (OOP) - Chapter 9: "Strings and Text I/O"
Java Strings
Java String
JavaYDL9
String handling session 5
Java - Strings Concepts
Introduction to Java Strings, By Kavita Ganesan
More about java strings - Immutability and String Pool
Java R20 - UNIT-5.docx
Java R20 - UNIT-5.pdf
What-is-String-in-Java.pptx
Strings in javamnjn ijnjun oinoin oinoi .ppt
Java Presentation on the topic of string
String and string manipulation
Java Strings Tutorial | String Manipulation in Java | Java Tutorial For Begin...
Ad

More from PawanMM (20)

PPTX
Session 48 - JS, JSON and AJAX
PPTX
Session 46 - Spring - Part 4 - Spring MVC
PPTX
Session 45 - Spring - Part 3 - AOP
PPTX
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
PPTX
Session 43 - Spring - Part 1 - IoC DI Beans
PPTX
Session 42 - Struts 2 Hibernate Integration
PPTX
Session 41 - Struts 2 Introduction
PPTX
Session 40 - Hibernate - Part 2
PPTX
Session 39 - Hibernate - Part 1
PPTX
Session 38 - Core Java (New Features) - Part 1
PPTX
Session 37 - JSP - Part 2 (final)
PPTX
Session 36 - JSP - Part 1
PPTX
Session 35 - Design Patterns
PPTX
Session 34 - JDBC Best Practices, Introduction to Design Patterns
PPTX
Session 33 - Session Management using other Techniques
PPTX
Session 32 - Session Management using Cookies
PPTX
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
PPTX
Session 30 - Servlets - Part 6
PPTX
Session 29 - Servlets - Part 5
PPTX
Session 28 - Servlets - Part 4
Session 48 - JS, JSON and AJAX
Session 46 - Spring - Part 4 - Spring MVC
Session 45 - Spring - Part 3 - AOP
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
Session 43 - Spring - Part 1 - IoC DI Beans
Session 42 - Struts 2 Hibernate Integration
Session 41 - Struts 2 Introduction
Session 40 - Hibernate - Part 2
Session 39 - Hibernate - Part 1
Session 38 - Core Java (New Features) - Part 1
Session 37 - JSP - Part 2 (final)
Session 36 - JSP - Part 1
Session 35 - Design Patterns
Session 34 - JDBC Best Practices, Introduction to Design Patterns
Session 33 - Session Management using other Techniques
Session 32 - Session Management using Cookies
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
Session 30 - Servlets - Part 6
Session 29 - Servlets - Part 5
Session 28 - Servlets - Part 4

Recently uploaded (20)

PDF
Smarter Business Operations Powered by IoT Remote Monitoring
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Dell Pro 14 Plus: Be better prepared for what’s coming
PDF
DevOps & Developer Experience Summer BBQ
PDF
Modernizing your data center with Dell and AMD
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PDF
Google’s NotebookLM Unveils Video Overviews
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
How to Build Crypto Derivative Exchanges from Scratch.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Enable Enterprise-Ready Security on IBM i Systems.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Smarter Business Operations Powered by IoT Remote Monitoring
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
CIFDAQ's Market Insight: SEC Turns Pro Crypto
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Dell Pro 14 Plus: Be better prepared for what’s coming
DevOps & Developer Experience Summer BBQ
Modernizing your data center with Dell and AMD
Chapter 2 Digital Image Fundamentals.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
NewMind AI Monthly Chronicles - July 2025
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Google’s NotebookLM Unveils Video Overviews
Chapter 3 Spatial Domain Image Processing.pdf
How to Build Crypto Derivative Exchanges from Scratch.pptx
NewMind AI Weekly Chronicles - August'25 Week I
Enable Enterprise-Ready Security on IBM i Systems.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication

Session 05 - Strings in Java

  • 1. Java & JEE Fast Track Training Session 5 – Handling Strings in Java
  • 2. Page 1Classification: Restricted Agenda • Memory Allocation & Garbage Collection • Strings in Java
  • 3. Memory Allocation & Garbage Collection
  • 4. Page 3Classification: Restricted Memory Allocation in Java and Garbage Collection • Java Heap Space • Used by Java runtime to allocate memory to Objects and JRE classes. • Any new Object is always created in Heap Space. • Garbage Collection runs on the heap memory to free the memory used by objects that doesn’t have any reference. • All instance and class variables are also stored in the heap. • Java Stack Memory • Used for execution of a thread. • Store method specific values, and “references” to Objects being used in the method. • Stack memory is LIFO (Last-In-First-Out) • Whenever a method is invoked, a new block is created in the stack memory for the method to hold local primitive values and reference to other objects in the method. As soon as method ends, the block becomes unused and become available for next method. • Stack memory size is very less compared to Heap memory.
  • 5. Page 4Classification: Restricted Memory Allocation in Java and Garbage Collection
  • 6. Page 5Classification: Restricted Memory Allocation in Java and Garbage Collection
  • 8. Page 7Classification: Restricted Creating String in Java There are two ways to create a String in Java • String literal • Using “new” keyword Does it make any difference? Well, yes!
  • 9. Page 8Classification: Restricted String Pool Concept in Java (String Interning)  String Intern Pool maintained in Java Heap Space
  • 10. Page 9Classification: Restricted String Pool Concept in Java (String Interning) • String is immutable in Java • All Strings are stored in String Pool (also called String Intern Pool) allocated within Java Heap Space • It is implementation of String Interning Concept. • String interning is a method of storing only one copy of each distinct string value, which must be immutable. • Interning strings makes some string processing tasks more time- or space- efficient at the cost of requiring more time when the string is created or interned. • The distinct values are stored in a string intern pool. • String pool is an example of Flyweight Design Pattern. • Using new operator, we force String class to create a new String object in heap space.
  • 11. Page 10Classification: Restricted Discussion: How many Strings are getting created here?
  • 14. Page 13Classification: Restricted Converting String to numbers and vice versa • String to Number int i = Integer.parseInt(str); Integer i = Integer.valueOf(str); double d = Double.parseDouble(str); Double d = Double.valueOf(str); Note: Both throw NumberFormatException If the String is not valid for conversion • String to Boolean boolean b = Boolean.parseBoolean(str); • Any Type to String String s = String.valueOf(value);
  • 15. Page 14Classification: Restricted String vs StringBuffer/StringBuilder Prefer StringBuffer or StringBuilder when performing multiple concatenations in your code.
  • 16. Page 15Classification: Restricted Exercise: What is the output? public static String someMethod( String str) //”Hello “ { if( str == null) return null; int len = str.length(); for( ; len > 0; len--) { if( ! Character.isWhitespace( str.charAt( len - 1))) break; } return str.substring( 0, len); }
  • 17. Page 16Classification: Restricted Exercise • Write a program to input 2 strings – string1 and string2, and concatenate the first 5 characters of string1 with last 5 characters of string2.
  • 18. Page 17Classification: Restricted Exercise • Write a program to input 3 strings and check if at least any two strings are equal.
  • 19. Page 18Classification: Restricted Exercise • Write a program to sort an array of Strings in ascending order.
  • 20. Page 19Classification: Restricted Topics to be covered in next session • Practice session • Object Oriented Programming (OOPs) Concepts