Identifiers and primitive data types

 

Uses of identifiers

 

Rules for identifiers

 

Examples of valid identifiers

yearlyGrossPay

_2002_Budget

JuneActual

$totalAmtDue

 

Programming conventions

 

Primitive data types

 

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?

 

What is Unicode?

 

Integer data format

Uses two's-complement notation. To determine the bit pattern of an integer value:

  1. If the number is greater than or equal to zero:  convert it to binary

  2. 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

 

More information

Java ranch has two great tutorials relating to topics covered in this lesson. Be sure to read the following "campfire stories":

 

Review questions

  1. Which of the following are valid Java identifiers?  (choose three)

  1. _$amount_due_2002

  2. 2002_sales

  3. class

  4. String

  5. FINANCE_CHARGE

  1. Which primitive data type may be used to store the integer value -473 ?  (choose three)

  1. char

  2. byte

  3. short

  4. int

  5. long

  1. Which of the following hexadecimal values might represent the contents of a negative, int variable?  (choose two)

  1. 1AF21110

  2. C8253B7C

  3. F63A

  4. FFFFFFFFFFFFFFFF

  5. 90001C58

  1. In hexadecimal notation, what is the value of -100 as a byte?

  1. FF9C

  2. 9C

  3. FF9B

  4. 9B

  5. a byte is too small an area to hold the value