Translate

Monday 30 December 2013

Static Initialization Blocks with a simple example

A static initialization block is a normal block of code enclosed 
in braces, { }, and preceded by the static keyword. Here is an example: 

static { 
 // whatever code is needed for initialization goes here
} 

A class can have any number of static initialization blocks, and they can appear anywhere in the class body. 
The run-time system guarantees that static initialization blocks are called in the order that they appear in the source code.
For example,
Consider following code.

class StaticBlockDemo 
{ 
static 
{ 
System.out.println("I am in static block 1"); 
} 
static 
{ 
System.out.println("I am in static block 2"); 
} 
public static void main(String args[]) 
{ 
System.out.println("I am in main() method"); 
} 
}

Output:
 
I am in static block 1
I am in static block 2
I am in main() method

So you can see that static block initialization takes place before main() method. 
Static blocks are executed sequentially. Here we have two static blocks. 
Block 1 executed prior to Block 2. 
If we change order of blocks to something like 

static 
{ 
System.out.println("I am in static block 2"); 
} 

static 
{ 
System.out.println("I am in static block 1"); 
} 

 
then output will be

I am in static block 2
I am in static block 1

 
Use of static block:

Generally, static block is used to perform initialization.

Tuesday 24 December 2013

Write a java code to print goat

class goat
{
public static void main(String abc[])
{
System.out.println();
System.out.println();
System.out.println("               <___>");
System.out.println("               (o-o) ");
System.out.println("   +============\\_/  ");
System.out.println("  / || %%%%%  ||      ");
System.out.println(" /  || %%%%%  ||      ");
System.out.println("*   ||--------||      ");
System.out.println("    \"\"        \"\"      ");
}
}

Note: Just copy paste the above code in notepad file and run it. I hope you will enjoy this. Also, from this code you will learn about the importance of escape characters in a program.

Sunday 29 September 2013

Write a java code to get an ascii value of a given character

Solution:

import java.io.*;
class CharToAscii
{
public static void main(String abc[])
{
System.out.println("Enter a character");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
int c=br.read();
System.out.println("The ascii value of above character is " + c);
}
catch(Exception e)
{
System.out.println("Error...");
}
}}

Output:
C:\> java CharToAscii
Enter a character
A
The ascii value of above character is 65

Write a java code to sort an array

Solution:

class Sort
{
public static void main(String abc[])
{
int temp;
int arr[]={3,7,8,5,2,1,9,6,4};
for(int i=0;i<arr.length;i++)
{
for(int j=i+1;j<arr.length;j++)
{

//Use arr[i]<arr[j] if you want to sort an array in descending order.
//Note: The below condition will sort an array in ascending order.

if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}

}

Write a java code to print square

*********
*           *
*           *
*           *
*    *     *
*           *
*           *
*           *
*********

Solution:-

class square
{
public static void main(String abc[])
{
int size=9;                 //You can change the value of size variable to minimize or maximize square
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(i>=1&&i<=size-2&&j>=1&&j<=size-2)
{
if(i==size/2&&j==size/2)
{
System.out.print("*");
}
else{
System.out.print(" ");
}
}
else
{
System.out.print("*");
}

}
System.out.println();
}
}

}

Thursday 18 July 2013

Write java code to print following pattern

  *
 ***
*****

 ***
  *

Solution:

class printDiamond
{
public static void main(String abc[])
{
int Diamond_Size=19;      

if(Diamond_Size%2==1)
{
printUpperTriangle(Diamond_Size);
System.out.println();
printLowerTriangle(Diamond_Size);
}
else
{
System.out.println("Please set limit to some odd number");
}
}
public static void printUpperTriangle(int limit)
{
int sp=limit/2;
for(int i=1,space=sp;i<=limit;i+=2,space--)
{
if(space>=0)
{
printSpace(space);
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
}
System.out.println();
}
}
public static void printLowerTriangle(int limit)
{
int sp=limit/2;
for(int i=limit-2,space=1;i>0;i-=2,space++)
{
if(space<=sp)
{
printSpace(space);
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
}
System.out.println();
}
}
public static void printSpace(int space)
{
for(int i=space;i>0;i--)
{
System.out.print(" ");
}
}
}
//[Note: Grow Diamond size by increasing value of Diamond_Size variable.
//for example Diamond_Size=29; 
//please set Diamond_Size variable to odd numbers only.]

