An Overview of Java Programming Language
Java is a computing platform as well as a programming language. You can download it for free. An object-oriented language, Java is a fast, secure and easily extendible language. Java is said to be more dynamic than C or C++ language because of its adaptable design.
Unlike many other programming languages, Java is a platform independent and Architecture-neutral language. Our massive collection of core java interview questions will definitely help you find a great job.
Development History of Java Programming Language
Sun Microsystems released its first version in 1995 as a core component of its platform. Majority of candidates are asked this information in advanced java interview questions.
Latest Version: The latest version is Java SE 11 (18.9 LTS), which was released in September 2018. This information is critical if you are preparing for Java interview questions.
Advantages of Java
- Simple and easy to learn
- Robust and multithreaded
- Enables high performance
- Follows WORA (write once, run anywhere) approach
Java interview questions and answers
- What is the difference between the ">>" and " >>>" operators in Java?
- What is Socket in Java?
- What is Network Programming in java?
- How we can generate random numbers in Java?
- How we can skip Finally block of exception even if some exception occurs in the exception block in Java?
- How we can make copy of a java object?
- What is the difference between Stack and Queue?
- Explain different states of a thread in Java?
- What is anonymous class in Java?
- What is the base class of all exception classes in Java?
- What is Java and why do we need it? Explain
- Explain the features of Java?
- What is the differences between C++ and Java? Explain
- Who developed Java?
- What is Java virtual machine? Explain
- Explain the difference between JDK, JRE, and JVM?
- What do you mean by classloader?
- Explain the advantages of Packages in Java?
- Explain the difference between an object-oriented programming language and object-based programming language?
- What are the Java IDE's? Explain
- What do you mean by Local variable and Instance variable?
- What is an Class?
- What is an Object?
- What is the constructor and how many types of constructors are used in Java?
- What is "this" keyword in java? Explain
- Explain the Inheritance?
- What is Polymorphism in Java?
- What is Method Overriding?
- What is Method Overloading?
- What is Interface in Java? Explain
- What is Abstract class? Explain
- What is the difference between Array and Array List in Java?
- What is the difference between HashMap and HashTable in Java?
- What is the difference between HashSet and TreeSet in Java?
- What is Collections in Java?
- Why Java is a platform independent? Explain
- What is the differences between Heap and Stack Memory in Java? Explain
- Explain the difference between abstract classes and interfaces in Java?
- What is servlet? Explain
- What is JIT compiler in Java?
- Define an applet in Java?
- What is finalize() function in Java?
- What is instanceOf operator used in Java?
- How we can make a read-only class in Java?
- How we can make a write-only class in Java?
- How we can create packages in Java?
- What is Exception Handling in Java?
- Explain the difference between throw and throws in Java?
- What is the purpose of the System class in Java?
Looking for a new job? Do not miss to read our Java interview questions and answers. Whether you are a fresher or an experienced, these questions and answers that can help you to crack your interview.
It is one endpoint of two-way communication between programs that are running on the same network.
A socket is tied to a port number in such a manner that the TCP layer can recognize the application where the data needs to be sent. If you use Socket instead of native code, your programs will communicate platform-independently.
It refers to writing programs or applications that run across multiple devices when the devices are connected on a network.
The java.net package contains a collection of interfaces and classes that communicate and allow programmers to write programs that focus on solving the current problem.
In Java, there are three ways to generate random numbers:
- java.util.Random class
- Math.random method
- ThreadLocalRandom class
This is done Using
import java.util.Random;//100 is the maximum and the 1 is our minimum.
Random rand = new Random();
int n = rand.nextInt(100) + 1;
For copying an object in Java, there are two ways- shallow copy and deep copy.
|Shallow Copy||Deep Copy|
|1.||When you want to copy only field values||All the objects are deeply copied|
|2.||Copy is dependant on original object||Copy is not dependent on earlier objects|
|1.||Objects accessed on Last In First Out (LIFO).||Objects accessed on First In First Out (FIFO)|
|2.||Object pushed on top of collection||Object inserted at the end|
|3.||Object removed from the top||Object removed from the beginning.|
|4.||Two operations are called push and pop||Two operations are called enqueue and dequeue|
In Java, there are various states of a thread. But at any point of time, it can exist in any one of these states:
- Timed Waiting
Java is a computing platform as well as a programming language. An object-oriented language, Java is a fast, secure and easily extendible language. Java is a platform independent and Architecture-neutral language.
It is used to create complete & powerful applications that may run on a single computer or be distributed among clients and servers in a network. I also can be used on the intranet applications.
- Simple and easy to learn
- Platform Independent
- Supports is Object Oriented Programming language
- Robust and multithreaded
- Enables high performance
- Java Interpreted
- Secure and its interpreter
- Follows WORA (write once, run anywhere) approach
|1.||Follows WOCA approach (Write once, compile anywhere).||Follows WORA approach (Write once, run anywhere).|
|2.||Runs as code on native machine.||Runs on virtual machines.|
|3.||Multiple binary compatibility standards are included.||Has single binary compatibility standard.|
|4.||All types of Pointers and pass-by-value are supported.||Primitive and reference types are only passed by value.|
It is a virtual machine that enables computers to run Java programs in addition to programs written in other languages but are compiled to Java bytecode. OpenJDK project has developed the Java virtual machine (JVM) reference as open source code. The reference includes a JIT compiler which is known as HotSpot.
|Java Development Kit (JDK)||JVM||JRE|
|1.||Core component of Java Environment||Converts Byte code to machine-specific code||Implementation of JVM.|
|2.||Provides tools and executables required for a Java Program||Platform dependent||Provides a platform for executing Java programs.|
|3.||It is platform specific||Provides core functions like garbage collection, memory management and security etc.||Consists of java binaries and classes.|
|Object-Based Languages||Object-Oriented Languages|
|1.||Does not support inheritance or polymorphism.||Supports polymorphism and inheritance|
|2.||Does not support built-in objects.||Support built-in objects.|
|Local Variables||Instance Variables|
|1.||Declared in constructor, method or block.||Declared in a class|
|2.||Not possible to use access modifiers||Access modifiers can be given|
|3.||Visible within the declared method||Visible for all methods|
|4.||No default value||Have default values|
The constructor is a method used to initialize an object. A normal java method has return type but the constructor does not have any explicit return type. It is called during the object creation time.
There is two type of Constructors - Default or no-arg Constructor, Parameterized Constructor.
It is a reference variable that refers to the current object. It can be used as follows:
- To refer instance variable of class
- To invoke class constructor
- Passed as an argument in method call
- Passed as an argument in the constructor call
- Used to return a class instance
It is the process when one class acquires the properties or methods or fields of another class. This process is used to arrange and manage information in hierarchical order. The class that inherits the properties is called subclass. The class whose properties get inherited is called superclass.
Method Overloading allows a class to have more than one methods of having the same name, but only if the argument lists are varying. This feature is similar to constructor overloading feature that allows more than one constructor to a class only if their argument lists are different.
Similar to class, an interface in Java is a reference type. An interface contains abstract methods, constants, static methods, default methods and nested types.
A class implements an interface and inherits the abstract methods of the same interface. Writing an interface is like writing a class.
|1.||Basic functionality||Part of collection framework.|
|2.||Fixed sized||Dynamic sized|
|3.||Contain primitive data types as well as objects of a class||Only supports object entries|
|2.||Not thread safe||Thread-safe|
|3.||Cannot be shared between threads without synchronization code||Can be shared with many threads|
|4.||Allows null key and multiple null values||Does not allow null key or value|
|1.||Offers constant time cost||Offers log time cost|
|2.||Does not maintain the order of elements||By default sorts elements in ascending order.|
|3.||Implemented using a hash table||Implemented using Binary Search Tree|
A Collection in Java is a group of individual objects that are represented as a single unit. The Collection Framework in Java defines classes and interfaces that represent a group of objects as a single unit.
The two main root interfaces are Collection interface and Map interface.
By being platform independent, we mean that the java source code can be run on all OS. The presence of a machine independent code called Byte Code, which once compiled, can be run on any platform, makes Java platform independent.
The compiler feature in Java converts or translates the high-level language into a common format understood by all the machines. The only requirement is the availability of JVM.
|Heap space||Stack Memory|
|1.||Used to allocate memory||Used for execution of the thread|
|2.||Instance variables are created||Local variables are created|
|3.||Contains Objects and reference variables||Contains methods and local variables|
|1.||Can only have abstract methods||Can have abstract, non-abstract methods|
|2.||Variables are by default final.||May contain non-final variables.|
|3.||Only has static and final variables||Can have non-final, final, non-static and static variables|
This is a Java programming class that runs on Java-enabled server or application server and extends the capabilities of servers that host applications.
A servlet possesses all the features of Java such as platform independence, portability, security and database connectivity.
JIT compiler is a component of JRE that improves the performance of applications.
JIT compiler improve the performance of JVM by compiling bytecode into machine code at runtime. Choosing the right compiler is one of the first and biggest decisions to be made when you are running a Java application.
This is a good question concerning core java interview questions for experienced professionals. finalize method is called by the Garbage Collector before performing the clean-up activity. Clean-up activity refers to the process of de-allocating all the resources, such as database connection and network connection, associated with an object.
instanceof operator is used in object reference variables. In this, the operator will check the class type or interface type of an object.
It is written as follows:
( Object reference variable ) instanceof (class/interface type)
By Read-only class we are referring to the "IMMUTABLE" concept. You can begin by defining a class so that no methods can cause changes to the internal state.
In such classes, aliasing will have no impact because its internal state is read-only.
Here are the steps for creating a package:
- Choose a name for the package
- On top of every source file, put the package statement with the package name. Source file must contain classes and types that you want in the package.
- Package statement is the first line of source file.
- You can only have one package statement in each file
- It applies to all file types.
Exception handling allows developers to handle runtime errors caused by exceptions. One way to handle exceptions is to print a simple message for users that help them correct the error that occurs due to bad data sent by the user.
Exception handling ensures the flow of the application does not break.
|1.||Used to throw an exception.||Used to declare an exception|
|2.||Followed by Exception class instance||Followed by exception class names|
|3.||Used in the method body||Used in method signature|
System class has useful fields, such as static members, that are related to the environment. This class is provided with standard streams like input, output and error.
These streams can be used to access the properties and environment variables that are externally defined.