Primitive Data Types and Operations in Java

Posted By: Java Examples - 5:25 PM


& Comment

 Primitive Data Types 
The primitive data types are predefined data types, which always hold the value of the same data type, and  the values of a primitive data type don't share the state with other primitive values. These data types are named by a reserved keyword in Java  programming language. 

There are eight primitive data types supported by Java programming language :


 The byte data type is an 8-bit signed two's complement integer. It ranges from -128 to127 (inclusive). This type of data type is useful to save   memory  in large arrays.. We can also use byte instead of int  to increase the limit of the code. The syntax of declaring a byte type variable is shown as:
 byte b = 5;


 The short data type is a 16-bit signed two's complement integer. It ranges from -32,768 to 32,767.short is used to save memory in large arrays. The syntax of declaring a short type variable is shown as:
 short s = 2;


  The int data type is used to store the integer values not the fraction values. It is a 32-bit signed two's complement integer data type. It ranges from -2,147,483,648 to 2,147,483,647 that is more enough to store large number in your program.  However for wider range of values use long. The syntax of declaring a int type variable is shown as:
 int num = 50;


 The long data type is a 64-bit signed two's complement integer. It ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Use this data type with larger range of values. The syntax of declaring a long type variable is shown as:
 long ln = 746; 


 The float data type is a single-precision 32-bit IEEE 754 floating point. It ranges from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative). Use a float (instead of double) to save memory in large arrays. We do not use this data type for the exact values such as currency. For that we have to use java.math.BigDecimal class. The syntax of declaring a float type variable is:
 float f = 105.65;
 float f = -5000.12;


  This data type is a double-precision 64-bit IEEE 754 floating point. It ranges from 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative). This data type is generally the default choice for decimal values. The syntax of declaring a double type variable is shown as:
 double d = 6677.60;


 The char data type is a single 16-bit, unsigned Unicode character. It ranges from 0 to 65,535. They are not integral data type like int, short etc. i.e. the char data type can't hold the numeric values. The syntax of declaring a char type variable is shown as:
 char caps = 'c'; 


 The  boolean data type represents only two values: true and false and occupy is 1-bit in the memory. These values are keywords in Java and  represents the two boolean states: on or offyes orno. We use boolean data type for specifying conditional statements as if, while, do, for. In Java, trueand false are not the same as True and False. They are defined constants of the language. The syntax of declaring a boolean type variable is shown as:
 boolean result = true;

 The  ranges of these data types can be described with default values using the following table:
Data TypeDefault Value (for fields)Size (in bits)Minimum RangeMaximum Range
 byte 0Occupy 8 bits in memory -128 +127
 short 0Occupy 16 bits in memory -32768 +32767
 int 0Occupy 32 bits in memory -2147483648 +2147483647
 long 0LOccupy 64 bits in memory -9223372036854775808 +9223372036854775807
 float 0.0fOccupy 32-bit IEEE 754 floating point1.40129846432481707e-45 3.40282346638528860e+38
 double 0.0dOccupy 64-bit IEEE 754 floating point 4.94065645841246544e-324d 1.79769313486231570e+308d
 char '\u0000'Occupy 16-bit, unsigned Unicode character 0 to 65,535
 boolean falseOccupy 1- bit in memory NA NA

Integer numbers

An integer data type can hold a whole number, but no fraction. Integers may be either signed (allowing negative values) or unsigned (nonnegative values only). Typical sizes of integers are:

SizeNamesSigned RangeUnsigned Range
8 bits
−128 to +127
0 to 255
16 bits
Word, short int
−32,768 to +32,767
0 to 65,535
32 bits
Double Word, long int (win32, win64, 32-bit Linux[1])
−2,147,483,648 to +2,147,483,647
0 to 4,294,967,295
64 bits
long int (C in 64-bit linux[1]), long long (C), long (Java, the signed integer variant only[2]))
−9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
0 to 18,446,744,073,709,551,615

About Java Examples

I’m passionate about Web Development and Programming and I go to extreme efforts to meet my passion. I’m a believer of learning the fundamentals first. I try to understand everything little bit more than the average.

Copyright © 2016 Java Examples ACHCHUTHAN.ORG. Designed by Templateism .