A.2. Variables and Types

Our program will be more interesting if it can store some information. We do this using variables, as shown in Figure A-2.

Line 3 declares a variable called name, which is of type String. We cannot use a variable until it has been declared.

The next thing we must do with a variable is initialize itthat is, give it a value. The variable name is initialized on line 4.

Figure A-2. This improved main() method for the Hello program greets the user by name.

1 /** Print a friendly greeting. */
2 public static void main(String[] args) {
3 String name;
4 name = "Bob";
5 System.out.println("Hello, " + name + "!");
6 }

It is legal to declare and initialize a variable on the same line. We could combine lines 3 and 4 of Figure A-2 into the single line:

String name = "Bob";

Now that name has a value, we can use it to stand for that value. On line 5, we build a longer String by concatenating together three Strings: "Hello, ", the value of name, and "!".

A few special Strings are given in Figure A-3.

Figure A-3. Special Strings.

String

Meaning

" "

newline

" "

tab

"""

quotation marks

"\"

backslash

The most common types for variables are listed in Figure A-4. The first four types are called primitive types, because they cannot be broken down into smaller pieces. A String, in contrast, can be broken down into chars. There are four other primitive types: short, long, byte, and float. These are rarely used.

Figure A-4. Commonly used types. Unicode is a character-encoding scheme similar to ASCII, but it encompasses characters from many languages.

(This item is displayed on page 523 in the print version)

Type

Range

Example

boolean

true or false

true

char

Unicode characters

'a'

double

roughly ± 1.8 x 10308

3.14159

int

roughly ± 2 billion

23

String

sequences of characters

"Hello"

We can convert between primitive numeric types (including chars) by casting. For example, if we want to convert the double 3.14159 into an int, the syntax is:

(int)3.14159

This conversion throws away any fractional part. In this case, the result is 3.

If we want to assign a variable of type int a value of type double, we must do this casting:

int n = (int)3.14159;

When converting from a less precise to a more precise type, we don't have to cast explicitly. For example:

double x = 3;

Arithmetic operations automatically convert to the most precise type involved, so the result of

3 * 2.0

is of type double.

As we will see in Chapter 1, it is possible (and quite useful) to define new types. It is not possible to define new primitive types.

We can also have an array of any type. To specify an array type, add [] to the end of another type name. For example, the argument to the main() method is an array args, which is of type String[]. When we run a Java program, any additional command-line arguments are placed into this array. If we run the program as

java Hello Akiko Bob Carlos

then args[0] is "Akiko", args[1] is "Bob", and args[2] is "Carlos". The length of the array, args.length, is 3.

The scope of a variable or argument is the part of the program in which we can refer to the variable or argument. Without going into too much detail, the scope of a variable or argument is generally the code between a pair of curly braces. Specifically, an argument like args or a variable like name is visible only within the current method.

Exercises

A.5

What happens if you declare two variables or arguments with the same name in the same scope? Specifically, what if you declare a variable args within the main() method?

A.6

What error message do you get if you declare a variable of type int and initialize it to 10,000,000,000?

A.7

What happens if you print args?


Part I: Object-Oriented Programming

Encapsulation

Polymorphism

Inheritance

Part II: Linear Structures

Stacks and Queues

Array-Based Structures

Linked Structures

Part III: Algorithms

Analysis of Algorithms

Searching and Sorting

Recursion

Part IV: Trees and Sets

Trees

Sets

Part V: Advanced Topics

Advanced Linear Structures

Strings

Advanced Trees

Graphs

Memory Management

Out to the Disk

Part VI: Appendices

A. Review of Java

B. Unified Modeling Language

C. Summation Formulae

D. Further Reading

Index



Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
ISBN: 0131469142
EAN: 2147483647
Year: 2004
Pages: 216
Authors: Peter Drake

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net