Chapter Table of Contents

Ch. 2 Output

Section Table of Contents

What is a String?

What are strings? Well, you've actually been using them! A string is pretty much a collection of characters. We can think of them as sentences, words or phrases.

A string is also a type of variable. This is probably the first time you've heard of a variable. Imagine them as little packets of information.

Programming is all about storing, processing, and sharing data, whether it's between a user or to the printer. The way a computer remembers and keeps track of all these pieces of data is by storing them in variables.

If you wanted to store something, such as your name, in a variable, it would be of type String. This is where the syntax comes into play. In Java you have to tell the computer what sort of data the computer is going to store.

Why Variables?

Watch this video for an introduction to what variables are and why they’re useful. You should stop watching at 0:32, since it gets into syntax at that point.

https://www.youtube.com/watch?v=Txs3MHINsjI

Declaration, Initialization, and Assignment of Variables

Before we dive into variables, let’s get some terms out of the way.

Declaration is when you tell the computer that the variable exists. In Java, you need to tell the computer what the type and name of the variable is. For example:

String name;

String is the type of the variable, which goes before the name of the variable (which is name).

Initialization is when you give a declared variable a value for the first time. For example, the 2nd line initializes the variable name:

String name;
name = "Kaz";
//When initializing strings, you must use quotation-marks ""

Note that once a variable is declared, you cannot give it a value that doesn’t match the declared type. For example, you will later learn that there is a type called int which is for integers. You cannot do something like the code below because 5 is not a String.