String playerName; (1)
playerName = "Bernard B"; (2)
Clément Levallois
It is useful to learn about coding by reading a lesson like this one, but it is more efficient if you can practice while you read.
Indeed, a key competency in coding is to learn the simple discipline of writing text carefully:
not forgetting a ;
at the end of the line
not forgetting to put a capitalized letter when it is necessary
learning how to spot opening accolades {
and closing accolades }
not confusing when to use commas ,
and semi-colons ;
, knowning wen to put double quotes ""
or not…
It is all very trivial, right?
And yet this is what needs the most practice, at the start.
So in this lesson, you will see plenty of interactive screens like this one:
"Interactive screen" means that you can write directly on it and see your code in action - yes, right here: click next to "java", a cursor will blink to show that you can write:
Last note before we start: this lesson is on the essentials of coding, so we go fast and discuss just the most important notions of coding.
If you are interested in (much) longer, more thorough approaches, you can have a look at these two interactive courses:
"Think Java" by Tinklet (Java is the same programmming language as the one we use here)
"Python for Everybody" also by Tinklet (Python is the most popular programming language for data science)
If we compare coding to cooking, you could say that variables
are the ingredients of the recipe.
→ variables are a way to create, define and store all the information we need in our mobile app.
What kind of information do we need to store?
if you create a gaming app, you might need to store the name of the player, the score, the energy level, etc…
if you create an app selling a service, you might need to store a login name, a password, a list of products and their prices…
As you see, variables can be of different types: text, numbers, numbers with decimals, …
You can create all the kinds of variables you want, but to help you, a number of them are already predefined because they are so common: |
A variable of type "String" specializes in storing textual information. It is created and defined this way:
String playerName; (1)
playerName = "Bernard B"; (2)
1 | We create the variable (this needs to be done just once) |
2 | A value is assigned to the variable (this can be done as many times as we need) |
There is a shortcut if you want to create a variable and give it a value, just in one line of code:
String playerName = "Bernard B";
So this single line of code creates a variable called playerName
, and I immediately give it a value: Bernard B
.
This will be handy when we need to show the player’s name on the screen of the app: we will just use the variable player
, and what ever value in it (the player’s name) will be shown.
Your turn!
Create a variable called "favoriteMovie" and store the name of your favorite movie in it
Here is the solution:
A variable of type "Integer" specializes in storing round numbers (like 1, 2, 3…). It is created and defined this way:
Integer playerAge; (1)
playerAge = 22; (2)
1 | We create the variable (this needs to be done just once) |
2 | A value is assigned to the variable (this can be done as many times as we need) |
To repeat: we did not put double quotes around 22! Double quotes " "
are only for String
variables!
There is the same shortcut as usual if you want to create a variable and give it a value, just in one line of code:
Integer playerName = 18;
So this single line of code creates a variable called playerAge
, and I immediately give it a value: 22
.
There is another way to use variables for round numbers:
int playerName = 18; (1)
1 | Integer has been replaced by int . Both are the same, except that int takes even less space in the memory of your program. |
Your turn!
Create an Integer variable called score
and store 999999
in it.
Create a variable called purchasedItems
, using int
instead of Integer
. Store 4
in it.
Here is the solution:
Variables of type "Float" and "Double" specialize in storing decimal numbers (like 1.4533).
The difference between Float and Double? Double can store even more decimals than Float.
Variable of type "Long" specializes in storing loooong numbers (like 9395353439449039035353). It is useful when you need to count milliseconds, for example.
These are created and defined this way:
Float averagePrice;
averagePrice = 15.34f; (1)
Double piValue;
piValue = 3.14159265358979323846d; (2)
Long timeOfPlayinMilliSeconds;
timeOfPlayinMilliSeconds = 72585335350535l; (3)
1 | We added the letter f at the end of our number, to signal that this is a Float. |
2 | We added the letter d at the end of our number, to signal that this is a Double. |
3 | We added the letter l at the end of our number, to signal that this is a Long. |
There is the same shortcut as usual if you want to create a variable and give it a value, just in one line of code:
Float averagePrice = 15.34f;
Double piValue = 3.14159265358979323846d;
Long timeOfPlayinMilliSeconds = 72585335350535l;
There is another way to use these types of variables, again for a gain in memory:
float averagePrice = 15.34f;
double piValue = 3.14159265358979323846d;
long timeOfPlayinMilliSeconds = 72585335350535l;
Float
is replaced by float
, Double
is replaced by double
, and Long
is replaced by long
.
This looks like just a small change, but the smaller cap version takes even less space in the memory of your program.
Your turn!
Create a variable called discountPercentage
and store 0.33
in it.
Create a variable called dollarToEuro
, using double
. Store 0.80240500000000003
in it.
Create a variable called milliseconds
, using long
. Store 25343353530285753
in it.
Here is the solution:
If you have never programmed before, this type of variable might be surprising to you.
Why do we need a type of variable that would just store 2 possible values: true
or false
?
After all, we could just use a String
type of variable for that:
String customerLoggedIn = "true";
String customerSubscribedToNewsletter= "false";
That would work very well, but in practice these true / false values are so common that a special type of variable called Boolean
has been created just for them:
Boolean hasSuperPowers = true;
Boolean isAClubMember = false;
Let’s notice that we did not put double quotes around the value true
or false
.
Just like before, there is another way to use these type of variables, again for a gain in memory:
boolean hasSuperPowers = true;
boolean isAClubMember = false;
Notice that Boolean
has been replaced by boolean
.
Your turn!
Create a variable called isPremiumMember
and store a true
value in it.
Create a variable called isReturningVisitor
and store a false
value in it
Here is the solution:
We know how to create and store text, number and true / false values: these ingredients that are the basis of our mobile app.
But we surely need to create a store many thing else… like:
some Forms, Buttons, Labels!
a date (April, 10 2010) ?
colors, pictures, etc.
These type of variables are called objects
. They are created and defined in the following way. Let’s take the example of a Form:
== !
Form myForm; (1)
myForm = new Form(); (2)
1 | We create the object (we declare it) |
2 | We create and store a value in our object (we instantiate the object) |
Just like for the variables we’ve seen above, we can take a shortcut: declare and instantiate an object in just one line:
Form myForm = new Form();
What we saw above is surely intimidating, but if you "get it", then you know how to create a huge variety of things.
(and we have a "cheatsheet" to help you memorize the essentials!)
For our mobile app, you can now create a Button
:
Button buttonCookieClicker = new Button();
Or a Label:
Label welcomeMessage = new Label();
The Label
is created in one line of code, but without text in it. That’s a bit useless.
Remember when I mentioned that the empty ()
could sometimes include a parameter? Label
is a good example:
Label welcomeMessage = new Label("Welcome to my app!");
Now, if you read this lesson with care, this alternative could get you a "ahah":
String welcomeMessage = "Welcome to my app!";
Label titleOfMyApp = new Label(welcomeMessage);
Let’s now try to create an object in the interactive screen?
Form
, Label
and Button
are objects provided by the plugin we installed in NetBeans, so they are not included in the interactive console here.
Instead, let’s create another object. The object of type Date
specializes in storing dates:
Your turn!
Create an object of type "Date" called dateOfSubscription
and store a new Date in it.
Here is the solution:
Look at the line in green: it gives the value stored in your variable dateOfSubscription
: by default, it is the day and time when the variable is instantiated. Very convenient!
Questions? Want to open a discussion on this lesson? Visit the forum here (need a free Github account).
Find references for this lesson, and other lessons, here.
Licence: Creative Commons, Attribution 4.0 International (CC BY 4.0). You are free to:
copy and redistribute the material in any medium or format
Adapt — remix, transform, and build upon the material
⇒ for any purpose, even commercially.
This course is designed by Clement Levallois.
Discover my other courses in data / tech for business: http://www.clementlevallois.net
Or get in touch via Twitter: @seinecle