What is the output?
public class SaravananK {
static int x=4;
public SaravananK() {
System.out.print(this.x);
}
public static void main(String[] args) {
System.out.println(new SaravananK().x);
}
}
Java programs for beginners, list of programs in java on various topics, interesting programs in java. Java programs on various topics
Sunday, December 16, 2012
Java program using Constructor
Java program using Dot Dot Dot (String ... args)
public class DotDotDot {
public static void main(String... args) {
System.out.println("this will work? or not");
}
}
public static void main(String... args) {
System.out.println("this will work? or not");
}
}
Java program using While loop
What is the out put?
public class ThisNumber {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int the_number=4;
int that_number;
while(the_number<10)
{
that_number=the_number;
the_number=the_number+that_number/2;
}
System.out.println(the_number);
}
}
public class ThisNumber {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int the_number=4;
int that_number;
while(the_number<10)
{
that_number=the_number;
the_number=the_number+that_number/2;
}
System.out.println(the_number);
}
}
An Java program using inheritance
An Java program using inheritance :
class BaseA
{
public BaseA(){
System.out.println("Base A constructor");
}
public void methodOne(){
System.out.println("Method one in base A");
}
}
class BaseB extends BaseA{
public BaseB(){
super();
System.out.println("Base b constructor");
}
public void methodOne(){
BaseA ba=new BaseA();
ba.methodOne();
System.out.println("Method one in base B");
}
}
public class Base{
public static void main(String args[])
{
BaseA ba=new BaseB();
BaseB bb=new BaseB();
ba.methodOne();
bb.methodOne();
Base base=new Base();
}
public static void main(String args[][])
{
BaseA ba=new BaseA();
}
}
class BaseA
{
public BaseA(){
System.out.println("Base A constructor");
}
public void methodOne(){
System.out.println("Method one in base A");
}
}
class BaseB extends BaseA{
public BaseB(){
super();
System.out.println("Base b constructor");
}
public void methodOne(){
BaseA ba=new BaseA();
ba.methodOne();
System.out.println("Method one in base B");
}
}
public class Base{
public static void main(String args[])
{
BaseA ba=new BaseB();
BaseB bb=new BaseB();
ba.methodOne();
bb.methodOne();
Base base=new Base();
}
public static void main(String args[][])
{
BaseA ba=new BaseA();
}
}
Saturday, December 15, 2012
Java Inner Class
What is out put?
// A program using in inner class and method over loading
class Outer{
protected int a=6;
public void methodOne(){
a=a++;
System.out.println("In methodOne outer class a "+a);
}
class Inner{
public void methodOne(){
int a=6;
System.out.println("In method one value of a "+a);
}
public void methodTwo(){
a=a--;
System.out.println("In method two value of a "+a);
}
}
public void methodTwo(){
System.out.println("In outerclass method Two value of a "+a);
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Outer out=new Outer();
Outer.Inner in=out.new Inner();
out.methodOne();
in.methodOne();
in.methodTwo();
out.methodTwo();
}
}
// A program using in inner class and method over loading
class Outer{
protected int a=6;
public void methodOne(){
a=a++;
System.out.println("In methodOne outer class a "+a);
}
class Inner{
public void methodOne(){
int a=6;
System.out.println("In method one value of a "+a);
}
public void methodTwo(){
a=a--;
System.out.println("In method two value of a "+a);
}
}
public void methodTwo(){
System.out.println("In outerclass method Two value of a "+a);
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Outer out=new Outer();
Outer.Inner in=out.new Inner();
out.methodOne();
in.methodOne();
in.methodTwo();
out.methodTwo();
}
}
Friday, December 14, 2012
Threads Question And Answers
What is thread?
It is the unit of execution which doesn't have any existence.
What is Synchronization?
It is the controlling mechanism when multiple threads are trying to access same resource.
How can be a thread Created?
By extending thread class and implementing runnable interface.
What is the use of sleep method?
Sleep method is used to stop the execution of a thread temporarily for a user defined milliseconds.
What is Preemptive Scheduling?
When an lower priority thread is already running that thread will be suspended and higher priority thread will get executed.
What is Time slicing?
It is the process where the time is sliced between equal priority threads.
What are the methods available in threads?
Thread t=new Thread();
t.getName()-To get the name of thread.
t.setName()-To set the name for thread.
t.getPriority()- To get the priority
t.setPriority()-To set the priority
constant properties of thread
MAX_PRIORITY=10
MIN_PRIORITY=1
NORM_PRIORITY=5
What are the states of an thread?
newborn()->runnable->running>dead.
What is Daemon Thread?
It is a thread which will be invoked by automatically without intervention of user. Garbage collector is example for Daemon Thread.
What is run Method?
Whether we use implement runnable or extending thread the public void run() method should be override. And using start() method we start running a thread.
It is the unit of execution which doesn't have any existence.
What is Synchronization?
It is the controlling mechanism when multiple threads are trying to access same resource.
How can be a thread Created?
By extending thread class and implementing runnable interface.
What is the use of sleep method?
Sleep method is used to stop the execution of a thread temporarily for a user defined milliseconds.
What is Preemptive Scheduling?
When an lower priority thread is already running that thread will be suspended and higher priority thread will get executed.
What is Time slicing?
It is the process where the time is sliced between equal priority threads.
What are the methods available in threads?
Thread t=new Thread();
t.getName()-To get the name of thread.
t.setName()-To set the name for thread.
t.getPriority()- To get the priority
t.setPriority()-To set the priority
constant properties of thread
MAX_PRIORITY=10
MIN_PRIORITY=1
NORM_PRIORITY=5
What are the states of an thread?
newborn()->runnable->running>dead.
What is Daemon Thread?
It is a thread which will be invoked by automatically without intervention of user. Garbage collector is example for Daemon Thread.
What is run Method?
Whether we use implement runnable or extending thread the public void run() method should be override. And using start() method we start running a thread.
Saturday, December 8, 2012
Java Find maximum value in an array
import java.util.Scanner;
public class ArrayMaximum {
public static void main(String args[])
{
System.out.println("Enter the limit for array :");
Scanner in=new Scanner(System.in);
int limit=in.nextInt();
int array[]=new int[limit];
System.out.println("enter the values for array :");
for(int i=0;i<array.length;i++)
{
array[i]=in.nextInt();
}
System.out.println("Values of array");
for(int m:array)
{
System.out.print(" "+m);
}
System.out.println();
int max=array[0];
for(int j=1;j<array.length;j++){
if((array[j])>max)
max=array[j];
else
max=array[0];
}
System.out.println("biggest Number in this array "+ max);
}
}
OUTPUT
Enter the limit for array :
5
enter the values for array :
4
5
6
7
8
Values of array
4 5 6 7 8
biggest Number in this array 8
public class ArrayMaximum {
public static void main(String args[])
{
System.out.println("Enter the limit for array :");
Scanner in=new Scanner(System.in);
int limit=in.nextInt();
int array[]=new int[limit];
System.out.println("enter the values for array :");
for(int i=0;i<array.length;i++)
{
array[i]=in.nextInt();
}
System.out.println("Values of array");
for(int m:array)
{
System.out.print(" "+m);
}
System.out.println();
int max=array[0];
for(int j=1;j<array.length;j++){
if((array[j])>max)
max=array[j];
else
max=array[0];
}
System.out.println("biggest Number in this array "+ max);
}
}
OUTPUT
Enter the limit for array :
5
enter the values for array :
4
5
6
7
8
Values of array
4 5 6 7 8
biggest Number in this array 8
Subscribe to:
Posts (Atom)