Java interview questions and answers is one of the most common reasons that people are searching for information online. If you have an interview or test coming up soon, obviously you want to be best prepared for that by studying the most likely questions that you are to be asked.
The Java ecosystem has been around for over 20 years and is huge, so we try and cover the core elements of Java, and help you get answers to the questions that you are likely to be faced with. The reason many interviewers and tester tend to focus on core java, is that there are many frameworks and librarys out there that extend the java enviroment with useful tools. But the basics that developers need to know to be successful are the core elements of the java language, as that will allow them the meet that challenges that java development will present them with.
What Do We Mean By Core Java?
There a few definitions for Core Java around, but the commonly accepted meaning is in relation to the main language elements, syntax and data structures of the Java language that are required for a developer. It generally excludes Servlets, JSPs, JPA etc.
What Are The Most Popular Core Java Interview Question Topics?
Heres a list of the more popular topics for Core Java interview questions. These are the areas that are most likely to come up in any interview.
- OOPS Concepts. Object Oriented Programming Concepts including abstraction, encapsulation, inheritance and polymorphism.
- Java Strings. String is the most widely used class in Java so you are very likely to see question on Strings or using Strings.
- Collections Framework. The Collections Framework is a set of interfaces and classes that provide groups of objects that can be dealt with in a single object.
- Multithreading. Logic to provide multiple threads of execution usually with the support of the operating system.
- Generics. Features for use of Generic Types and Methods
- Exception Handling. Handling of errors and other exceptional steps.
- Stream API. Language features to support operations on streams of elements.
- Lambda Expressions. A concise way of writing anonymous functions that can be passed as if they were variables.
- Latest Release Features. New features available in each release of Java
Java String Interview Questions
What Is A String in Java?
In Java a String is a class in java.lang. A String represents a sequence of characters, such as “Hello World”. A string is an immutable object in java. An immutable objects state cannot be changed once it has been created.
Why Is A String Immutable In Java?
As we mentioned above, an immutable objects state cannot be changed after it has been created. The key benefits of making Strings immutable in Java are performance and security.
The String is one of the most commonly used, and probably the most commonly used data structure or class in Java. So because they are so widely used, its very likely that there are a lot of the same strings in use at any one time in Java. These strings could be taking up a lot of space in memory.
To prevent that as an issue, Java stores strings in memory in the JVM, and ensures that there is only one copy of any String literal in memory. By use of a String pool, Java is able to optimize the processing of Strings by ensuring that any variable that references the same sequence of characters actually points to the same String in the String pool. The String also ensures faster access to strings.
From a security point of view, making Strings immutable also means that at any point, we know that the String cant be changed. This is useful if we are processing values like credit card details, or usernames and passwords.
How Do You Create A String Object?
The main way to create a string, is to assign a literal to a variable such as:
String name = "Colin";
When we create a string using a quoted literal like above, the JVM will look for the literal string in the thread pool first. If it finds it then the reference is returned and assigned to the variable. If its not found then a String will be created and added to the thread pool and the reference assigned to the variable.
The alternative way to create a String is to use the new operator.
String name = new String("Colin");
With the use of the new operator, a new String object is created that doesnt get added to the thread pool
How To Reverse A String In Java
A commonly asked interview question for beginner is how to reverse a String in Java and there are a number of ways to do this. The simplest method is to use the reverse() method on the StringBuffer or StringBuilder classes.
@Test
public void reverseString() {
String name = "ColinWilliams";
System.out.println(name);
StringBuilder sb = new StringBuilder(name);
String reversedName = sb.reverse().toString();
System.out.println(reversedName);
assertThat("The name has been reversed", reversedName, is("smailliWniloC"));
}
and the output for that
ColinWilliams
smailliWniloC
How To Reverse A String In Java without StringBuffer or StringBuilder
Of course the interviewer may say that they want you to reverse the string without any additional classes so lets see a way to do that.
@Test
public void reverseString2() {
String name = "JoeBloggs";
System.out.println(name);
String reversedName = "";
for (int i = name.length() - 1; i >= 0; i--) {
reversedName = reversedName + name.charAt(i);
}
System.out.println(reversedName);
assertThat("The name has been reversed", reversedName, is("sggolBeoJ"));
}
As shown above one of the simplest ways to do that is to read the String character by character in reverse or with a descending value for loop
JoeBloggs
sggolBeoJ
How To Remove A Character From A String?
https://www.javatpoint.com/how-to-remove-a-particular-character-from-a-string
How To Make A String Upper Or Lowercase?
https://www.guru99.com/java-string-tolowercase-methods.html
How Do You Compare 2 Strings In Java?
https://www.baeldung.com/java-compare-strings
What Is The Difference between String, StringBuffer and StringBuilder?
https://stackoverflow.com/questions/2971315/string-stringbuffer-and-stringbuilder
How Do You Split A String In Java?
The string.split() method is the most popular way to split a string in Java. See here for examples of splitting a string with java.
What is the String intern() method?
https://dzone.com/articles/string-interning-what-why-and
Is String thread-safe?
Why is String used as a HashMap key in Java?
https://www.linkedin.com/pulse/10-things-java-developer-should-know-hashmap-chinmay-parekh