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.
|
|
|
Name | Size | Range |
byte | 8 bit | -27 to 2 7-1 |
short | 16 bit | -215 to 215-1 |
int | 32 bit | -231 to 231-1 |
long | 64 bit | -2 63 to 2 63-1 |
|
|
|
|
|
|
table ref:http://www.jchq.net/certkey/0406certkey.html
|
|
|
-----------------------------------------------------------
A simple byte program
/*
Java Byte Example,Byte is a wrapper class provided to wrap byte primitive value. It has a single field of type byte.
*/
public class ByteExample {
public static void main(String[] args) {
//create a Byte object using one of the below given constructors
//1. Create a Byte object from byte
byte b = 10;
Byte bObj1 = new Byte(b);
/*
2. Create Byte object from String. Please note that this method can
throw NumberFormatException if string doesnt contain parsable number.
*/
Byte bObj2 = new Byte("4");
//print value of Byte objects
System.out.println(bObj1);
System.out.println(bObj2);
}
}
/*
Output of the program would be
10
4
*/
------------------------------------------------------------
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
----------------------------------------------------------------