Sunday, October 2, 2011

How to accept char,string and data types from keyboard

Accepting a single char from keyboard
{
BufferReader br = new BufferReader(new InputStreamReader(Sytem.in));
System.out.println("enter a char");
char ch = (char) br.read();
System.out.println("you have entered a character " + ch);
}


Accepting a string from keyboard
{
BufferReader br = new BufferReader(new InputStreamReader(Sytem.in));
System.out.println("enter a name");
String name = br.readLine();
System.out.println("you have entered a name " + name);
}





Accepting Integer values from keyboard
{
BufferReader br = new BufferReader(new InputStreamReader(Sytem.in));
System.out.println("enter a int");
int n = Integer.parseInt(br.readLine());
System.out.println("you have entered a number " + n);
}


Accepting a float value from keyboard
{
System.out.println("enter a value");
float n = Float.parseFloat(br.readLine());
System.out.println("you have entered a number " + n);
}





What is the difference between float and double datatypes?

float datatype:
Memory size 4 bytes. float is a short form for single precision floating point number. It is a 32-bit precision.
float represents upto 7 digits accurately after decimal point.
         float pi= 3.141F;
double datatype:
Memory size 8 bytes. double is a short form for double precision floating point number.it is a 64 bit precision.
double represents upto 15 digits accurately after decimal point.
         double distance = 1.98e^8;
more about data types click here 

Saturday, October 1, 2011

What do you understand by Synchronization?

Synchronization :

   Two or more threads trying to access the same method at the same point of time leads to synchronization. If that method is declared as Synchronized, only one thread can access it at a time. Another thread can access that method only if the first thread's task is completed.

Example:

Synchronizing a function:

public synchronized void Method () {
// Appropriate method-related code.
}
Example:

Synchronizing a block of code inside a function:

public myFunction (){
synchronized (this) {
// Synchronized code here.
}
}

More about Synchronization click here

What's the difference between constructors and normal methods?

Constructor:
It is automatically invoked when an object is created of a class. It has the same name of its class. Constructor is invoked by using new operator and it has no return type. A constructor can be overloaded but can not be overridden. Default constructor is automatically generated by compiler if class does not have once.

Example: 

Class A
{
     A()
  {
    System.out.println( " this is an example of constructor" );
  }
}
 
Method:

It is just an ordinary member function in a class. Method is invoked by using a dot(.) operator. It has its own name and return type.

Example:

class A
{
    voidDisplay()
  {
      System.out.println(" This is an example of method ");
  }
}

Find more about this question click here