Data Types:
1.primitive Data types
Datatype size Description
Example
byte 1byte smallest integer type byte
a=10;
short 2bytes small integer type short
b=1000;
int 4bytes common integer type int
x=50000;
long 8bytes
float 4bytes
double 8bytes
char 2bytes
Boolean 1bit
Boolean flag=True;
2.non-primitive:
classes
interfaces
array
string
object syntax:
class name object name=new class name
Types of contructor:1.Default con--->Student()
2.Parameterized constructor--->Student(String n,...)
contructor name and class name must be same.
contructor: in java is a special method that is used to initialize objects when
they are created
-->It has the same name as the class,and it does not have a return type
-->Access modifiers
They are 4 types:
1.public:that can be used anywhere
2.private:it cannot be used anywhere particular to that code(we use seters and
getters)
3.protected:same package but only in subclasses
4.default:same package
Abstraction in java:
Absrtaction in java is process of hiding the internal implementation details and
showing only the essential features of an object
.It helps to reduce complexity and focus only on what an object does,not how it
does it.
ex:ATM,online application.
program:
abstract class Animal{ //abstract class
abstract void sound(); //Abstract method(no body)
void sleep(){ //regular method
System.out.println("sleeping...");
}
}
class Dog extends Animal{ // subclass provides implementation
void sound(){
System.out.println("Dog barks");
}
}
public class Main{
public static void main(String args[]){
Dog d = new Dog();
d.sound(); // o/p of dog barks
d.sleep(); // o/p of sleeping
}
}
Polymorphism:
polymorphism in java means many forms it is the ability of an object to take
multiple forms
Two types:1.Run time polymorphism---> method over loading-->same method different
parameters.
2.Compile time polymorphsim-->method over ridding-->same method same parameters
Type Conversion:
Type conversion in java(and most programming languages) refer to changing the data
type of value from one type to another.
two types:
1.Implicit conversion :(automatic)widening conversion-->smaller to bigger.Happens
when smaller data type is assigned to a bigger data type.
byte b0=127b;//wide casting;assiging lower value to higher value
short s0=b0;
int i0=b0;
long 10=b0;
float f0=b0;
double d0=b0;
ex:int =4bytes double=8
int a=10;
double b=a;
2.Explicit conversion: Narrowing conversion or type casting.Happens when bigger
data type is assigned to smaller data type.-->bigger to smaller.
syntax:
Data type variable=data type value
ex:converting big to small
int a=10;
byte b=(byte)a;
sop(b);
String to primitive conversion(parsing)
from-->To method to use example
int->String String.valueOf(i)
Exception:
An exception is a problem that happens during the program execution(run time).when
somthing unexcepted happens,java creates an exception object and throws is to
signal
java provides 5 main keywords for exception
keyword
try--->used to monitor code that might throw an exception
catch--->Handles the exception thrown in try block
finally--->Block that always exceutes-whether exception occurs or not
throw--->used to manuaaly throw an exception
throws-->used to declare that a method may throw an exception.
Error type
outofmemoryError----->JVM ran out of memory(eg:too many objects created)
Stackoverflowerror--->Infinite recursion or deep method calls
virtualMachineError---->issues releeated to the JVM itself
NoclassdefoundError_---->class definition not found at runtime
AssertionError**--->**when an assertion fails
Try:
The try block is used to wrap code that might cause an exception.It monitors the
code and passes the error to the corresponding catch block ifb any exception occurs
__________________
Syntax:
try{
int a=10/0;
arithmeticException
}
_____________________
Catch:
The catch block is used to handle the exception thrown from t etry block.you can
have multiple catch blocks for different exceoptions.
syntax:
try{
int a=10/0;
}
catch(arithmeticException e){
System.out.println("Cannot didvide by zero,");
__________________
finally:
The Finally block always exception is thrown or not.
syntax:
try{
//risky mode
}
catch (excpection e ){
//handing code
}finally{
}//clean code
_________________
Example
throw:
The throw keyword is used to manually throw exception.you can throw either built in
or custom exception
you can throw either built in or custom exception.
Syntax:
throw new ExceptionType("error message");
ex:
public class voting{
public static void checkage(int age){
if(age<18){=-------;;;'''''.,,,
15:14 06-08-2025
List interface
Set interface
Map interface
Hashmap
Treemap--->Maintaining data in sorted order.
--->NavigableMap use cases
--->implementing leader board system
Lambda expression
Streams-->data transform
Collection in java/collection
The java collection frame works provides a set of interface and classes to
implement various data structures and algoritghms.