Transcript for:
Beginner's Guide to Java Programming

if you're struggling learning java i understand how you're feeling it can feel like everyone around you is some kind of programming genius who just gets everything with no problem at all and you feel like you must be the only one having trouble with it you're absolutely not alone and you're in the right place i remember in my first java class in college i really struggled to understand some of just the basic concepts and really just felt overwhelmed but now i've been programming in java for a living for the past 10 years and i want to make sure you don't have to struggle the same way i did my name's john and i love sharing what i've learned about java in a clear and understandable way so please consider subscribing if you'd like to see more helpful java videos in this video we'll go over everything you need to know to get started writing great java programs before you can code anything in java there's a couple of things you have to have installed if you've already done that awesome and if not it's super easy it just takes a few minutes i have a video here going through the whole process one of the things you'll want to install is called an ide and integrated development environment and that's what i have open here the name sounds really complicated but it's basically just a program that makes it easier to write code kind of like microsoft word makes it easier to write school papers here the ide that i'm using is called eclipse basically we're going to write our java programs here in eclipse and then it is going to take our programs and automatically translate them into a format the computer can understand something called byte code you know essentially the ones and zeros that make the computer happy and it's going to run it when you open eclipse to get started creating a java program just right click here in this area called uh the package explorer and go to new java project you can give it whatever name you want we'll just call it a project pineapple you can uncheck this module thing down here we don't need that and click finish you can see it created our project for us here project pineapple then you can expand this drop down and right click this src folder here which stands for source and go to new class a class is basically just a fancy name for a java file give that name to whatever you want like awesome java program and you want to check this box down here that says public static void main string args we'll talk a little more about what that means later then click finish and then eclipse will automatically generate this kind of framework for a working java program for you now all of these programming looking terms and keywords might feel really intimidating but really all this is saying is hey java i've got this new program called awesome java program and when i tell you to run here's all the stuff that i want you to do this whole section here is what we call the main method and all the commands we put in between these two curly brackets in the main method are what it's going to do when we tell it to run so that's where we're going to be writing most of our code this line here with the two slashes is just an automatically generated thing that we don't need so we can just get rid of it just about everything we'll do in java revolves around creating and using things called variables variables are just used to store data so here's an example and myint equals seven now variables have a name a type and a value for this variable int is the type it stands for integer int is basically just a number this my int is the name of the variable we can give it whatever name we want and this equals seven here is giving this my into the value of seven so this just says hey java i have a number here i want to call it myant and please give it the value of seven to start out with and then we have this semicolon in java most lines will end with a semicolon it's just telling java that's the end of the statement now anywhere after that line in our program we can get the value stored in that variable just by using its name for example if we want to just print it out the command to print something out in java is system.out.println and in parentheses we specify the name of that variable myant and we end with a semicolon and we can click save and then click run to run our program and we see our output of the program here in our console so it just prints out the value of this my int variable which is seven int isn't the only type of variable we can create for example if you want to create a decimal number you can use double let's say we want to make a shoe size double shoe size equals 9.5 if you want to store a character you can use the char type char my initial equals j that'll be in single quotes a lot of the magic encoding happens when we take these variables and have them interact with each other so we can take this mayan variable and multiply it by the shoe size if we want in java to multiply you just use star so my int times shoe size save and run it we see that we get the result 66.5 which is 7 times 9.5 and also instead of just printing out that result you can actually store the result of that calculation in another variable so we can say double result equals my int times shoe size all of these types so far and double and char those are called primitive types in java there are eight primitive types total in java and i have a video going over all of them and how to use them in detail here primitives are like the absolute basic types in java like the sticks and stones you'll know it's a primitive type in java if it starts with a lowercase letter every other type you use in java is built on top of these primitive types but there are tons of other types you can use and you can even create your own like we'll see later probably the non-primitive type that you're going to use most is called string string variables are used to hold text string my name equals john for strings you put them in double quotes instead of single quotes you can tell that string is not a primitive type because it starts with a capital s and also the primitive types have a different coloring in eclipse than other types do when you create a non-primitive variable like this there's also even more stuff you can do with it that can be different for each type to see what you can do with the variable if you're using a fancy ide like eclipse you can start out by just typing the variable name then type a period and you'll immediately see all the cool things you can do with this variable these are all called methods and when we use any of them it's called calling a method as you're scrolling through these you can click one and you'll get a description of what that method does for example this length method says it returns the length of this string and if we want to use it we just double click it or you can also just type it out if you want to so if we move this down here so we can print out the result of this method call it prints out four because john the value of the my name variable is four characters long but there are a ton of other methods available like for example there's one called a two uppercase and if we use that it gives you an all uppercase version of that string and if you already have the method out here in your code and you want to remind yourself what it does you can just hover over the method name and a little pop-up will appear with the description one thing you can't do is you can't call a method on a primitive variable like my interior if i try to do my int dot nothing pops up it just doesn't work speaking of methods you can also create your own to create a new method here in the same java file i'll type it out first and then we'll talk a bit about what it means private static void burp this whole line here is called the method declaration and this part right here before the parentheses is the name of the method here we called it burp and any code that we put between these two curly brackets is what it's going to do when this method is called let's just have it print out burp but if we go ahead and save and run our code now nothing happens nothing's printed out why is that that's because if you remember when we run our java program java just runs everything that we put inside our main method and right now there's nothing in here that calls this other method that we created so let's change that now if we want to call this method all we have to do is use its name burp and we also need an open and close parentheses whenever you see this open and close parentheses you'll know that it's a method being called now if we save and run our program successfully burps so now we can have our program burp whenever we want just by calling burp of course a method can be a whole lot more complicated than this and a big reason to make a method is because you might have a whole big complicated chunk of code that you don't want to repeat package it up in a method and you can just call it as many times you want from anywhere in your code just by using its name now a method can also take what is called parameters so for example if this method was called print name between these two parentheses is where you would put the parameters let's say it had a string parameter that was called name and then just printed out my name is plus name this plus is how you link two strings together in java that's called concatenating strings but now when you call this print name method you have to pass in a value for that name parameter and you pass in that value by putting it between these two parentheses uh john and we can run it and see that it prints out my name is john you can even take multiple parameters if you want just separate them by commas so i can also take in like int number and when you're calling it you just do the same thing you just separate the values that you're passing in with a comma if you want you can also have your method return a value right here in the method declaration where we have this void void means it's not returning any values but if you want it to return a value this is where we specify the type of the thing that it's going to return so if we want it to return a string we can just change this to a string but now we have to actually return a string somewhere in our method so what we can do instead of just printing out my name is blah we can just change this to return that value my name is blah the code that calls this can do whatever it wants with the string that's returned for example we could just print it out ourselves here or if we want since this returns a string we can actually save that resulting value in our own string variable so string name equals the result of this method call one of the most useful things you'll ever use in java is called conditional statements that's your basic if this then that conditional statements in java will look like this if whatever condition we put in here is true run this stuff otherwise if whatever condition we put in here is true run this stuff and if neither one of those are the case run this and you don't necessarily need all three pieces for this you might just have an if you might just have an if else you don't need all three so what kind of conditions can we have in here so we could say like if name equals john then print this guy is awesome else if the name equals larry this guy is okay i guess and if it's not john and not larry prince i don't know this guy at all this example uses strings as its conditions but if you want to use the like a number as the condition you can do things like if number double equals five in java you use double equal to check equality like this that's because the single equals is already being used for assigning values to variables so just remember to use double equals but with numbers you don't always just have to check for equals you can do other things like if the number is greater than 5 or less than 5 or if you want to say if the number is not equal to 5 you use exclamation point equals next if you want to be able to repeat a piece of code a whole bunch of times without actually copying and pasting the same code over and over and over again you can use what's called a loop one type of loop in java is a for loop and it looks like this and i equals 0 i less than 10 i plus plus what this will do is run everything inside these curly brackets 10 times how it does that is it declares this variable i and says hey keep looping while i is less than 10. and every time it goes through the loop increment i by one that's what this i plus plus does so we can have it print out these pretzels are making me thirsty if we run it it'll print out these pretzels are making me thirsty 10 times and we can have this for loop do this however many times we want so if we change this 10 to a thousand and run our program it'll print it out a thousand times one huge thing about java is that it's an object oriented programming language a big part of being object oriented is that you can take a whole bunch of code and put it in another java file called a class and you can use that code in some other java file like your main file so let's do that now we can go over and right click on source again and go to new class say we want to create a cat class so we'll call it cat and you can create a method in your class like this public static void ding dong which prints out ding dong this public keyword here makes it so this method can be called from any other java file if you remember in our other method we used private private makes it so this method can only be called from within this file that was fine because that's all we needed it for but if you make it public it can be used anywhere so now we can call this cat ding dong method from our main program like this we just say cat dot ding dong so you can absolutely use a separate class just like this a place to put other methods so they don't clutter up your main class but the main use of a separate class like this is actually as a blueprint to create what are called objects so here we have a cat class now this class itself isn't a cat object but we can use this class to create cat objects as many as we want and here's how so back in our main program we can just say cat i cat equals new cat this creates a brand new cat object and stores it in this variable my cat and we can create another one if we want so cat another cat equals new cat right now we haven't really put much of anything in this cat class so our individual cats that we've created from it can't really do a whole lot but we can change that so we know a cat in real life probably has a name and an age and it can meow right we can make our cat class like that too so that the cats we create can do those things so here's how we do that we just add string name int age and we'll create a new method public void meow that prints meow now note that we didn't actually set a value for the name or the age here remember the class isn't a cat in itself it's a blueprint for creating cats and it tells you what kind of attributes it has and what it can do so what this is saying is that each individual cat made from this class can have a name it can have an age and it can do this thing it can meow and back here where we're actually creating our individual cats that's when we can actually set the name and age of these individual cats my cat dot name equals fred my cat.age equals six we use this dot to access the fields on these objects very similarly to how we call methods but you'll know the difference because when we call methods we always have an open and close parentheses and when we're accessing a field on that class like name or age we don't have any parentheses and on another cat a whole separate cat object we can set its own values so another cat.name equals stella stella's age is five later on in our program we can get those values and do whatever we want with them so if we want to print out fred's age later we can do that icat dot age and it prints out six one huge point of confusion for me personally when i was learning java is this whole static thing you might have noticed in our methods sometimes we use this static keyword and sometimes we didn't all this static keyword means is that this method can be called without using an individual cat object for example we made this ding dong method static so back here in our main method we could just call cat dot ding dong notice we're just calling this method on the clasp itself not on any specific cat object that we created we don't need to create a cat object first if we just want to use this method but if it's not static like this meow method you can only call that method using an individual cat object so back here notice that i can't call hat.meow just using the class name it gives me an error that says i can't make a static reference to this non-static method but down here where i've actually created a cat object like my cat i can call meow on that just fine whether a certain method that you make is static or non-static just depends on what makes sense for what you're trying to do of course java is much deeper than what can be covered in one video like this but this should give you a ton of tools that you can use and build off of to start making some awesome programs i do have a full java course available in a link down in the description if you're interested that goes way more into depth on a ton of topics but if not that's awesome too i'm just thrilled to have you here with me my goal is really just to help as many people as i possibly can for real if you ever have any sort of java questions that i might be able to help with just put them in the comments and i'll do my best to respond thanks so much for watching i'm so glad to have you here with me and i'll see you in the next video