Skip to main content

Java Core - Top 10 Questions Every Developer Should Know

#RandomlyPickedByMe

  • What is the difference between Javascript and Java?
  • Difference between StringBuilder and StringBuffer?
  • Why do I get "SomeType@a3fde" when I print my code?
  • Why is String immutable?
  • Why "equals" method when we have "==" operator?
  • Is List<Dog> a subclass of List<Animal>?
  • Why shouldn't we use raw type?
  • Is Java “pass-by-reference” or “pass-by-value”?
  • What's the advantage of a Java enum versus a class with public static final fields?
  • Why "double x = 0.1 + 0.2" and result of print(x) is 0.30000000000000004?

1. What is the difference between Javascript and Java?

Holy crap! (Vietnamese: Thế quái nào lại có câu hỏi ngớ ngẩn vậy chứ?)

"Java and Javascript are similar like Car and Carpet are similar." - Greg Hewgill (on StackOverflow)

2. Difference between StringBuilder and StringBuffer

String is immutable. StringBuilder and StringBuffer are mutable. StringBuffer is thread-safe. StringBuilder is modern than StringBuffer.

"As of release JDK 5, StringBuffer class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization."
http://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html

3. Why do I get "SomeType@a3fde" when I print my code?

Because all classes extend the class Object which has the method toString().
public String toString(){
 return getClass().getName() + "@" + Integer.toHexString(hasCode());
}

4. Why is String immutable?

String s = "Hello";
s.replace("e", "E");
System.out.println(s); //"Hello"

s is only a reference to an String object "Hello";

5. Why "equals" method when we have "==" operator?

"equals" can be overridden, but "==" not.

6. Is List<Dog> a subclass of List<Animal>?

No. Because a List<Dog> is not a List<Animal>.

7. Why shouldn't we use raw type?

Runtime trouble, ClassCastException.

8. Is Java “pass-by-reference” or “pass-by-value”?

Java has only pass by value. A very simple example to validate this.
public void test() {
    MyClass obj = null;
    init(obj);
    //After calling init method, obj still points to null
    //this is because obj is passed as value and not as reference.
}
private void init(MyClass objVar) {
    objVar = new MyClass();
}

Imagine that likes the Shortcut (Reference) and the real file (object) in Windows. Shortcut can change content of its current point to but also can the reference to new file.

9. What's the advantage of a Java enum versus a class with public static final fields?

Consider the purpose! You should use enum types any time you need to represent a fixed set of constants such as the planets in our solar system. However, if we concerns technically the features between these; there are some more benefit when using enum: type-safe, singleton, switch case, built-in methods, etc.

10. Why "double x = 0.1 + 0.2" and result of print(x) is 0.30000000000000004?

Because double/float simply can't represent a number like 0.1.  This is caused by the way computers store floating-point numbers.
Detail: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

Reference:
[1]. https://stackoverflow.com/tags/java/info

Comments

Popular posts from this blog

DevOps for Dummies

Everyone talks about it, but not everyone knows what it is. Why DevOps? In general, whenever an organization adopts any new technology, methodology, or approach, that adoption has to be driven by a business need. Any kind of system that need rapid delivery of innovation requires DevOps (development and operations). Why? DevOps requires mechanisms to get fast feedback from all the stakeholders in the software application that's being delivered. DevOps approaches to reduce waste and rework and to shift resources to higher-value activities. DevOps aims to deliver value (of organization or project) faster and more efficiently. DevOps Capabilities The capabilities that make up DevOps are a broad set that span the software delivery life cycle. The following picture is a reference architecture which provides a template of a proven solution by using a set of preferred methods and capabilities. My Remarks Okay, that sounds cool. What does it simply mean, again? The f...

Automating deployment and managing apps on OpenShift

Previously, we maintained OpenShift templates for deploying apps in development environments as well as delivering these templates to our customers for their on-prem deployment. Customers who refer to our templates (as well as documentation) have their own configuration management tools to automate the deployment such as ArgoCD and FluxCD. My son's buildings Our developers usually modify templates (YAML) directly on OpenShift for testing and then adjust the corresponding templates stored in the Git repository in Bitbucket. This sometimes causes an issue that delivered templates are incorrect because: - Developers forget to update the templates in Git repositories. - Developers don’t test the templates Therefore, our goal was to integrate a tool into our CI/CD that can automate and manage the configuration of OpenShift apps. The delivered templates should be the ones that are able to run on our OpenShift with the following purposes: - Automate deployment from templated in Git repos...

Java 8 - Persistent data structure

The following is a series of posts about "functional programming in Java" which is the result of my understanding by reading the book " Java 8 in Action: Lambdas, Streams, and Functional-style Programming, by Alan Mycroft and Mario Fusco ". 1. Why functional programming? 2. Functional programming in Java 8 3. Java 8 - Using Functions as Values 4. Java 8 - Persistent data structure Persistent data structure is also known as a simple technique but it's very important. Its other names are functional data structure and immutable data structure. Why is it "persistent"? Their values persist and are isolated from changes happening elsewhere. That's it! This technique is described as below: If you need a data structure to represent the result of a computation, you should make a new one and not mutable an existing data structure. Destructive updates version public static A doSomething(A a){ a.setProp1("new value"); return...

Generating PDF/A From HTML in Meteor

My live-chat app was a folk of project Rocket.Chat which was built with Meteor. The app had a feature that administrative users were able to export the conversations into PDF files. And, they wanted to archive these files for a long time. I happened to know that PDF/A documents were good for this purpose. It was really frustrated to find a solution with free libraries. Actually, it took me more than two weeks to find a possible approach. TL, DR; Using Puppeteer to generate a normal PDF and using PDFBox to load and converting the generated PDF into PDF/A compliance. What is PDF/A? Here is a definition from Wikipedia: PDF/A  is an  ISO -standardized version of the  Portable Document Format  (PDF) specialized for use in the  archiving  and long-term  preservation  of  electronic documents . PDF/A differs from PDF by prohibiting features unsuitable for long-term archiving, such as  font  linking (as opposed to  font em...

Multiple Inheritance of State and Implementation

Today, I was just curious about why an enum can not extend anything else. I took a look on the Oracle document here , and I found the answer is below: "All enums implicitly extend java.lang.Enum. Because a class can only extend one parent (see Declaring Classes), the Java language does not support multiple inheritance of state (see Multiple Inheritance of State, Implementation, and Type), and therefore an enum cannot extend anything else." I have been learned of it before. But, wait a sec...! Why Java does not support multiple inheritance of state? Since I have worked with other programming languages like C++, I was able to make a class extend some other classes. The short answer is to avoid the issues of multiple inheritance of state .  I wonder if other programming languages have these below terms but Java does. Multiple inheritance of state It is the ability to inherit fields from multiple classes. There is a problem and Java avoids it. "For exa...