Oct 262012
 
Article Java

In our previous post introducing the Java Language we have already mentioned that variables in this language must be declared before being referenced in the code. Besides, a variable can only hold values of a given data type, specified in the variable declaration. The sample code below exemplifies how variables are declared and initialized, and the primitive data types available in Java:

  • A boolean variable can only have values ‘true’ or ‘false’.
  • A byte variable is signed. It is stored in a byte, and can have values from -128 to 127.
  • A short variable is stored in two bytes, and int variable in four bytes, and a long variable in eight bytes. All of these numeric variables are signed.
  •  A long numeric literal is written with a trailing ‘l’ character.
  • In Java there is no “unsigned” modifier to make a numeric variable unsigned. This is not a generally a big issue. We can always use an int to store unsigned bytes with values from 0 to 255, a long to store unsigned int values, etc.
  • Underscores can be written anywhere between two digits, to make the number easily readable.
  • There are also “float” and “double” primitive data types.
  •  A float literal is written with a trailing “f” character.
  • A char variable value is a single unicode character, and is stored in two bytes.
  • A String variable stores a variable length sequence of unicode characters.

Assigning variables to other variables of a different data type

The java compiler allows assignment of the value of a numeric variable to another numeric variable of higher precision: a byte into an integer, and integer into a long… However, trying to assign to a variable of lower precision generates a compile time error.

If we add to our sample code the sentences:

then compiler complains:

We can avoid these errors by explicitly casting into the destination data types, using the following syntax:

Indeed, if the value assigned is greater than the maximum value the destination variable can hold, it is truncated. For instance, assigning the value 416 (0x01a0 hex) to a byte results in the value -96 (0xa0) being actually assigned.

Arrays

Java implements arrays of one or several dimensions. The syntax used to define and initialize an array is shown in the following sample code:

Notes:

  • The elemens in an array must be all of the same data type.
  • The elements in an array are numbered starting with number zero.
  • The array dimensions are specified in the variable declaration, and cannot be dinamically changed.
  • Arrays can hold objects of any kind. In the sample above, “Point” is assumed to be a java class defined elsewhere in the code. The elements of the “line” array will be instances of the Point class.

Vectors and Hashtables

Other very useful data structures in Java are Vectors (similar to arrays of variable length) and Hashtables (associative arrays, sets of (key,value) pairs) that will be treated in other posts in this series.

 Posted by at 12:13 pm

 Leave a Reply

(required)

(required)