white notebook

Java: Types of Exceptions, JVM, JDK vs JRE, Binary Search, System.gc() vs Runtime.gc(), HashMap vs Hashtable

Types of Exceptions in Java

In Java, there are two types of exceptions: checked exceptions and unchecked exceptions.

Differences between Checked and Unchecked Exceptions

Checked exceptions are checked at compile-time, meaning that the compiler will check if the code handles or declares these exceptions. Examples of checked exceptions include IOException and SQLException. On the other hand, unchecked exceptions are not checked at compile-time. These exceptions are subclasses of RuntimeException and Error, such as NullPointerException and OutOfMemoryError.

Java Virtual Machine (JVM) and Platform Independence

JVM stands for Java Virtual Machine. It is responsible for running Java bytecode, which is platform-independent code generated by the Java compiler. Java is known as a “platform independent programming language” because the JVM allows Java programs to run on any operating system or hardware platform that has a compatible JVM installed.

Difference between JDK and JRE

JDK stands for Java Development Kit, while JRE stands for Java Runtime Environment. JDK includes tools and libraries necessary for developing Java applications, while JRE includes only the necessary components to run Java applications. In other words, JDK is used by developers, while JRE is used by end-users to run Java applications.

Binary Search in Java

Binary search is an algorithm used for searching elements in a sorted array. It works by repeatedly dividing the search space in half until the desired element is found or the search space is empty. Binary search can be implemented in various programming languages, including Java and JavaScript.

System.gc() and Runtime.gc() Methods

The System.gc() and Runtime.gc() methods are used to suggest the JVM to perform garbage collection. Garbage collection is the process of automatically reclaiming memory occupied by objects that are no longer needed. However, calling these methods does not guarantee immediate garbage collection, as it is ultimately up to the JVM to decide when to perform garbage collection.

Differences between HashMap and Hashtable

HashMap and Hashtable are both implementations of the Map interface in Java. However, there are some differences between them. Hashtable is synchronized, meaning it is thread-safe and can be used in multi-threaded environments. HashMap, on the other hand, is not synchronized and is not thread-safe. Additionally, Hashtable does not allow null keys or values, while HashMap allows one null key and multiple null values.

Comments

Leave a Reply

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