Variables, Casting & Printing Data – Getting Started with Java 1 (IntelliJ IDEA)

Well hello, future programmer. In this very
first tutorial of my Java tutorial series, I’ll be teaching you the core basics of Java,
including displaying text to the user, and creating variables and doing simple math operations
with them. To get started, you’re gonna need to have an application to program in. I’m currently
using the free version of IntelliJ IDEA, and there’s a link to a video on how to install
that in the description of this video. But first, if you see me do something you don’t
understand, or you want to know something else about programming in general, you can
leave a comment down below and I’d be happy to answer it. Now that’s being said, we’re
gonna need some document to type our code in, so go to “src”, “new”, “Java Class”, and
give it a name, starting with an uppercase letter, so for example, “CurrentLesson”. We
now have our first Java class file, and we need to tell Java that this is indeed a Java
class file, and to do that, you type “public”, meaning every other Java class file can access
this file, and public is a keyword, and keywords are just words in Java that have a special
meaning, and I will be going over all of these keywords in numerous new videos. Then we have
to type “class”, meaning that this is a class, and then the name of the file without the
extension, so “CurrentLesson” in this case, and then we’re gonna need to create a body,
by doing two curly brackets, and everything between these two curly brackets is gonna
be part of your class. Now that Java knows this is a class file, it’s gonna look for
the “main” method, and the “main” method is the code that’s gonna be executed when your
program starts. So to create this method you go “public”, meaning every other method can
access this method. Then “static”, meaning that this method belongs to the class and
not to an individual object, and I know that doesn’t make sense right now, but we will
be covering that in later tutorials. Then “void”, meaning that this method doesn’t return
anything. Then the name of the method, “main”. Then we need parentheses, and in between the
parentheses we’re gonna give it the parameters, and the parameters are, “String[] args”, then
we need to give a body to our method, just like we gave a body to the “CurrentLesson”
class, and we do that exactly the same, by using curly brackets, and everything between
the curly brackets is part of the “main” method. I know that was a lot to take in, and don’t
worry about remembering all of it just yet. You will get it sooner or later. But if you
have a question about it, please don’t be shy and ask it in the comments. But now that
we have our “main” method and Java knows what to do when our program starts, we can finally
do a lot more. For example displaying text to the user. And to do that, we have to call
a method inside of another Java class that knows how to display text on the screen. And
calling a method basically means, to ask it to execute its code, because a method has
a body, just like our main method, and everything inside that body will be executed. To access
this method that knows how to display text on the screen, we have to type, “System.out.print()”,
and print is the name of this method, then type parentheses, and in between the parentheses,
we need to give this method the information that it needs to display text on the screen.
So in this case, it’s the actual text that you want to display, and in Java, if you want
to give it some text and not just code, we need to use quotation marks, and everything
between quotation marks will be seen as text instead of code. So if we want to say, “Hello
World”, we would do that like that. And at the end we need a semicolon (“;”), so Java knows
this is the end of this line. Then we want to execute our code by right mouse button,
“run ‘CurrentLesson.main()'”, and as you can see it returns “Hello World”. Now let’s say
we want to print this another time. Well, if we execute this, you will see that there’s
no extra line break in between these two sentences, and that is because “print()” just prints
the thing and then it’s done. If you want an extra line break after it, you have to
use another function, and that is called “println()”. So if we use “println()” instead of “print()”,
it will put an extra line break after it, as you can see. Now let’s say we have four
print statements, and we want to change all of them to “Bye World”. We would go, “Bye
World” over here, and “Bye World” over here and that would be kind of boring to do four
times or maybe even a hundred times. Now wouldn’t it be handy if we could use a variable, and
only change the value of this one variable, and that all the print statements would print
the new value of this variable. Well, we can do this by creating a “String” variable. And
in Java, all variables have a type, a name and a value. So the type would be “String”
because the variable is a placeholder for a String. Then we have to give it a name,
for example “byeMessage”. Then we have to assign a value to it, by using the “=” sign.
And then the message, “Bye World”, and “Bye World” is the String, “byeMessage” is the
placeholder for the String, i.e. the variable name. Then a “;” because this is the
end of the statement. And then we can simply copy this name of the variable inside of the
“println()” statements, and we don’t need quotation marks because it is only a placeholder
for a String, byeMessage is not a String itself. So when we print this, as you can see it will
print “Bye World” four times. And by changing this one String to “Bye Worlds”, all the “println()”
statements will print the new message. Now think about this, if we use quotation marks
to tell Java this is a String, can we print quotation marks themselves? We have to escape
these quotation marks that we want to print, and you escape a character by using a backslash (“”) and
then the character you want to use. And as you can see you can escape all of these characters.
So you can add a new line, or a new tab, or you can escape the “” itself. So what if
we wanted to add a new line after “Bye “. It would say “Bye Worlds” in between quotation
marks and with a new line. Because we use this syntax, “/*”, and here “*/”, everything
in between them is ignored by Java, it’s just for the user to explain his code. And the
same can be done for a single line of comment by using “//”. So this entire line is a comment,
but when we go to the next line, it won’t be a comment and will give you an error. Now
that we know how to use comments I’m going to comment out these four print statements
by going “/*”, and then at the end “*/”. We now know how to make a String variable, but
you can also make a variable of for example numbers or characters. And to do that we have
to use one of Java’s eight primitive types, and those are the byte, the short, the
int, the long, the float, the double, the boolean and the char or character,
and you can see their description over here, and I’ll give an example of all eight of them.
Let’s start by creating a byte variable, so “byte myByte=120;” for example, because
a byte can only be a number between -128 and 127. Then we’ll create a short, “short myShort
=16000;”, because a short can be between -32768 and 32767. Then an integer, “int myInt
=200000000;” or something like that, and it can be up to a few billion big. And then
we have something even larger, called a long, “long myLong=123981029389123823L;”, and
this is a special one, because when we create a long we have to use an “L” at the end, because
without an “L”, Java will think, “Well, this number is way too big to be an integer, so
I’ll just give you an error.”. Well, by saying “L”, Java now knows this is a long and it
won’t give you any error. Then a float, “float myFloat=2.5F;”, the same as with the long
we need to tell Java this isn’t the default type for floating point numbers because the
default type for floating point numbers is a double, so we need this “F” to tell Java,
“Well, this is a float and not a double”. Then we have a double, “double myDouble=3.1415;”,
and the difference between a float and a double is that a double is much more precise because
a double has about fifteen decimal places after the decimal point and a float only has
six, so that’s a lot more precise. Then we have a boolean, which can either be “true” or “false”,
not 1 or 0, “true” or “false”. And then last but not least, there are two ways to represent
a character, by the character itself, or by the number that it’s associated with. So we
are first going to make a character using the number, so “char myChar=66;”, and the
number 66 represents an uppercase “B”, but we could also just do, “char myChar2”, because you can’t make two variable with the same name, “=’B’;” and we have to use apostrophes to tell Java, “This
is a character, this isn’t code or a variable name.”. For characters we use apostrophes
and for Strings we use quotation marks. And these are the two ways to create characters.
Now let’s say we want to print our name and our age to the screen. We could put our name
in a String variable, so “String name=”Richard”;”, because that is my name, and don’t forget
the quotation marks, and then our age in a byte variable, because I’m not older than
127 years, so that will do. We could first type, “System.out.println()”, and then print
the name, and then do the same thing for the age, or we could do all of this in one line
by going “System.out.println();”, and in between parentheses “name” for the variable name of
“name”, and then “+ age”, and then a “;” at the end. So this will first replace “name”
with “Richard”, then replace “age” with 17, and will just print the whole thing without
a space because we haven’t defined a space after “Richard”. We can do that by just adding
one over here, “+”, a String of just a ” “, then another “+” and “age”. It will first replace
“name” with the value of “name”, which is “Richard”, then it will add a space, and then
it will replace “age” with the value of “age”, which is 17. Now let’s make a nice sentence
of that, “Richard”, ” is “, 17, “+”, ” years old”. It will say “Richard is 17 years old”.
I will be turning 18 soon, so I would like to tell you that. Well, we could create a
new variable, or we could just change the value of “age”. Well, we’ve already defined
“age”, so we don’t have to tell that it is a byte again. “age=18;” now. So then we
can do the same thing and print this out. ” is almost “, 18, ” years old”. “Richard
is almost 18 years old”. And this 17 just remains the same because only here it was
changed to 18, before that “age” was still 17. Now what if I lied to you and I’m actually
18 years old. I would have to change “age” over here and I have to change it over here,
so wouldn’t it be handy if I could just add 1 to the current value of “age” here. Well,
I think it would, so we can actually do that by using this syntax, “age=age + 1;”, it
will give an error and I will explain that in just a second. But what this does, is it
just gets the current value of “age”, that’s 18, it’s going to add 1 to it, so 18 + 1=19,
and then it’s going to assign that new value to “age”. That’s what it does and that
works with all the types, byte, short, int, long, float and double. Now, we can’t do this
because we’re trying to add an integer to a byte, because remember, the default type
of none floating point numbers is integer. And you can’t add an integer to a byte, that’s
just insane! So to do this, we have to temporarily change “age” and 1 to a byte. And we do that
by casting it. And you cast something by using parentheses, and in between the parentheses
you put the type you want to cast to, you want it to temporarily convert to. And what
you want to be cast has to be in between parentheses as well. When we now run it, it will say,
“Richard is 18 years old”, and “Richard is almost 19 years old”. And when we change this
to 30, it will still work, it will say, “Richard is 30 years old” and “Richard is almost 31
years old”. So that’s how you do it, and you can change this 1 to any other number, as
long as it can fit inside of a byte. You can add, substract, multiply and divide by other
numbers, but you can also use something called the modulus (“%”). I’m going to print “9 % 4”,
modulus is simply the percent sign, and when we’re going to run this, it will return 1,
because the modulus will give you the remainder of the division. So 9 / 4 will give you 2.25.
Well, then you have 1 left, 4 doesn’t fit in that 1, so it will give you the remainder,
the 1. So that’s what the modulus does. We’ve just talked about casting and turning bytes
into ints and longs into shorts, and even doubles into longs, because it will just get
rid of all the decimal places after the decimal point. Because remember, longs, ints, shorts
and bytes don’t have decimal places, so when you convert a double or a float into a none
floating point number, it can’t keep the numbers after the decimal point, so it will just get
rid of them. But we haven’t talked about converting a String into a double or a boolean into a
String. So let’s talk about that. So I’ve created three variables to test this, and
we’re going to convert all of them to another type. So we’re going to convert this first
double “myPi” to a String. And you do that by saying “Double.toString(“myPi);”. And remember,
because we have parentheses over here, this is a method and this method is inside of the
“Double” class, just like this is a class. Now our “myPi” variable is a String instead
of a double. And in the same way we can convert our “favoriteNumber”, 17, to an integer, and
to do that, we use “Integer.parseInt(favoriteNumber);”. And now we can do calculations with this number,
17. Because before it was just text, and now by turning it into an integer, it’s actually
a number. And to do calculations with this new number instead of this String, we need
to save it in a variable, for example, “int a=Integer.parseInt(favoriteNumber);”.
So “a” is now equal to 17, even though it was a String before. And the same can be done with booleans. So we have a boolean that’s equal to “false”, well, we want to change that to a String. So we go, “Boolean.toString(trueOrFalse);”. And now this boolean has been turned into
a String. So that’s how you convert Strings into other types and other types into Strings.
And to show you 17 has been changed into an integer, we’ll multiply it by 2, so we do,
“a=a * 2;”, and then we’re going to print it. So when we run it, you’ll see 34. But
there’s a shortcut to do this. Instead of “a=a * 2;”, you can type “a *=2;”, and
that will do the same thing. So now 34 times 2 equals 68. Now I want to change this 68
to 69, so we have to add 1 to a. And we can do that by typing “a=a + 1;” or
“a +=1;”, that’s a lot shorter, but there’s an even shorter way if you only want to add 1
to something or only want to substract 1 from something, and that is “a++;”. So when
we print this, it will say 69, and the same goes for “a–;”. So it will say 67. And that’s
about it for this tutorial. If there was something you didn’t understand, please leave a comment
and I’d be happy to answer it. And also if you want to know something else about something
I didn’t teach in this video, just leave a comment and I will answer it as soon as possible.
But before you click this video away, in the description is a link to the source code of
what we did today, and there will be a lot of comments in it, like this, with extra information
about what everything does. So it’s highly recommended to just read it, and maybe code
something yourself! Because you already have a lot of information, so the possibilities
are almost endless. Hopefully I’ll see you all next time when we’re going to talk about
user input and maybe even a little bit of object oriented programming. Bye Bye.

Leave a Reply

Your email address will not be published. Required fields are marked *