Java Reference
In-Depth Information
Understanding Classes and Data Types
Programming involves working with data. Data is stored in the computer's memory, and the
program creates and manipulates this data. In Java, the type of data you are working with
needs to be specifically declared. For example, if you want to store something simple such
as an integer value, you need to specify exactly how much storage space that integer
needs. If you want to store complex data such as all the information that an employer
needs to know about employees, this data also needs to be specifically defined.
In the case of storing an integer value, you can use one of the eight built-in data types.
In the case of an employee, you would write a class describing the type of data that makes
up an employee. By writing a class to describe an employee, you are creating a new data
type, one that was not built into the Java language.
This employee class would most likely consist of a combination of the built-in data types
and other classes. (These other classes are either ones you wrote or those in the J2SE.) For
example, there is a String class in the J2SE to represent strings. The employee class could
use the String class to store the employee's first and last name. There is a Data class in the
J2SE for representing a calendar date. The Date class could be used to store the hire date
of an employee.
By combining built-in data types and classes (either J2SE classes or user-defined
classes), you create new data types. Your new data types can now be used just like any of
the existing data types.
I want to emphasize this point again: When you write a class in Java, you are creating a
new data type. This concept of creating data types and developing programs based on the
program's data is the basis of object-oriented programming. We will discuss the details of
writing classes in Chapter 4, “Classes and Objects.”
Variables
Variables are used to store data. In Java, a variable needs to be declared.
Declaring a variable involves two steps: giving the variable a name and stating
what type of data is to be stored in the variable.
For example, the following statements are variable declarations:
short x;
int age;
float salary;
Search WWH ::




Custom Search