© We Can Code IT, LLC
We saw literals yesterday. In the statement:
System.out.println("Hello, World!");
"Hello, World!"
is a String literal. It's a String because it's a sequence of characters surrounded by quotation marks. It's a literal because it always has that value.
In contrast, we have variables. In Java, a variable's type is defined, but its value can vary (thus the name).
System.out.println("Hello, World!");
String message = "Hello, World!";
System.out.println(message);
Variables store a value, but allow us to change the value that is stored.
String message = "I am Sam";
System.out.println(message); /* prints "I am Sam" */
message = "I am Spartacus";
System.out.println(message); /* prints "I am Spartacus" */
Notice that in the previous example, when we first used our message
variable, we specified its type:
String message = "I am Sam";
System.out.println(message);
message = "I am Spartacus";
System.out.println(message);
That's because we had already declared the variable. We declare a variable only once.
When we do this:
String message = "I am Sam";
String message;
message = "I am Sam";
We declare a variable by giving it a type and a name. That's what we did here:
String message;
The variable's type is String
. Its name is message
.
When we did this, we assigned a value of "I am Sam"
to the variable. Initializing is only different than assigning a value in that we haven't assigned a value yet:
String message;
message = "I am Sam";
We will talk about naming conventions quite a bit. They're important, since they reduce the cognitive load of people that need to look at your code. (This person might be you.)
Apart from constants (more on that later), we use camelCase
to name variables. The first letter should be lowercase, and the first letter of each additional word in a variable's name should be uppercase:
String myVariable = "my variable's value"; // Do this!
String myvariable = "my variable's value"; // Not this.
String MyVariable = "my variable's value"; // Not this, either.
Java is not a pure object-oriented language. That is to say that it allows for primitive types as well as objects. For now, recognize that primitive types are declared in lowercase. Real objects will have their types capitalized.
A boolean
variable holds either the value true
or false
.
boolean writingCodeIsFun = true;
boolean rootCanalsAreFun = false; // unless you're a dentist?
There are four sizes of integer (whole number) types: byte
, short
, int
, and long
. They vary in their maximum and minimum values. A byte
will hold a number that can be stored within one byte (8 bits). short
supports a smaller range of values than int
supports. int
supports a smaller range of values than long
supports. We use int
s (short for "integer") for most practical purposes. We often see long
s for things like database record ids since we may have a lot of records in a database.
All of the following are valid:
byte answerByte = 42;
short answerShort = 42;
int answerInt = 42;
long answerLong = 42;
Floating point (numbers with decimal fractions) types are float
and double
. double
is more precise than float
.
Let's try it. (Note that we append 'f' to the value for the float variable):
public class FloatingPointDemo {
public static void main(String[] args) {
double doubleValue = 2.16440330489723961032;
System.out.println(doubleValue);
float floatValue = 2.16440330489723961032f;
System.out.println(floatValue);
}
}
The values assigned to each variable are the same. Try it. What do you see?
Computers aren't as good at math as you thought, eh?
The char
type holds individual characters. We define char
literals by using single apostrophes.
char myChar = 'a';
Strings are proper objects. We'll talk more about them later. We've seen them. We denote their values with quotation marks:
String myDeclaration = "I can code";