ЁЯТ╗

Java Basics Overview and Exercises

Sep 8, 2024

Apna College Java Basics Lecture

Topics Covered

  • Printing Output in Java
  • Variables in Java
  • Input from the User
  • Data Types

Printing Output in Java

  • Basic Command: System.out.print() or System.out.println()
    • print() keeps the cursor on the same line
    • println() moves the cursor to the next line after printing
  • Example: System.out.println("Hello World");
  • Character Escape for New Line: \n
    • Can be used within strings to move to the next line

Variables in Java

  • Definition: Containers for storing data values
  • Examples:
    • int a = 25;
    • String name = "Tony Stark";
  • Variable Characteristics:
    • Variables are defined with a type (e.g., int, String)
    • Their values can change over time

Data Types

  • Primitive Data Types:
    • byte, short, int, long (for integers)
    • float, double (for floating-point numbers)
    • char (for characters)
    • boolean (for true/false)
  • Non-Primitive Data Types:
    • Customizable, includes Strings, Arrays, etc.
  • Memory Allocation:
    • Different data types occupy different sizes in memory

Input from the User

  • Using Scanner Class:
    • Import with import java.util.Scanner;
    • Create scanner object: Scanner sc = new Scanner(System.in);
    • Different methods for input:
      • sc.nextInt() for integers
      • sc.nextLine() for strings

Key Concepts and Examples

  • Variable Operations:
    • Arithmetic operations can be performed between variables
    • Example: int sum = a + b;
  • Output Examples and Exercises:
    • Printing stars in a pattern
    • Calculating arithmetic expressions
  • Importance of Fundamentals:
    • Understanding basic concepts is crucial for solving more complex problems later

Exercises

  • Write a program to print a pattern of stars
  • Calculate sum, difference, and products of two numbers
  • Quiz on evaluating expressions with operator precedence

Next Steps

  • Further exploration of advanced topics such as loops, data structures
  • Practice with more complex coding exercises

Additional Resources

  • Refer to lecture notes for extended concepts and additional exercises