Saturday, 20 December 2014

Meaning for public static void main () in java

Meaning for public static void main () in java
public  is a modifier indicates that method can be used by other java clases
static  is a modifier indicates that main() doesn’t require any object to access the method
void -no value is returned

main() -flow control is given to main().Execution order is depend on this instruction order  in the main() function

Tuesday, 16 December 2014

Class Key Words And Identifiers In Java


public class Display{

public is a keyword which means means what we are named can use by other classes
class is a keyword which indicates a class is being named
Display is an identifier gives the name of the class

{   Opening braces delimiter  indicates description of class members 

Wednesday, 8 October 2014

Write a program to reverse a string

import java.util.*;
 class ReverseAString           //class name
{
   public static void main(String args[])  //main function
   {
      String original, reverse = "";    //string declaration
      Scanner in = new Scanner(System.in);                //to get input from- //-user

      System.out.println("Enter a string to reverse"); //Ask //string to reverse from user
original = in.nextLine();                            //getting input from user //using ‘in’ object

      int length = original.length();            //finding length of the //inputted string

      for ( int i = length - 1 ; i >= 0 ; i-- )  //using for loop //getting each character by using charAt(i) method
         reverse = reverse + original.charAt(i);  //reversed string is //stored in reverse string

      System.out.println("Reverse of entered string is: "+reverse);
//reversed sting is outputted to user
   }
}

Java Interview questions

·        Collections in java
·        Interfaces in java
·        Why static is used in public static void main()
Question
·        Difference between jre and jdk
Answer
·        JRE: Java Runtime Environment.
JRE basically the Java Virtual Machine where Java programs run on and  also includes browser plugins for Applet execution.
·        JDK: Java Development Kit

It is a Software Development Kit for Java, including JRE, and compilers and tools (eg. JavaDoc,, Java Debugger) to create and compile programs.

Sunday, 28 September 2014

Final Variable With Example


There is a final variable a, we are going to change the value of this variable, but It can't be changed because final variable once assigned a value can never be changed.

Example Program

class Super
{
                       final int a=100;
                       void show()
                       {
                        //a=200;error , can't be changed because final variable once assigned a value can never be changed.

                        System.out.println("Welcome to the final Keyword                       "+a);
                       }
}

class Demofinal
{
                       public static void main(String args[])
                       {
                        Super obj=new Super();
                        obj.show();
                       }
}


Output

D:\>javac Demofinal

D:\>java Demofinal

Welcome to the final Keyword    100

Wednesday, 24 September 2014

Constructor Overloading with Example

Constructor Overloading

Constructor overloading in java allows to have more than one constructor inside one Class. 

Example Program

class Demo
{
                       Demo()
                       {
                        System.out.println("Constructor without parameter");
                       }
                       Demo(int a)
                       {
                        System.out.println("Constructor with integer parameter "+a);
                       }
                       Demo(float b)
                       {
                        System.out.println("Constructor with float parameter "+b);
                       }
}
public class Democonover {
                       public static void main(String args[])
                       {
                        Demo obj1=new Demo();
                        Demo obj2=new Demo(4);
                        Demo obj3=new Demo(5.9f);
                       }
}


Output

D:\> javac Democonover.java
D:\>java Democonover
Constructor without parameter
Constructor with integer parameter 4
Constructor with float parameter 5.9


Explanation



Tuesday, 23 September 2014

Constructor program with parameter passing

 Constructor program with parameter passing

 Program

class Parmpass
{
          int i;
          Parmpass(int a)
          {
                   i=a;
                   System.out.println("Constructor with parameter "+i);
          }
}
class ParmpassMain
{
          public static void main(String args[])
          {
                   int b=10;
                   Parmpass obj= new Parmpass(b);
          }
}

Output   

D:\>java ParmpassMain
Constructor with parameter 10


Explanation 





Monday, 4 August 2014

Java program to find first 5 largest numbers in a list




import java.util.*;
class big
{
public static void  main(String args[])
{
int i;
System.out.println("Enter the size of the list (It should be greater than five) ");
Scanner sc=new Scanner(System.in);
int n=Integer.parseInt(sc.nextLine());
int a[]=new int[n];
if(n>5)
{
System.out.println("Enter the elements ");
for(i=0;i<n;i++)
{
a[i]=Integer.parseInt(sc.nextLine());
}
System.out.println("Entered elements are ");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}

//sorting the elements
for(i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("Sorted list");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
System.out.println("First 5 largest elements are ");
for(i=n-1;i>=n-5;i--)
{
System.out.println(a[i]);
}

}
else
{
System.out.println("INVALID entry!!!!!");
System.out.println("Enter the size of the list greater than 5!!! ");
}

}

}



output



D:\java\notepad>javac big.java

D:\java\notepad>java big
Enter the size of the list (It should be greater than five)
1
INVALID entry!!!!!
Enter the size of the list greater than 5!!!

D:\java\notepad>javac big.java

D:\java\notepad>java big
Enter the size of the list (It should be greater than five)
6
Enter the elements
11
23
42
7
3
90
Entered elements are
11
23
42
7
3
90
Sorted list
3
7
11
23
42
90
First 5 largest elements are
90
42
23
11
7


D:\java\notepad>

Sunday, 3 August 2014

Java Program to find number of ones between 1 to 100

class ones
{
public void number()
{

int a[]=new int[20];
int j=0,count=0;;
for(int i=2;i<100;i++)
{
if(i%10==1)
count++;
if(i/10==1)
count++;

}
System.out.println("Number of ones between 1 to 100 is  "+count);

}
public static void main(String args[])
{
System.out.println("Welcome");
ones obj=new ones();
obj.number();

}
}