Monday, 31 March 2014

Integers Data types Simplified.....

TARGET LANGUAGE:JAVA
THEORY:(DATA TYPE:INT)


  • Data type is one of java's most fundamental  three elements(data types, variables, arrays).
  • As like all programming languages java supports several types of data.
JAVA is a SRONGLY TYPED LANGUAGE
  • First, every variable has a type, every expression has a type, every type is strictly defined.
  • Second, all assignments whether explicit or via parameter passing in method calls, are checked for type compatibility.
  • Any type mismatch or error must be corrected before compilation.   
Primitive Types

 Data types are primarily used in computer programming, in which variables are created to store data. Each variable is assigned a data type that determines what type of data the variable may contain. Java defines eight types of data: byte, short, int, long, char, float, double, and Boolean.
We are going to see only integer data type.
    • Integer-
      • It includes byte, short, int, and long, which are whole valued signed numbers.
      • All of them are signed(positive and negative values).
      • Java does not support unsigned integers
The width and ranges if these integer types vary widely, as shown in the table.
      
     
    
NameSizeRange
byte8 bit-27 to 2 7-1
short16 bit-215 to 215-1
int32 bit-231 to 231-1
long64 bit-2 63 to 2 63-1
      
    table ref:http://www.jchq.net/certkey/0406certkey.html
-----------------------------------------------------------      
A simple byte program
  1. /*
  2.   Java Byte Example,Byte is a wrapper class provided to wrap byte primitive value. It has a single field of type byte.
  3. */
  4. public class ByteExample {
  5.   public static void main(String[] args) {
  6.     //create a Byte object using one of the below given constructors
  7.     //1. Create a Byte object from byte
  8.     byte b = 10;
  9.     Byte bObj1 = new Byte(b);
  10.    
  11.     /*
  12.     2. Create Byte object from String. Please note that this method can
  13.     throw NumberFormatException if string doesnt contain parsable number.
  14.     */
  15.     Byte bObj2 = new Byte("4");
  16.    
  17.     //print value of Byte objects
  18.     System.out.println(bObj1);
  19.     System.out.println(bObj2);
  20.   }
  21. }
  22. /*
  23. Output of the program would be
  24. 10
  25. 4
  26. */
------------------------------------------------------------
Byte

  • It is the smallest integer type.
  • It is a signed 8 bit type that has a range from -128 to 127
  • It is useful when you are working with a stream of data from network or file.

Short

  • It is a signed 16 bit type.
  • It has a range from -32,768 to 32,768
  • It is probably the least used Java type.
Declaration:

short s;
short t;

int 

  • It is commonly used integer type.
  • It is a signed 32 bit that has a range of -2,147,483,647 to 2,147,483,647.

long

  • It is a signed 64 bit type for those occasions when int is not large enough to hold the desired value 
  • For example below is a program which calculates the number of miles light will travel in specific number of days.

class Light
{
  public static void main(String args[])
  {
   int lightspeed;
  double days;
  double seconds;
  double distance;
   
   lightspeed=186000;//approximate speed of light
   days=1000;//specify the number of days here
   seconds=days*24*60*60;//convert to seconds
   distance=lightspeed*seconds;
   System.out.println("In "+days);
   System.out.println(" days light will travel about ");
   System.out.println(distance+" miles.");
  }
}
---------------------------------------------------------
The program will generate the following o/p

In 1000 days light will travel about 1607040000000 miles

Clearly,the result could not be held in an int variable
--------------------------------------------------------------
next publication will consist of floating point types ---------------------------------------------------------------- 

Friday, 28 March 2014

My first Blog....

TARGET LANGUAGE:JAVA


PROGRAMME NO 1
 
----------------------------------------------------------------

public class hello
 {
        public static void main(String args[])
      {
          System.out.print("Hello world");
      }
}

//Output:-Hello world
--------------------------------------------------------------------------------------------------------------------
From the above program we can conclude the following features of JAVA

1. java is simple

2. java is secure i.e.
  • It cannot harm other systems
3. It encourages error free programming i.e. it is ROBUST.
-------------------------------------------------------------------------------------------------------------------