1
JAVA PROGRAMMING : Data types
By
Dr.T.Abirami
Associate Professor
Department of IT
Kongu Engineering College Perundurai
2/3/2021
Program Structure
Documentation Section - suggested
Package Statement - optional
Import Statement - optional
Interface Statement - optional
Main method class - Essential
{ main method definition }
2
Dr.T.Abirami/Department of IT
Dr.T.Abirami/Department of IT 3
package util;
Import java.util.Date
Java Variables
4
Dr.T.Abirami/Department of IT
Syntax
 datatype variable = value;
int myNum = 5;
float myFloatNum = 5.99f;
char myLetter = 'D';
boolean myBool = true;
String myText = "Hello";
5
Dr.T.Abirami/Department of IT
Data type
 Data type specifies the size and type of
values that can be stored in an identifier.
 Different data types allow you to select
the type appropriate to the needs of the
application.
Dr.T.Abirami/Department of IT 6
Data Types
Dr.T.Abirami/Department of IT 7
Java Data Types
Data types are divided into two groups:
 Primitive data types
 - includes byte, short, int, long, float, double,
boolean and char
 Non-primitive data types
 - such as String, Arrays and Classes
8
Dr.T.Abirami/Department of IT
Java Data Types
Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes
Stores whole numbers from -32,768 to
32,767
int 4 bytes
Stores whole numbers from -2,147,483,648
to 2,147,483,647
long 8 bytes
Stores whole numbers from -
9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float 4 bytes
Stores fractional numbers. Sufficient for
storing 6 to 7 decimal digits
double 8 bytes
Stores fractional numbers. Sufficient for
storing 15 decimal digits
boolean 1 bit Stores true or false values
char 2 bytes
Stores a single character/letter or ASCII
values
9
Dr.T.Abirami/Department of IT
Range Calculation
 The range of values is calculated as
−(2n−1) to (2n−1)−1;
 where n is the number of bits required.
For example,
the byte data type requires 1 byte = 8 bits.
Therefore, the range of values that can be
stored in the byte data type is
−(28−1) to (28−1)−1
= −27 to (27) -1
= −128 to 127
Dr.T.Abirami/Department of IT 10
Integer
 Integer types can hold whole numbers.
Ex: such as 123 and −96.
Dr.T.Abirami/Department of IT 11
Type Size Range of values that can be stored
byte 1 byte −128 to 127
short 2 bytes −32768 to 32767
int 4 bytes −2,147,483,648 to 2,147,483,647
long 8 bytes
9,223,372,036,854,775,808 to
9,223,372,036,854,755,807
Integer Types
 byte myNum = 100;
System.out.println(myNum);
 short myNum = 5000;
System.out.println(myNum);
 int myNum = 100000;
System.out.println(myNum);
 long myNum = 15000000000L;
System.out.println(myNum);
Note:you should end the value with an "L"
12
Dr.T.Abirami/Department of IT
Floating Point
 It is used to represent numbers with a
fractional part.
 Single precision floating point numbers
occupy 4 bytes and
 Double precision floating point numbers
occupy 8 bytes.
There are two subtypes:
Dr.T.Abirami/Department of IT 13
Type Size
Range of values that can
be stored
float 4 bytes 3.4e−038 to 3.4e+038
double 8 bytes 1.7e−308 to 1.7e+038
General Terms of fractional nos
Dr.T.Abirami/Department of IT 14
(-1)Sign × Significand × BaseExponent 1.2345=12345x10-4
Single Precision:
Single Precision is a format proposed by IEEE for representation of
floating-point number. It occupies 32 bits in computer memory.
Range of numbers in single precision : 2^(-126) to 2^(+127)
 Double Precision:
Double Precision is also a format given by
IEEE for representation of floating-point
number. It occupies 64 bits in computer
memory.

Dr.T.Abirami/Department of IT 15
General Terms of fractional nos
Range of numbers in double precision : 2^(-1022) to 2^(+1023)
Floating Point Types
The float data type can store fractional numbers from 3.4e−038 to 3.4e+038.
Note that you should end the value with an "f":
 float myNum = 5.75f;
System.out.println(myNum);
 double myNum = 19.99d;
System.out.println(myNum);
 The precision of a floating point value indicates how many digits the
