To Documents
Constants, Datatypes, and Variables
Constants
In IT 313, we will use five different types of Java constants:
int,
double,
char, string, and
boolean.
- Integer
- Floating Point
- See Wikipedia for more information about the
IEEE 754 Floating Point Standard.
- For general information about floating point arithmetic, check out this link:
- Character
- Escape characters starting with a backslash are used to represent
non-printable characters:
Character | Meaning |
'\'' | Single Quote |
'\"' | Double Quote |
'\\' | Backslash |
'\t' | Tab |
'\n' | New Line |
'\r' | Carriage Return |
'\f' | Form Feed |
'\b' | Backspace |
'\0' | Null Character (Ascii Code 0) |
- Unicode characters can also be represented in Java. Here are some examples:
Character | Appearence | Language |
'\u03b1' | α | Greek |
'\u03b2' | β | Greek |
'\u03b3' | γ | Greek |
'\u05d0' | ℵ | Hebrew |
'\u221e' | ∞ | Math Symbol |
- Here is the official Unicode site containing codes for all registered characters:
unicode.org.
- Unicode characters are difficult to work with at the DOS command prompt.
They are usually used for graphical applications or when reading from or writing to files on disk.
- Java can only handle two byte unicode characters. There are so many Chinese
characters (more than 65,000) represented in Unicode so that occasionally four
bytes are needed for such a Chinese character.
- See the Unicode1 and Unicode2 Examples in the unicode examples file.
- String
- Boolean
- A boolean constant takes on either of the values
true or false.
Datatypes
- A datatype is a way of representing data internally in a computer.
- Java uses these primitive datatypes:
Datatype | Representation | Default Value | Size (bytes) | Range | Sig. Digits |
byte | Unsigned Integer |
0 | 1 | 0 to 2^8-1 = 255 | |
short | Signed Integer | 0 | 2 | -2^15 to -2^15-1 | |
int | Signed Integer | 0 | 4 | -2^31 to -2^31-1 | |
long | Signed Integer | 0L | 8 | -2^63 to -2^63-1 | |
double | Double Precision Floating Point | 0.0 |
16 | -1.798*10^308 to 1.798*10^308 | 14 or 15 |
float | Single Precision Floating Point | 0.0 |
8 | -3.403*10^38 to 3.403*10^38 | 7 |
char | Binary Unicode Code |
'\0' | 2 | Unicode Character | |
String | String of Characters |
"" | 4 or 8 byte Object Reference | String of length 2^31-1 | |
boolean | false stored as 0
true stored as 1 | false | 1 |
false or true | |
Variables
Java is case sensitive, which means that capitalization matters. The names
numberOfCustomers
and
numberofcustomers
represent two different variables.
By convention, Java spells local variable names in lower camel casing (first
letter lower case, each new word starts with upper case) and uses upper camel
casing to separate words
(same as lower camel casing, but start with upper case letter).
An example of a local variable name is numberOfCustomers.
The address of a variable is the location
in the computer's memory where the data is stored. The Ruby interpreter
keeps a symbol table that maintains the correspondence between the
variable's name and its address, so the programmer need not be
explicity concerned about the address and, in fact, does not have access to the
address of a given variable.
The value of a variable is the piece of data currently stored at its address,
which is interpreted according to the datatype of the variable.
A variable can only contain one value at a time.
Java variables must be declared. For example:
int n;
double x, y;
char c, d;
String s, t;
boolean f;
These variables can be later assigned values like this:
n = 39;
x = 534.56;
y = 3.45e12;
c = 'a';
d = '\t';
s = "dog";
t = "cat";
f = false;
Variables may be initialized at the same time they are declared:
int n = 39;
double x = 534.36, y = 3.45e12;
char c = 'a', d = '\n';
String s = "dog", t = "cat";
boolean f = false;
The Assignment Operator
- The assignment operator is used to assign a value on the right side of an
expression to a variable on the left side.
For example,
x = 5;
assigns the value 5 to the variable x.
Question: why is the following not a legal assignment?
5 = x;
The + operator
- The + operator is an overloaded operator. It's behavior depends on the datatypes of its operands.
- + performs addition if both of its operators are numeric (integer or floating point).
- + performs concatenation if at least of its operators is
String.
- Here are some examples:
int n = 5, m = 7;
String s = "dog", t = "cat";
System.out.println(n + m); // Output: 12
System.out.println(s + t); // Output: dogcat
System.out.println(n + t); // Output: 5cat
System.out.println(s + m); // Output: dog7