Starting with variables

Introduction to Java

Jim White

Java Developer

Variables are boxes

A box labeled as Books, equivalent to a variable

Introduction to Java

Boxes store different information

A second box labeled Kitchen

Introduction to Java

Variables have type, name, and value

Every variable has a type, name, and value

Introduction to Java

Assigning variables - String keyword

String

Components:

  • String keyword
Introduction to Java

Assigning variables - name

String variableName 

Components:

  • String keyword
  • Variable name
Introduction to Java

Assigning variables - syntax

String variableName = 

Components:

  • String keyword
  • Variable name
  • = to assign value
Introduction to Java

Assigning variables - text

String variableName = "text in double quotation marks";

Components:

  • String keyword
  • Variable name
  • = to assign value
  • The actual value
  • Semicolon ;
// Example
String message = "Hello, Java!";
Introduction to Java

Assigning variables - whole numbers

  • Use int for whole numbers
int age = 29;
Introduction to Java

Equal sign doesn't mean equal

Single equal sign is an assignment operator

Introduction to Java

Variables have a type

We need to specify a type when declaring a variable, such as String for a text variable

Introduction to Java

Naming convention

Good variable names use CamelCase

Introduction to Java

Using variables

class InfoAboutJava {
  public static void main(String[] args) {

    String message = "Java is awesome!";

    // Print the message variable
    System.out.println(message);
    }
}

Java is awesome!
Introduction to Java

Summary

  • Variables store values
  • type variableName = value;
  • Variable names:
    • Start with letter
    • Follow camelCase
    • Cannot contain space
  • Use variables instead of their value

    class InfoAboutJava {
    public static void main(String[] args) {  
    
      String message = "Java is awesome!";
    
      // Using variable directly
      System.out.println(message);
      }
    }
    
Introduction to Java

Let's practice!

Introduction to Java

Preparing Video For Download...