总结帖

http://www.journaldev.com/2366/core-java-interview-questions-and-answers#interface-vs-abstract-class

Primitive

https://www.ntu.edu.sg/home/ehchua/programming/java/J3d_String.html

The designers of Java decided to retain primitive types in an object-oriented language, instead of making everything an object, so as to improve the performance of the language. Primitives are stored in the call stack, which require less storage spaces and are cheaper to manipulate. On the other hand, objects are stored in the program heap, which require complex memory management and more storage spaces.

Why Java doesn’t support multiple inheritance?

http://www.journaldev.com/1775/multiple-inheritance-in-java

  • diamond problem

What is static keyword?

Static keyword can be used with class level variables to make it global i.e all the objects will share the same variable.

Static keyword can be used with methods also. A static method can access only static variables of class and invoke only static methods of the class.

What are Wrapper classes?

Java wrapper classes are the Object representation of eight primitive types in java. All the wrapper classes in java are immutable and final. Java 5 autoboxing and unboxing allows easy conversion between primitive types and their corresponding wrapper classes.

What is Enum in Java?

Enum was introduced in Java 1.5 as a new type whose fields consists of fixed set of constants. For example, in Java we can create Direction as enum with fixed fields as EAST, WEST, NORTH, SOUTH.

enum is the keyword to create an enum type and similar to class. Enum constants are implicitly static and final.

////////////////////////using enum//////////
public enum ThreadStates {
    START,
    RUNNING,
    WAITING,
    DEAD;
}

////////////////or ///////////////////////
public class ThreadStatesConstant {
    public static final int START = 1;
    public static final int WAITING = 2;
    public static final int RUNNING = 3;
    public static final int DEAD = 4;
}
Problems solved by enum

We can pass any int constant to the simpleConstantsExample method but we can pass only fixed values to simpleEnumExample, so it provides type safety.

We can change the int constants value in ThreadStatesConstant class but the above program will not throw any exception. Our program might not work as expected but if we change the enum constants, we will get compile time exception which removes any possibility of runtime issues.

What is this keyword?

this keyword provides reference to the current object and it’s mostly used to make sure that object variables are used, not the local variables having same name.

results matching ""

    No results matching ""