0% found this document useful (0 votes)
2 views5 pages

Basiccorejavaiq

The document provides an overview of various Java concepts including wrapper classes, singleton classes, memory management (heap vs stack), access modifiers, object-oriented programming principles, and the differences between local and instance variables. It also discusses the final keyword, Java String Pool, and the characteristics of collections and maps in Java. Additionally, it explains method overloading and overriding, encapsulation, and relationships in OOP such as association, aggregation, and composition, along with object cloning and the differences between shallow and deep cloning.

Uploaded by

aravindhkumar311
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Basiccorejavaiq

The document provides an overview of various Java concepts including wrapper classes, singleton classes, memory management (heap vs stack), access modifiers, object-oriented programming principles, and the differences between local and instance variables. It also discusses the final keyword, Java String Pool, and the characteristics of collections and maps in Java. Additionally, it explains method overloading and overriding, encapsulation, and relationships in OOP such as association, aggregation, and composition, along with object cloning and the differences between shallow and deep cloning.

Uploaded by

aravindhkumar311
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

wrapper classes: they “wrap” the primitive data type into an object of that class

Singleton class is a class whose only one instance can be created at any given
time, in one JVM. A class can be made singleton by making its constructor private

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

difference between Heap and Stack memory are


-------------------------------------------------
Stack memory
--------------
1. Stack memory is used only by one thread of execution.
2. Stack memory can’t be accessed by other threads.
3. Management Follows LIFO manner to free memory.
4. Exists until the end of execution of the thread.
5. Stack memory only contains local primitive
and reference variables to objects in heap space.

Heap memory
----------------------
1.Heap memory is used by all the parts of the application
2.Objects stored in the heap are globally accessible.
3. Memory management is based on the generation associated with each object.
4.Heap memory lives from the start till the end of application execution.
5.Whenever an object is created, it’s always stored in the Heap space.

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

Why pointers are not used in Java?


------------------------------------
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.

What is JIT compiler 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.
What are access modifiers in Java?
-----------------------------------
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.

What is Object Oriented Programming?


-------------------------------------
Object-oriented programming or popularly known as OOPs is a programming model or
approach
where the programs are organized around objects rather than logic and functions.
In other words, OOP mainly focuses on the objects that are required to be
manipulated instead of logic.
This approach is ideal for the programs large and complex codes and needs to be
actively updated or maintained.

What are the main concepts of OOPs in Java?


----------------------------------------------
Object-Oriented Programming or OOPs is a programming style that is associated with
concepts like:

Inheritance: Inheritance is a process where one class acquires the properties of


another.
Encapsulation: Encapsulation in Java is a mechanism of wrapping up the data and
code together as a single unit.
Abstraction: Abstraction is the methodology of hiding the implementation details
from the user and only providing the functionality to the users.
Polymorphism: Polymorphism is the ability of a variable, function or object to take
multiple forms.

What is the difference between a local variable and an instance variable?


---------------------------------------------------------------------------
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.

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.

What is final keyword in Java?


--------------------------------
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:

1. 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.

2. final method
When a method is declared final then it can’t be overridden by the inheriting
class.

3. 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.

What is Java String Pool?


---------------------------
Java String pool refers to a collection of Strings which are stored in heap memory.
In this, whenever a new object is created, String pool first checks whether the
object is already present in the pool or not.
If it is present, then the same reference is returned to the variable else new
object will be created in the String pool and the respective reference will be
returned.

Difference between String, String Builder, and String Buffer.


-------------------------------------------------------------

Factor String String Builder String


Buffer
-------- ---------- --------------- --------------
Storage Area Constant String Pool Heap Area Heap Area
Mutability Immutable Mutable Mutable
Thread Safety Yes Yes No
Performance Fast Slow Fast

Why Java Strings are immutable in nature?


------------------------------------------
In Java, string objects are immutable in nature which simply means once the String
object is created its state cannot be modified.
Whenever you try to update the value of that object instead of updating the values
of that particular object, Java creates a new string object.
Java String objects are immutable as String objects are generally cached in the
String pool.
Since String literals are usually shared between multiple clients, action from one
client might affect the rest.
It enhances security, caching, synchronization, and performance of the application

What is a Map in Java?


