Wednesday, 25 April 2018

Core Java: Data Type

In Java, there are two broad categories of types: primitive types (e.g., int, double) and reference types (e.g., objects and arrays). We shall describe the primitive types here and the reference types (classes and objects) in the later chapters on "Object-Oriented Programming".
 
TYPE
DESCRIPTION
Byte
Integer
8-bit signed integer
The range is [-2^7, 2^7-1] = [-128, 127]
Short
16-bit signed integer
The range is [-2^15, 2^15-1] = [-32768, 32767]
Int
32-bit signed integer
The range is [-2^31, 2^31-1] = [-2147483648, 2147483647] (≈9 digits)
Long
64-bit signed integer
The range is [-2^63, 2^63-1] = [-9223372036854775808, +9223372036854775807] (≈19 digits)
Float
Floating-Point
Number
32-bit single precision floating-point number
(≈6-7 significant decimal digits, in the range of ±[≈10^-45, ≈10^38])
Double
64-bit double precision floating-point number
(≈14-15 significant decimal digits, in the range of ±[≈10^-324, ≈10^308])
Char
Character
Represented in 16-bit Unicode '\u0000' to '\uFFFF'.
Can be treated as 16-bit unsigned integers in the range of [0, 65535] in arithmetic operations.
Boolean
Binary
Takes a value of either true or false.
The size of boolean is not defined in the Java specification, but requires at least one bit.



Built-in Primitive Types
 
·        Primitive type are built-in to the languages. Java has eight primitive types, as listed in the above table:
·        There are four integer types: 8-bit byte, 16-bit short, 32-bit int and 64-bit long. They are signed integers in 2's complement representation, and can hold an integer value of the various ranges as shown in the table.
·        There are two floating-point types: 32-bit single-precision float and 64-bit double-precision double, represented as specified by IEEE 754 standard. A float can represent a number between ±1.40239846×10^-45 and ±3.40282347×10^38, approximated. A double can represented a number between ±4.94065645841246544×10^-324 and ±1.79769313486231570×10^308, approximated. Take note that not all real numbers can be represented by float and double. This is because there are infinite real numbers even in a small range of say [1.1, 2.2], but there is a finite number of patterns in a n-bit representation. Most of the floating-point values are approximated to their nearest representation.
·        The type char represents a single character, such as '0', 'A', 'a'. In Java, char is represented using 16-bit Unicode (in UCS-2 format) to support internationalization (i18n). A char can be treated as a 16-bit unsigned integer (in the range of [0, 65535]) in arithmetic operations. For example, character '0' is 48 (decimal) or 30H (hexadecimal); character 'A' is 65 (decimal) or 41H (hexadecimal); character 'a' is 97 (decimal) or 61H (hexadecimal).
·        Java introduces a new binary type called "boolean", which takes a value of either true or false.
 
Example: The following program can be used to print the maximum, minimum and bit-length of the primitive types. The maximum, minimum and bit-size of int are kept in constants INTERER.MIN_VALUE, INTEGER.MAX_VALUE, INTEGER.SIZE.
/*
 * Print the minimum, maximum and bit-length for primitive types
 */
public class PrimitiveTypesMinMax {
   public static void main(String[] args) {
      // int (32-bit signed integer)
      System.out.println("int(min) = " + Integer.MIN_VALUE);
      System.out.println("int(max) = " + Integer.MAX_VALUE);
      System.out.println("int(bit-length) = " + Integer.SIZE);
      // byte (8-bit signed integer)
      System.out.println("byte(min) = " + Byte.MIN_VALUE);
      System.out.println("byte(max) = " + Byte.MAX_VALUE);
      System.out.println("byte(bit-length)=" + Byte.SIZE);
      // short (16-bit signed integer)
      System.out.println("short(min) = " + Short.MIN_VALUE);
      System.out.println("short(max) = " + Short.MAX_VALUE);
      System.out.println("short(bit-length) = " + Short.SIZE);
      // long (64-bit signed integer)
      System.out.println("long(min) = " + Long.MIN_VALUE);
      System.out.println("long(max) = " + Long.MAX_VALUE);
      System.out.println("long(bit-length) = " + Long.SIZE);
      // char (16-bit character or 16-bit unsigned integer)
      System.out.println("char(min) = " + (int)Character.MIN_VALUE);
      System.out.println("char(max) = " + (int)Character.MAX_VALUE);
      System.out.println("char(bit-length) = " + Character.SIZE);
      // float (32-bit floating-point)
      System.out.println("float(min) = " + Float.MIN_VALUE);
      System.out.println("float(max) = " + Float.MAX_VALUE);
      System.out.println("float(bit-length) = " + Float.SIZE);
      // double (64-bit floating-point)
      System.out.println("double(min) = " + Double.MIN_VALUE);
      System.out.println("double(max) = " + Double.MAX_VALUE);
      System.out.println("double(bit-length) = " + Double.SIZE);
   }
}
 
The expected outputs are:
 
int(min) = -2147483648
int(max) = 2147483647
int(bit-length) = 32
byte(min) = -128
byte(max) = 127
byte(bit-length)=8
short(min) = -32768
short(max) = 32767
short(bit-length) = 16
long(min) = -9223372036854775808
long(max) = 9223372036854775807
long(bit-length) = 64
char(min) = 0
char(max) = 65535
char(bit-length) = 16
float(min) = 1.4E-45
float(max) = 3.4028235E38
float(bit-length) = 32
double(min) = 4.9E-324
double(max) = 1.7976931348623157E308
double(bit-length) = 64


















































































No comments:

Post a Comment

Please write your view and suggestion....