💻

Java Basics Overview

Jul 26, 2025

Overview

This lecture introduces Java programming basics, including data types, variables, methods, objects, classes, control flow, and how to use external code via APIs.

Setting Up Java

  • Use an IDE like Eclipse or IntelliJ to write, compile, and run Java code.
  • Create a new Java Project and Class, ensuring the main method is included for program execution.

Data Types and Variables

  • Primitive types include int (integer), char (character), long, and double.
  • Non-primitive types like String store sequences of characters.
  • Most statements end with a semicolon.

Storing and Using Data

  • Assign values to variables using = (e.g., int a = 5;).
  • String methods can be accessed using a dot (.), such as name.toUpperCase() or name.toLowerCase().

Methods

  • Methods perform actions; those with parentheses are usually methods.
  • Define a method with public static void methodName(parameters) {}.
  • Use return to send a value back from a method; change void to the return type.

Objects and Classes

  • A class is a blueprint for an object.
  • Create an object with ClassName objectName = new ClassName();.
  • Access object methods with the dot (.) notation (e.g., object.method()).

ArrayLists and Imports

  • ArrayList stores lists of items; use angle brackets for type (e.g., ArrayList<String>).
  • Import external classes using the import statement.

Control Flow: Logic and Loops

  • Use if, else if, and else for conditional logic.
  • Repeat code with for loops (for(int i = 0; i < n; i++)) and while loops.
  • ++ increases a variable by one.

Exceptions

  • Use try and catch blocks to handle errors; if an exception occurs, code in catch runs.

Using External APIs

  • APIs are libraries of pre-written code from others, imported via JAR files.
  • Add external JARs in project properties and import them to use in your code.

Key Terms & Definitions

  • Primitive type — Basic data type built into Java (e.g., int, char).
  • Variable — Named storage for data.
  • String — Object for text data.
  • Method — Block of code performing a task, possibly returning a value.
  • Object — Instance of a class.
  • Class — Blueprint for creating objects and methods.
  • ArrayList — Resizable array-like data structure.
  • Import — Statement to include external code.
  • API — Application Programming Interface, code library for reuse.

Action Items / Next Steps

  • Practice creating and running a Java project with basic variables and methods.
  • Experiment with control flow constructs (if, for, while).
  • Read about exception handling and importing external libraries.
  • Try using an ArrayList and one external API in a sample program.