Lesson 04 Data Types Variables and Arrays Data

Lesson 04 Data Types, Variables and Arrays

Data types § Java defines eight simple types of data: 1. 2. 3. 4. 5. 6. 7. 8. byte short int long float double char boolean 2

Data types § Integer Types 1. 2. 3. 4. § byte short integer long Floating Point Types 5. float 6. double § Characters Types 7. char § Boolean Types 8. boolean 3

Integer Types

Numeric Data Types § Numeric primitive data types and their size: Type Storage Min Value Max Value byte short int long 8 bits 16 bits 32 bits 64 bits -128 -32, 768 -2, 147, 483, 648 < -9 x 1018 127 32, 767 2, 147, 483, 647 > 9 x 1018 float double 32 bits 64 bits +/- 3. 4 x 1038 with 7 significant digits +/- 1. 7 x 10308 with 15 significant digits

1. Byte § Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int. § Space: 1 Byte § Range from -128 to 127 § Default value zero (0) § E. g § Byte c, d; 6
![1. Byte public class Type_Byte { public static void main(String[] args) { //Byte values 1. Byte public class Type_Byte { public static void main(String[] args) { //Byte values](http://slidetodoc.com/presentation_image_h2/3ac5fb67e515806e780600de314c2922/image-7.jpg)
1. Byte public class Type_Byte { public static void main(String[] args) { //Byte values byte b 1 = 0 x. D; System. out. println(b 1); byte b 2 = 127; System. out. println(b 2); byte b 3 = -128; System. out. println(b 3); } } Output: 13 127 -128 7

2. Short § It occupies 16 bit and rarely used in java § It has a range from – 32, 768 to 32, 767. § Least used java type. § E. g § short s = 10000, short r = -20000; 8
![2. Short public class Type_short { public static void main(String[] args) { //Type short 2. Short public class Type_short { public static void main(String[] args) { //Type short](http://slidetodoc.com/presentation_image_h2/3ac5fb67e515806e780600de314c2922/image-9.jpg)
2. Short public class Type_short { public static void main(String[] args) { //Type short (2 Bytes) short s 1 = 28; short s 2 = 0 x. FF; //hexadecimal short s 3 = 065; // octal (at start 0 (zero) for octal) System. out. println("s 1= "+s 1+" s 2= "+s 2+" s 3= "+s 3); } } Output: s 1= 28 s 2= 255 s 3= 53 9
- Slides: 9