Core java notes, Exception, reference variables, String,
Core java notes, Exception, reference variables, String,
import java.util.Scanner;
}
}
05-march-2024
Iside class you will have to define
1. Instance Variables
2. Constructor
3. Getters and Setter
a. Instance Variables:- those variables that are declared/define
inside class without using static keyword are known as
instance variable.
Thses variables will be attribute of object and hold states of
object.
Since object is an instance of the class that’s why these
variables are known as instance variables.
Public class product{
// Declaring instance variables
Private int pid;
Private string name;
Private string brand;
Private int price;
}
These commands(command to create instance variable) are
executed each time object from that class will be created by
any program.
These variable will be initialize(assign value first time)
also
// default value will be 0
// Default value java int =0, int series(byte,int, short)
//float or double 0.0
//boolean : False
//char: Null character
// long :0
// float: 0.0
// char: null character (`/u0000`)
//non primitive:- null
//class
# New product();
Command to create from class product
Command to create object from Class:
Or new Classname(parameter);
Constructor
1. It’s a special type of method
2. Name of constructor must be name of class
3. Return type of constructor is not allowed
4. It is used to initialized objects.
02April2024
What is Az:
During Execution of program some unexpected/ unusual / abnormal condition might
be occurred. These unexpected condition/Situation are known as exception these
exception handling by program otherwise execution of program will be
terminated abruptly.
1. Divide by 0 (Arithmetic Exception)
2. File not found(FileNotFoundException).
3. Databases table not Exist(SQLException).
4. Array index out range(ArrayIndexofBoundsException) etc.
If program is not handling exceptions the JVM will terminates executions of that
program.
Exception Classes: These classes are in hierarchical manner every exception class must
be child class/subclass of throwable class either directly or Indirectly
Error class: it has several child classes. Few of them are as follows
1. VirtualmachineError class
2. NosuchMethodError Class
3. StackOverflowError class etc.
Exception of these classes can not be caught
Exception Classes:
it has several child classes. Few of them are as follows
1. ClassNotFoundException class
2. File not found Exception
3. IOException
4. SqlException
5. RuntimeException class etc
Exception of these classes can be caught
03-04-2024
How to handle exception?
Use try and catch block ho handle exception
Try catch:
Inside this block write those commands that might throw exception
Try
{
//commands
}
Catch block:
Just immediately after try block, write catch block. This block will accept exception
object and display exception message
Catch(ArithmeticException)
{
System.out.println(“Customized message”)
System.out.println(ex)
Ex.printStackln();
}
Types of exception:
1. Checked exception
2. Unchecked exception
Checked exception:
Handling these exceptions are mandatory compiler will check existence of handler (try and
catch) in the source code of program. If not found then error will be generated by compiler.
Except Exception class, all child of Exception class are checked exceptions
Public class MyException extends Exception
{
}
Checked exception are also known as compiler time exceptions.
Unchecked exception:
Handling these exceptions are not mandatory compiler will check existence of handler (try and
catch) in the source code of program. If not found then error will be generated by compiler.
Except Exception class, all child of Exception class are unchecked exceptions
Public class MyException extends Exception
{
}
unchecked exception are also known as compiler time exceptions
Finally Block:
Along try and catch block we can write this block also this block is always executed whether
exception is thrown or not and caught or not. So command written inside this block will always
be executed
Try
{
//Command
}
Catch(…….)
{
//command
}
Finally
{
//command
}
Interview Question:
Final,Finally,Finalize
Finalize: Finalize is a method of object class it is called automatically when garbage of an
object is collected
Freeing memory of the object is called garbage collection in java. This is done by JVM .
}
Use throws statement exception created and thrown is checked exception
Type of reference variable:
1. The class which will be instantiated
2. CC obj=new CC();
2)
04-04-24
String handling
===============
What is string?
===============
It is set of characters
For example
"rehanahmad9919@gmail.com"
char type array is required to keep string
Program must create char type array to keep string
You do not need to write code to create this array
This code is already defined in following three classes of library
1)java.lang.String
2)java.lang.StringBuffer
3)java.lang.StringBuilder
Our program will create object from one of the abbove class to keep string.These
classes have several methods.Our program will call these methods to
handle/manage string
Our program can call methods to perform following tasks
1)Compare two string
2)Concatenate
3)Reverse
4)Finding Substring
5)Finding characters from string
Etc.
String str1=new String("Amit");
StringBuffer str2=new StringBuffer("Manoj");
StringBuilder str3=new StringBuilder("Imran");
Note
====
In java reference of the object can not be shown
When you will try to print reference of the object following method of Object class will
be called and value returned by this method will be printed
Note
====
In java reference of the object can not be shown
When you will try to print reference of the object following method of Object class will
be called and value returned by this method will be printed
Equal Method:
It is a method of object class
It Compare reference of two objects and returns a Boolean value string class has
overridden this method. It is compares value of string class object.
Multithreading
It is one of the way to achieve multitasking
Performing more than one task simultaneously is called multitasking we can build
multitasking application by using multithreading A multitasking application creates
and starts multiple threads at a time (per task at a time)
What is thread?
It is a smallest unit of processing
Every thread can vet rated as a program in java every thread is represented in the
form of object.
This object should be an instance of thread class or its child class