Showing posts with label Java Beginners. Show all posts
Showing posts with label Java Beginners. Show all posts

Thursday, 15 March 2012

Array Concept in java

Array Definition:
        Array is an variable that holds multiple values of same data type.

How to get array length in java? 
array length Definition: 
arr.length give the length of the array where arr is the array name.
Syntax:
arrayname.length;


Array simple Program: 
package array_pack;
class arr
{
      private int arr[] = new int[2];
      public void array_init()
      {
      arr[0] = 1;
      arr[1] = 2;
      }
      public void array_print()
      {
      for(int i = 0; i<arr.length; i++)
      System.out.println(arr[i]);
      }
}
public class arr_process {
      public static void main (String args[])
      {
            arr a = new arr();
            a.array_init();
            a.array_print();
      }
}

Output: 
1
2

Explanation: 
           First the program creates the instance for the class arr..Now with the help of the instance we are calling the array_init() function and array_print() function.

Note :
arr.length give the length of the array where arr is the array name.

Tags:
Array Declaration,Array,Array in java,A simple array program,array definition,array concept in java,programs,simple programs,java beginners,array length,

Static Concepts in java

Static Definition:
        In classes, a static method is one that is called without an instance of that class(object), while a non-static method is called by instances of the class(object).  

Static Program: 
package static_pack;
public class stat {
      static int i = 12;
      int j = 20;
      static
      {
            subclass sub = new subclass();
            System.out.println("The Static value is: " +i);
            //System.out.println("The Static value is: " +j);
           
            sub.display();
      }
      public static void main(String[] args)
      {
            System.out.println("Oops Main class after static block...");
            subclass.print();
      }
}
create a subclass class in the same package:

package static_pack;
public class subclass
{
      static void print()
      {
            System.out.println("In Subclass static method");
      }
      public void display()
      {
            System.out.println("Static yaar!!");
      }
}

Output:-
The Static value is: 12
Static yaar!!
Oops Main class after static block...
In Subclass static method

Explanation :
                 From the above program u can understand that Static block gets executed before the public static void main(String args[]). The static block gets executed and then the main block gets executed

Tags:
        Static program in java, Static program with Example, Static concept java, simple Static program,interview static,static program,use of static block,static block,Static function,Static Declaration

Friday, 9 March 2012

A Simple Hello World Program in Java

To run a simple hello world program in java First we need to write the program in the notepad
as give below:

Step1:
Class Helloworld{
   public static void main(String args[])
   {
System.out.println("Hello World..!");
   }
}

Step2:
Save it with the file name Helloworld.java[The File name must be the name of the class].

Step 3:
Compile the program in command prompt(cmd)javac HelloWorld.java is used to compile the source code
Note:
When u compile the program it will create a byte-code file name Helloworld.class

Step 4:
Run the program in comand Prompt(cmd)
java HelloWorld
Note:
Now the byte code is excected in java interpreter with the command given above

Output of the Program :
Hello World..!

Tags:
          simple java Program,java,class,bycode,interpreter,source code,.java