Java Basic

By | December 10, 2020

1. What is Class, Object, State, Behavior?

  • Class: A template that describes the kinds of state and behavior that objects of its type support
  • Object: At runtime, when the Java Virtual Machine (JVM) encounters the new keyword, it will use the appropriate class to make an object which is an instance of that class. That object will have its own state, and access to all of the behaviors defined by its class
  • State (instance variables): Each object (instance of a class) will have its own unique set of instance variables as defined in the class.
  • Behavior (methods): Methods are where the class’ logic is stored

2. Classes vs. Files

  • Each Java class is defined in its own *.java file
  • Even put two classes in the same file. One of the classes in the file is allowed to be public.
  • A public class needs to match the file name. public class Animal2 would not compile in a file name

3. What is the main() method?

  • The gateway between the startup of a Java process (managed by the JVM) and the beginning of the programmer’s code. The JVM calls on the underlying system to allocate memory, CPU time, access file, and so on.

4. Can we overload/override the main method?

  • Overload: Yes, We can overload the main method in java but JVM only calls the original main method, it will never call our overloaded main method.
  • Override: No, we cannot override the main method of java because a static method cannot be overridden.
  • how-to-overload-and-override-main-method-in-java

5. Distinguishing b/w Object References and Primitives

Primitives typesReference types
Hold their values in the memory where the variable is allocatedDo not hold the value of the object they refer to. Instead, a reference “points” to an object.
Throw a compiler error if assigning them NULLCan be assigned NULL, which means they do not currently refer to an object
Can be assigned to another object of the same type.
Can be assigned to a new object using the NEW keyword

6. Differences for local, instance, and class variables

  • Local variables: defined within a method, MUST be initialized before use. They do not have a default value and contain garbage data until initialized.
  • Instance variables (Fields – Nonstatic fields of your class): in scope from declaration until object garbage collected.
  • Class variables (static fields within a class): in the scope of declaration until the program ends.
  • Instance and class variables do not require to initialize them, instead of default values.

7. Object vs. References

– A reference may or may not be created on the HEAP.
– All references are the same size, no matter what their type is, and accessed by their variable name
– Objects are always on the HEAP.
– They have no name and can only be accessed via a reference. Objects vary in size depending on their class definition.

8. What is String Pool

  • Since strings are everywhere in Java, they use up a lot of memory. Java realizes that many strings repeat in the program and solves this issue by reusing common ones. The String pool (location in the JVM) collects all these strings.
  • Remember two lines are subtly different.
String name = "Hello"; // use the string pool normally
String name = new String("Hello"); 
// No, JVM, I really don't want to use the String pool. 
// Please create a new object for me even though it is less efficient

9. Garbage collector runs automatically? what will happen if i call system.gc() in my program

  • Garbage collection refers to the process of automatically freeing memory on the heap by deleting objects that are no longer reachable in your program
  • An object is no longer reachable when one of two situations occurs: The object no longer has any references pointing to it OR all references to the object have gone out of scope
  • There is no guarantee that System.gc() and Runtime.getRuntime().gc() methods will definitely run Garbage Collector.

What are Access Modifiers?

  • private: Only accessible within the same class.
  • default (package private) access: private and other classes in the same package.
  • protected: default access and child classes (subclasses).
  • public: protected and classes in the other packages
Category: Q&A

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.