value can have after the decimal point.
 The precision of float is only six or seven decimal digits,
 while double variables have a precision of about 15 digits.
 Therefore it is safer to use double for most calculations.
 A floating point number can also be a scientific number with an "e" to
indicate the power of 10:
16
Dr.T.Abirami/Department of IT
Scientific Notation
 E is used to mean "10 to the power of."
 For example,
1.314E+1 means
1.314 * 101 which is 13.14.
Dr.T.Abirami/Department of IT 17
Example : to indicate the power of 10:scientific
number with an "e"
 float f1 = 35e3f;
 double d1 = 12E4d;
System.out.println(f1);
System.out.println(d1);
18
Dr.T.Abirami/Department of IT
Booleans
 boolean data type represents one bit of
information
 There are only two possible values: true
and false
 This data type is used for simple flags that
track true/false conditions
 Default value is false
 Example:
boolean one = true;
Dr.T.Abirami/Department of IT 19
Booleans
boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun); // Outputs true
System.out.println(isFishTasty); // Outputs false
20
Dr.T.Abirami/Department of IT
Ascii
 It stands for American Standard code for
information interchange.
 It uses 8-bit encoding.
 ASCII uses 7 bits to represent a character.
 By using 7 bits, - > maximum of 2^7 (= 128)
distinct combinations*.
 Which means that we can represent 128
characters maximum.
Why 7 bits not 8 bits (1 byte)
 The last bit (8th) is used for avoiding errors as
parity bit.
Dr.T.Abirami/Department of IT 21
ASCII
 ASCII characters are printable characters
of the alphabet
 such as abc, ABC, 123, ?&!, etc.
 The others are control characters such as
carriage return, line feed, tab, etc
Dr.T.Abirami/Department of IT 22
binary representation of a few characters in ASCII:
0100101 -> % (Percent Sign - 37)
1000001 -> A (Capital letter A - 65)
1000010 -> B (Capital letter B - 66)
1000011 -> C (Capital letter C - 67)
0001101 -> Carriage Return (13)
ASCII was meant for English only.
what about the others needing a completely different
alphabet? Greek? Russian? Polish? Chinese and the likes?
ASCII Extended - Unicode
 Unicode Transformation Format
 Encodings: UTF-7, UTF-8, UTF-16, and UTF-32
 UTF-8 and UTF-16 are variable length encodings.
 In UTF-8, a character may occupy a minimum of 8
bits.
 In UTF-16, a character length starts with 16 bits.
 UTF-32 is a fixed length encoding of 32 bits.
 UTF-8 uses the ASCII set for the first 128
characters. - ASCII text is also valid in UTF-8.
Dr.T.Abirami/Department of IT 23
Unicode represents most written languages in the world while ASCII
does not.
Char
 char data type is a single 16-bit Unicode
character
 Minimum value is 'u0000' (or 0)
 Maximum value is 'uffff' (or 65,535
inclusive)
 Char data type is used to store any
character
 Example: char letterA = 'A'
Dr.T.Abirami/Department of IT 24
Why char uses 2 byte in java and
what is u0000 ?
 It is because java uses Unicode system
not ASCII code system.
 The u0000 is the lowest range of Unicode
system.
Dr.T.Abirami/Department of IT 25
Characters
 char myGrade = 'B';
System.out.println(myGrade);
 char a = 65, b = 66, c = 67;
