Saturday, April 02, 2005

Primitive Types

Introduction

A primitive type is also called a

  • elemental type
  • atomic type

A type has a set of values associated with the type.

Discussion

Concept: Types provides meaning and readability. Types can be checked automatically at compile time, saving you a lot of time debugging.

For programming, we need primitive types that are suitable to the programming task.

Grammar:
PrimitiveType =
    NumericType | 'boolean' ;
NumericType =
    IntegralType | FloatingPointType ;
IntegralType =
    'byte' | 'short' | 'int' | 'long' | 'char' ;
FloatingPointType =
    'float' | 'double' ;

Note: A variable has one of 8 primitive types;
boolean, char, byte, short, int, long, float or double

Note: Not everything in Java is objects. Variables declared with the primitive types in Java are not OOP-objects.

Primitive Types
Type Name Bits Group Value Range (Inclusive) Example
boolean 8, unsigned bool true and false boolean n = true;
byte 8 integral from -128 to 127 byte b = -128;
short 16 integral from -32768 to 32767 short s = 32767;
int (default) 32 integral from -2147483648 to 2147483647 int i = 2147483647;
long 64 integral from -9223372036854775808 to 9223372036854775807 long l = 21L;
char 16, unsigned integral from 0 to 65535 char c = 'A';
float 32 floating point from 1.4E-45 to 3.4028235E38 float f = 1.41f
double (default) 64 floating point from 4.9E-324 to 1.7976931348623157E308 double d = 3.14;

No comments: