Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

Java Notes

Uploaded by

samboxer985
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Java Notes

Uploaded by

samboxer985
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Wrapper Class – It provide the mechanism to convert primitive data type into object and object

into primitive . The automatic conversion of primitive in to an object is known as autoboxing and
vice-versa unboxing.

Use of Wrapper Class in Java –


1. Change the value in Method
2. Serialization
3. Synchronization
4. Collection Framework

Primitive Type Wrapper class

boolean
Boolean

char Character

byte Byte

short Short

int Integer

long Long

float Float

double Double

Program –
public class Wrapp

public static void main(String args[])

int marks = 50;

Integer m1 = new Integer(marks);

System.out.println("Integer object m1: " + m1); //autoboxing

int iv=m1.intValue(); //unbpxing

System.out.println("int value, iv: " + iv);

}
Access Modifier in Java – The access modifiers in Java specifies the accessibility or scope of a
field, method, constructor or class. We can change the access level of fields, constructors, methods
and class by applying the access modifier on it.

1. Private – The access level of private modifier is only within the class. It can not be
accessed from outside the class.
2. Default – The access Level of default modifier is only within the package.
3. Protected – The access level of protected modifier is within the package and outside the
package through child class.
4. Public – The access level of a public modifier is everywhere,. It can be accessed from within
the class, outside the class.

Private --
1. class A{
2. private int data=40;
3. private void msg(){System.out.println("Hello java");}
4. }
5.
6. public class Simple{
7. public static void main(String args[]){
8. A obj=new A();
9. System.out.println(obj.data);//Compile Time Error
10. obj.msg();//Compile Time Error
11. }
12. }

Protected –
1. //save by A.java
2. package pack;
3. public class A{
4. protected void msg(){System.out.println("Hello");}
5. }
1. //save by B.java
2. package mypack;
3. import pack.*;
4.
5. class B extends A{
6. public static void main(String args[]){
7. B obj = new B();
8. obj.msg();
9. }
10. }

Exception Handling – It is one of the powerful mechanism to handle the runtime errors so that the normal flow
of the application can be maintained. Exception is an abnormal condition which disrupts the normal flow of the program.
It is an object which is thrown at runtime.

Try Block – The “try” keyword is used to specify a block where we should place an exception code. It mean we
can’t use try block alone. The try block must be followed by either catch or finally.

Catch Block – The “catch” block is used to handle the exception. It must be preceded by try block which means
we can’t use the catch block alone. It can be followed by finally block later.
Finally – The “finally” block is used to execute the necessary code of the program. It is executed whether an
exception is handled or not.

Throw – The “throw” keyword is used to throw an exception.


Throws – The “throws” keyword is used to declare exceptions. It specifies that there may occur an exception in the
method. It doesn’t throw an exception. It is always used with method signature.

Program – divide by zero


class Exhand
{
public static void main(String num[])
{
int a=6,b=0,result=0;
try
{
result=a/b;
}
catch(ArithmeticException r)
{
System.out.println(" Exception handing is created by dividing zero");
}
System.out.println(" Hello how r u ");
}
}

Program – Multi catch blocks


class Exmulcatch
{
public static void main(String num[])
{
int a=5,b=0,result=0;
int arr[]={2,3,5};
try
{
System.out.println(arr[7]);
result=a/b;

}
catch(ArithmeticException e)
{
System.out.println("Arthimetic Excaption error created");
}
catch(ArrayIndexOutOfBoundsException o)
{
System.out.println("Array Error Created");
}
System.out.println(" Out of Catch Block ");
System.out.println(result);
}
}

Program – throw keyword to make an exception manually


class Throww
{
static void fn() // body of main fn
{
throw new ArithmeticException();
}
public static void main(String num[])
{
fn(); // fn declaration
}
}

Program – Throws keyword to solve a used defined exception


class Throwww
{
static void fn() throws ArithmeticException
{
throw new ArithmeticException();
}
public static void main(String num[])
{
try
{
fn();
}
catch(ArithmeticException e)
{
System.out.println(" Error "+e);
}
System.out.println(" Hello ");
}
}

Java Threads - It s a fundamental concept in Java programming, allowing developers to execute multiple tasks
concurrently within a single program. Threads are lightweight processes that run within the context of a larger process,
enabling efficient utilization of system resources and enhancing application responsiveness.
Java, threads are represented by instances of the Thread class or by implementing the Runnable interface. The Thread class
provides built-in support for multithreading, while the Runnable interface defines a single method, run() that contains the code
to be executed by the thread. By implementing the Runnable interface, we can decouple the task from the thread itself,
promoting better code organization and reusability.

Program –
class a extends Thread
{
public void run()
{
int i;
for(i=1;i<=5;i++)
{
System.out.println(" \t from thread A : i = "+i);
}
System.out.println(" exit from A ");
}
}
class b extends Thread
{
public void run()
{
int j;
for(j=1;j<=5;j++)
{
System.out.println(" \t from thread B : j = "+j);
}
System.out.println(" exit from B ");
}
}
class c extends Thread
{
public void run()
{
int k;
for(k=1;k<=5;k++)
{
System.out.println(" \t from thread C : k = "+k);
}
System.out.println(" exit from C ");
}
}
class thread
{
public static void main(String args[])
{
a obj=new a();
b obj1=new b();
c obj2=new c();
obj2.setPriority(obj2.MAX_PRIORITY);
obj1.setPriority(obj1.getPriority()+1);
obj.setPriority(Thread.MIN_PRIORITY);
System.out.println(" Start thread A");
obj.start();
System.out.println(" Start thread B");
obj1.start();
System.out.println(" Start thread C");
obj2.start();
}
}

You might also like