Primitive Data Types


x

Lesson:
Main Points:
  1. Java's primitive data types are: boolean, char, byte, short, int, long, float, and double.
  2. Variables of type boolean may only have the values of true or false.
  3. The four signed integral data types are: byte, short, int, and long.
  4. The char type is integral but unsigned. The range of a variable of type char is from 0 through 216-1.
  5. The floating-point types are float and double.

Primitive Data Types and Their Sizes
TypeRepresentation Size (bits) TypeRepresentation Size (bits)
boolean1char16
byte8short16
int32long64
float32double64

Ranges of the Integral Primitive Types
TypeSizeMinimumMaximum
byte8 bits-2727-1
short16 bits-215215-1
int32 bits-231231-1
long64 bits-263263-1

Floating-Point Types:
The two floating-point types are:
  1. float
  2. double

The Float and Double classes have special values defined to represent negative infinity, positive infinity, and not-a-number:
  1. Float.NaN
  2. Float.NEGATIVE_INFINITY
  3. Float.POSITIVE_INFINITY
  4. Double.NaN
  5. Double.NEGATIVE_INFINITY
  6. 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); } }
x

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