Introduction to Java tutorial 2: Printing and variable declaration in Java


Hi guys my name is Luthando This is our second video on Java programming So I’m assuming you that you have already downoaded eclipse to your PC Which is the environment we will be using to create our programs If you have not done that yet,please check my first tutorial video on the description below I’m assuming that at this point you you are where I am currently (your screen is showing the same thing) the first thing we are going to do is go to create new project give your project a name we will call this one maybe Project A so this is the project location so you should check this directory, that’s the location where you will find all the programs you do, so this might differ as yours will be dependent on your PC it will not be the same as mine maybe on yours there wont be paths like”/student/” but “/eclipse-workspace/” will most likely be part of your path directory as well then at the end will be the name of your project once you are done with that, click “finish” if something like that appears just close it afterwards you will then see a space like this one then go to ProjectA and expand(click the>) that thing you see on Project A you will right click(on you laptop touchpad/computer mouse) when you right click there you will see something like this so when you get here you will go to package in creating a package we have not yet began programming but we want to organize/ package our programs or put them together in one place (like a store room) so we will say my package 1 that’s where we will store our code/program. click finish then we start with the real stuff right click mypackage1, then select new then go to class, we will be creating a class then here you will create a class name, we will call the class name “Khodi” and then click there that method, public static void main, so for now, just keep on ticking this method I will explain a bit later, what what that method does but basically, it makes sure that whatever code we write executes on the console, whatever program that we write then click “finish” So Java is an is an object oriented programming language, so most of the time you will find yourself having to work with classes and objects I will explain most of these things(theory) as continue and go deeper into java programming. So once you are here, the first thing we have to do is, you will remeber I spoke about that the method public static void main(), will help us to be able to see our outputs on the console the console is this space it’s where the output will be displayed for all that we will be programming on top so the first thing I want us to do is to see if we can display any output there, so(write) System dot out, dot print line and then “Sawbona(meaning: Hello)” Sawbona(“Hello”) you see, Sawbona(“Hello”) and then go there and then select “OK” Sawbona(“Hello”), you see it showed on the console so everytime you write your statement or command(piece of code) on JAVA, you must always close your statement with that semicolon at the end you close your statement like that. If you put something like a word or string word(even sentences) in programming language are termed strings. That is the type of data we identify them as we categorize them as string type data so if you want to output a word or sentence you put those inverted comas unless you will do it like this unless you will do it like this declare say we want to use variables or we want to store a certain value/data to a variable we have to first declare the variable type so to do that, we will say for an example “String” activity so my variable is activity in this case we will then say “Kokota” we will say “Kokota” and then we will put activity (inside the print method) acti vity activity so you see, it automatically links the input to the declared variable so that word(“Kokota”) was stored in that variable name (activity) we called activity, so everytime when we print activity, the output will be the output must be “Kokota” which is the string that has been stored “Kokota”, you see, it shows at the output so we declared a variable as a String, this is how you would declare a variable so what you are saying is that the variable “activity” will be a string datatype, so if i put something else here which is not a string, that should give me an error message maybe say 23 you see, an error exists in ProjectA so we can’t proceed with that launch so you see that 23 is not a String 23 is a number, it’s not a string the only thing that would make 23 to be intepreted by JAVA as a String is if we include those inverted commas only now is 23 appearing on the output, even so although it has been printed as the output it is not handled as a number, but rather as a word or string of somesort I hope you understand that another thing, there are many other datatypes, it’s not just Strings or just words(including sentences) that we can manipulate, there are also integers, I can have integer a maybe is equal to 2 you see, so this is how you would declare an integer every time you want to declare a variable, a new variable maybe like a, or anything else you must first specify what is the type of that variable in this case, the type is an integer then maybe we declare another integer that will be “b” If you want maybe maybe to declare a decimal a number that is a decimal we use double a decimal that will be for example 0.235 1.5 or 3.41 3.41 it can be any number let’s declare c as a double maybe say c is equal to or we can not specify the value of c at this point (This is optional) you don’t necessarily have to equate c to a value (allocate a value to c) even “a” we could say like this and b, like this it is still the same thing. Then we specify the values of those variables(a, b and c) say we want “a” to be 2 we want “b” to be maybe 3, then you notice that I always add the semicolons at the end otherwise that command or piece of code will not execute and then c will equal, maybe say “a” plus “b” you see, it’s “a” plus “b” what will that be, 3 plus 2 we expecting an answer of 5 let’s output it here system dot out, dot print line, “c” Ok, let’s see 5, so this five will be given as 5.0 because “c”, you remember, we declared “c” as declared “c” as a double or rather type double or a datatype that would be a decimal that’s why it gave 5.0 so there are a lot of commands that you can actually execute on JAVA, especially those mathematical commands(in the context of this video) others if you want to do some complex stuff(in the context of the level of this tutorial content) you can also have things like “a” times “b” “a” times “b”, that would be something like 6 if you want to execute commands that are more more complex you have a maths library in JAVA that is built in, where you can have you have your maths functions you can start with math dot, say pow for an example. So pow “a” a,b, that would output something like this 8, what that does is it takes the first input/entrance(value of “a”) which was “a” and then raises it to the exponent “b” so that is 2 exponent 3, which would give you 8 There are a lot of other mathematical operations you can perform you can check them out here, you can even output PI you see, just like that you see, 3.14 …… you also have many more others, like if you want to do sin, cosine, arccos, arctan all of them are here, you just have to say(write) math dot and all available functions will show, your logs and all the others depending on what you want your program to do for today, I think we can wrap it up here Please subscribe, if you’ve not done so already and please do let me know on what you wish I should do, or how I can improve to your convenience, so that you clearly understand the content of these tutorials. For now, this is goodbye see you on the next tutorial, adios

Leave a Reply

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