Wednesday, July 24, 2013

Variables and Types

As I mentioned earlier, each variable that you declare can store values only of a type consistent with the data type of that variable. You specify the type of a particular variable by using a type name in the variable declaration. For instance, here’s a statement that declares a variable that can store integers:

int numberOfCats;

The data type in this case is int and the variable name is numberOfCats. The semicolon marks the end of the statement. The variable, numberOfCats, can only store values of type int. Of course, int is a keyword. Many of your variables will be used to reference objects, but let’s leave those on one side for the moment,as they have some special properties. The only things in Java that are not objects are variables that correspond to one of eight basic data types, defined within the language. These fundamental types are referred to as primitive types, and they allow you to define variables for storing data that fall into one of three categories:

[1] Numeric values, which can be either integer or floating point
[2] Variables that store the code for a single Unicode character
[3] Logical variables that can assume the values true or false

All of the type names for the basic variable types are keywords in Java so you must not use them for other purposes. Let’s take a closer look at each of the primitive data types and get a feel for how you can use them.

No comments:

Post a Comment

Thanks for Commenting......