SlideShare a Scribd company logo
1 of 21
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

What's hot

Session 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, SetsSession 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, SetsPawanMM
 
Session 06 - Practice Session
Session 06 - Practice SessionSession 06 - Practice Session
Session 06 - Practice SessionPawanMM
 
OOPS features using Objective C
OOPS features using Objective COOPS features using Objective C
OOPS features using Objective CTiyasi Acharya
 
Session 03 - Elements of Java Language
Session 03 - Elements of Java LanguageSession 03 - Elements of Java Language
Session 03 - Elements of Java LanguagePawanMM
 
10. Introduction to Datastructure
10. Introduction to Datastructure10. Introduction to Datastructure
10. Introduction to DatastructureNilesh Dalvi
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and VariablesNilesh Dalvi
 
Session 01 - Introduction to Java
Session 01 - Introduction to JavaSession 01 - Introduction to Java
Session 01 - Introduction to JavaPawanMM
 
C++ unit-2-part-1
C++ unit-2-part-1C++ unit-2-part-1
C++ unit-2-part-1Jadavsejal
 
C++ unit-2-part-2
C++ unit-2-part-2C++ unit-2-part-2
C++ unit-2-part-2Jadavsejal
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structuresagorolabs
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in JavaMudit Gupta
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template LibraryNilesh Dalvi
 

What's hot (16)

Session 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, SetsSession 17 - Collections - Lists, Sets
Session 17 - Collections - Lists, Sets
 
Session 06 - Practice Session
Session 06 - Practice SessionSession 06 - Practice Session
Session 06 - Practice Session
 
OOPS features using Objective C
OOPS features using Objective COOPS features using Objective C
OOPS features using Objective C
 
Session 03 - Elements of Java Language
Session 03 - Elements of Java LanguageSession 03 - Elements of Java Language
Session 03 - Elements of Java Language
 
10. Introduction to Datastructure
10. Introduction to Datastructure10. Introduction to Datastructure
10. Introduction to Datastructure
 
8. String
8. String8. String
8. String
 
12. Stack
12. Stack12. Stack
12. Stack
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
 
Session 01 - Introduction to Java
Session 01 - Introduction to JavaSession 01 - Introduction to Java
Session 01 - Introduction to Java
 
C++ unit-2-part-1
C++ unit-2-part-1C++ unit-2-part-1
C++ unit-2-part-1
 
Basics of oops concept
Basics of oops conceptBasics of oops concept
Basics of oops concept
 
C++ unit-2-part-2
C++ unit-2-part-2C++ unit-2-part-2
C++ unit-2-part-2
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structures
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in Java
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
Basic concepts of oops
Basic concepts of oopsBasic concepts of oops
Basic concepts of oops
 

Similar to Session 05 - Strings in Java

Learning core java
Learning core javaLearning core java
Learning core javaAbhay Bharti
 
Javascript for Intermediates
Javascript for IntermediatesJavascript for Intermediates
Javascript for IntermediatesAnkit Agrawal
 
javastringexample problems using string class
javastringexample problems using string classjavastringexample problems using string class
javastringexample problems using string classfedcoordinator
 
Java string handling
Java string handlingJava string handling
Java string handlingSalman Khan
 
Data Handling and Function
Data Handling and FunctionData Handling and Function
Data Handling and FunctionRatnaJava
 
Java core - Detailed Overview
Java  core - Detailed OverviewJava  core - Detailed Overview
Java core - Detailed OverviewBuddha Tree
 
OCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIsOCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIsİbrahim Kürce
 
10 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 810 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 8Garth Gilmour
 
Java Hands-On Workshop
Java Hands-On WorkshopJava Hands-On Workshop
Java Hands-On WorkshopArpit Poladia
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2Sherihan Anver
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryPray Desai
 
Java Course 3: OOP
Java Course 3: OOPJava Course 3: OOP
Java Course 3: OOPAnton Keks
 
First fare 2011 frc-java-introduction
First fare 2011 frc-java-introductionFirst fare 2011 frc-java-introduction
First fare 2011 frc-java-introductionOregon FIRST Robotics
 

Similar to Session 05 - Strings in Java (20)

Jvm2
Jvm2Jvm2
Jvm2
 
Learning core java
Learning core javaLearning core java
Learning core java
 
ppt_on_java.pptx
ppt_on_java.pptxppt_on_java.pptx
ppt_on_java.pptx
 
Javascript for Intermediates
Javascript for IntermediatesJavascript for Intermediates
Javascript for Intermediates
 
javastringexample problems using string class
javastringexample problems using string classjavastringexample problems using string class
javastringexample problems using string class
 
Java67
Java67Java67
Java67
 
Java string handling
Java string handlingJava string handling
Java string handling
 
java introduction
java introductionjava introduction
java introduction
 
Javasession6
Javasession6Javasession6
Javasession6
 
