Q2. Explain Public Static Void Main (String Args ) in Java
Q2. Explain Public Static Void Main (String Args ) in Java
Q2. Explain Public Static Void Main (String Args ) in Java
JDK: It stands for Java Development Kit. It is the tool necessary to compile, document and JRE +
development tools.
JRE: It stands for Java Runtime Environment JRE refers to a runtime environment in which Java
bytecode can be executed. It’s an implementation of the JVM which physically exists.
JVM: It stands for Java Virtual Machine. It is an abstract machine. It is a specification that provides a
run-time environment in which Java bytecode can be executed. JVM follows three notations:
Specification, Implementation, and Runtime Instance.
main() in Java is the entry point for any Java program. It is always written as public static void
main(String[] args).
public: Public is an access modifier, which is used to specify who can access this method.
Public means that this Method will be accessible by any Class.
static: It is a keyword in java which identifies it is class-based. main() is made static in Java
so that it can be accessed without creating the instance of a Class. In case, main is not made
static then the compiler will throw an error as main() is called by the JVM before any objects
are made and only static methods can be directly invoked via the class.
void: It is the return type of the method. Void defines the method which will not return any
value.
main: It is the name of the method which is searched by JVM as a starting point for an
application with a particular signature only. It is the method where the main execution occurs.
String args[]: It is the parameter passed to the main method.
Java is called platform independent because of its byte codes which can run on any system
irrespective of its underlying operating system.
Wrapper classes convert the Java primitives into the reference types (objects). Every primitive data
type has a class dedicated to it. These are known as wrapper classes because they “wrap” the primitive
data type into an object of that class. Refer to the below image which displays different primitive type,
wrapper class and constructor argument.
In Java, constructor refers to a block of code which is used to initialize an object. It must have the
same name as that of the class. Also, it has no return type and it is automatically called when an object
is created.
Equals() method is defined in Object class in Java and used for checking equality of two objects
defined by business logic.
“==” or equality operator in Java is a binary operator provided by Java programming language and
used to compare primitives and objects. public boolean equals(Object o) is the method provided by
the Object class. The default implementation uses == operator to compare two objects. For example:
method can be overridden like String class. equals() method is used to compare the values of two
objects.
Packages in Java, are the collection of related classes and interfaces which are bundled together. By
using packages, developers can easily modularize the code and optimize its reuse. Also, the code
within the packages can be imported by other classes and reused. Below I have listed down a few of
its advantages:
Java doesn’t use pointers because they are unsafe and increases the complexity of the program. Since,
Java is known for its simplicity of code, adding the concept of pointers will be contradicting.
Moreover, since JVM is responsible for implicit memory allocation, thus in order to avoid direct
access to memory by the user, pointers are discouraged in Java.
JIT stands for Just-In-Time compiler in Java. It is a program that helps in converting the Java
bytecode into instructions that are sent directly to the processor. By default, the JIT compiler is
enabled in Java and is activated whenever a Java method is invoked. The JIT compiler then compiles
the bytecode of the invoked method into native machine code, compiling it “just in time” to execute.
Once the method has been compiled, the JVM summons the compiled code of that method directly
rather than interpreting it. This is why it is often responsible for the performance optimization of Java
applications at the run time.
In Java, access modifiers are special keywords which are used to restrict the access of a class,
constructor, data member and method in another class. Java supports four types of access modifiers:
1. Default
2. Private
3. Protected
4. Public
A class in Java is a blueprint which includes all your data. A class contains fields (variables) and
methods to describe the behavior of an object. Let’s have a look at the syntax of a class.
1class Abc {
2member variables // class body
3methods}
An object is a real-world entity that has a state and behavior. An object has three characteristics:
1. State
2. Behavior
3. Identity
Object-Oriented Programming or OOPs is a programming style that is associated with concepts like:
1. Inheritance: Inheritance is a process where one class acquires the properties of another.
2. Encapsulation: Encapsulation in Java is a mechanism of wrapping up the data and code
together as a single unit.
3. Abstraction: Abstraction is the methodology of hiding the implementation details from the
user and only providing the functionality to the users.
4. Polymorphism: Polymorphism is the ability of a variable, function or object to take multiple
forms.
In Java, a local variable is typically used inside a method, constructor, or a block and has only local
scope. Thus, this variable can be used only within the scope of a block. The best benefit of having a
local variable is that other methods in the class won’t be even aware of that variable.
Example
1if(x > 100)
2{
3String test = "AMS";
4}
Whereas, an instance variable in Java, is a variable which is bounded to its object itself. These
variables are declared within a class, but outside a method. Every object of that class will create it’s
own copy of the variable while using it. Thus, any changes made to the variable won’t reflect in any
other instances of that class and will be bound to that particular instance only.
1
class Test{
2
public String EmpName;
3
public int empAge;
4
final is a special keyword in Java that is used as a non-access modifier. A final variable can be used in
different contexts such as:
final variable
When the final keyword is used with a variable then its value can’t be changed once assigned. In case
the no value has been assigned to the final variable then using only the class constructor a value can
be assigned to it.
final method
When a method is declared final then it can’t be overridden by the inheriting class.
final class
When a class is declared as final in Java, it can’t be extended by any subclass class but it can extend
other class.
break continue
2. It causes the switch or loop statements to 2. It doesn’t terminate the loop but causes the loop
terminate the moment it is executed to jump to the next iteration
3. It terminates the innermost enclosing loop 3. A continue within a loop nested with a switch will
or switch immediately cause the next loop iteration to execute
Example break:
3if (i == 3)
4{
5break;
6}
7System.out.println(i);
8}
Example continue:
2{
3if(i == 2)
4{
5continue;
6}
7System.out.println(i);
8}
An infinite loop is an instruction sequence in Java that loops endlessly when a functional exit isn’t
met. This type of loop can be the result of a programming error or may also be a deliberate action
based on the application behavior. An infinite loop will terminate automatically once the application
exits.
For example:
2{
4for(;;)
5System.out.println("Welcome to Edureka!");
7}
8}
}
}
Should the JVM call new JavaClass(int)? What should it pass for x? If not, should the JVM instantiate
JavaClass without running any constructor method? because that will special-case your entire class –
sometimes you have an instance that hasn’t been initialized, and you have to check for it in every
method that could be called. There are just too many edge cases and ambiguities for it to make sense
for the JVM to have to instantiate a class before the entry point is called. That’s why main is static.
What happens if you remove static modifier from the main method?
Program compiles successfully . But at runtime throws an error “NoSuchMethodError”.
Member Variables (Class Level Scope) : The member variables must be declared inside class
(outside any function). They can be directly accessed anywhere in class
Local Variables (Method Level Scope) : Variables declared inside a method have method
level scope and can’t be accessed outside the method.
Loop Variables (Block Scope) : A variable declared inside pair of brackets “{” and “}” in a
method has scope withing the brackets only.
What is an abstract class? How abstract classes are similar or different in Java from C++?
Abstract classes are classes that contain one or more abstract methods. An abstract method is a
method that is declared, but contains no implementation. Abstract classes may not be instantiated, and
require subclasses to provide implementations for the abstract methods.
Like C++, in Java, an instance of an abstract class cannot be created, we can have references
of abstract class type though.
Like C++, an abstract class can contain constructors in Java. And a constructor of abstract
class is called when an instance of a inherited class is created
In Java, we can have an abstract class without any abstract method. This allows us to create
classes that cannot be instantiated, but can only be inherited.
Abstract classes can also have final methods (methods that cannot be overridden). For
example, the following program compiles and runs fine.
The normal main method acts as an entry point for the JVM to start the execution of program.
We can overload the main method in Java. But the program doesn’t execute the overloaded
main method when we run your program, we need to call the overloaded main method from
the actual main method only.
1. In Java, all classes inherit from the Object class directly or indirectly. Therefore, there
is always a single inheritance tree of classes in Java, and Object class is root of the
tree.
2. In Java, members of the grandparent class are not directly accessible. See this G-Fact
for more details.
3. The meaning of protected member access specifier is somewhat different in Java. In
Java, protected members of a class “A” are accessible in other class “B” of same
package, even if B doesn’t inherit from A (they both have to be in the same package).
4. Java uses extends keyword for inheritance. Unlike C++, Java doesn’t provide an
inheritance specifier like public, protected or private. Therefore, we cannot change the
protection level of members of base class in Java, if some data member is public or
protected in base class then it remains public or protected in derived class. Like C++,
private members of base class are not accessible in derived class.
Unlike C++, in Java, we don’t have to remember those rules of inheritance which are
combination of base class access specifier and inheritance specifier.
5. In Java, methods are virtual by default. In C++, we explicitly use virtual keyword. See
this G-Fact for more details.
6. Java uses a separate keyword interface for interfaces, and abstract keyword for
abstract classes and abstract functions.
7. Unlike C++, Java doesn’t support multiple inheritance. A class cannot inherit from
more than one class. A class can implement multiple interfaces though.
8. In C++, default constructor of parent class is automatically called, but if we want to
call parametrized constructor of a parent class, we must use Initializer list. Like C++,
default constructor of the parent class is automatically called in Java, but if we want to
call parameterized constructor then we must use super to call the parent constructor.