In this section we will cover the most important concepts of Core Java with clear and simple explanation. Here in this section we have covered concepts in the form of questions. Moreover, I am sure that these concepts will be beneficial to new developers & to those who are preparing for interviews. We will call this section ‘Core Java Basics Tutorial Questions’. Core Java Basics Tutorial Questions are like must to know topic for every Java professional. In addition, we have a separate section for Java Interview Questions. Let’s start learning basics of Core Java in our topic ‘Core Java Basics Tutorial Questions’.
Table of Contents (Click on links below to navigate)
- 1 Q: What is the history of Java ? How popular it is ?
- 2 Q: What are the features of Java ?
- 3 Q. How is Java a platform independent language?
- 4 Q. What is JRE, JDK, JVM in java and what are the differences between them?
- 5 Q. What is a Java Bean ?
- 6 Q: How to create custom annotations in Java?
- 7 Q. What is an Interface in Java 8 ?
- 8 Q. What is difference between Abstract class & Interface in Java 8?
- 9 Q. How many ways are there to implement Functional Interface in java?
Q: What is the history of Java ? How popular it is ?
In the early nineties, Java was created by a team led by James Gosling for Sun Micro-systems (Now Oracle). It was originally designed for use on digital mobile devices, such as cell phones. However, when Java 1.0 was released to the public in 1996, its main focus shifted to use on the Internet. It provided more interactivity with users by giving developers a way to produce animated web pages . Over the years it has evolved as a successful language for use both on and off the Internet. A decade later, it’s still an extremely popular language, with over 6.5 million developers worldwide. In the last more than 25 years Java is accepted by the majority of clients as the best language to develop applications. Java features specially Java 8 features make Java more popular among its clients and users.
Q: What are the features of Java ?
Ans : There are 12 important buzzwords(features) in java.
- Simple : Easy to learn, most of the difficult features like pointers, multiple inheritance concepts have been removed from Java.
- Platform independent : Write once run anywhere.
- Architecture Neutral : If you perform any changes in your architecture of system, java program will still run without any issue.
- Portable : We can migrate java code from windows to Linux easily.
- Secure : Lets you creating applications that can’t be invaded from outside.
- Object Oriented : From JDK 1.8 onwards Java has also incorporated functional(procedural) programming.
- Multi-threaded : You can build applications with many concurrent threads of activity, resulting in highly responsive applications.
- Robust : Having reliable programming habits for creating highly reliable applications.
- Distributed : It facilitates users to create distributed applications in Java. To create distributed applications we use RMI, EJB, CORBA etc .
- Interpreted : Java integrates the power of Compiled Languages with the flexibility of Interpreted Languages.
- High Performance : Java code after compilation converts to bytecode which is highly optimized by the Java compiler, so that the Java virtual machine (JVM) can execute Java applications at full speed.
- Dynamic : It supports dynamic loading of classes. It means class loading happens on demand.
Q. How is Java a platform independent language?
Ans : In general the compiler’s job in a programming language is to convert source code into machine understandable code( also called executable code). There is one more state of code between source code & machine understandable code in java which is called the byte-code. In fact converting java source code to machine understandable code is two step process. One is from source code to byte-code(.class file) which is done by compiler. Other is from byte-code to machine understandable code which is done by the JVM. However JVM is a software which comes automatically with JDK installation.
JVM is platform dependent. Therefore while converting from byte-code to executable code JVM makes it compatible with the platform to which it belongs. In this complete process there are two translators which makes this possible ie. compiler & JVM. Compiler can read source file(.java file). Similarly JVM can read byte-code(.class file). Because of the feature of platform in-dependency Java’s slogan in Write Once Run Anywhere(WORA). Also below points are important to keep in mind.
Java Source Code - platform independent Java Bytecode - platform independent Java Compiler - platform independent Java Executable Code - platform dependent Java Virtual Machine - platform dependent Java Software(jdk) - platform dependent Java Program - platform independent Java Software Application - platform independent
The languages where conversion of source code to machine language is a single process, those languages come under platform dependent category like C, C++.
Q. What is JRE, JDK, JVM in java and what are the differences between them?
Ans: First of all let’s understand what are the usage of these terms in java. JRE(Java Runtime Environment) helps us to run the compiled code in java whereas JDK(Java Development Kit) helps us to compile & run the code. Hence if you want to run already compiled code such as a code in the form of jar/war/ear, only JRE is sufficient. On the other hand if want to compile & then run your code, you must have JDK in your system. JVM(Java Virtual machine) helps us to convert compiled code(byte code) into machine language code. JRE contains JVM and JDK contains JRE.
JRE = Java API + JVM JDK = Compiler + JRE JVM = Interpreter + JIT
JVM runs java program with the help of interpreter.
JIT (Just in time compiler) helps interpreter to run fast when its slow.
Java API carries predefined programs like String, System, Thread, Exception, FileInputStream, FileOutputStream, ArrayList, Collections etc….
Q. What is a Java Bean ?
Ans : Java Bean is a simple java helper class, used to transfer data between classes or applications. It never acts as a main class but like postman or delivery boy between two classes. It doesn’t contain any logic. In order to pass more than 3 values from one class to another or from one project to another, we should take support of Java beans. If less than 3 values, we should use method arguments to transfer the data. However a good design in java doesn’t recommend us to have more than 3 method parameters. You can check java library(java doc), you will not find more than 3 parameters in a methods, very rarely it may have 4 but not more than that.
There are some standard guidelines to develop a Java Bean class :
1. We must declare Java Bean as a public class.
2. It is a recommendation to implement java.io.Serializable (used to sent data across network as we can easily send serializable data over the network). It is optional if you don’t want to send data over network.
3. All member variables(bean properties) should be private & non-static (if we want to send data of 20 employees, 20 values will be sent. if its static only one value will be sent, so declaring non-static becomes mandatory to get full benefit of Java Bean)
4. Also Every bean property should have one setter and one getter method (Accessor Methods).
Q: How to create custom annotations in Java?
Ans: We need to create an interface with ‘@’ as shown in the following code snap
Now Lets apply this custom annotation in a class ‘MyClass’. Below is the code.
Further to learn complete annotations and how to create custom annotations, kindly visit Annotations In Java tutorial.
Q. What is an Interface in Java 8 ?
Ans: Interfaces form an agreement between the class and the outside world, and this agreement is enforced at build time by the compiler. In object-oriented programming, it is sometimes helpful to define what a class must do but not how it will do it. Then we use Interfaces. It specifies what must be done, but not how. Once an interface defined, any number of classes can implement it. Also, one class can implement any number of interfaces.
From Java 8 onward, An interface can have abstract or default or static methods or any combination of two of them or all of them. To implement an interface, a class must provide implementations for the abstract methods described by the interface. Each class is free to determine the details of its own implementation. Thus, two classes might implement the same interface in different ways, but each class still supports the same set of methods. For example, observe the below code.
As shown in the code snippet & unlike to interfaces till java 7, from Java 8 onward apart from abstract methods now an interface can have default & static methods with method bodies.
Q. What is difference between Abstract class & Interface in Java 8?
Ans: After learning Interfaces in context of Java 8, one doubt may arise in your mind that what is difference between interface & abstract classes after introduction of default & static methods. Let’s clear the doubt from the table below:
Q. How many ways are there to implement Functional Interface in java?
Ans : There are five ways to implement Functional Interface in Java.