2. Simple Program in JAVA
package oop; //importing the package
public class Test
{
public static void main(String [] args)
{
System.out.println(“basic java program”);
}
}
3. Constructor in Java
Types of Constructor in java
1. Default constructor //no argument
2. User-define constructor //any no. of argument
•It is an special type of method that has same name as the class name itself .
•It does not specify any return type not even void.
•Constructors are invoked at the time object creation .
•It is used to initialize the object .
4. package q8C;
import java.util.Scanner;
public class Digit
{
static int n;
Digit()
{
int fd,ld,sum;
Scanner sc=new Scanner(System.in);
System.out.println("enter a 5 digit number :");
n=sc.nextInt();
fd=n/10000;
ld= n%10;
sum=ld+fd;
System.out.println("the sum is "+ sum);
}
public static void main(String[] args)
{
Digit n = new Digit();// creating object of constructor
}
}
OUTPUT : enter a 5 digit number : 12345
the sum is 6
}
}
5. package cw;
// parameterized constructor
public class C2007
{
C2007(String ch)
{
System.out.println(ch);
}
public static void main(String[] args)
{
C2007 t= new C2007("Am parameterized constructor");
}
}
**BRIEF BREAKDOWN**
//
In this code, we have used the parameterized constructor by passing a specific
argument when initializing the object of C2007 class.
The Argument is passed to the constructor as a String type and will be stored
In the ch variable.
//
6. Methods in JAVA
Types of Method
1. Instance method
2. Static method
• Methods are used to provide logic of the project.
• It also specifies behavior. A method is also called function in C.
• Inside the class, it is possible to declare any no. of methods based on
developer requirement, it will improve reusability of the code, and we
can optimize the code.
7. 1. Instance method
Example: void m1()
{
// this area is called instance area
}
2. Static method
Example: static void m1()
{
// this area is called static area
}
8. package cw;
public class B1907 //instance with parameters
{
public void IT(int i ,String ch)
{
System.out.println(i+ " " + ch);
}
public static void main(String[] args)
{
B1907 b= new B1907();
b.IT(1, “Training in java");
}
}
}
***Breakdown of the code***
In this code, IT is an instance method which is printing the String
we passed by first creating the object of the class and using the object we
called the method IT for performing the desired output.
9. package cw;
public class D1907
{ //static function with parameter
passing
static void IT(int i,String ch)
{
System.out.println(i + " "+ ch);
}
public static void main(String[] args)
{
IT(1,"hello");
}
}
**breakdown of the code**
In this code I showed the use of static function by using the keyword “static”
before the method IT, with the static method, the method can be called without
creating any object of it.
10. Errors and Exceptions in Java
• In Java, errors and exceptions are both conditions that indicate
something has gone wrong, but they differ in several ways.
• Errors are caused by system or environmental issues, such as hardware
failures, while exceptions are caused by issues in the application code.
• Errors are not recoverable and cause the program to terminate
abnormally, while exceptions can be caught and recovered.
• TYPES OF EXCEPTIONS :
1.Check
2.Uncheck
3.Error
11. Check Exceptions
These exceptions occur at compile time :
• ClassNotFoundException
• NoSuchException
• IllegalAccesException
• NoSuchFieldException
• InteruptedException
12. Uncheck Exceptions
These exceptions occur during runtime :
• ArithmeticException
• ArrayIndexOutOfBoundsException
• NullPointerException
• InputMismatchException
• IllegalArgumentException
13. Error
Error occur due to a lack of system resources.
E.g.
•StackOverflow error
•OutOfMemory error
•Assertion error
14. Try – Catch Block in Java
In Java, the try-catch block is used for handling exceptions
(runtime errors).
It allows the programmer to define a block of code to be
tested for exceptions, and if an exception occurs,
it can be caught and handled.
15. package nul;
class Test
{
public static void main(String args[])
{
int a= 10;
int b = 5,c=5;
int x, y;
try
{
x=a/(b-c);
}
catch(ArithmeticException e)
{
System.out.println("division by zero");
}
y= a/b + c;
System.out.println(“y = " + y);
}
}