Primitive Data Types
Lesson:
Main Points:
- Java's primitive data types are: boolean, char, byte, short, int, long,
float, and double.
- Variables of type boolean may only have the values of true or
false.
- The four signed integral data types are: byte, short, int, and
long.
- The char type is integral but unsigned. The range of a variable of type
char is from 0 through 216-1.
- The floating-point types are float and double.
Primitive Data Types and Their Sizes
Type | Representation Size (bits) |
Type | Representation Size (bits) |
boolean | 1 | char | 16 |
byte | 8 | short | 16 |
int | 32 | long | 64 |
float | 32 | double | 64 |
Ranges of the Integral Primitive Types
Type | Size | Minimum | Maximum |
byte | 8 bits | -27 | 27-1 |
short | 16 bits | -215 | 215-1 |
int | 32 bits | -231 | 231-1 |
long | 64 bits | -263 | 263-1 |
Floating-Point Types:
The two floating-point types are:
- float
- double
The Float and Double classes have special values defined to represent
negative infinity, positive infinity, and not-a-number:
- Float.NaN
- Float.NEGATIVE_INFINITY
- Float.POSITIVE_INFINITY
- Double.NaN
- Double.NEGATIVE_INFINITY
- Double.POSITIVE_INFINITY
The sample program will deal mostly with the char type, but there is an
example showing the use of negative infinity.
import java.awt.*;
import java.applet.*;
public class ex3 extends Applet{
public void init(){
setBackground(Color.black);
}
public void paint(Graphics g){
g.setColor(Color.cyan);
for(int i = 70, c=0; i<76; i++, c++){
//by placing (char) in front of a variable of type int
//that variable is displayed as a char value
g.drawString("int value: "+i+", char value: "+(char)i, 10, 20+(c*15));
}
g.drawString("Negative Infinity: "+Float.NEGATIVE_INFINITY, 10, 110);
}
}
Assignment:
Your job is to write an applet which displays the entire lower-case alphabet
along with the integer value for each letter. You may have to do some
research OR experimentation to figure out where the lower-case alphabet
starts and ends. There are several ways of approaching this problem. You
just have to figure out one that works.
Present your code along with your applet on a web page.