Lesson Completion
Back to course

Module 02: Java Basics

Overview

This module covers the fundamental building blocks of Java programming including data types, variables, constants, literals, and basic input/output operations.

Learning Objectives

  • Understand different data types in Java
  • Learn about variables and constants
  • Master type conversion and casting
  • Work with literals and keywords
  • Perform basic input/output operations
  • Understand scope and lifetime of variables

📚 Lessons in This Module

2.1 Identifiers and Keywords

Learn about naming rules, Java keywords, and best practices for identifiers.

  • What are Identifiers?
  • Rules for Naming Identifiers
  • Reserved Keywords in Java
  • Contextual Keywords
  • Best Practices for Naming

2.2 Data Types

Master all primitive and non-primitive data types in Java.

  • 8 Primitive Data Types (byte, short, int, long, float, double, char, boolean)
  • Non-Primitive Types (String, Arrays, Classes, Interfaces)
  • Choosing the Right Data Type
  • Default Values

2.3 Variables

Understand variables, their types, scope, and lifetime.

  • Variable Declaration and Initialization
  • Local Variables
  • Instance Variables
  • Static Variables
  • Variable Naming Conventions

2.4 Constants and Final Keyword

Learn how to create immutable values using the final keyword.

  • Declaring Constants
  • final Keyword Usage
  • Static Final Variables
  • Naming Conventions (UPPER_SNAKE_CASE)
  • Benefits of Using Constants
  • Blank Final Variables

2.5 Literals

Master all types of literals used in Java programming.

  • Integer Literals (Decimal, Binary, Octal, Hexadecimal)
  • Floating-Point Literals (float, double, scientific notation)
  • Character Literals (basic chars, escape sequences, unicode)
  • String Literals (basic strings, text blocks)
  • Boolean Literals (true/false)
  • Null Literal
  • Underscore in Numeric Literals for Readability

2.6 Type Conversion and Casting

Understand how to convert between different data types.

  • Implicit Type Conversion (Widening)
  • Explicit Type Conversion (Narrowing/Casting)
  • Type Promotion in Expressions
  • String to Primitive Conversions
  • Primitive to String Conversions
  • Data Loss and Overflow Issues
  • Explicit Type Conversion (Narrowing)
    • Type Casting Syntax
    • Data Loss
  • Type Promotion in Expressions
  • Wrapper Class Conversion

2.7 Input and Output

  • Output Operations
    • System.out.print()
    • System.out.println()
    • System.out.printf()
    • Formatted Output
  • Input Operations
    • Scanner Class
    • BufferedReader Class
    • Console Class
    • Reading Different Data Types
  • Command Line Arguments

2.8 Comments

  • Single-Line Comments (//)
  • Multi-Line Comments (/* */)
  • Documentation Comments (/** */)
  • Best Practices for Comments
  • Javadoc Comments

2.9 Operators Introduction

  • What are Operators?
  • Types of Operators Overview
  • Operands
  • Expression Evaluation

2.10 Variable Scope and Lifetime

  • Local Scope
  • Class Scope

2.7 Input and Output

Learn how to read user input and display formatted output.

  • Output Methods (print, println, printf)
  • Format Specifiers and Formatting
  • Scanner Class for Input
  • BufferedReader Class
  • Console Class
  • Reading Different Data Types
  • Common Input/Output Patterns

2.8 Comments in Java

Master the art of code documentation and commenting.

  • Single-Line Comments (//)
  • Multi-Line Comments (/* */)
  • Javadoc Comments (/** */)
  • Best Practices for Writing Comments
  • Generating Documentation with Javadoc
  • When and How to Comment

2.9 Operators Introduction

(Coming Soon)

  • Types of Operators
  • Operator Precedence
  • Basic Arithmetic Operations

2.10 Variable Scope and Lifetime

(Coming Soon)

  • Block Scope
  • Variable Shadowing
  • Memory Allocation and Deallocation

Hands-on Exercises

  1. Declare and initialize variables of all primitive types
  2. Perform type conversions and observe data loss
  3. Create a program to read user input using Scanner
  4. Write a program using all types of literals
  5. Demonstrate variable scope with examples
  6. Create a formatted output program (receipt/invoice)
  7. Build a simple calculator using Scanner and printf
  8. Practice with different number systems (binary, octal, hexadecimal)
  9. Create a student registration form with input validation
  10. Write well-documented code using all comment types

Key Takeaways

  • Java has 8 primitive data types with specific sizes and ranges
  • Variables can be local, instance, or static
  • Constants use the final keyword and follow UPPER_SNAKE_CASE naming
  • Literals represent fixed values directly in code
  • Type casting is necessary for narrowing conversions
  • Scanner class is commonly used for user input
  • printf() provides powerful formatted output with format specifiers
  • Comments improve code readability and maintainability
  • Javadoc generates professional API documentation
  • Proper naming conventions improve code readability

Common Mistakes to Avoid

  • Using undeclared variables
  • Implicit narrowing without casting
  • Ignoring data overflow when casting
  • Poor variable naming
  • Not closing Scanner/BufferedReader objects
  • Forgetting to consume newline after nextInt()
  • Using double quotes for char literals
  • Forgetting 'f' suffix for float literals
  • Writing obvious or outdated comments
  • Over-commenting self-explanatory code

Additional Resources

  • Java Language Specification - Data Types
  • Oracle Java Tutorials - Variables
  • Effective Java - Item on Constants
  • Java I/O Documentation
  • Javadoc Tool Documentation

Assessment

  • Quiz on data types, variables, and literals
  • Practical: Create a student information input program with formatted output
  • Debug exercises with type conversion errors
  • Write and document a utility class with Javadoc
  • Build a calculator with multiple operations

Previous Module

Module 01: Introduction to Java

Next Module

Module 03: Operators and Expressions