|
|
|
|
|
|
|
|
|
|
||||||||||
|
Java
supports most common data types in conventional programming languages like
integer, character, floating point, boolean, etc. However, Java specifies
everything and nothing is left to the compiler. For example, in C and C++,
the size of int is machine dependent which is never the case in Java. In
addition, casting between arbitrary variables are not allowed. You can
only cast between numeric values and between super and subclasses of the
same object. All numeric values are signed in Java.
boolean, byte, short, int, long, float, double, char are allowed types and they have 1-bit, 1 byte, 2 bytes, 4 bytes, 8 bytes (two's complement), 4 bytes (IEEE 754), 8 bytes (IEEE 754) and 2 bytes. Only char and boolean types are unsigned in Java. char is not the same as byte, int, short or Strings. There is no restriction on the length of variable names. However, you'd better to use understandable and meaningful names for variables. Although Java handles the conversion between numeric values, it is better to explicitly warn the compiler to make casting. In my opinion, you should always explicitly define the type of result. Although Java does its own truncation and/or rounding, this may yield in the loss of precision in one of your trials. Especially when the results are too big or too small enough to store in the resulting variable, Java will possibly do it wrong. It is possible to convert strings to integers or vice versa easily in Java. toString() and valueOf() methods performs this job.
double d = Double.valueOf("3.14").doubleValue(); // assigns the number 3.14 to double d Since array is an indexed collection of similar type of data, we should first specify its data type. Global array declaration is as follows:
For multidimensional arrays, we use:
|
|
|
|
|
|
|