Skip to content

Latest commit

 

History

History
230 lines (168 loc) · 6.56 KB

File metadata and controls

230 lines (168 loc) · 6.56 KB

Understanding Numeric Literals in Java 🔢

For Beginners: This guide simplifies a complex topic. We'll focus on what you need NOW and clearly mark what you can come back to later.

What You Need to Know Today (The 80/20 Rule)

In 90% of the code you'll write as a beginner, you only need two number types:

1. Whole Numbers → Use int

int age = 25;
int studentsInClass = 42;
int daysInWeek = 7;

2. Decimal Numbers → Use double

double price = 19.99;
double temperature = 36.5;
double piValue = 3.14159;

That's it! This covers most situations you'll encounter while learning.

The Simple Rule

  • Write a whole number like 42 → Java treats it as int
  • Write a decimal number like 3.14 → Java treats it as double

Common Beginner Mistake (And How to Avoid It)

// ❌ This won't work - can't fit a decimal into an integer type
double price = 19.99;
int dollars = price;  // COMPILE ERROR

// ✅ Fix #1: Keep it as double
double dollars = price;

// ✅ Fix #2: Convert to int (chops off decimal part)
int dollars = (int) price;  // dollars will be 19

Key insight: Java is strict about types. An int can only hold whole numbers, a double can hold decimals.


What You Can SKIP For Now ⏭️

The following topics are in your course material but are NOT essential for beginners. You can safely skip them and come back when you encounter them in real code:

Skip These (For Now):

byte and short types

  • When you'll need it: Memory optimization in Android apps or embedded systems
  • Why skip now: int works fine for learning

long with the L suffix

long bigNumber = 9223372036854775807L;  // Notice the 'L' at the end
  • When you'll need it: Working with timestamps, file sizes, or database IDs
  • Why skip now: Regular int handles numbers up to about 2 billion - plenty for practice

float with the F suffix

float smallDecimal = 3.14f;  // Notice the 'f' at the end
  • When you'll need it: GPU programming, game development, or when a library requires it
  • Why skip now: double is almost always the better choice

❌ Hexadecimal (0x), Octal (0), Binary (0b) notation

int hexColor = 0xFF5733;    // Hexadecimal
int octalValue = 027;       // Octal (note the leading zero!)
int binaryValue = 0b101010; // Binary
  • When you'll need it: Colors (Android/web), bit manipulation, system programming
  • Why skip now: You'll recognize the pattern when you see it in real code

❌ Scientific notation

double scientificNum = 1.5e-4;  // = 0.00015
  • When you'll need it: Scientific computing, very large or small numbers
  • Why skip now: Not common in everyday programming

❌ Underscores for readability

int million = 1_000_000;  // Same as 1000000
  • When you'll need it: Large numbers in production code
  • Why skip now: Nice feature, but not essential for learning

When Will I Actually Use These? 🤔

Don't worry - these aren't useless! Here's when they become relevant:

Real-World Example 1: Timestamps

// Working with dates/times in milliseconds
long timestamp = 1707753600000L;  // Needs 'L' because it exceeds int range

You'll see this in: backend APIs, database timestamps, date handling

Real-World Example 2: Color Values

// Android or web development
int backgroundColor = 0xFF5733;  // Orange color in hexadecimal

You'll see this in: UI development, graphics programming

Real-World Example 3: File Sizes

// Working with large files
long fileSize = 5368709120L;  // 5GB in bytes

You'll see this in: file handling, cloud storage apps

Real-World Example 4: Memory Optimization

// Only when memory is critical (rare for beginners)
byte smallNumber = 127;  // Uses 1 byte instead of 4 bytes (int)

You'll see this in: embedded systems, Android resource optimization, network protocols


One Important Warning ⚠️

Watch Out for Leading Zeros!

int decimal = 27;   // This is 27 (what you expect)
int octal = 027;    // This is 23! (Octal notation)

Rule: Never put a leading zero on a number unless you specifically mean to use octal notation (which you probably don't).


Practice Exercises 💪

Try these simple examples to get comfortable:

Exercise 1: Basic Types

// What type should each variable be?
___ numberOfStudents = 30;
___ averageGrade = 87.5;
___ piValue = 3.14159;
___ daysInYear = 365;
Click to see answers
int numberOfStudents = 30;
double averageGrade = 87.5;
double piValue = 3.14159;
int daysInYear = 365;

Exercise 2: Spot the Error

// Which lines will cause compile errors?
int count = 100;           // Line 1
double price = 29.99;      // Line 2
int total = price;         // Line 3
double average = 85;       // Line 4
Click to see answer

Line 3 will cause an error: can't assign double to int without casting.

Line 4 is FINE: Java automatically converts int to double (widening conversion).


Summary: Your Cheat Sheet 📋

For 99% of your learning:

  • Whole numbers → int
  • Decimals → double
  • That's it!

Remember:

  • Java won't let you mix types without being explicit
  • int can become double automatically
  • double to int needs a cast: (int) myDouble

When you see weird notation:

  • 0x... = hexadecimal (colors, low-level code)
  • 0b... = binary (bit manipulation)
  • Leading 0 = octal (almost never used intentionally)
  • ...L = long (big numbers)
  • ...f = float (GPU/graphics work)

Pro tip: Don't try to memorize all the types. Learn int and double thoroughly. When you encounter others in real code, you'll understand why they exist.


Questions or Confused? 🤝

Remember: This is complex stuff even for experienced developers! The course material covers many edge cases that you won't use for months (or years).

Focus on understanding int and double first. Everything else can wait.

Post your questions in the #java-bootcamp Slack channel - we're building this together! 🚀


Additional Resources


This guide is part of the WCC Java Bootcamp

Licensed under MIT License - feel free to share and adapt!