1. Variables and Data Types
Variables are one of the most basic elements of programming. However, they are also one of the most powerful.
Here is an example of a variable :
String myVariable = "Hello, World!" System.out.println(myVariable);
If you want to run this code, don’t forget to put it inside a method. The full code to run this actually looks like this (this file has to be named Main.java
– just set the class name to Main
when you create the file) :
public class Main { public static void main(String[] args) { String myVariable = "Hello, World!"; System.out.println(myVariable); } }
Here, myVariable
is a variable.
You can think of variables as labelled boxes. Each box has its own name and can hold only a specific type of data – in this case the box is labelled myVariable
and can hold String
s.
Here is how you declare a variable :
[Type] [Name] ( = [Value]);
Type
is what kind of data this variable will hold – a data type. In our case, we set this to String
, so our variable will hold String
s.
Name
is the name of our variable – the label that will go on the box. In our case, we set this to myVariable
so the content of the box can be accessed by using that name.
Value
is optional – you don’t need to set one. In our case, we set our variable to have the value "Hello, World!"
, but this is also valid :
String myVariable; myVariable = "Hello, World!"; System.out.println(myVariable);
Here you can see that we don’t give a value to myVariable
straight away. You can also see that once a variable has been declared there is no need to put the type again – when we actually set myVariable
we don’t have to tell it again that it is a String
.
By default, variables without a value are given the value null
. null
in Java just means “nothing” or “empty”. So String myVariable;
is the same as String myVariable = null;
2. Data types
There are two different kinds of data types in Java : primitive data types and non-primitive data types.
Here are all the primitive data types and what they are:
int
– Short for integer, this data type holds whole numbers.int test = 0;
float
– This data type holds floating-point numbers.float test = 0.5f;
– Notice thef
at the end to inform java that this is a float.
double
– This data type also holds floating-point numbers. However, it uses more bytes behind the scenes to represent its value than afloat
(this makes it more precise and gives it a bigger range of possible values).double test = 0.5;
double test = 0.5d;
– In this case thed
is optional.
long
– This data type also holds whole number. However, it uses more bytes behind the scenes to represent its value than anint
(this gives it a bigger range of possible values).long test = 15L;
– Not theL
at the end to inform Java that this is a long.
short
– This data type holds whole numbers. However, it uses less bytes behind the scenes to represent its value than anint
(this makes it more memory-efficient).short test = 15;
char
– Short for character, this data type holds single letters.char test = 'a';
– Notice the use of single quotes ('
). In java, double quotes ("
) are used to create aString
and single quotes to create achar
.
byte
– This data type holds a single byte.byte test = 0x00000000;
– Initializes the byte with a value of binary00000000
. Binary is a base-2 number system, which means each digit is either0
or1
.00000000
in binary is0
in decimal.
boolean
– This data type holds a boolean. A boolean is eithertrue
orfalse
.boolean test = true;
Primitive data types are special in the fact that they cannot be null
. You don’t need to remember all of them, the ones you will be using most often are int
, float
, boolean
and char
.
Non-primitive data types are anything else – these can be String
s, File
s, List
s or even custom data types, which we’ll get on to in a couple of posts.
3. Rules for variables and scope
There are a few rules that variables have to follow, and your code won’t work if these rules are not meant so be careful.
- You cannot have two variables with the same name in the same scope
OK, well that seems obvious. But what do we mean by scope?
String s = "Hello, World!"; String s = "Hi there!";
This is invalid code. We are declaring the variable s
twice,so this would cause an exception.
if (test) { String s = "test is true!"; } else { String s = "test is false!"; }
Notice the use of the if
and else
keywords here – we’ll have a look at those next time. Here I am just using them
This, however, is valid Java code. Even though we define the variable s
twice, we don’t do it in the same scope.
To put it simply, a variable’s scope is just the area of code from which it can be accessed and is generally just the code inside the curly brackets {
}
that surround the block of code that the variable was declared in.
Here are a few examples to get your head around it, as this can be complicated :
public static void main(String[] args) { String s = "test"; // System.out.println("Hi there!"); // System.out.println("I have a variable named s");// System.out.println("Here is its value: "); // System.out.println(s); // }
All the lines that end with //
are inside s
‘s scope
public static void main(String[] args) { String s = "test"; // System.out.println("Hi there!"); // System.out.println("I have a variable named s"); // System.out.println("Here is its value: "); // System.out.println(s); // System.out.println("I'm going to do an if statement now!");// if (args.length == 0) { // System.out.println("No args"); // } else { // System.out.println("Args!"); // } // }
As you can see, scope is restricted by the brackets surrounding the variables, but other brackets inside those brackets are fair game, which means that this is still invalid code :
String s = "test"; if (test) { String s = "test2"; //This will cause an error, because the variable we declared above is already in this scope }
Now you know what scope is, we can move on to the next rule :
- You cannot change a variables type once you’ve declared it in a specific scope
String s = "test"; int s = 0;
This too is invalid code. We declared s
as a string but we try to change it to an int
, which we can’t do.
- You cannot access variables outside of their scope
if (test) { String s = "Hello!"; } System.out.println(s);
This is also invalid code, because s
‘s scope ends at the closing bracket, but we try to access it after the bracket.
And that’s all for this time! Next time we will look at if statements and loops.