Uses of identifiers
To name a variable (a data area that takes on different values during processing)
To name a method (a module of code)
To name a class (a definition from which objects can be constructed)
To label a statement (for later referencing)
Rules for identifiers
It must not be a Java keyword
It must begin with a letter, dollar sign ( $ ), or underscore ( _ )
Subsequent characters may be letters, dollar signs, underscores, or digits
Case matters. For example, the three identifiers MAIN, Main, and main are different.
Examples of valid identifiers
yearlyGrossPay
_2002_Budget
JuneActual
$totalAmtDue
Programming conventions
Are not enforced by the compiler but make it easier to read and maintain Java code
Most programmers use "camel caps" (such as firstQuarterSalesTotal) to handle long identifiers that appear to contain multiple words. Some use undercores (like first_Quarter_Sales_Totals).
Begin variable names, method names, and labels with a lower case letter. For example, totalAmount, hoursWorked, employeeName, calcBonus, and main.
Begin class names with a capital letter. For example, Person, Customer, SalesRep, and Part.
Names of constants should be all capitals. For example, TAX_RATE, COMMISSION, and DEPOSIT_CODE.
Primitive data types
Are part of the language (built-in)
Represent different kinds and sizes of data
Are always the same regardless of the platform
Type |
Bit Size | Usage | Signed | Range of Values |
boolean | 1 | true or false | no | true or false |
char | 16 | Unicode character | no | 0 to 216 - 1 |
byte | 8 | very small integer number | yes | -27 to 27 - 1 |
short | 16 | small integer number | yes | -215 to 215 - 1 |
int | 32 | integer number | yes | -231 to 231 - 1 |
long | 64 | large integer number | yes | -263 to 263 - 1 |
float | 32 | single precision decimal number | yes | + 3.4e38 to + 1.4e-45 |
double | 64 | double precision decimal number | yes | + 1.8e308 to + 4.9e-324 |
What about strings?
There is no primitive data type in Java to represent strings (such as "Java is fun")
Several packaged classes provide for string manipulation and will be covered later
What is Unicode?
An international (ISO) standard. For detailed information, go to www.unicode.org
Provides a unique bit pattern to represent every character or symbol in all the world's character sets
Each character has an equivalent, positive integer value corresponding to the bit pattern
Integer data format
Uses two's-complement notation. To determine the bit pattern of an integer value:
If the number is greater than or equal to zero: convert it to binary
If the number is negative: take its absolute value, subtract one, convert the result to binary, and reverse all the bits
The left-most (high-order) bit is the sign bit with 0 indicating positive and 1 indicating negative.
Here are some examples using a 16 bit (short) data format:
Action | Base 10 | Binary | Hexadecimal |
Integer to be represented | -5 | ||
1. Absolute value | 5 | ||
2. Minus 1 | 4 | 0000 0000 0000 0100 | 0004 |
3. Reverse bits | 1111 1111 1111 1011 | FFFB | |
Integer to be represented | 67 | 0000 0000 0100 0011 | 0043 |
Integer to be represented | -44 | ||
1. Absolute value | 44 | ||
2. Minus 1 | 43 | 0000 0000 0010 1011 | 002B |
3. Reverse bits | 1111 1111 1101 0100 | FFD4 |
For simplicity, hexadecimal (base 16) notation is used to represent binary numbers. Each hexadecimal digit represents four binary digits as follows:
Hexadecimal Digit | Binary Equivalent |
0 | 0000 |
1 | 0001 |
2 | 0010 |
3 | 0011 |
4 | 0100 |
5 | 0101 |
6 | 0110 |
7 | 0111 |
8 | 1000 |
9 | 1001 |
A | 1010 |
B | 1011 |
C | 1100 |
D | 1101 |
E | 1110 |
F | 1111 |
Floating-point (decimal) types
Conform to the IEEE 754 specification for representing real numbers in a binary format. For the certification exam, you will not need to know the internal representation or the range of maximum and minimum values.
double allows up for twice as many significant digits as float and should be used when extremely large numbers or greater precision are required
More information
Java ranch has two great tutorials relating to topics covered in this lesson. Be sure to read the following "campfire stories":
Cup Size -- a story about variables
Cat and Mouse Games with Bits
Review questions
Which of the following are valid Java identifiers? (choose three)
_$amount_due_2002
2002_sales
class
String
FINANCE_CHARGE
Which primitive data type may be used to store the integer value -473 ? (choose three)
char
byte
short
int
long
Which of the following hexadecimal values might represent the contents of a negative, int variable? (choose two)
1AF21110
C8253B7C
F63A
FFFFFFFFFFFFFFFF
90001C58
In hexadecimal notation, what is the value of -100 as a byte?
FF9C
9C
FF9B
9B
a byte is too small an area to hold the value