L5:Reference Data Types
Introduction to Data Types in Java
Primitive Data Types: Basic data types in Java are categorized as
numerical (integers and real numbers) and non-numerical (characters and
boolean).
Reference Data Types: These include objects and arrays which are not
covered in this session.
Numerical Data Types
1. Integer Types:
byte: 1 byte, range: -128 to 127
short: 2 bytes, range: -32,768 to 32,767
int: 4 bytes, range: -2^31 to 2^31-1
long: 8 bytes, range: -2^63 to 2^63-1 (must suffix with L or l )
2. Real Number Types:
float: 4 bytes, 6-7 decimal digits precision (suffix with f or F )
double: 8 bytes, 15-16 decimal digits precision (default for real
numbers)
Declaring and Using Variables
Syntax:
dataType variableName = value;
Example:
float myFloat = 12.34f;
double myDouble = 123.4567890123456;
L5:Reference Data Types 1
Float and Double
Float:
Uses 4 bytes.
Precision: Up to 6-7 decimal places.
Example:
float myFloat = 12.123456f;
System.out.println(myFloat); // Output: 12.123456
Double:
Uses 8 bytes.
Precision: Up to 15-16 decimal places.
Example:
double myDouble = 12.123456789012345;
System.out.println(myDouble); // Output: 12.12345678
9012345
Rounding Issues:
Floating-point arithmetic may introduce rounding errors.
Example:
float myFloat = 12.123456789f; // Actual stored valu
e might be slightly different
Printing with Precision
Using printf for formatted output:
Syntax: System.out.printf(formatString, arguments);
Format Specifiers:
%f for floating-point numbers.
%.nf for floating-point numbers with n decimal places.
L5:Reference Data Types 2
Example:
System.out.printf("%.2f", myFloat); // Output: 12.12
Boolean Data Type
Usage:
Represents true/false values.
Uses 1 bit of memory.
Example:
boolean isJavaFun = true;
boolean isFishTasty = false;
Character Data Type
Usage:
Represents a single 16-bit Unicode character.
Uses 2 bytes of memory.
Example:
char myChar = 'A';
char myCharNumeric = 65; // ASCII value of 'A'
ASCII Values:
Characters can be represented using ASCII values.
Example:
char myChar = 65; // A
char myDigit = 49; // 1
Practical Example
1. Float Example:
L5:Reference Data Types 3
float floatVar = 12.123456f;
System.out.println("Float value: " + floatVar); // Float
value: 12.123456
System.out.printf("Formatted float value: %.2f\\n", floa
tVar); // Formatted float value: 12.12
2. Double Example:
double doubleVar = 12.123456789012345;
System.out.println("Double value: " + doubleVar); // Dou
ble value: 12.123456789012345
System.out.printf("Formatted double value: %.5f\\n", dou
bleVar); // Formatted double value: 12.12346
3. Boolean Example:
boolean isJavaFun = true;
System.out.println("Is Java fun? " + isJavaFun); // Is J
ava fun? true
4. Character Example:
char charVar = 'A';
char charFromASCII = 65; // ASCII value for 'A'
System.out.println("Character: " + charVar); // Characte
r: A
System.out.println("Character from ASCII: " + charFromAS
CII); // Character from ASCII: A
Summary
Java provides various data types to handle different types of data
efficiently.
float and double handle real numbers with varying degrees of precision.
boolean represents true/false values and is used in decision-making.
char represents single characters using Unicode.
L5:Reference Data Types 4
Understanding and using these data types correctly is crucial for effective Java
programming.
L5:Reference Data Types 5