🔰 Java Developer 🔰
Last Updated: 2023-09-23- What are the features of Java Programming Language?
- What is the importance of the main() method in Java?
- What is the difference between path and classpath variables?
- Is Java Pass By or Pass By Reference?
- What is the final Keyword in Java?
- What are the access modifiers in Java?
- What is a static import?
- What is Enum in Java?
- What is Composition in Java?
- What is the Java Reflection API?
- What is marker interface?
- What is the difference between the user thread and daemon thread?
- What is CountDownLatch in Java?
- What is Compare and Swap (CAS) algorithm?
- What is the volatile keyword in Java?
- What is the Java timer class? How to schedule a task to run after the specific interval?
- How to write a custom exception in Java?
- What is OutOfMemoryError in Java?
- Can we have an empty catch block?
- What happens when an exception is thrown by the main method?
- What is JDBC API and when do we use it?
- What are the different types of JDBC drivers?
- What is JDBC ResultSet?
- What is “dirty read” in JDBC? Which isolation level prevents dirty read?
- What is 2 phase commit?
Following are the features of Java Programming Language:
Simple: If you know C++ already, it will easier for you to write a program because Java syntax is fully based on C++ which makes easier to learn.
Object–Oriented: Java is not fully object-oriented but it is to the extent and works on an object-oriented paradigm which allows managing the code with the help of objects.
Portable: Java works on the principle of read-once-write anywhere approach which can be executed in every machine.
Platform Independent: Java doesn’t depend upon the operating system to execute the code as it comes with its own platform where its code is executed.
Secured: Java doesn’t use any pointers it has its own mechanism of handling memory management and with perfect authorization process then only it gives access to the data to the program. Also, it uses the concept of Byte Code and Exception handling which makes it more secured.
Robust: The concepts like Automatic garbage collection, Exception handling, etc. make it more robust because it uses strong memory management mechanism.
Distributed: Java is distributed because it helps users to create distributed applications in Java. For creating distributed RMI and EJB are used by which we can access files by calling the methods from any machine on the internet.
Dynamic: Java is a dynamic programming language. It supports dynamic loading of classes which means classes are loaded on demand.
main() method in java is the entry point for any java program. The syntax which is used for the main method in java programming is-:
public static void main(String args[])
With the main method being public and static it helps java to access it without initializing the class. The value which is passed in the input parameter is an array of String by which runtime arguments are passed.
The path is an environment variable which is used by operating systems to locate the executables. That’s the reason why when we install java for the first time or want an executable to be found by OS, we need to add the directory location in the Path variable.
Classpath is very specific to Java and used for locating class files by java executables. It can be a directory, ZIP files JAR files, etc. when we provide classpath location while running java application.
This is one of the most confusing questions about the Java, some believe that it is Pass by reference and others believe that it is Pass by value. But according to the Java Spec stated that everything in Java is Pass by Value. These terms are directly associated with variable passing and method calling as method parameters. In Java, when we pass a reference of complex types as any method parameters then the memory address is copied to a new reference variable in the exact same manner.
Final keyword is used with the class to make sure that any other class can’t extend it. For example String class is final and we can‘t extend it. Final keyword is also used with different methods so that it can’t be overridden by any child classes.
Variable is also made final so it is only assigned once. Java interfaces variable is also by default final and static.
Oracle Java Certifications
Access Modifiers are the keywords which are used for set accessibility to classes, methods, and other members. In Java, these are the four access modifiers:
Public: The classes, methods, variables, and other members who are defined as public, can be accessed by any class or method.
Protected: As it sounds, it has special characteristics. Classes or methods that are defined as protected can be accessed by the class of the same package, or by the sub-class of the parent class, or within the same class.
Default: Default is accessible within the package only. All the classes, methods, and variables are of default when the public, protected, or private are not used.
Private: Classes, methods, and variables which are defined as private can be accessed within the class only.
If we have to use any static variable or method from other class, usually we import the class and then import the method/variable with the class name.
import java.lang.Math;
//inside class
double test = Math.PI * 8;
We can do the same thing by importing the static method or variable then by only we can use it in the same class it belongs to.
import java.lang.Math.PI;
//no need to refer to the class now
double test = PI * 8;
Overuse of static import create confusion and make the program unreadable and unimaginable.
Enum was introduced in with Java 1.5 version as a new type whose field consists of fixed sets of constants. For example, in java direction like East, West, North, and South are created with enum as the fixed set of fields. enum is a keyword used to create enum type which is similar to a class. Enum constants are final and implicitly in nature.
The composition is the design technique used in java to implement a has-a relationship in classes. It is used for Object composition for code reuse. It is achieved by using instance variables that refer to a different object. The main benefit of using composition is that it provides control over the visibility of other objects to client classes and reuse what we really need.
Java Reflection API provides the ability to inspect and modify the behavior of any application during runtime. We can inspect any java class, enum, and interface and also get their methods and field details. It is an advanced topic and used for breakage of any design pattern such as Singleton pattern by invoking the private constructor which also violate the rules of access modifiers.
The marker interface in java is a design pattern used to provide run-time information about the objects. It provides external means to add metadata with a class where language does not have any explicit support for metadata. In a java program, it is used as an interface with no method specified. Serializable interface is a good example of marker interface
When a thread is created in a java program, it is known as user thread. A daemon thread is a thread which runs on background and doesn’t prevent JVM from terminating. When there is no user thread running then JVM shutdowns the program and quits. A child thread created from daemon thread is known as a daemon thread.
CountDownLatch in Java is synchronizer which allows one Thread to wait for one or more Threads before start processing. This is a very crucial requirement and often used in a server-side core java application and having this function simplifies the process of development. This is one of the most important questions and often asked in big java interviews.
Compare and Swap (CAS) is an atomic instruction used for achieving synchronization in multithreading. It compares the content of a given memory location and if matches then it modifies the value of the content of that memory location to a new given value. This is a single atomic operation and the new value is calculated which depends upon the up-to-date information and if it is used by another thread then the write operation would fail.
Volatile keyword in Java is used with variables and all the threads read its value directly from the memory location and don’t cache it. Volatile keyword makes sure that the read value is exactly the same as in the memory location.
Java timer class is a subclass of java.util package and it is a utility class used for scheduling of a thread which will be executed in a certain time in future. It is used for scheduling of a task which can run one time or be run in regular intervals. Java.util.TimerTask is an abstract class which uses the runnable interface and we need to extend this class to create our own TimerTask which can be scheduled by using java Timer class.
By extending the Exception class or any of its subclasses we can create custom exception class in java. A custom exception can have its own methods and variables which are used for passing the error codes or any exception related information to the exception handler.
Creation of Custom exception in java:
Package com.examples.exceptions;
import java.io.IOException;
public class MyException extends IOException {
private static final long serialVersionUID = 4664456874499611218L;
private String errorCode = “Unknown_Exception”;
public MyException(String message, String errorCode) {
super(message);
this.errorCode=errorCode;
}
Public String getErrorCode() {
Return this.errorCode;
}
}
OutOfMemoryError in Java is a subclass of java.lang.VirtualMachineError and its thrown by the JVM when it ran out of the heap memory. This error can be fixed by providing more memory to run in the java application using following java options:
$>java MyProgram –Xms1024m –Xmx1024m –XX:PermSize=64m –XX:MaxPermSize=256m
Yes, we can use an empty catch block but it’s not a right way of programming. When we use an empty catch block and if it catches any exception then we will have no information about that exception and it will be a nightmare of the situation to debug it. There should be a log statement file for logging all the exception details.
When the main0 method throws out an exception then the Java Runtime Environment terminates the program and the print the exception message which was stacked in system console.
Java DataBase connectivity API allows us t work with the relational database in java programming. JDBC API classes and interfaces are the part of java.sql and javax.sql package. It is used for getting the database connection, run SQL queries and stored procedures in the database server and process the results.
JDBC API is designed in a way which allows loose coupling between our java program and actual JDBC drivers which makes our life easier from switching one database server to another.
There are four types of JDBC drivers available. Any program which has data connectivity works on the two parts, first is JDBC API and second is the drivers which do the actual work.
JDBC-ODBC plus ODBC Driver (Type 1)
This uses ODBC drivers to connect with the database. ODBC drivers must be installed to connect with the database.
Native API partly Java technology-enabled driver (Type 2)
This driver used to convert the JDBC class to client API for the database servers. Database client API should be installed because of the extra dependency on the database client API drivers.
Pure Java driver for Database Middleware (Type 3)
This driver is used to send the JDBC calls to a middleware server which helps in connecting with the different database Middleware server must be installed to work with it.
Direct-to-Database Pure Java Driver (Type 4)
This driver is used to send JDBC calls to the network protocol which is understood by the database server. This is a simple suitable driver for database connectivity over the network. For using this driver, Oracle DB, MySQL connector should be installed.
JDBC ResultSet is like a table of data which represents a database result set, which is generated when a database is queried by an executing statement. ResultSet object manages a cursor pointing which points to a current row of the data. At starting it positioned before the first row. By the use of next() method, it moves to the next row. If there is no row to move forward then the next() method returns false and it can be used in while loop to iterate through the result set.
When transactions taking place and there is some that a row is updated at the same time some query can read the updated value. This dirty read happens because that the updated value is not permanent yet, the transaction that has been updated could rollback to a previous value which will result in invalid data.
Isolation levels are used for preventing the dirt read:
TRANSACTION_READ_COMMITED, TRANSACTION_REPEATABLE_READ, and TRANSACTIO_SERIALIZABLE.
In a distributed systems environment where multiple databases are used, it required to use 2 phase commit protocol. It is an atomic commitment protocol used in distributed systems. In the first phase of this protocol, the transaction manager sends commits requests to all the available transaction resources. If it gets an ok signal from all the transaction resources then all the transaction changes to the resources are committed by the transaction manager. If a transaction resource gets an abort signal then all the changes are rollback by the transaction manager.
Last Updated: 2023-09-23