Java was designed so applications can be executed on any platform that supports Java without having to be recompiled.

What is Java Virtual Machine?

  • For the JVM to work, it has to be implemented on a given platform according to the Java Virtual Machine specification.
  • An abstract machine that can be mapped to any platform.
  • It describes a set of instrcutions, registers, stack, heap, and method area.

Once the specification in the JVM have been implemented for a given platform, any Java program, which, after compilation, is called bytecode, can run on that platform.

  • JVM provides a platform-independent way of executing code.
  • No JVM implementation is the same.
  • No major effects on your programs.

In short, the Java Virtual Machine is what allows Java to be run on platforms without being recompiled or rewritten. The JVM lives inside of the JRE, the Java Runtime Environment.

What is JDK and JRE?

  • The JRE, short for Java Runtime Environment, contains the Java Virtual Machine, as well as browser plug-ins for executing Java applets.
  • The JDK, short for Java Development Kit, is a fully-featured software development kit for Java, including everything from the JRE, as well as compilers, debuggers, and tools to create programs.

static method vs. non-static methods

static method

  • class method
  • belong to the class itself
  • do not need an instance in order to use a static method
  • method depends on class

non-static method

  • instance method
  • belong to each object genderated from the class
  • need an instance to use
  • method depends on individual characteristics of the object

When to use static / non-static methods?

If you want to use a method without creating an instance of the class, static methods are the way to go. Static methods can only access class variables.

Nonstaic methods can access non-static and static variables. If your method is related to an object's attributes, it should be nonstatic.

What is a String?

A String is comprised of a set of characters, including letters, numbers, and spaces.

  • Strings are immutable[不可变的] in Java.
  • Classes like StringBuilder and StringBuffer allow you to make string-like objects that are mutable.
  • StringBuffer is thread-safe because it has synchronized methods.

Java - Interview for Java 01_javaJava - Interview for Java 01_sed_02


Java - Interview for Java 01_sed_03Java - Interview for Java 01_java_04

Linked List

A linked list is a linear data structure where elements containing data of the same type are linked using pointers.

Java - Interview for Java 01_ide_05


A stack is an ordered list of objects that are inserted and removed following a last-in-first-out, LIFO, policy.

Why use Stack?

Java - Interview for Java 01_sed_06

What is abstraction?

  • Abstraction hides the implementation complexity offered by an API, design or system so that we can achieve generalizetion.
  • All the user needs to know is an example input, output and a broad description of what the function does --- this allows the function to be used for multiple purposes.

Hiding the implementation, means hiding exactly how a method or function does what it does so that we can use it for multiple purposes.

Why use abstraction?

  • Abstraction allows only essential details to be displayed to the user so that the feature can be used in more ways rather than in one super specific way.
  • Let you foucus on the required characteristics and ignore irrelevant implementation details, providing generalizetion to the program.

What is encapsulation[封装]?

  • Binding an object's state and behaviors together(In Java, fields, and methods)
  • A way of wrapping data and code acting on the data into a single unit. (In Java, a class)
  • Keep classes separated and prevents coupling.
  • With encapsulation, a class' data is hidden from other classes and can only be accessed through specific methods of its own class --- this is called data hiding.
  • In Java, we achieve encapsulation by declaring all the fields in a class as private and writing public methods in the class to set and get the values of variables.

Why encapsulation?

  • Use encapsulation so that user has no idea of the inner implementation of a given class and the data it contains.
  • Allow you to hide how values are stored within a given class.

encapsulation vs abstraction

  • Encapsulation diffs from abstraction. Abstraction provides generalizetion. Encapsulation hides unwanted implementation details from the users of an object.

What is Inheritance?

  • Inheritance is the process where one class acquire the fileds and methods of another.
  • With inhertance, we can write the common properties and functionality in one class and have other classes with unique features all extend this one class, making code more reuseable.

Why use Inheritance?

  • The main advantage of inhertance is minimizing the amout of duplicate code.
  • Inheritance allows data hiding, where the super base class can keep some data private, and this data cannot be accessed by the subclass.

Disadvantages of Inheritance

  • Superclass and subclass can become tightly coupled, meaning they cannot be used independently of each other.
  • The program extends increased effort to jump through all the levels of implementation to get to the appropriate functionality.

What is Polymorphism[多态]?

  • Polymorphism is the ability for an object or function to take many forms.
  • Runtime polymorphism is achieved through method overrding.
  • Compile time polymorphism is achieved through method overloading.

Compile Time Polymorphism

  • Compile time polymorphism is achieved through method overloading.
  • Method overloading is a feature that allows a class to have more than one method having the same name, if their argument lists are different.
  • This is similar to before where one method or function has many different forms.