Starting with variables

Introduction to Java

Jim White

Java Developer

Variables are boxes

A box with books

Introduction to Java

Variables are boxes

A box with kitchen essentials

  • Allow to store different kinds of information
  • Have a type, a name, and a 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 ;
String message = "Hello, Java!";
Introduction to Java

Assigning variables - whole numbers

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

Single equal sign

  • Doesn't mean mathematical equal ❌

Mathematical equal

  • Assignment operator ✅

Putting books into the box

Introduction to Java

Declaring variables

  • Always specify the data type
    • Text -> String
    • Number -> int

$$

$$

$$

$$

  • Other data types: char, boolean, float, and more

Exclamation sign

Introduction to Java

Naming convention

  • Good variable names:
int age = 31;

String welcomeMessage = "Hello";

String daysToSummer = "Too many"; String myName = "Jim";

$$

  • Use camelCase: lowercase first word, capitalize next words
  • Bad variable names:
int 4age = 31;          

String welcome message = "Hello";

String days_to_summer = "Too many"; String MyName = "Jim";
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...