- 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