
Domains
Agile Management
Master Agile methodologies for efficient and timely project delivery.
View All Agile Management Coursesicon-refresh-cwCertifications
Scrum Alliance
16 Hours
Best Seller
Certified ScrumMaster (CSM) CertificationScrum Alliance
16 Hours
Best Seller
Certified Scrum Product Owner (CSPO) CertificationScaled Agile
16 Hours
Trending
Leading SAFe 6.0 CertificationScrum.org
16 Hours
Professional Scrum Master (PSM) CertificationScaled Agile
16 Hours
SAFe 6.0 Scrum Master (SSM) CertificationAdvanced Certifications
Scaled Agile, Inc.
32 Hours
Recommended
Implementing SAFe 6.0 (SPC) CertificationScaled Agile, Inc.
24 Hours
SAFe 6.0 Release Train Engineer (RTE) CertificationScaled Agile, Inc.
16 Hours
Trending
SAFe® 6.0 Product Owner/Product Manager (POPM)IC Agile
24 Hours
ICP Agile Certified Coaching (ICP-ACC)Scrum.org
16 Hours
Professional Scrum Product Owner I (PSPO I) TrainingMasters
32 Hours
Trending
Agile Management Master's Program32 Hours
Agile Excellence Master's ProgramOn-Demand Courses
Agile and ScrumRoles
Scrum MasterTech Courses and Bootcamps
Full Stack Developer BootcampAccreditation Bodies
Scrum AllianceTop Resources
Scrum TutorialProject Management
Gain expert skills to lead projects to success and timely completion.
View All Project Management Coursesicon-standCertifications
PMI
36 Hours
Best Seller
Project Management Professional (PMP) CertificationAxelos
32 Hours
PRINCE2 Foundation & Practitioner CertificationAxelos
16 Hours
PRINCE2 Foundation CertificationAxelos
16 Hours
PRINCE2 Practitioner CertificationSkills
Change ManagementMasters
Job Oriented
45 Hours
Trending
Project Management Master's ProgramUniversity Programs
45 Hours
Trending
Project Management Master's ProgramOn-Demand Courses
PRINCE2 Practitioner CourseRoles
Project ManagerAccreditation Bodies
PMITop Resources
Theories of MotivationCloud Computing
Learn to harness the cloud to deliver computing resources efficiently.
View All Cloud Computing Coursesicon-cloud-snowingCertifications
AWS
32 Hours
Best Seller
AWS Certified Solutions Architect - AssociateAWS
32 Hours
AWS Cloud Practitioner CertificationAWS
24 Hours
AWS DevOps CertificationMicrosoft
16 Hours
Azure Fundamentals CertificationMicrosoft
24 Hours
Best Seller
Azure Administrator CertificationMicrosoft
45 Hours
Recommended
Azure Data Engineer CertificationMicrosoft
32 Hours
Azure Solution Architect CertificationMicrosoft
40 Hours
Azure DevOps CertificationAWS
24 Hours
Systems Operations on AWS Certification TrainingAWS
24 Hours
Developing on AWSMasters
Job Oriented
48 Hours
New
AWS Cloud Architect Masters ProgramBootcamps
Career Kickstarter
100 Hours
Trending
Cloud Engineer BootcampRoles
Cloud EngineerOn-Demand Courses
AWS Certified Developer Associate - Complete GuideAuthorized Partners of
AWSTop Resources
Scrum TutorialIT Service Management
Understand how to plan, design, and optimize IT services efficiently.
View All DevOps Coursesicon-git-commitCertifications
Axelos
16 Hours
Best Seller
ITIL 4 Foundation CertificationAxelos
16 Hours
ITIL Practitioner CertificationPeopleCert
16 Hours
ISO 14001 Foundation CertificationPeopleCert
16 Hours
ISO 20000 CertificationPeopleCert
24 Hours
ISO 27000 Foundation CertificationAxelos
24 Hours
ITIL 4 Specialist: Create, Deliver and Support TrainingAxelos
24 Hours
ITIL 4 Specialist: Drive Stakeholder Value TrainingAxelos
16 Hours
ITIL 4 Strategist Direct, Plan and Improve TrainingOn-Demand Courses
ITIL 4 Specialist: Create, Deliver and Support ExamTop Resources
ITIL Practice TestData Science
Unlock valuable insights from data with advanced analytics.
View All Data Science Coursesicon-dataBootcamps
Job Oriented
6 Months
Trending
Data Science BootcampJob Oriented
289 Hours
Data Engineer BootcampJob Oriented
6 Months
Data Analyst BootcampJob Oriented
288 Hours
New
AI Engineer BootcampSkills
Data Science with PythonRoles
Data ScientistOn-Demand Courses
Data Analysis Using ExcelTop Resources
Machine Learning TutorialDevOps
Automate and streamline the delivery of products and services.
View All DevOps Coursesicon-terminal-squareCertifications
DevOps Institute
16 Hours
Best Seller
DevOps Foundation CertificationCNCF
32 Hours
New
Certified Kubernetes AdministratorDevops Institute
16 Hours
Devops LeaderSkills
KubernetesRoles
DevOps EngineerOn-Demand Courses
CI/CD with Jenkins XGlobal Accreditations
DevOps InstituteTop Resources
Top DevOps ProjectsBI And Visualization
Understand how to transform data into actionable, measurable insights.
View All BI And Visualization Coursesicon-microscopeBI and Visualization Tools
Certification
24 Hours
Recommended
Tableau CertificationCertification
24 Hours
Data Visualization with Tableau CertificationMicrosoft
24 Hours
Best Seller
Microsoft Power BI CertificationTIBCO
36 Hours
TIBCO Spotfire TrainingCertification
30 Hours
Data Visualization with QlikView CertificationCertification
16 Hours
Sisense BI CertificationOn-Demand Courses
Data Visualization Using Tableau TrainingTop Resources
Python Data Viz LibsCyber Security
Understand how to protect data and systems from threats or disasters.
View All Cyber Security Coursesicon-refresh-cwCertifications
CompTIA
40 Hours
Best Seller
CompTIA Security+EC-Council
40 Hours
Certified Ethical Hacker (CEH v12) CertificationISACA
22 Hours
Certified Information Systems Auditor (CISA) CertificationISACA
40 Hours
Certified Information Security Manager (CISM) Certification(ISC)²
40 Hours
Certified Information Systems Security Professional (CISSP)(ISC)²
40 Hours
Certified Cloud Security Professional (CCSP) Certification16 Hours
Certified Information Privacy Professional - Europe (CIPP-E) CertificationISACA
16 Hours
COBIT5 Foundation16 Hours
Payment Card Industry Security Standards (PCI-DSS) CertificationOn-Demand Courses
CISSPTop Resources
Laptops for IT SecurityWeb Development
Learn to create user-friendly, fast, and dynamic web applications.
View All Web Development Coursesicon-codeBootcamps
Career Kickstarter
6 Months
Best Seller
Full-Stack Developer BootcampJob Oriented
3 Months
Best Seller
UI/UX Design BootcampEnterprise Recommended
6 Months
Java Full Stack Developer BootcampCareer Kickstarter
490+ Hours
Front-End Development BootcampCareer Accelerator
4 Months
Backend Development Bootcamp (Node JS)Skills
ReactOn-Demand Courses
Angular TrainingTop Resources
Top HTML ProjectsBlockchain
Understand how transactions and databases work in blockchain technology.
View All Blockchain Coursesicon-stop-squareBlockchain Certifications
40 Hours
Blockchain Professional Certification32 Hours
Blockchain Solutions Architect Certification32 Hours
Blockchain Security Engineer Certification24 Hours
Blockchain Quality Engineer Certification5+ Hours
Blockchain 101 CertificationOn-Demand Courses
NFT Essentials 101: A Beginner's GuideTop Resources
Blockchain Interview QsProgramming
Learn to code efficiently and design software that solves problems.
View All Programming Coursesicon-codeSkills
Python CertificationInterview Prep
Career Accelerator
3 Months
Software Engineer Interview PrepOn-Demand Courses
Data Structures and Algorithms with JavaScriptTop Resources
Python TutorialProgramming
4.7 Rating 60 Questions 25 mins read6 Readers

