Full Transcript

IT2402 Data Types Data encoded to a program is stored in memory and can have and be in several types—for example, using a person’s basic information such as name, age, and address. The name will be stored as characters, the age will be stored as a num...

IT2402 Data Types Data encoded to a program is stored in memory and can have and be in several types—for example, using a person’s basic information such as name, age, and address. The name will be stored as characters, the age will be stored as a numeric value, and the address will be stored as an alphanumeric value. Data Types describe possible operations on the data and the storage method. The data types in Java are classified into two (2): primitive or standard data types and abstract or derived data types. Primitive Data Types (Agarwal & Bansal, 2023) Primitive Data Types are built into the Java language. These are data that are not objects and have no methods. The Java compiler has detailed instructions on the valid operations this data type supports. Here are the eight (8) Primitive types that Java defines. Data Type Description Size Default Value Range Byte Byte Length Integer 8 bit 0 -128 to +127 signed Short Short Integer 16 bit 0 -32768 to +32767 Int Integer (number) 32 bit 0 -2,147,483,648 to 2,147,484 Long Long Integer 64 bit 0L -263 to 263-1 Float Single Precision Floating Point 32 bit 0.0f +1-about 1039 Double Double-Precision Floating Point 64 bit 0.0d 1-about 10317 Char A simple character 8 bit \u0000 0 to 65535 Boolean A Boolean (true or false) 1 bit false - Table 1. Data types Integer Types Java defines Byte, Short, Int, and Long as Integer data types. Since Java does not support unsigned integers, these four (4) are signed, positive, and negative values. Java does not support unsigned integers as these are mostly used to specify the behaviors of the high-order-bit. High-order bit defines the sign of an integer value, and Java manages it differently by adding a special “unsigned right shift” operation. a. Byte: The smallest integer type that represents a small number. It is used to declare a variable holding a natural number between -128 and 127. Byte variables are declared by use of the byte keyword. Syntax: byte b; b. Short: An integer is considered Short if its value is between -32768 and 32767. Short data types declare a variable that can hold numbers in that range. Syntax: short a; For example: public class ShortSample { public static void main(String[] args){ short total_stu = 1192; System.out.println("The total number of students are " + total_stu); } } A hexadecimal number can also be initialized as a Short variable instead of a normal decimal integer. So, the 3001 integer can be written as 0x4A8. 0x is used in Java to represent hexadecimal. 03 Handout 1 *Property of STI  [email protected] Page 1 of 6 IT2402 c. Int: This data type can handle larger numbers and is considered the best choice when an integer is needed because when Byte and Short values are used in an expression, they are promoted to Int automatically. For example: public class IntSample { public static void main(String[]args) { int days = 279; System.out.println("The days are " + days); } } d. Long: This data type holds a number higher than a regular integer. The Long integer is a variable that can hold a very large number. A capital letter ‘L’ is placed right after the value. For example: public class LongSample { public static void main(String[] args) { long distance = 84564L; System.out.println("The distance is " + distance); } } Floating Point Types Floating points are also called “real numbers”, and are used when evaluating an expression that needs fractional accuracy such as the computation of square root or a transcendental (sine and cosine). Float and Double are both floating-point types. a. Float is described as a single precision floating point. A real number qualifies as a single precession when it only needs a limited number of bits. Single precision takes half the space of a double precision and is much faster on some processors. Floats are used to represent dollars and cents, so a small letter ‘f’ is placed to the right of the value when initializing the variable. For example: public class FloatSample { public static void main(String[] args) { float pi = 3.14f; System.out.println("The value of pi is " + pi); } } b. Double: Double precision can be faster than single precision on modern processors that have been optimized for high-speed mathematical calculations. It can handle larger numbers than Float. For example: public class DoubleSample { public static void main(String[] args) { double pi = 3.141590; // a d/D suffix is optional. System.out.println(pi); } } 03 Handout 1 *Property of STI  [email protected] Page 2 of 6 IT2402 Character Types Character types can be divided into two (2): (a) A Byte for a character (ASCII) and (b) a type in itself (Unicode). a. A character is recognized by its ASCII (American Standard Code for Information Interchange – a code that assigns numbers to letters, digits, etc.) number. It allows Byte data types to be used to declare a variable that would hold a single character to initialize such variable or assign a single quoted character. For example: public class CharByte { public static void main(String[] arg) { byte Gender = 'F'; System.out.println("Gender is " + Gender); } } b. To support characters that are not traditionally used in (US) English and do not work with compilers, the Unicode character format was created which includes Latin-based and non-Latin-based languages. The Char data type was created to support Unicode characters in Java. To initialize, assign the character variable in single quotes. For example: public class CharUnicode{ public static void main(String a[]) { char gender ='f'; System.out.println("Gender is " + gender); } } Boolean This data type is used for logical values and can only hold true or false values. It is a type returned by all relational operators such as =, >, and

Use Quizgecko on...
Browser
Browser