Data type & Usage
Before we using a variable, we should specify what type (datatype) of variable it is.Because, when we specify the datatype, the system can understand the memory requirements and the operations allowed on the corresponding variables.
Suppose we want to store age of a person, then we specify the variable as an integer. Guess we want to store temperature of a city, then we define the it as double.
Different data types in Java
Each primitive type has its significance. Out of this eight primitive data types in Java , we have four datatypes to work with integer numbers, two datatypes to work with real numbers (numbers with fraction part), one datatype for character data, and one datatype for logical (Boolean) data.
- Byte - As it occupies 1 byte of memory, we can store a value between -128 to 127. If we try to store a value bigger than that we will get a compilation error.
e.g. byte b = 25; is valid
but byte b = 200; is invalid
2. Short - It is used to store integers in the range -32768 to 32767. Any value out of this range cannot be kept as short. In that case, we should use int. (In C, short is a data modifier but in Java short is a datatype).
3 .Integer -This is the default integer type. Most of the times we use int type only to work with whole numbers even though they can be managed with byte or short. We can use up to a 10 digit number (up to 200 crores nearly) with int type.
4.Long -When we want to store a value bigger than int range, we should use long type. With long, we can store up to a 19 digit number. (in C, long is a data modifier but in Java long is a data type).
When using a constant bigger than int range, we should suffix it with ‘l’ or ‘L’ to indicate it to be a long value.
Eg: long a=131009; is fine
long b=123456789012345; is not fine
long b=123456789012345L; is fine
5. Char -In Java, char type takes 2 bytes of memory to support UniCode characters ( In C, char type is 1 byte as it supports only ASCII characters). As per UniCode, we have ” 65536 (216) “ characters numbered from 0 to 65535. The char type cannot take negative values.
Numeric type can take both positive and negative values. The first 256 (numbered from 0 to 255) characters of UniCode are the ASCII set of characters only. So UniCode is compatible with ASCII. So both in ASCII and UniCode ‘A’ is 65, ‘B’ is 66, ‘C’ is 67, ‘ a’ is 97, ‘b’ is 98, ‘0’ is 48, ‘1’ is 49, ‘\n’ is 10, ‘\t’ is 9.
Eg: char a=55; is valid
char b=’A’; is valid
char c=-35; is not valid
6. float -
To work with numbers with a fractional part, we can use float type. Even though we have float and double types to work with a fractional (decimal) type of data, double is preferred over the float. Float take 32 bits storage and double takes 64 bit storage. If we are not append F with value it will automatically converted to double
Because accuracy with double is better than float. Allowed range of values is more in double than in float. The float cannot take a decimal value directly.
It should be converted to float before assignment. So, an almost float is not used in regular programming.
float a=3.6; is not valid
float b=3.6f; is valid
float c=(float)3.6; is valid
7.Double -
This is the default datatype to store decimal (real numbers) values. By default, any value with a fractional part is treated as double by the system.
A bigger type (double) value can’t be given to a smaller type (float, int, byte, etc) variable. So the following assignments will result in compilation error.
Eg: float a=5.8;
- int b=5.8;
- short c=5.8;
A smaller type value can be given to bigger type variable. So the following statements are valid.
Eg: double a=3.9;
- double b=834;
- double c=’A’;
8.Boolean
This is the type used to store logical values. In Java, ‘true’ and ‘false’ are reserved words to represent logical (boolean) values.
These are not compatible with other datatypes. So boolean values cannot be assigned to other types and another type of values cannot be assigned to boolean variables.
In java a logical expression results in a logical value (true or false).
Comparison
Float
|
Double
|
|---|---|
| float is represented in 32 bits, with 1 sign bit, 8 bits of exponent, and 23 bits of the significand (or what follows from a scientific-notation number: 2.33728*1012; 33728 is the significand). | •double is represented in 64 bits, with 1 sign bit, 11 bits of exponent, and 52 bits of significand. |
| You should use floats instead of double when memory usage is critical. | If you need more precise computations, use doubles. |
| The type float specifies a single-precision value that uses 32 bits of storage. Single precision is faster on some processors and takes half as much space as double precision, but will become imprecise when the values are either very large or very small. Variables of type float are useful when you need a fractional component, but don't require a large degree of precision. | Double precision, as denoted by the double keyword, uses 64 bits to store a value. Double precision is actually faster than single precision on some modern processors that have been optimized for high-speed mathematical calculations. All transcendental math functions, such as sin( ), cos( ), and sqrt( ), return double values. When you need to maintain accuracy over many iterative calculations, or are manipulating large-valued numbers, double is the best choice. |
| if you are certain that your program requires small numbers and an overflow won't occur with your use of float, then the use of float will largely improve your space complexity as floats require half the memory as required by double. | vice versa |
| In Java, when you type a decimal number as 3.6 , its interpreted as a double . double is a 64-bit precision IEEE 754 floating point, while float is a 32-bit precision IEEE 754 floating point. ... If you want to create a float, you should end your number with f (i.e.: 3.6f ). | You can apply a post fix of d or D after double variable. |
Comparison
|
int
|
long
|
|---|---|---|
Comparison
|
int
|
long
|
| Basic | The datatype int is of 32-bits. | The data type long is of 64-bits. |
| Bytes | If counted in bytes, int is 4 bytes. | If counted in bytes, long is 8 bytes. |
| Range | In Java the range of type int is –2,147,483,648 to 2,147,483,647. | In Java the range of type long is –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. |
| Keyword | Keyword used to declare a variable of int type is "int". | Keyword used to declare a variable of long type is "long". |
| Memory | Memory required to store an int variable is less as compared to long. | Memory required to store a long variable is larger as compared to int. |
Comments
Post a Comment