Data Handling and Function
Data Handling and FunctionData Handling and Function
Data Handling and Function
 
Java core - Detailed Overview
Java  core - Detailed OverviewJava  core - Detailed Overview
Java core - Detailed Overview
 
OCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIsOCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIs
 
10 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 810 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 8
 
Java Hands-On Workshop
Java Hands-On WorkshopJava Hands-On Workshop
Java Hands-On Workshop
 
Java
JavaJava
Java
 
Java interview questions 2
Java interview questions 2Java interview questions 2
Java interview questions 2
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud Foundry
 
Java ce241
Java ce241Java ce241
Java ce241
 
Java Course 3: OOP
Java Course 3: OOPJava Course 3: OOP
Java Course 3: OOP
 
First fare 2011 frc-java-introduction
First fare 2011 frc-java-introductionFirst fare 2011 frc-java-introduction
First fare 2011 frc-java-introduction
 

More from PawanMM

Session 48 - JS, JSON and AJAX
Session 48 - JS, JSON and AJAXSession 48 - JS, JSON and AJAX
Session 48 - JS, JSON and AJAXPawanMM
 
Session 46 - Spring - Part 4 - Spring MVC
Session 46 - Spring - Part 4 - Spring MVCSession 46 - Spring - Part 4 - Spring MVC
Session 46 - Spring - Part 4 - Spring MVCPawanMM
 
Session 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOPSession 45 - Spring - Part 3 - AOP
Session 45 - Spring - Part 3 - AOPPawanMM
 
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based ConfigurationSession 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based ConfigurationPawanMM
 
Session 43 - Spring - Part 1 - IoC DI Beans
Session 43 - Spring - Part 1 - IoC DI BeansSession 43 - Spring - Part 1 - IoC DI Beans
Session 43 - Spring - Part 1 - IoC DI BeansPawanMM
 
Session 42 - Struts 2 Hibernate Integration
Session 42 - Struts 2 Hibernate IntegrationSession 42 - Struts 2 Hibernate Integration
Session 42 - Struts 2 Hibernate IntegrationPawanMM
 
Session 41 - Struts 2 Introduction
Session 41 - Struts 2 IntroductionSession 41 - Struts 2 Introduction
Session 41 - Struts 2 IntroductionPawanMM
 
Session 40 - Hibernate - Part 2
Session 40 - Hibernate - Part 2Session 40 - Hibernate - Part 2
Session 40 - Hibernate - Part 2PawanMM
 
Session 39 - Hibernate - Part 1
Session 39 - Hibernate - Part 1Session 39 - Hibernate - Part 1
Session 39 - Hibernate - Part 1PawanMM
 
Session 38 - Core Java (New Features) - Part 1
Session 38 - Core Java (New Features) - Part 1Session 38 - Core Java (New Features) - Part 1
Session 38 - Core Java (New Features) - Part 1PawanMM
 
Session 37 - JSP - Part 2 (final)
Session 37 - JSP - Part 2 (final)Session 37 - JSP - Part 2 (final)
Session 37 - JSP - Part 2 (final)PawanMM
 
Session 36 - JSP - Part 1
Session 36 - JSP - Part 1Session 36 - JSP - Part 1
Session 36 - JSP - Part 1PawanMM
 
Session 35 - Design Patterns
Session 35 - Design PatternsSession 35 - Design Patterns
Session 35 - Design PatternsPawanMM
 
Session 34 - JDBC Best Practices, Introduction to Design Patterns
Session 34 - JDBC Best Practices, Introduction to Design PatternsSession 34 - JDBC Best Practices, Introduction to Design Patterns
Session 34 - JDBC Best Practices, Introduction to Design PatternsPawanMM
 
Session 33 - Session Management using other Techniques
Session 33 - Session Management using other TechniquesSession 33 - Session Management using other Techniques
Session 33 - Session Management using other TechniquesPawanMM
 
Session 32 - Session Management using Cookies
Session 32 - Session Management using CookiesSession 32 - Session Management using Cookies
Session 32 - Session Management using CookiesPawanMM
 
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
Session 31 - Session Management, Best Practices, Design Patterns in Web AppsSession 31 - Session Management, Best Practices, Design Patterns in Web Apps
Session 31 - Session Management, Best Practices, Design Patterns in Web AppsPawanMM
 
Session 30 - Servlets - Part 6
Session 30 - Servlets - Part 6Session 30 - Servlets - Part 6
Session 30 - Servlets - Part 6PawanMM
 
Session 29 - Servlets - Part 5
Session 29 - Servlets - Part 5Session 29 - Servlets - Part 5
Session 29 - Servlets - Part 5PawanMM
 
Session 28 - Servlets - Part 4
Session 28 - Servlets - Part 4Session 28 - Servlets - Part 4
Session 28 - Servlets - Part 4PawanMM
 

More from PawanMM (20)

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

Recently uploaded

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 

Recently uploaded (20)

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 

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