Friday, February 16, 2018

Integers In Java


Java defines four integer types: byte,short,int and long. All of these are signed, positive and negative values. Java does not support unsigned, integers,positive only integers.Many other computer languages support both signed and unsigned integers.The width of integer type should not be thought of as the amount of storage it consumes, but rather as the behavior it defines for variables and expressions of that type.The Java run time environment is free to use whatever size it wants, as long as the types behave as you declared them.

The width and ranges of these integer types are :-

Name Width Range
long 64 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
int 32 –2,147,483,648 to 2,147,483,647
short 16 –32,768 to 32,767
byte 8 –128 to 127

Now let's look at each type of Intger.

byte :-

The Smallest integer type is byte. This is a signed 8-bit type that has a range  from -128 to 127. Varaibles of type byte are especially useful when you are working with a stream of data from a network or file.
Byte varaibles are declared by use of the byte keyword. For example, the following declares two byte varaibles called b, and c.

short:-


short is signed 16-bit type. It has a range from -32,768 to 32,767.It is probably the least-used java type.Here are some example of short varable declaratiions.

short s;
short t;

int:-

The most commonly used integer type is int.It is a signed 32-bit type that has a range from –2,147,483,648 to 2,147,483,647. In addition to other uses, variables of type int are commonly employed to control loops and to index arrays.

long:-


The Long is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value. The range of a long is quit
large.The range of a long is quite large. This makes it useful when big, whole numbers are needed. For example, here is a program that computes the number of miles that light will travel in a specified number of days:

class Light {
  public static void main(String args[]) {
    int lightspeed;
    long days;
    long seconds;
    long distance;
    // approximate speed of light in miles per second
    lightspeed = 186000;
    days = 1000; // specify number of days here
    seconds = days * 24 * 60 * 60; // convert to seconds
    distance = lightspeed * seconds; // compute distance
    System.out.print("In " + days);
    System.out.print(" days light will travel about ");
    System.out.println(distance + " miles.");
  }

This program generates the following output:

    In 1000 days light will travel about 16070400000000 miles.

Clearly, the result could not have been held in an int variable.

Data Types in java
A First Simple HelloWorld Program In Java

No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...