Java defines eight simple types:
2)short –16-bit integer type
3)int–32-bit integer type
4)long –64-bit integer type
5)float –32-bit floating-point type
6)double –64-bit floating-point type
7)char –16-bit symbols in a character set
8)boolean–logical values true and false
Java Defines Eight Primitve Data Types |
byte: 8-bit integer type.
Range: -128 to 127.
Example: byte b = -15;
Usage: particularly when working with data streams.
short: 16-bit integer type.
Range: -32768 to 32767.
Example: short c = 1000;
Usage: probably the least used simple type.
int: 32-bit integer type.
Range: -2147483648 to 2147483647.
Example: intb = -50000;
Usage:1) Most common integer type.
2) Typically used to control loops and to index arrays.
3) Expressions involving the byte, short and intvalues are promoted to intbefore calculation.
long: 64-bit integer type.
Range: -9223372036854775808 to 9223372036854775807.
Example: long l = 10000000000000000;
Usage: 1) useful when int type is not large enough to hold the desired value
float: 32-bit floating-point number.
Range: 1.4e-045 to 3.4e+038.
Example: float f = 1.5;
Usage:
1) fractional part is needed
2) large degree of precision is not required
double: 64-bit floating-point number.
Range: 4.9e-324 to 1.8e+308.
Example: double pi = 3.1416;
Usage:
1) accuracy over many iterative calculations
2) manipulation of large-valued numbers
char: 16-bit data type used to store characters.
Range: 0 to 65536.
Example: char c = ‗a‘;
Usage:
1) Represents both ASCII and Unicode character sets; Unicode defines acharacter set with characters found in (almost) all human languages.
2) Not the same as in C/C++ where char is 8-bit and represents ASCII only.
boolean: Two-valued type of logical values.
Range: values true and false.
Example: booleanb = (1<2);
Usage:1) returned by relational operators, such as 1<2
2) required by branching expressions such as if or for
Constants
Java uses variables to store data.
To allocate memory space for a variable JVM requires:
1) to specify the data type of the variable
2) to associate an identifier with the variable
3) optionally, the variable may be assigned an initial valueAll done as part of variable declaration.
- Basic Variable Declaration
- datatype identifier [=value];
- datatype must be
- A simple datatype
- User defined datatype (class type)
- Identifier is a recognizable name confirm to identifier rules
- Value is an optional initial value.