Java Notes
Java Notes
into primitive . The automatic conversion of primitive in to an object is known as autoboxing and
vice-versa unboxing.
boolean
Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
Program –
public class Wrapp
}
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.
}
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);
}
}
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();
}
}