Java is an object-oriented and platform independent language. It has the following features-
To compile a Java source file you need to use the Java compiler which is called javac.
Syntax- javac [options] FileName.java
JIT stands for Just In Time compiler. It is considered as a part of JVM. Its primary objective is to enhance the performance of JVM by compiling some code block into native machine language instructions.
Following are the elements that live on the stack-
Following are the elements that live on the heap-
The reason of this slogan is that- Java compiler compiles the source code into bytecode, which is not executable code. Bytecode is somewhere in the middle of source code and executable code. This bytecode is platform independent and can be executed on any system.
James Gosling developed Java programming language in 1992-1994. And he is know as "The Father of Java".
Bytecode contains instructions in the form of machine language program for a Java processor.
Java supports the following operators-
Operators | Symbols |
---|---|
Arithmetic Operators | +, -, *, /, %, ++, -- |
Relational Operator | >, >=, <, <=, ==, != |
Logical Operators | &, |, ^, ~, !, &&, || |
Assignment Operators | =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=, >>>= |
In while loop, the condition is checked at the beginning of the loop and if the condition evaluates to true then code inside the while loop is executed.
In do while loop, the condition is checked at the end of the loop. Even if the condition evaluates to fails, the code inside the loop is executed at least once.
Class that is declared outside of any class is called top-level class.
Access modifiers control the access of the class members (instance variables, methods, and nested classes). Java supports four access modifiers which are described below-
Access Modifiers | Explanation |
---|---|
public | A class or class members that are declared public can be accessed by any code from any class. This modifier makes the member most accessible. |
protected | A member declared protected is accessed by all classes in the same package. You cannot declare a class protected. This modifier provides less accessibility than the public modifier. |
default | If you do not specify any access modifier while declaring a class or a class member, the default access is assumed. A class or a class member with default access can be accessed by any class in the same package. This modifier provides less accessibility than the protected modifier. |
private | This access modifier cannot be applied to a top-level class. It can be applied to the members of a top-level class which are instance variables, methods, and inner classes. This modifier makes a member least accessible. |
Yes, it is valid to declare a class inside another class. This is referred to as nested classes. In this, a class is declared as a member of another class. There are four types of nested classes-
To call static methods, an object of a class is not required. If we declare main() function non-static then JVM must first create object and then call main() method. This will create the problem of extra memory allocation.
The constructor is a special method that has the same name as that of class and has no return type. It is called whenever any object of a class is created.
Syntax access-modifier className(parameters){ //Code to be executed. }For More Details...
There are three types of constructor in Java-
There are two rules for calling a constructor-
Using this() as a method name, you can call a constructor from another constructor. It must appear at the beginning of the constructor otherwise compile-time error will occur.
Example class Point2D{ private int x; private int y; public Point2D(){ this(0,0); } public Point2D(int a){ this(a,0); } public Point2D(int a, int b){ x=a; y=b; } public Point2D(Point p){ this(p.x, p.y); } public void show(){ System.out.println(); } }For More Details...
True
No, constructor cannot be declared as final.
this keyword basically refer to the current object. Following are the usage of this keyword-
When you have more than one functions with the same name but with the different number of arguments, type of arguments, or the order of arguments, then such concept is called method overloading. In method overloading, the return type of overloaded functions may be same or different.
void add(int a, int b) void add(float a, float b) //valid as the type of arguments have changed. void add(float a, int b) //valid as the type of arguments have changed. void add(int a, float b) //valid as the order of arguments have changed. float add(float a, int b, float c) //valid the number of arguments have changed.
All the above methods are valid overloaded functions
For More Details...A class having multiple constructors is called constructor overloading.
For More Details...public class Animal{ public void sound(){ System.out.println("Animal makes sound"); } } class Dog extends Animal{ public void sound(){ System.out.println("Dog barks"); } public static void main(String args[]){ Dog d = new Dog(); d.sound(); } } Output Dog barksFor More Details...
Method Overriding | Method Overloading |
---|---|
In method overriding, the signature of overriding method must be same as that of overridden method. In other words, the number of arguments, their types, and the order must be same in both overriding and overridden methods. | In method overloading, we have more than one functions with the same name but with different sets of argument types or the same set of argument types in different order. |
The return type of both overriding and overridden method must be same. | The return type of overloaded functions may be same or different. |
It is an example of run-time polymorphism. | It is an example of compile-time polymorphism. |
You cannot make overriding method less public than the overridden method. | Overloaded functions can have different access modifiers. |
There are two usage of super keywords-
Polymorphism is a Greek word which means "several forms". It is the most important feature of any object-oriented programming language.
For More Details...static method | instance method |
---|---|
A method that is declared static is called static method. | A method that is not declared without static keyword is called instance method. |
static method can be called by class name or an instance of the class. | Instance method can be only be called by object of the class. |
It cannot be access non-static members of the class. | Both static and non-static members can be access by instance methods. |
Syntax static{ //code to be executed }For More Details...
J2SE 5.0 introduced a new feature that allows us to define methods with a variable number of arguments so that we can call the same method with a variable number of parameters. This is called variable-length argument method. Following are the rules to define this method-
Syntax access-modifier return-type methodName([parameters], type... name){ //code to be executed. } Example public int add(int... values){ int sum=0; for(int v : values){ sum+=v; } return v; } add(1,2); add(1,2,3); add(1,2,3,4);
Inheritance is a fundamental feature of object-oriented programming. It enables the programmer to write a class based on an already existing class. The already existing class is called the parent class, or superclass, and the new class is called the subclass, or derived class. The main advantages of inheritance are-
Java only supports single inheritance. While programming you may run into a situation where mulitple inheritance is required. In that case, use interface to overcome this problem.
A constant is declared using final modifier. Example- final float pi = 3.14;
If a method is declared final, then you cannot override that method.
Final class means the class cannot be extended.
For More Details...Object is the super class for all the classes.
No, local variables and method cannot be defined by access modifier. An attempt to do so will prevent compilation error.
You are not allowed to write super() and this(). Doing so will give compile time error.
A class can be defined as abstract class even if it does not have any abstract methods.
You cannot create an object of an abstract class. You can only use its variable to refer to objects of its derived class.
If a derived does not implement abstract methods, then you must define it as an abstract derived class.
A derived class cannot apply access modifier other than public on the methods inherited from the interface.
With the help of extends keyword, an interface can extend one or more interfaces.
abstract Class | Interface |
---|---|
An abstract class may or may not have abstract method. | All the methods inside an interface are by default abstract. |
An abstract class can contain constructor. | You cannot define constructor in an interface. |
In an abstract class, the data variable may or may not be defined constant. | An interface can only have constants. |
It supports single level inheritance. | It supports multiple inheritance. |
At most one top-level class may be declared public.
java.lang package is imported automatically by the JVM.
Static import is the feature that allows us to import static variables of a class so that we can refer to them without using the class name. The syntax of this feature is- import.static.
For More Details...An exception is an error that happens when an application is running. When an unexpected condition arises, the exception is thrown, and it changes the normal execution flow of the program. Java offers a very good system for exception handling.
In a program, if you perform an operation that causes an exception that is an exception is thrown, you can alway catch the exception and deal with it. This is called Exception Handling.
For More Details...The Java exception-handling mechanism contains five keywords: try, catch, throw, throws, and finally.
Throwable class.
No, if you want to execute some code regardless of whether or not there is matching catch block, you can put those code inside the finally block.
For More Details...There are two categories of exception-