👉Variables and Data Types in Java Programming
Variables are locations in memory in which values can be
stored. They have a name, a type, and a value. Before you can use a variable,
you have to declare it. After it is declared, you can then assign values to it.
Java
actually has three kinds of variables: instance variables, class variables, and
local variables.
Instance variables, as you learned yesterday, are used to
define attributes or the state for a particular object. Class variables are
similar to instance variables, except their values apply to all that class’s
instances (and to the class itself) rather than having different values for
each object.
Local variables are declared and
used inside method definitions, for example, for index counters in loops, as
temporary variables, or to hold values that you need only inside the method
definition itself. They can also be used inside blocks ({}), which you’ll learn
about later this week. Once the method (or block) finishes executing, the
variable definition and its value cease to exist. Use local variables to store
information needed by a single method and instance variables to store
information needed by multiple methods in the object.
Although all three kinds of variables are declared
in much the same ways, class and instance variables are accessed and assigned
in slightly different ways from local variables. Today, you’ll focus on
variables as used within method definitions; tomorrow, you’ll learn how to deal
with instance and class variables.
👇
Declaring Variables
To use any variable in a Java
program, you must first declare it. Variable declarations consist of a type and
a variable name:
int myAge; String myName; boolean isTired;
public static void main (String args÷])
{ int count; String title; boolean isAsleep;... }
You can string together variable
names with the same type:
int x, y, z; String firstName, LastName;
You can also give each variable
an initial value when you declare it:
int myAge, mySize, numShoes = 28; String myName = “Laura”; boolean isTired = true; int a = 4, b = 5, c = 6;
If there are multiple variables
on the same line with only one initializer (as in the first of the previous
examples), the initial value applies to only the last variable in a
declaration. You can also group individual variables and initializers on the
same line using commas, as with the last example, above.
Local variables must be given values before they can be
used (your Java program will not compile if you try to use an unassigned local
variable). For this reason, it’s a good idea always to give local variables
initial values. Instance and class variable definitions do not have this
restriction (their initial value depends on the type of the variable: null
for instances of classes, 0 for numeric variables, ‘\0’ for characters, and false
for booleans).
Notes on Variable Names
Variable names in Java can start
with a letter, an underscore (_), or a dollar sign ($) . They cannot start with a
number. After the first character, your variable names can include any letter
or number. Symbols, such as %, *, @, and so on, are often reserved for operators in
Java, so be careful when using symbols in variable names.
Caution: The Unicode specification is a two-volume set of lists
of thousands of characters. If you don’t understand Unicode, or don’t think
you have a use for it, it’s safest just to use plain numbers and letters in
your variable names. You’ll learn a little more about Unicode later on.
|
In addition, the Java language uses the Unicode
character set. Unicode is a character set definition that not only offers
characters in the standard ASCII character set, but also several million other
characters for representing most international alphabets. This means that you can use accented characters and other glyphs as legal
characters in variable names, as long as they have a Unicode character number
above 00C0.
Finally,
note that the Java language is case-sensitive, which means that uppercase
letters are different from lowercase letters. This means that the variable X
is different from the variable x, and a rose is not a Rose is not a ROSE.
Keep this in mind as you write your own Java programs and as you read Java code
other people have written.
By convention, Java variables have meaningful names, often
made up of several words combined. The first word is lowercase, but all
following words have an initial uppercase letter:
Button theButton; long reallyBigNumber; boolean
currentWeatherStateOfPlanetXShortVersion;
Variable Types
In addition to the variable name, each variable declaration
must have a type, which defines what values that variable can hold. The
variable type can be one of three things:
■
One of the eight basic primitive data types
■
The name of a class
■
An array
You’ll learn about how to declare and use array variables in
Chapter 5.
The eight primitive data types
handle common types for integers, floating-point numbers, characters, and
boolean values (true or false). They’re called primitive because they’re built
into the system and are not actual objects, which makes them more efficient to
use. Note that these data types are machine-independent, which means that you
can rely on their sizes and characteristics to be consistent across your Java
programs.
There are four Java integer
types, each with different ranges of values (as listed in Table 3.1). All are
signed, which means they can hold either positive or negative numbers. Which
type you choose for your variables depends on the range of values you expect
that variable to hold; if a value becomes too big for the variable type, it is
truncated.
Post a Comment