Wrtite java code to print triangle

*****
  ***
    *

Solution:

class printTriangle
{
public static void main(String abc[])
{
int Triangle_Size=19;    

if(Triangle_Size%2==1)
{
printTriangle(Triangle_Size);
}
else
{
System.out.println("Please set limit to some odd number");
}
}
public static void printTriangle(int limit)
{
int sp=limit/2;
for(int i=limit-2,space=1;i>0;i-=2,space++)
{
if(space<=sp)
{
printSpace(space);
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
}
System.out.println();
}
}
public static void printSpace(int space)
{
for(int i=space;i>0;i--)
{
System.out.print(" ");
}
}
}
//[Note: Grow Triangle_Size by increasing value of Triangle_Size variable.
//for example Triangle_Size=29;

//please set Triangle_Size variable to odd numbers only.]

Write java code to print diamond

  *
 ***
*****
 ***
  *

Solution:- [Read last comment in program to enjoy the code]

class printDiamond
{
public static void main(String abc[])
{
int Diamond_Size=19;      

if(Diamond_Size%2==1)
{
printUpperTriangle(Diamond_Size);
printLowerTriangle(Diamond_Size);
}
else
{
System.out.println("Please set limit to some odd number");
}
}
public static void printUpperTriangle(int limit)
{
int sp=limit/2;
for(int i=1,space=sp;i<=limit;i+=2,space--)
{
if(space>=0)
{
printSpace(space);
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
}
System.out.println();
}
}
public static void printLowerTriangle(int limit)
{
int sp=limit/2;
for(int i=limit-2,space=1;i>0;i-=2,space++)
{
if(space<=sp)
{
printSpace(space);
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
}
System.out.println();
}
}
public static void printSpace(int space)
{
for(int i=space;i>0;i--)
{
System.out.print(" ");
}
}
}
//[Note: Grow Diamond size by increasing value of Diamond_Size variable.
//For example Diamond_Size=29; 
//Please set Diamond_Size variable to odd numbers only.]

Tuesday 16 July 2013

Write a java code to print following pattern

A
AB
ABC
ABCD

Solution:-

class printABCD
{
     public static void main(String abc[])
     {
        for(int i=1;i<5;i++)
        {
            int alphabet=65;                          //ASCII value of A
            for(int j=1;j<=i;j++)
              {
                System.out.print((char)alphabet);
                alphabet++;
              }
          System.out.println();
         }
     }
}

Monday 24 June 2013

Parameterless Constructors

It can be tedious to initialize all of the variables in a class each time an instance is created. Because the requirement for initialization is so common, Java allows objects to initialize themselves when they are created. This automatic initialization is performed through the use of a constructor.

A  constructor initializes an object immediately upon creation. It has the same name as the class in which it resides and is syntactically similar to a method. Once defined, the constructor is automatically called immediately after the object is created, before the new operator completes. Constructors look a little strange because they have no return type, not even void. This is because the implicit return type of a class constructor is the class type itself. It is the constructor's job to initialize the internal state of an object so that the code creating an instance will have a fully initialized, usable object immediately.

Example:

class ConstructorDemo
{
int count;
String strName;

// This is constructor for class
ConstructorDemo()             // Constructor Name=Class name
{
count=10;                            // initializing int type variable
strName="First String";       // initializing string type variable
System.out.println("I am inside the Constructor");    
}
}

class CreateConstructorDemoObject
{
public static void main(String args[])
{
ConstructorDemo obj=new ConstructorDemo();    // This statement calls Constructor
System.out.println("I am outside the Constructor");
}
}

When this program is run, it generates the following results:

I am inside the Constructor
I am outside the Constructor