------------------------
In Java, Map is an interface of Util package which maps unique keys to values.
The Map interface is not a subset of the main Collection interface and thus it
behaves little different from the other collection types.
Below are a few of the characteristics of Map interface:
Map doesn’t contain duplicate keys.
Each key can map at max one value.

What is collection class in Java?


--------------------------------
In Java, the collection is a framework that acts as an architecture for storing and
manipulating a group of objects.
Using Collections you can perform various tasks like searching, sorting, insertion,
manipulation, deletion, etc.

What is method overloading and method overriding?


----------------------------------------------------
Method Overloading :
- - - - - - - - - -
In Method Overloading, Methods of the same class shares the same name but each
method must have a different number of parameters or parameters having different
types and order.
Method Overloading is to “add” or “extend” more to the method’s behavior.
It is a compile-time polymorphism.
The methods must have a different signature.
It may or may not need inheritance in Method Overloading.

Method Overriding:
- - - - - - - - - -
In Method Overriding, the subclass has the same method with the same name and
exactly the same number and type of parameters and same return type as a
superclass.
Method Overriding is to “Change” existing behavior of the method.
It is a run time polymorphism.
The methods must have the same signature.
It always requires inheritance in Method Overriding.

Can you override a private or static method in Java?


-----------------------------------------------------
You cannot override a private or static method in Java.
If you create a similar method with the same return type and same method arguments
in child class then it will hide the superclass method;
this is known as method hiding.
Similarly, you cannot override a private method in subclass because it’s not
accessible there.
What you can do is create another private method with the same name in the child
class.

What is encapsulation in Java?


-------------------------------
Encapsulation is a mechanism where you bind your data(variables) and code(methods)
together as a single unit.
Here, the data is hidden from the outer world and can be accessed only via current
class methods.
This helps in protecting the data from any unnecessary modification.
We can achieve encapsulation in Java by:
* Declaring the variables of a class as private.
* Providing public setter and getter methods to modify and view the values of
the variables.

What is an association?
------------------------
Association is a relationship where all object have their own lifecycle and there
is no owner.
Let’s take the example of Teacher and Student.
Multiple students can associate with a single teacher and a single student can
associate with multiple teachers
but there is no ownership between the objects and both have their own lifecycle.
These relationships can be one to one, one to many, many to one and many to many.

What do you mean by aggregation?


----------------------------------
An aggregation is a specialized form of Association where all object has their own
lifecycle
but there is ownership and child object can not belong to another parent object.
Let’s take an example of Department and teacher.
A single teacher can not belong to multiple departments,
but if we delete the department teacher object will not destroy.
What is composition in Java?
-----------------------------
Composition is again a specialized form of Aggregation and we can call this as a
“death” relationship.
It is a strong type of Aggregation.
Child object does not have their lifecycle and if parent object deletes all child
object will also be deleted.
Let’s take again an example of a relationship between House and rooms.
House can contain multiple rooms there is no independent life of room and
any room can not belongs to two different houses if we delete the house room will
automatically delete

marker interface?
-------------------
A Marker interface can be defined as the interface having no data member and member
functions.
In simpler terms, an empty interface is called the Marker interface.
The most common examples of Marker interface in Java are Serializable, Cloneable
etc.

What is object cloning in Java?


----------------------------------
Object cloning in Java is the process of creating an exact copy of an object.
It basically means the ability to create an object with a similar state as the
original object.
To achieve this, Java provides a method clone() to make use of this functionality.
This method creates a new instance of the class of the current object and
then initializes all its fields with the exact same contents of corresponding
fields.
To object clone(), the marker interface java.lang.Cloneable must be implemented
to avoid any runtime exceptions.
One thing you must note is Object clone() is a protected method, thus you need
to override it.

Difference between clonable interface


--------------------------------------

Feature Shallow Cloning


Deep Cloning
Object Copying Copies fields, but nested objects are referenced
Creates a fully independent copy, including nested objects
Reference Behavior Changes in cloned object's nested objects reflect in the
original Changes in cloned object's nested objects do not affect the
original
Performance Faster (only references are copied)
Slower (complete object duplication)
Use Case Simple objects, immutable fields
Complex objects, independent modifications required

You might also like