JAVA DATATYPES  

Posted by ROCKING GUY in

 Data Types
 One of the fundamental concepts of any programming language is that of data types. Data types define the storage methods available for representing information, along with how the information is interpreted. Data types are linked tightly to the storage of variables in memory because the data type of a variable determines how the compiler interprets the contents of the memory. You already have received a little taste of data types in the discussion of literal types.
 To create a variable in memory, you must declare it by providing the type of the variable as well as an identifier that uniquely identifies the variable. The syntax of the Java declaration statement for variables follows:
 Type Identifier [, Identifier];

 The declaration statement tells the compiler to set aside memory for a variable of type Type with the name Identifier. The optional bracketed Identifier indicates that you can make multiple declarations of the same type by separating them with commas. Finally, as in all Java statements, the declaration statement ends with a semicolon.
 Java data types can be divided into two categories: simple and composite. Simple data types are core types that are not derived from any other types. Integer, floating-point, Boolean, and character types are all simple types. Composite types, on the other hand, are based on simple types, and include strings, arrays, and both classes and interfaces in general. You’ll learn about arrays later in this chapter. Classes and interfaces are covered in Chapter 14, “Classes, Packages, and Interfaces.”
 Integer Data Types
 Integer data types are used to represent signed integer numbers. There are four integer types: byte, short, int, and long. Each of these types takes up a different amount of space in memory, as shown in Table 12.4.

 Table 12.4. Java integer types.

 *Type                                       *Size
 Byte                                              8 bits
 Short                                            16 bits 
 Int                                                 32 bits
 Long                                            64 bits
                                           
                                                     
                                                          
 To declare variables using the integer types, use the declaration syntax mentioned previously with the desired type. The following are some examples of declaring integer variables:

 int i;
short rocketFuel;
long angle, magnitude;
byte red, green, blue;

 Floating-Point Data Types
 Floating-point data types are used to represent numbers with fractional parts. There are two floating-point types: float and double. The float type reserves storage for a 32-bit single-precision number and the double type reserves storage for a 64-bit double-precision number.
 Declaring floating-point variables is very similar to declaring integer variables. The following are some examples of floating-point variable declarations:
 float temperature;
double windSpeed, barometricPressure;

 boolean Data Type
 The boolean data type is used to store values with one of two states: true or false. You can think of the boolean type as a 1-bit integer value, because 1 bit can have only two possible values: 1 or 0. However, instead of using 1 and 0, you use the Java keywords true and false. true and false aren’t just conveniences in Java; they are actually the only legal Boolean values. This means that you can’t interchangeably use Booleans and integers like in C/C++. To declare a Boolean value, just use the boolean type declaration:
 boolean gameOver;

 Character Data Type
 The character data type is used to store single Unicode characters. Because the Unicode character set is composed of 16-bit values, the char data type is stored as a 16-bit unsigned integer. You create variables of type char as follows:
 char firstInitial, lastInitial;

 Remember that the char type is useful only for storing single characters. If you come from a C/C++ background, you might be tempted to try to fashion a string by creating an array of chars. In Java this isn’t necessary because the String class takes care of handling strings. This doesn’t mean that you should never create arrays of characters, it just means that you shouldn’t use a character array when you really want a string. C and C++ do not distinguish between character arrays and strings, but Java does.

This entry was posted on Saturday, December 20, 2008 at 5:38 AM and is filed under . You can follow any responses to this entry through the comments feed .

0 comments

Post a Comment