System.out.println(a);
System.out.println(b);
System.out.println(c);
26
Dr.T.Abirami/Department of IT
27
Char Numb
er
Description
0 - 31 Control characters
(see below)
32 spaceTry it
! 33 exclamation
markTry it
" 34 quotation markTry
it
# 35 number signTry it
$ 36 dollar signTry it
% 37 percent signTry it
& 38 ampersandTry it
' 39 apostropheTry it
( 40 left parenthesisTry
it
) 41 right
parenthesisTry it
A 65 uppercase ATry it
B 66 uppercase BTry it
C 67 uppercase CTry it
D 68 uppercase DTry it
E 69 uppercase ETry it
F 70 uppercase FTry it
G 71 uppercase GTry it
H 72 uppercase HTry it
I 73 uppercase ITry it
J 74 uppercase JTry it
K 75 uppercase KTry it
L 76 uppercase LTry it
M 77 uppercase MTry it
N 78 uppercase NTry it
O 79 uppercase OTry it
P 80 uppercase PTry it
Q 81 uppercase QTry it
R 82 uppercase RTry it
S 83 uppercase STry it
T 84 uppercase TTry it
U 85 uppercase UTry it
V 86 uppercase VTry it
W 87 uppercase WTry it
X 88 uppercase XTry it
Y 89 uppercase YTry it
Z 90 uppercase ZTry it
l 108 lowercase lTry it
m 109 lowercase mTry it
n 110 lowercase nTry it
o 111 lowercase oTry it
p 112 lowercase pTry it
q 113 lowercase qTry it
r 114 lowercase rTry it
s 115 lowercase sTry it
t 116 lowercase tTry it
u 117 lowercase uTry it
v 118 lowercase vTry it
w 119 lowercase wTry it
x 120 lowercase xTry it
y 121 lowercase yTry it
z 122 lowercase zTry it
{ 123 left curly braceTry
it
| 124 vertical barTry it
} 125 right curly braceTry
it
~ 126 tilde
ASCII Printable Characters
Dr.T.Abirami/Department of IT
* 42 asteriskTry it
+ 43 plus signTry it
, 44 commaTry it
- 45 hyphenTry it
. 46 periodTry it
/ 47 slashTry it
0 48 digit 0Try it
1 49 digit 1Try it
2 50 digit 2Try it
3 51 digit 3Try it
4 52 digit 4Try it
5 53 digit 5Try it
6 54 digit 6Try it
7 55 digit 7Try it
8 56 digit 8Try it
9 57 digit 9Try it
: 58 colonTry it
; 59 semicolonTry it
< 60 less-thanTry it
= 61 equals-toTry it
> 62 greater-thanTry it
? 63 question markTry
it
@ 64 at signTry it
28
[ 91 left square
bracketTry it
 92 backslashTry it
] 93 right square
bracketTry it
^ 94 caretTry it
_ 95 underscoreTry it
` 96 grave accentTry it
a 97 lowercase aTry it
b 98 lowercase bTry it
c 99 lowercase cTry it
d 100 lowercase dTry it
e 101 lowercase eTry it
f 102 lowercase fTry it
g 103 lowercase gTry it
h 104 lowercase hTry it
i 105 lowercase iTry it
j 106 lowercase jTry it
k 107 lowercase kTry it
ASCII Printable Characters
Dr.T.Abirami/Department of IT

JAVA PROGRAMMING : Data types

  • 1.
    1 JAVA PROGRAMMING :Data types By Dr.T.Abirami Associate Professor Department of IT Kongu Engineering College Perundurai 2/3/2021
  • 2.
    Program Structure Documentation Section- suggested Package Statement - optional Import Statement - optional Interface Statement - optional Main method class - Essential { main method definition } 2 Dr.T.Abirami/Department of IT
  • 3.
    Dr.T.Abirami/Department of IT3 package util; Import java.util.Date
  • 4.
  • 5.
    Syntax  datatype variable= value; int myNum = 5; float myFloatNum = 5.99f; char myLetter = 'D'; boolean myBool = true; String myText = "Hello"; 5 Dr.T.Abirami/Department of IT
  • 6.
    Data type  Datatype specifies the size and type of values that can be stored in an identifier.  Different data types allow you to select the type appropriate to the needs of the application. Dr.T.Abirami/Department of IT 6
  • 7.
  • 8.
    Java Data Types Datatypes are divided into two groups:  Primitive data types  - includes byte, short, int, long, float, double, boolean and char  Non-primitive data types  - such as String, Arrays and Classes 8 Dr.T.Abirami/Department of IT
  • 9.
    Java Data Types DataType Size Description byte 1 byte Stores whole numbers from -128 to 127 short 2 bytes Stores whole numbers from -32,768 to 32,767 int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647 long 8 bytes Stores whole numbers from - 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits boolean 1 bit Stores true or false values char 2 bytes Stores a single character/letter or ASCII values 9 Dr.T.Abirami/Department of IT
  • 10.
    Range Calculation  Therange of values is calculated as −(2n−1) to (2n−1)−1;  where n is the number of bits required. For example, the byte data type requires 1 byte = 8 bits. Therefore, the range of values that can be stored in the byte data type is −(28−1) to (28−1)−1 = −27 to (27) -1 = −128 to 127 Dr.T.Abirami/Department of IT 10
  • 11.
    Integer  Integer typescan hold whole numbers. Ex: such as 123 and −96. Dr.T.Abirami/Department of IT 11 Type Size Range of values that can be stored byte 1 byte −128 to 127 short 2 bytes −32768 to 32767 int 4 bytes −2,147,483,648 to 2,147,483,647 long 8 bytes 9,223,372,036,854,775,808 to 9,223,372,036,854,755,807
  • 12.
    Integer Types  bytemyNum = 100; System.out.println(myNum);  short myNum = 5000; System.out.println(myNum);  int myNum = 100000; System.out.println(myNum);  long myNum = 15000000000L; System.out.println(myNum); Note:you should end the value with an "L" 12 Dr.T.Abirami/Department of IT
  • 13.
    Floating Point  Itis used to represent numbers with a fractional part.  Single precision floating point numbers occupy 4 bytes and  Double precision floating point numbers occupy 8 bytes. There are two subtypes: Dr.T.Abirami/Department of IT 13 Type Size Range of values that can be stored float 4 bytes 3.4e−038 to 3.4e+038 double 8 bytes 1.7e−308 to 1.7e+038
  • 14.
    General Terms offractional nos Dr.T.Abirami/Department of IT 14 (-1)Sign × Significand × BaseExponent 1.2345=12345x10-4 Single Precision: Single Precision is a format proposed by IEEE for representation of floating-point number. It occupies 32 bits in computer memory. Range of numbers in single precision : 2^(-126) to 2^(+127)
  • 15.
     Double Precision: DoublePrecision is also a format given by IEEE for representation of floating-point number. It occupies 64 bits in computer memory.  Dr.T.Abirami/Department of IT 15 General Terms of fractional nos Range of numbers in double precision : 2^(-1022) to 2^(+1023)
  • 16.
    Floating Point Types Thefloat data type can store fractional numbers from 3.4e−038 to 3.4e+038. Note that you should end the value with an "f":  float myNum = 5.75f; System.out.println(myNum);  double myNum = 19.99d; System.out.println(myNum);  The precision of a floating point value indicates how many digits the value can have after the decimal point.  The precision of float is only six or seven decimal digits,  while double variables have a precision of about 15 digits.  Therefore it is safer to use double for most calculations.  A floating point number can also be a scientific number with an "e" to indicate the power of 10: 16 Dr.T.Abirami/Department of IT
  • 17.
    Scientific Notation  Eis used to mean "10 to the power of."  For example, 1.314E+1 means 1.314 * 101 which is 13.14. Dr.T.Abirami/Department of IT 17
  • 18.
    Example : toindicate the power of 10:scientific number with an "e"  float f1 = 35e3f;  double d1 = 12E4d; System.out.println(f1); System.out.println(d1); 18 Dr.T.Abirami/Department of IT
  • 19.
    Booleans  boolean datatype represents one bit of information  There are only two possible values: true and false  This data type is used for simple flags that track true/false conditions  Default value is false  Example: boolean one = true; Dr.T.Abirami/Department of IT 19
  • 20.
    Booleans boolean isJavaFun =true; boolean isFishTasty = false; System.out.println(isJavaFun); // Outputs true System.out.println(isFishTasty); // Outputs false 20 Dr.T.Abirami/Department of IT
  • 21.
    Ascii  It standsfor American Standard code for information interchange.  It uses 8-bit encoding.  ASCII uses 7 bits to represent a character.  By using 7 bits, - > maximum of 2^7 (= 128) distinct combinations*.  Which means that we can represent 128 characters maximum. Why 7 bits not 8 bits (1 byte)  The last bit (8th) is used for avoiding errors as parity bit. Dr.T.Abirami/Department of IT 21
  • 22.
    ASCII  ASCII charactersare printable characters of the alphabet  such as abc, ABC, 123, ?&!, etc.  The others are control characters such as carriage return, line feed, tab, etc Dr.T.Abirami/Department of IT 22 binary representation of a few characters in ASCII: 0100101 -> % (Percent Sign - 37) 1000001 -> A (Capital letter A - 65) 1000010 -> B (Capital letter B - 66) 1000011 -> C (Capital letter C - 67) 0001101 -> Carriage Return (13) ASCII was meant for English only. what about the others needing a completely different alphabet? Greek? Russian? Polish? Chinese and the likes?
  • 23.
    ASCII Extended -Unicode  Unicode Transformation Format  Encodings: UTF-7, UTF-8, UTF-16, and UTF-32  UTF-8 and UTF-16 are variable length encodings.  In UTF-8, a character may occupy a minimum of 8 bits.  In UTF-16, a character length starts with 16 bits.  UTF-32 is a fixed length encoding of 32 bits.  UTF-8 uses the ASCII set for the first 128 characters. - ASCII text is also valid in UTF-8. Dr.T.Abirami/Department of IT 23 Unicode represents most written languages in the world while ASCII does not.
  • 24.
    Char  char datatype is a single 16-bit Unicode character  Minimum value is 'u0000' (or 0)  Maximum value is 'uffff' (or 65,535 inclusive)  Char data type is used to store any character  Example: char letterA = 'A' Dr.T.Abirami/Department of IT 24
  • 25.
    Why char uses2 byte in java and what is u0000 ?  It is because java uses Unicode system not ASCII code system.  The u0000 is the lowest range of Unicode system. Dr.T.Abirami/Department of IT 25
  • 26.
    Characters  char myGrade= 'B'; System.out.println(myGrade);  char a = 65, b = 66, c = 67; System.out.println(a); System.out.println(b); System.out.println(c); 26 Dr.T.Abirami/Department of IT
  • 27.
    27 Char Numb er Description 0 -31 Control characters (see below) 32 spaceTry it ! 33 exclamation markTry it " 34 quotation markTry it # 35 number signTry it $ 36 dollar signTry it % 37 percent signTry it & 38 ampersandTry it ' 39 apostropheTry it ( 40 left parenthesisTry it ) 41 right parenthesisTry it A 65 uppercase ATry it B 66 uppercase BTry it C 67 uppercase CTry it D 68 uppercase DTry it E 69 uppercase ETry it F 70 uppercase FTry it G 71 uppercase GTry it H 72 uppercase HTry it I 73 uppercase ITry it J 74 uppercase JTry it K 75 uppercase KTry it L 76 uppercase LTry it M 77 uppercase MTry it N 78 uppercase NTry it O 79 uppercase OTry it P 80 uppercase PTry it Q 81 uppercase QTry it R 82 uppercase RTry it S 83 uppercase STry it T 84 uppercase TTry it U 85 uppercase UTry it V 86 uppercase VTry it W 87 uppercase WTry it X 88 uppercase XTry it Y 89 uppercase YTry it Z 90 uppercase ZTry it l 108 lowercase lTry it m 109 lowercase mTry it n 110 lowercase nTry it o 111 lowercase oTry it p 112 lowercase pTry it q 113 lowercase qTry it r 114 lowercase rTry it s 115 lowercase sTry it t 116 lowercase tTry it u 117 lowercase uTry it v 118 lowercase vTry it w 119 lowercase wTry it x 120 lowercase xTry it y 121 lowercase yTry it z 122 lowercase zTry it { 123 left curly braceTry it | 124 vertical barTry it } 125 right curly braceTry it ~ 126 tilde ASCII Printable Characters Dr.T.Abirami/Department of IT
  • 28.
    * 42 asteriskTryit + 43 plus signTry it , 44 commaTry it - 45 hyphenTry it . 46 periodTry it / 47 slashTry it 0 48 digit 0Try it 1 49 digit 1Try it 2 50 digit 2Try it 3 51 digit 3Try it 4 52 digit 4Try it 5 53 digit 5Try it 6 54 digit 6Try it 7 55 digit 7Try it 8 56 digit 8Try it 9 57 digit 9Try it : 58 colonTry it ; 59 semicolonTry it < 60 less-thanTry it = 61 equals-toTry it > 62 greater-thanTry it ? 63 question markTry it @ 64 at signTry it 28 [ 91 left square bracketTry it 92 backslashTry it ] 93 right square bracketTry it ^ 94 caretTry it _ 95 underscoreTry it ` 96 grave accentTry it a 97 lowercase aTry it b 98 lowercase bTry it c 99 lowercase cTry it d 100 lowercase dTry it e 101 lowercase eTry it f 102 lowercase fTry it g 103 lowercase gTry it h 104 lowercase hTry it i 105 lowercase iTry it j 106 lowercase jTry it k 107 lowercase kTry it ASCII Printable Characters Dr.T.Abirami/Department of IT