Java
Java
JAVA
History of Java
James Gosling initiated Java language project in June 1991 for use in one of his many set-top
box projects. The language, initially called ‘Oak’ after an oak tree that stood outside
Gosling's office, also went by the name ‘Green’ and ended up later being renamed as Java,
from a list of random words.
Sun released the first public implementation as Java 1.0 in 1995. It promised Write Once,
Run Anywhere (WORA), providing no-cost run-times on popular platforms.
On 13 November, 2006, Sun released much of Java as free and open source software under
the terms of the GNU General Public License (GPL).
File Structure
Each source file can contain only one public class or interface. Two public
classes in the same file are not allowed.
A source file can contain any number of non-public classes. Each class will be
compiled into a separate .class file.
The source file name must be the name of the public class or the interface.
The source file structure should be as follows :
Package declaration
Import statement
Class or interface definition
A package statement must appear as the first statement of a source file
followed by import statements and class or interface definition.
Packages are a collection of related classes and interfaces.
If a class or interface is without a package name, then those are put into a
default no-name package.
java.lang package is imported by default, we don't have to import it explicitly.
main method
Primitives
There are 8 primitive data types in java. They are not objects.
boolean is 8 bits in size and stores true or false. It's default value is false.
byte is 8 bit signed integer and stores values in the range of -2 7 to 27 -1.It's
default value is 0.
char is 16 bit unsigned integer and stores values in the range of 0 to 2 16-1 ie
between '\u0000' to '\uffff'. It's default value is '\u0000'.
short is 16 bit signed integer and stores values in the range of -2 15 to 215 -1.
It's default value is 0.
int is 32 bit signed integer and stores values in the range of -2 31 to 231 -1. It's
default value is 0.
long is 64 bit signed integer and stores values in the range of -2 63 to 263 -1. It's
default value is 0l.
float stores 32 bit floating point values. It's default value is 0.0f.
double stores 64 bit floating point values. It's default value is 0.0d.
Variables
Operators
The modoulo (%) operator gives the remainder as result, when one operand
is divided by another.
When a int is divided by zero, java.lang.ArithmeticException will be thrown.
== and != operators are used to check, if the objects are of the same instance.
equals() method of the object class is used to check, if two objects have the
same value.
instanceof operator is used to determine, if an object reference on the left
hand side is an instance of the class or an interface on the right operand.
Arrays
Access Modifiers
Static Modifier
static access modifier is applicable to a class a whole and not to any particular
instance.
A method or a variable or a block of code can be declared as static.
When a variable is declared as static, there is only one copy for all instances
of the class. If any one of the instance change the value, the change gets
reflected to other instances.
When a method is declared as static, it can be called without creating an
instance of the class.
A static method should not refer to instance variables without using an
instance of the class. 'this' keyword should not be used when referring to the
instance.
Static block gets executed when the class is loaded. Static block gets
executed even before instance creation.
Final Modifier
Abstract modifier
Synchronized Modifier
Transient Modifier
Native Modifier
Volatile Modifier
Flow Control
Constructors
Inheritance,Overloading,Overriding
Interfaces
Interfaces along with abstract classes provide abstraction in java.
Interfaces are used to define abstract methods and constant(static final)
variables.
Classes use implements keyword to make use of interfaces. A class can
implement any number of interfaces.
An interface can extend more than one interface. But the same is not true for
classes.
A class implementing a interface should provide implementation for all the
methods declared in the interface. If it doesn't, the class becomes abstract.
Variable declartion inside a interface can be public or package access and/or
abstract.
Methods declartion inside a interface can be public or package access and/or
static/final.
Exception Handling
Garbage Collection
Threads
ABSTRACTION
We try to obtain an abstract view, model, or structure of a real-life problem and
reduce its unnecessary details. With the definition of properties of problems,
including the data which are affected and the operations which are identified, the
model abstracted from problems can be a standard solution to this type of problem.
It is an efficient way since there are nebulous real-life problems that have similar
properties.
In simple terms, it is hiding the unnecessary details & showing only the essential
parts/functionalities to the user.
DATA BINDING
Data binding is a process of binding the application UI and business logic. Any
change made in the business logic will reflect directly to the application UI.
ABSTRACTION IS ACHIEVED IN 2
WAYS :
Abstract class
Interfaces (Pure Abstraction)
ABSTRACT CLASS
An abstract class must be declared with an abstract keyword.
It can have abstract and non-abstract methods.
It cannot be instantiated.
It can have constructors and static methods also.
It can have final methods which will force the subclass not to change the
body of the method.
INTERFACES
All the fields in interfaces are public, static and final by default.
All methods are public & abstract by default.
A class that implements an interface must implement all the methods
declared in the interface.
Interfaces support the functionality of multiple inheritance.
2. What is an interface?
a. The static method belongs to the class level, not the object level.
In method overriding, it is the object that decides which method is to
be called.
b. Also, for class-level methods i.e static methods, the type reference
decides which method is called not the object being referred. It
concludes the method called is determined at compile time.
method signature(combination of
as shown below */
No, there is one scenario where the finally block does not execute.
When you run System.exit(0) in the try or catch block, then finally
block does not execute.
https://javahungry.blogspot.com/2020/10/java-interview-questions-for-2-years-
experience.html
https://bookdown.org/content/e12712f9-eea3-49cb-ad8d-a3e908f52a2f/traditional-
project-management.html#background