Core Java
Core Java
Index
1) Variables in java
2) Data Types
3) Operators in java
4) Methods/Functions in java
7) Access Specifiers/Modifiers
8) Control Statements
9) Array
a) Inheritance
b) Polymorphism
c) Encapsulation
d) Abstraction
11) Generalization
13) String
14) Collection
Variables in java
Variable is piece of memory which is used to store the data in the program.
Note:- According to all programming language dealing with information directly is not a good practice to
overcome this variables are introduced.
3) Variable use
Practice
Types of variables
1) Local variable
The variable declared inside of method/code block/constructor is called local variable.
2 Types
We can call global variable directly or with class name, it is also called class variable.
B) Non-Static variable / instance variable
Global variables without static keyword called non-static variables.
To call non-static global variable we have to create object (instance), and with reference to
that object we can call variable.
Data Types
Data type are used to represent the type of data/information used in java program.
2 types
Computer read data in binary, so each character/number/alphabet have one particular binary
number allotted.
Eg. Byte data type has size of 1byte = 1 * 8 = 8 bits
Each bit has value of 0 or 1.
Like this example.
1 0 0 1 1 0 1 0
1. Arithmetic Operator:
Addition +
Subtraction –
Multiplication *
Division /
Mod %
Increment a++
Decrement a - -
Ex. a = 2;
Difference between division and mod.
Method / Functions in java
It is a block of code or group of statements to perform certain task/operation.
Method declaration
Types
1) System defined method/ Pre defined method
A) Static Method
If we use static keyword in method header is called static method.
Static method called directly in any method
Static method does not require Object to call
Memory allocation for static method at the time
of compilation.
Use of Constructor
1. To copy/load non-static members of class into object --> when we create object of class
Types of Constructor
1. Default Constructor
If Constructor is not declared in java class then at the time of compilation compiler will provide
Constructor for the class. The Constructor provided by compiler at the time of compilation is known as
Default Constructor
If programmer has declared the constructor in the class then compiler will not provide default
Constructor.
If programmer is declaring (writing) constructor in java class then it is considered to be as User defined
constructor.
While creating object we are passing “abc” string as parameter in constructor call, this string value will
store in local variable h. Then we are assigning value of h to global variable b.
We can get different value of global variable b, when we pass different variable with creation of
different object
Class
It is collection of object where we can declare data members (Variables), member functions
(Methods), constructor and code block.
Static
Static members of a class are declared or invoke in the memory at the time of class loading.
Scope of the static members are throughout the program execution.
To use the static elements class name is used.
To declare the static elements static keyword is used
Non - Static
Non static elements of the class are declared or invoked in the memory when the object or
instance of the class is created Scope of the static members are throughout the program
execution. i.e., Non static members are declared or invoke at the time of program execution.
Scope of the non-static elements or members is throughout the object.
To use the non-static elements object reference is use.
Object in Java
It is a copy of a class, which is used to load the non-static elements of a class.
It is also called as “ instance of a class ”
Syntax
1) Class1 Airtel
2) Class2 Jio
Access specifier are used to represent the scope of member declared in the class.
Protected: if any member of a class is declared with protected access specifier then scope of the
members within the package only.
**The class which is present outside the package can also access protected member on one
condition which is inheritance operation.
Private: if any member of a class is declared with private access specifier then scope of that member
remains only within the class. i.e., private member cannot be access in other classes.
**If the class is declared with private access specifier then so it cannot be inherited. And it is
not allowed.
2) Default
Scope of this access specifiers is within same package.
Elements having no access specifiers can be used in any class within same package
Keyword -- no keyword
3) Protected
Scope of this access specifiers is same as default.
Elements having default access specifiers can be used in different package with
condition(inheritance)
For non-static protected method, element can be called by using sub class object
Keyword -- “protected”.
4) Private
Scope of this access specifiers is within class.
Elements having private access specifiers can be used only in same class
Keyword -- “private”.
Why main method is static?
To find the main method compiler will not create any object, because of static method
compiler can call main method directly.
Control Statement
The statement is used to control the flow of code, is called control statement.
1) Selection statement
2) Iteration Statement
3) Jump Statement
1) “If” conditional statement
Syntax
Date 21-02-2022
2) Write code for login functionality (Make two string variables with username and password,
take inputs from user for username and password and match with variables.)
3) Switch statement
Switch is based on equality principle.
It is case sensitive
break; keyword is used to stop validation (checking).
default; keyword is used, if any case is not matched it will run default case.
Example
B) Iteration/Loop statements
1) for loop
for loop is used to repeat the same code for particular number of times.
For loop is used when we know number of iteration we have to perform.
Syntax
1) break;
It is keyword.
It can only be used inside loop or any code block.
By using break, execution will throw out of the loop.
2) continue;
It is keyword.
It can only be used inside loop or any code block.
By using continue , it will skip the following code of than iteration.
3) return;
1) It is keyword
2) If we are not using void for method we have to use return keyword at the end of that
method.
3) return gives the return value from that method, of same data type.
Array
Array is the collection of similar data type elements.
Elements of array stored in contiguous memory location. (allocated memory is resides at one
place)
Once array size is fixed it cannot be changed at run time.
In first method we are declaring the array variable and giving the size and then we are adding the
values to the array.
In second method we are declaring the array giving the size and adding the values at a time.
Indexing of array
Ans : -
Ans : -
How to copy array
Ans : -
Ans : -
3) Sort array in increasing order
Ans: -
10) OOP’s Concept (object oriented programming)
Like real world objects we create objects in java and use them efficiently and securely with the help of
OOP’s concepts.
Basically, Java OOP concepts let us create working methods and variables, then re-use all or part of
them without compromising security.
1. Inheritance
2. Polymorphism
3. Encapsulation
4. Abstraction
1) Inheritance
Abstracting or inheriting properties of one class (Super class) to another class (sub class) is called
inheritance.
Class A Class B
Example.
Super class
Sub Class
Types of inheritance
1) Single level inheritance
In this inheritance there are two classes, one super class and one sub class.
Super class
ClassA
Sub class
ClassB
extends
We can call properties of ClassA and ClassB by creating object of ClassB (sub class).
Super class
ClassA
Sub class
extends ClassB
sub class
extends
3) Multiple inheritance
In this inheritance there are more than two classes are involved.
In this one class is acquiring the property of two super classes.
ClassA ClassB
ClassC
We are calling properties of Super class ClassA and ClassB in sub class – ClassC.
Error
1) Object Class is the super most class in java for all Classes that we have created.
2) When there is inheritance between two classes, Object class is not inherit its properties in sub class
In multiple inheritance one sub class has two or more than two super classes, then every super
Properties from Object Class (SUPERMOST class) enters in Super class and from super class to
Sub class.
While entering from super class to sub class it will create confusion, same property is coming
And this confusion is called diamond ambiguity (Looks like diamond shape)
To avoid confusion multiple inheritance is not possible in JAVA.
4) Hierarchical inheritance
In this inheritance there are more than two classes, one super class and more than one sub class.
Super class
ClassA
extends extends
ClassB ClassC
We are calling properties of same super (ClassA) class in to many sub classes (ClassB / ClassC).
Three important concepts
1) Method overloading
2) Method overriding
3) Method Hiding
Before learning these concepts we have to see JVM memory
JVM Memory
In JVM there are four types of memory
1) Stack area
2) Static pool area
3) Heap area
4) Method area
A) Static methods
A) Non-static methods
Method declaration attached with method definition with the help of arguments.
Method overloading can be performed with static methods as well as non-static methods.
In method overloading all methods must be static or all methods must be non-static.
There is no overloading between static and non-static methods declaration is in static pool area
and non-static method declaration is in heap area.
2) Method Overriding
Acquiring the methods from superclass to sub class we change the implementation of the same method
this concept is called method overloading.
Method must have same name and same parameters in super and sub class.
Method overriding can be performed only with non-static methods.
Method declaration attached with method definition with the help of object name.
3) Method Hiding
Declaring a static method in subclass and super class with same name and argument (Parameters) is
called method hiding.
Method hiding can done with static methods when there is inheritance.
Sub class method hides the implementation of super class method.
Main method is the best example of method hiding.
Here if we have two classes with inheritance and we try to override static method in sub class then
two different references created in static pool area with class names. (Test.demo();,
subclass.demo();)
Interview Question
We cannot override main method, it is static method and it will get hide behind subclass main
method.
Good example:
There are two types of polymorphism depending upon binding of method declaration (header) with
definition (implementation/body)
Two types
Static non-static
Examples
Static Non-Static
Binding Compile time Binding Compile time
Initialization Compile time Initialization Run time
Method declaration and method definition is blinded together at run time is called run time
polymorphism.
This polymorphism occurs when object invokes (object invokes at the time of execution means at
the run time).
Method overriding is best example of run time polymorphism.
In method overriding method declaration and method definition is bind together at the time of run
with the help of object reference variable.
Non-static
Binding Run time
Initialization Run time
3) Encapsulation
Hiding the data members from other classes and accessing them with the help of public
methods is called encapsulation.
In java we are encapsuling the private variables in public method and using them.
To achieve encapsulation
In encapsulation we declare fields as private to prevent them to access directly them from other
classes.
Create public methods called Getters and Setters.
It is also called as data hiding.
Advantages
4) Abstraction
It is the process of hiding the implementation from end user and showing them only
functionality.
a. Fan - When we press the fan button we can see fan is running but we don’t know how it is
running (Internal functionality)
b. Break - When we press break of car, car will stop but we don’t know how it is stopping the car
(Internal functionality).
c. Sending an Email – While sending email it asks only sender email id, attachment and body and
sends mail, We don’t know how it sends mail.
Hiding implementation means hiding the code inside method.
Showing only functionality to user means, showing only function or method name.
1) Abstract class
2) Interface
1) Abstract Class
To complete abstract methods from abstract class we create concrete class. In concrete class we
complete all abstract methods (give body to abstract methods).
Abstract Class
Concrete Class
Advantages
2) Interface
Interface can be created with the help of “interface” keyword instead of class.
Interface contains
1) Constants final variables (which are by default public, static and final).
2) Abstract methods incomplete methods (Which are by default public and abstract)
To complete abstract methods from interface we create implementation class. To inherit
interface in implementation class we use “implements” keyword.
Interface 1
Interface 2
Implementation class
We can inherit one interface in to another interface with the help of extends keyword
Important points
Variables are always static so, we have to all them with interface name.
We will create only one method of same name in interface and give implementation, it can be
called from any one the interface.
We will make one public method of same name and inside that method we have to call
default method like interface_Name.super.method_Name();
Generalization
- Extracting all common & important properties & declaring it in super class (i.e. interface) &
providing implementation / definition according to sub class specification is called
generalization.
- Generalization file can be simple java class, abstract class or interface, but always
interface is preferred.
Interface
Implementation Class
1) SBI
2) HDFC
3) Kotak
Normal Class for execution
Every time when we create string it will create new object of string.
String pool area contains two areas 1) Non-constant pool area 2) constant pool area
All the string object created using new keyword is stored in non-constant pool area
All object is allotted with different memory location even though they are same with same
data.
String object created without new keyword first checks is that data is already present in
memory location, if it is present then it will just create reference link to connect with that
data.
If it is different data then it will store at different location.
== Compares memory addresses
All global variables (static and non-static) are stored in heap area only.
All primitive local variables are stored in stack memory only.
Result
Why string is immutable.
So, it will create new object (memory location), it will not modify original object.
Original memory (Velocity) don’t have any reference now so it will go for garbage collection.
Why it is immutable
Here we have two variables (literals) with same data (so they have same memory location)
If we need string not to be immutable then we use StringBuffer and StringBuilder Class.
Example
RuntimeExceptions
Runtime Exception is handled with the help of try-catch block.
Try-catch block
2) There must be a catch block with Exception, Which will occur in try block.
Finally block
3) If there is no exception occur then finally block will run after try block.
4) If we are not writing catch block after try block to handle exception then we have to write
finally block. In this case if any exception occur then before throwing exception finally block will
run.
Result
Question)
- We have to create an exception class by exception name and inherit Exception class.
- We override toString method to get comment after throwing exception.
- To throw exception runtimeException explicitly we use throw keyword.
List Set
1) List Allows Duplicate Value 1) Set don’t allow the duplicate value
2) List Allow any number of null value 2) Set allow only one null value
3) List maintain insertion order 3) Set don’t maintain any insertion order
List
ArrayList
List
1) Array List
First element added and its place is first, Second element added its place is second like wise.
Default capacity is 10. (When we declare arraylist it acquires capacity to store 10 elements).
Incremental capacity is. New capacity = ((current capacity * 3)/2) + 1
Data structure is resizable
It is best size for data retrieval operation.(It goes to direct particular index to get data)
It is worst choice for data manipulation (Add and remove) operation. (Because adding data in
between it changes the memory location of other element).
Linked list
Duplicate value is allowed
Any number of null value is allowed.
Order of insertion in array list is sequential.
First element added and its place is first, Second element added its place is second like wise.
No default capacity.
No incremental capacity.
Data structure is Linear
It is best size for data Manipulation (Add and remove element) operation.(Because if we add any
data it will store at any random location and just link the previous data with new data with the help
of memory address so, no shifting of data to new memory location).
It is worst choice for data retrieval operation. (It has to move from first element to particular
element to retrieve data).
Vector
Duplicate value is allowed
Any number of null value is allowed.
Order of insertion in array list is sequential.
First element added and its place is first, Second element added its place is second like wise.
Default capacity is 10. (When we declare vector it acquires capacity to store 10 elements).
Incremental capacity is. New capacity = ((current capacity *2)
Data structure is doubly
It is best size for data retrieval operation.(It goes to direct particular index to get data)
It is worst choice for data manipulation (Add and remove) operation. (Because adding data in
between it changes the memory location of other element).
1) HashSet
2) LinkedHashSet
1) HashMap
2) HashTable
Result