- At this point, you should already have downloaded an Integrated Development Environment., or IDE, and be familiar with using this IDE. For this series, I'm using NetBeans. But if you prefer to use Eclipse or BlueJ, which are also free interactive java development environments, that's fine, and most of the examples are easily adapted to those environments. If you need help downloading an Integrated Development Environment, please see "Up and Running with Java," on lynda.com. As you know, Java is a strict data-typing language.
That is, every variable must be declared with the data type, and your program cannot change the type of data stored in that variable. For example, if you declare a variable to store numbers, you cannot try to store Strings in the same variable. You would get an error message, "Incompatible types: String cannot be converted to int." Each programming language has the same basic parts. Let's do a quick review. Comments. Comments can be single-line or multi-line.
Variables include the data type and the name. Expressions, which are equations or statements. Decisions, such as "if something is true." Loops, which allow the program to repeat. Input and Output, which can be from the console or files. And Methods. Some languages refer to Methods as functions. And there are more parts, but those are the ones you want to concentrate on for now. Let's take a minute to quickly review expressions with data types.
Here is an example Java program with the data types we just reviewed. On line 12 we declare a character variable called "letter," and we assign it the value capital "A." Notice the single-quotes for characters. Then we have a Boolean variable called "done," and it's set to false. Boolean variables can be true or false. We have an integer variable called "radius," and it is set to the value "10." We have a byte variable called "red." Sometimes byte variables are used to hold the saturation level of green, red, and blue when trying to create colors.
In this case, I have red set to "127." A byte can be from -127, to +127, or unsigned values from 0 to 255. On line 16 we have a data type called "short." It's also an integer, but it's much smaller than an int. Short age equals 21. Sometimes you need to hold a very large integer. Then you want to use "long." And finally rational numbers, such as "float" and "double." Notice that when you declare a variable of type "float," you need to include the "f" after the number, "10.59f," and finally we have a String variable named = "Peggy Fisher." Strings are not technically a primitive data type, but since they are used so often, it is usually included when discussing primitive data types.
Strings are actually variables that are stored as an array of characters. Okay, so what does it mean when I'm talking about primitive data types versus structured data types? The data types we just discussed are all primitive data types. The other type is structured, and we will discuss them more when we get to arrays, classes, and objects. Primitive data types store their values directly in memory, such as "int x = 5". In memory, x has the value 5. Structured data types, such as a String that's called "fruit" and is set to the word "Apple," creates a reference value, or the address of the location in memory where the data exists.
In this case, my variable "fruit" has the address "@31eb494e." That's the hashcode for where the data is located in memory. Later we'll talk more about objects and classes, and it's helpful to know that every primitive data type has a corresponding wrapper class. This allows us to create objects that contain values, such as integers, characters, etc. For example, "int" has a corresponding wrapper class called "Integer," "double" has a corresponding class called "Double." Notice the capital "I" in "Integer" and the capital "D" in "Double," as well as the "B" in "Boolean" and the "C" in "Character," because classes always start with a capital letter.
This was hopefully a review for you. If not, you might want to go back and review "Up and Running with Java."
- Getting started with parsing
- Reviewing data types
- Using decisions
- Creating user-defined methods
- Command-line debugging
- Exploring the Java API
- Creating and instantiating classes
- Working with interfaces
- Storing items with arrays