JavaScript required
We’re sorry, but Coda doesn’t work properly without JavaScript enabled.
Skip to content
Gallery
Java Programming Fundamentals
Java Programming Fundamentals
More
Share
Explore
Java Variables and Data Types
Summary - Java Variables and Data Types
Variables in Java
What is a Variable?
A container for storing information, reducing redundancy in code.
Naming Conventions
Variables can start with an underscore (_), dollar sign ($), or letter, and include numbers, underscores, and dollar signs.
Cannot start with numbers, contain spaces, or be reserved keywords.
CamelCase Style
First word lowercase, subsequent words capitalized (e.g., sumOfNumbers).
Introduction to Data Types
Declaring and Initializing Variables
Declaration
: Specify data type and variable name.
Initialization
: Assign a value to the variable.
Primary Data Types
Integer
: Whole numbers (byte, short, int, long).
Floating Point
: Decimal numbers (float, double).
Character
: Symbols and letters (char).
Boolean
: True/false values (boolean).
Character & Boolean Data Types
Character (
char
)
Symbols and letters, enclosed in single quotes ('A').
Boolean (
boolean
)
True or false values.
Storing Char and Bool Data Types
ASCII and Unicode
ASCII
: Encodes characters in 7 or 8 bits, limited to 128 symbols.
Unicode
: Supports over a million characters, using 1 to 32 bits.
UTF-8
: Most common format, 1 to 4 bytes.
Boolean Values
Represented as 1 (true) and 0 (false).
Integer Data Type
Two’s Complement
Method for storing negative numbers in binary.
Floating Point Numbers
Basics
Float
: 4 bytes (32 bits), smaller decimals.
Double
: 8 bytes (64 bits), higher precision.
Common Mistakes with Float
Precision Issues
Floating-point numbers can't always be exactly represented in binary, causing precision errors.
Solution: Tolerance Value
Use a tolerance value (epsilon) for accurate comparisons.
Overflow and Underflow
Overflow
Value exceeds data type limit, wraps around to minimum value.
Underflow
Value goes below data type limit, wraps around to maximum value.
Real-World Example: YouTube's View Counter
Upgraded to a 64-bit integer to avoid overflow issues.
Understanding TypeCasting
Widening Type Casting
Automatic conversion to a higher data type.
Narrowing Type Casting
Manual conversion to a lower data type, potentially losing precision.
Representing Floating Point Numbers
Scientific Notation
Simplifies representation of large and small numbers using mantissa and exponent.
Normalization
Standardizes number representation for efficient storage and precise calculations.
Exponent Biasing
Converts signed exponents into an unsigned form for easier storage and computation.
Common Mistakes: With Float
Precision Issues
Floating-point arithmetic can result in rounding errors.
Solution: Tolerance Value
Use a tolerance value (epsilon) for accurate comparisons.
These concepts of Java variables and data types are very useful for understanding core Java. For more detailed information, refer to the Notion link:
Java Variables and Data Types
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
Ctrl
P
) instead.