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
Arrays in Java
PPTX
Session 04 - Arrays in Java
PPTX
Session 14 - Object Class
PPSX
Object Class
PPSX
Strings in Java
PPTX
Session 10 - OOP with Java - Abstract Classes and Interfaces
PPSX
Practice Session
PPTX
Session 18 - Review Session and Attending Java Interviews
Arrays in Java
Session 04 - Arrays in Java
Session 14 - Object Class
Object Class
Strings in Java
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
PPTX
Learning core java
PPTX
ppt_on_java.pptx
PDF
Javascript for Intermediates
PPTX
javastringexample problems using string class
DOCX
PDF
JAVA Class Presentation.pdf Vsjsjsnheheh
PPTX
Java string handling
PPT
java introduction
PPTX
Javasession6
PPTX
Data Handling and Function
PPT
Java core - Detailed Overview
PPTX
OCA Java SE 8 Exam Chapter 3 Core Java APIs
PPTX
10 Sets of Best Practices for Java 8
PPTX
Java Hands-On Workshop
PPTX
PPTX
Java interview questions 2
PPTX
Scala, Play 2.0 & Cloud Foundry
PPTX
Java ce241
PDF
Java Course 3: OOP
Learning core java
ppt_on_java.pptx
Javascript for Intermediates
javastringexample problems using string class
JAVA Class Presentation.pdf Vsjsjsnheheh
Java string handling
java introduction
Javasession6
Data Handling and Function
Java core - Detailed Overview
OCA Java SE 8 Exam Chapter 3 Core Java APIs
10 Sets of Best Practices for Java 8
Java Hands-On Workshop
Java interview questions 2
Scala, Play 2.0 & Cloud Foundry
Java ce241
Java Course 3: OOP
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
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Spectroscopy.pptx food analysis technology
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Review of recent advances in non-invasive hemoglobin estimation
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Spectroscopy.pptx food analysis technology
MYSQL Presentation for SQL database connectivity
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Network Security Unit 5.pdf for BCA BBA.
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Advanced methodologies resolving dimensionality complications for autism neur...
Digital-Transformation-Roadmap-for-Companies.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Encapsulation_ Review paper, used for researhc scholars
The Rise and Fall of 3GPP – Time for a Sabbatical?
MIND Revenue Release Quarter 2 2025 Press Release
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy

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