|
|
|
1000 Java Tips ebook
|
|
 

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java
programming articles and tips. Please take your copy here.
Take your copy of free "Java Technology Screensaver"!. |
|
The Java Lesson 3: Identifiers and primitive data types. Page 2
|
JavaFAQ Home » Java Lessons by Jon Huhtala

Primitive data
types
1. Uses of identifiers in Java | 2. Primitive data types in Java
-
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?
What is Unicode?
-
An international (ISO)
standard. For detailed information, go to http://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":
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
Printer Friendly Page
Send to a Friend
..
Search here again if you need more info!
|
|
|