TARGET LANGUAGE:JAVA
THEORY:(DATA TYPE:INT)
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.
- 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.
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.
| |||||||||||||||||
| |||||||||||||||||
table ref:http://www.jchq.net/certkey/0406certkey.html
| |||||||||||||||||
-----------------------------------------------------------
A simple byte program
Byte
Short
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.
{
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 ----------------------------------------------------------------
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 ----------------------------------------------------------------
No comments:
Post a Comment