450 likes | 976 Views
Java Data types and Conversions. By Srinivas Reddy.S. www.java9s.com. Primitive Data Types. E.g. 10 -15 30000 555 4567. byte short int long. www.java9s.com. Primitive Data Types. 25.6 44.567 -23.46. float double. ‘a’ ‘u0000 ’. char boolean. true / false. www.java9s.com.
E N D
Java Data types and Conversions By SrinivasReddy.S www.java9s.com
Primitive Data Types E.g. 10 -15 30000 555 4567 byte short int long www.java9s.com
Primitive Data Types 25.6 44.567 -23.46 • float • double ‘a’ ‘\u0000’ • char • boolean true / false www.java9s.com
Declaration • byte a = 20; • short b = 22; • int c = 34567; • long l = 34905857; • float e = 324.5f; / 324.5F; • double g = 2234.5; / 2234.5d; / 2234.5D; • char h = ‘a’; • boolean = true; / false; www.java9s.com
Memory Allocation -Ve +Ve • byte – 8bits – 28 - 256 -128 -1 |0 - 127 128 128 • short – 16 bits – 216 - 65536 -32768 -1 | 0-32767 • int – 32 bits – 232 - 4,294,967,296 • long – 64 bits – 264 - 18,446,744,073,709,551,616 www.java9s.com
Memory Allocation • float – 32 bits • double – 64 bits • char – 16 bits - Unicode • boolean – JVM Dependent www.java9s.com
Understanding the Storage limit Ariane 5 www.java9s.com
Default values • byte • short • int • long • float • double • char - ‘\u0000’ • boolean - false 0 0.0 www.java9s.com
Type Conversion • byte short int long (implicitly) • byteshortintlong (Explicit casting) • float double (implicit) • floatdouble (explicit) • boolean(Cannot be casted) www.java9s.com
Type Casting Implicit Casting byte c = 120; short d = c; Explicit Casting short c = 234; byte d = (byte)c; (We need to explicitly mention to cast) www.java9s.com
Representing +ve and –ve numbers • byte b = 120 • b 01111000 (8 bits) • byte c = -120 • Twos complement • Flip all the bytes • Add 1 • 120 01111000 • Flip 10000111 • Add 1 1 • -120 10001000 • If you have 1 at the MSB, then it will be interpreted as a negative number. www.java9s.com
WWW.JAVA9S.COM www.java9s.com