Programming languages make a distinction between procedures (methods), entities that do things, and data, entities that are the inputs to and outputs from procedures and that represent the state of the system as it is running. Data include numbers, words, graphical objects such as windows and buttons, colors, as well specific types of objects created by a particular program, for example, the plants, rocks, and cells in the Smart program.
A variable is a sort of place in the program for holding a piece of data. Each variable has an identifier, which can be any word that does not already have a special significance in Java. For example, you cannot use the word "class" as an identifier for a variable. By convention, variable names in Java are lowercase, except for characters within When you create a variable in Java, you declare it.
Each piece of data and each variable has a type, and in Java when you declare a variable you have to give its type as well as its identifier. Types in Java fall into two categories, primitive types and objects. Primitive types represent things such as numbers and characters, and objects represent more complex or abstract things. As we will see later, you can define types of objects in your program. In fact this is what you're doing when you write the definition for a new class.
The primitive types that we'll use in this course include
boolean,
int,
double,
and
char.
Variables that can have only two values are
boolean.
These are represented by the type boolean, which has
the possible values true and false.
There are various ways to represent integers in Java; one is the type
int.
Possible ints are 1, -65,
0.
There are various ways to represent real numbers in Java; one is the
type double.
Possible doubles are 1.0, 0.3553,
-30001.783.
There are various ways to represent characters in Java; one is the
type char.
Possible chars are 'a', 'A', '0', and ';'.
There are many object types built in to Java.
Among the most important for us will be strings,
whose type is String.
Note that object type identifiers are conventionally capitalized.
A string may consist
of a literal string (a sequence of characters between double quotes)
or an expression consisting of literal strings and other
expressions separated by +s.
For example, each of the following is a representation of a string,
assuming the variables and method calls are legitimate: