Best Blogger Tips

Wednesday, 19 June 2013

Static block in Java

0 comments

Static block

Static blocks in Java are initialize before any objects.They can access only static data types that are declared global or are instance variables. But can declare local variables of non-static within their scope.

Example


public class StaticDemonstration {

 static int a;
 static{
  System.out.println("Hello Static");
  a=10;
 int b = 10;
 System.out.println(a==b ? "Right":"Wrong");
 }
 
 public static void main(String[] args) {
 //No Objects
 }

}

Output

Hello static
Right

Tuesday, 18 June 2013

Easy Use of switch in Java!

0 comments

Switch

Switch is used as an alternative of if else series of statements. It is more efficient and easy to use than if else if ladder.

Syntax

Switch value{body.. }

Test application

I am going to create a test application in Java using switch that will allow the user to enter any integer numbers from 0 to 10 and the program will produce output in alphabets against any number entered.
import java.util.Scanner;


public class SwitchDemo {

 
 public static void main(String arg[]) {
  
  
    
  System.out.println("Enter the Number to convert " +
    "in alphabets");
  Scanner in = new Scanner(System.in);
  int a;
  do{
   a = in.nextInt();
  
  
   switch(a){
   case 0:
    System.out.println("Zero");
    break;
   case 1:
    System.out.println("One");
    break;
   case 2:
    System.out.println("two");
    break;
   case 3:
    System.out.println("Three");
    break;
   case 4:
    System.out.println("Four");
    break;
   case 5:
    System.out.println("Five");
    break;
   case 6:
    System.out.println("Six");
    break;
   case 7:
    System.out.println("Seven");
    break;
   case 8:
    System.out.println("Eight");
    break;
   case 9:
    System.out.println("Nine");
    break;
   case 10:
    System.out.println("Ten");
    break; 
   default:
    System.out.println("Wrong input");
    break;
   }
  }while(a<=10); 
    
   
   
  }
  
 }


Use of right shift and left shift operator in Java!

0 comments

Left Shift "<<"

The left shift "<<" shifts the highest order bit to the right for number of times the user specified.

Syntax

value << position 
The value is increased with the double effect according to the value of the position specified.

Example

int value = 10;
int position = 2;

int result = value << position;
So the final value that will be stored in the result variable will be 40.(10,20,40)

Right shift ">>"

The right shift ">>" shifts the lowest order bit to the left for number of times the user specified.

Syntax

value >> position 
The value is decrease with the double effect according to the value of the position specified.

Example

int value = 10;
int position = 2;

int result = value >> position;
So the final value that will be stored in the result variable will be 2.(10,5,2.5(2))

Saturday, 15 June 2013

Creating a beautiful login web page.

0 comments
Creating a beautiful login web page.

Screen Shot

login page
 

| Bloggers Broadcast © 2010. All Rights Reserved | Back To Top |

Your Text Link Here