String in general is a sequence of characters. For example, “upgrad” is a string which contains a sequence of characters ‘u’, ‘p’, ‘g’, ‘r’, ‘a’, and ‘d’. We use double quotes to represent a string in Java. For example,
String str = “string programming interview questions in java”;
Here, we defined string variable named str and the variable value is initialized as “string programming interview questions in java”
In Java, String is an immutable object meaning once string object is created its value cannot be modified, it has lot of useful functions and it is present in java.lang package and we can import String in any class as below:
import java.lang.String;
String can be created in two different ways:
When we create a String using string literal, JVM looks into String pool to find if any existing String already created with the same value. If JVM finds similar string already exists in String pool, it does not create new String instead it points new String literal (or reference new literal) to existing string, in string pool, else if it does not find already existing string, it creates a new string in string pool and points new literal to it.
When string is created using new operator it is not stored in string pool, instead it is created in Java Heap memory.
This is a frequently asked question in Java string interview questions.
An immutable class in Java is a class whose state of an object cannot be changed once it's created. Immutable objects are thread-safe and it is preferred to use an immutable object in a multithreaded environment. All the wrapper classes in Java are immutable, for example, Integer, Long, Short, Byte, Boolean, Float, and Double.
In Java, the string class is also immutable. We can make any custom class immutable by following certain rules. Immutable objects are final that means every time you want to change the state of an object you need to create a new object instead, as you cannot modify the state of immutable objects.
A mutable class in Java is a class whose objects are mutable meaning their state can be changed after they are created. If the state of an object can be changed or mutated after it has been created, it is called mutable object in java. In Java programming language String class is immutable, however Java.util.Date, StringBuffer and StringBuilder are mutable classes.
We need to be very cautious using mutable objects in multithreading as they might produce unexpected results. In single-threaded programs it is recommended to use mutable objects like StringBuilder as they have better performance. In multi-threaded programs it is recommended to use String objects as they are immutable.
Expect to come across this popular question in String interview questions.
This is another important string related interview question in java that you should know about. As we know immutable class is a class whose state cannot be modified once it is created. We need to follow certain steps to make the class immutable, as explained below:
Example:
public class StringProgram {
public static void main(String[] args) {
ImmutableClass object = new ImmutableClass(1, "upgrad");
System.out.println("id: "+object.getId());
System.out.println("name: "+object.getName());
}
}
final class ImmutableClass {
private final int id;
private final String name;
public ImmutableClass(int id, String name){
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
} Output:
id: 1 name: upgrad
One of the most frequently posed Java string programs for interview, be ready for it.
Before Java 6 it was not possible to use string in switch case, however, from Java 7 or higher versions we can now use string in switch case statements. Java switch case String make code more readable by removing the multiple if-else chained conditions.
Java switch case String is case sensitive, it uses equals() method to compare the passed value.
Example:
public class StringProgram { public static void main(String[] args) { printMonth("Jan"); } static void printMonth(String month) { switch (month) { case "Jan": System.out.println("January"); break; case "Feb": System.out.println("February"); break; default: System.out.println("Invalid Month"); } } }
String split() function splits or breaks the given string based on the matches of the given regular expression. It returns array of string[] with string values which are split.
We need to provide a regular expression which is the condition on which you want to break or split the given string.
Let us see an example:
public class StringProgram { public static void main(String[] args) { String str = "java string manipulation interview questions "; String[] strings = str.split(" "); for (int i = 0; i < strings.length; i++) { System.out.println(strings[i]+" "); } } }
Output:
java string manipulation interview Questions
A staple in questions on string in Java, be prepared to answer this one.
Both statements compare string but there is an important difference in the above object comparison. In first statement str.equals(“upgrad”) if str is “upgrad” it will return true, and if str is null it will throw NullPointerException, whereas in the second statement “upgrad”.equals(str) if str is null it will return false.
Let us see an example:
public class StringProgram {
public static void main(String[] args) {
String str = "upgrad";
System.out.println("upgrad".equals(str));
System.out.println(str.equals("upgrad"));
}
} Above two statements will print true.
However, consider below example:
public class StringProgram {
public static void main(String[] args) {
String str = null;
System.out.println("upgrad".equals(str));
System.out.println(str.equals("upgrad"));
}
} In above case, first statement will return false whereas second statement will throw NullPointerException. This is one of tricky string related interview questions in java.
+ operator is the only overloaded operator. You can use it for both adding two numbers as well as for string concatenation purposes.
If you are using the Java version 1.5 or above, string concatenation internally uses the append() method of the StringBuilder. And for versions lower than 1.5, it uses the append() method of the StringBuffer class.
Example:
public class StringProgram { public static void main(String[] args) { String str1 = "java string "; String str2 = "manipulation interview questions "; String str3 = str1 + str2; System.out.println(str3); } }
Output
java string manipulation interview questions
public class StringProgram { public static void main(String[] args) { String str1 = "upgrad"; String str2 = "upgrad"; String str3 = "upgrad"; } }
As String initialized using string literals are stored in string pool, any number of string variables having same string value will point to one same string in string pool. In above code only one string object is created in string constant pool.
Expect to come across this popular question in string questions in Java.
In Java String objects are immutable for multiple reasons:
A must-know for anyone heading into a Java string interview, this question is frequently asked in string interview questions.
A map is a collection in Java that stores key value pairs. The keys of this must not be null and each key should be unique and point to only one value. It is represented by the Map interface of java.util package. There are various classes that provide implementation to this interface.
While storing data in a hashmap a hashcode of key is calculated, and its value is stored at the position represented by the hashcode. Similarly, while fetching the data from the hashmap, the hashcode is again calculated on provided key, and the value in the position represented by the code is returned. So, whether you are adding an object or retrieving an object from hashmap you need to calculate its hashcode.
As strings are immutable, the advantage is that, its hashcode always remains same. As string hashcode remains same hashcode value is cached, and no need to calculate the hashcode again while retrieving objects from hashmap.
It's no surprise that this one pops up often in string questions in Java.
Since Strings are immutable, if you store the password as plain text, it will be available in memory until the Garbage collector clears it. Since String is used in the String pool for reusability there is a pretty high chance that it will remain in memory for a long duration, which poses a security threat.
Anyone who has access to memory dump can find the password in clear text and that is another reason you should always use an encryptedpassword instead of plain text. Since Strings are immutable there is no way the contents of Strings can be changed because any change will produce new String. As array data can be easily wiped off by overwriting array values with something different. And array even after printed by mistakenly in code will not real the content that it stored instead prints object reference.
It will print false. Because str1 and sb objects are of different types. If you look at the equals method implementation in the String class, you will find a check using instance of operator to check if the type of passed object is String? If not, then return false.
It will print true. As intern() method will return the String object reference from the string pool since we assign it back to str2 hence both str1 and str2 are having the same reference. It means that str1 and str2 references point to the same object.