Java Variables Explained


We'll break down one line of code to learn how a variable is declared, named, and assigned. We'll also get introduced to a data type known as "String", which is used to represent text data in Java.

Video Transcript

  • 0:00

    What you just typed out was a variable declaration and assignment.

  • 0:03

    In Java, you can think of a variable as a container

  • 0:06

    that you can name and store data in.

  • 0:08

    Variables are common to almost every programming language,

  • 0:11

    because it makes it easy to hold on to data

  • 0:13

    that you want to reuse later.

  • 0:15

    If you want to display a piece of data in multiple places,

  • 0:17

    variables make it easy,

  • 0:19

    because you only have to set the data in 1 place.

  • 0:22

    It also makes it really easy to change the data,

  • 0:24

    because we only have to change it in 1 place.

  • 0:26

    And then that change is reflected throughout the code.

  • 0:29

    The 1st word in this line of codes, string,

  • 0:31

    is the data type of the variable,

  • 0:33

    and it tells us what kind of data will be stored in the variable.

  • 0:35

    We'll look at a lot of different data types,

  • 0:38

    like numbers, yes-or-no values, and dates,

  • 0:40

    and in each case, there's a special key word like string

  • 0:43

    that tells the computer what kind of data to expect.

  • 0:45

    The second word is the name of the variable.

  • 0:48

    We can name variables whatever we want

  • 0:50

    according to a few basic rules:

  • 0:52

    Use letters and numbers, don't start with a number,

  • 0:55

    and don't use any special characters besides underscores.

  • 0:58

    Okay, so far, we have a variable named my name

  • 1:00

    that we set to hold string data,

  • 1:02

    so let's put a string in it.

  • 1:04

    In Java, as in many programming languages,

  • 1:07

    strings are represented as text surrounded by double quotes.

  • 1:11

    In this case, the text is my name,

  • 1:13

    and I make it a string by adding the quotes around it.

  • 1:16

    We then use the equal sign

  • 1:18

    to put the string into the variable, or in other words,

  • 1:20

    set my name equal to Ben.

  • 1:23

    Now the next thing you'll notice in this line is the semicolon.

  • 1:26

    Semicolons in Java are kind of like periods for sentences.

  • 1:30

    We use them to mark when we're done saying something.

  • 1:33

    Each statement or command in Java

  • 1:35

    must always end with a semicolon.

  • 1:37

    That tells the computer that we're done with this step

  • 1:39

    and ready to move on to the next one.

  • 1:42

    I think you're ready for a new code challenge.

  • 1:44

    Fix this line to set the favorite color variable

  • 1:46

    to your favorite color.

  • 1:48

    You might have to move some things around and add or change some text.

Show full transcript


You have to sign up for Treehouse in order to create workspaces.

Sign up


You have to sign up for Treehouse in order to download course videos.

Sign up


  • Ben Jakuben

    Ben is a father, husband, son, friend, and teacher. He is firmly committed to the belief that the world is evolving to a better place. Find him on Twitter @benjakuben.