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 the 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 is 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, an 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 bytecode, 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 the Package Explorer and go to New, Java Project. You can give it whatever name you want. We'll just call it 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 too, 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 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. int myInt equals 7. 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 myInt is the name of the variable. We can give it whatever name we want. And this equals 7 here is giving this myInt the value of 7. So this just says, hey Java, I have a number here.
I want to call it myInt and please give it the value of 7 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, myint. 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 7. 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 my int 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 and 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 could say double result equals my int times shoe size.
All of these types so far, int, 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'll 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 toUppercase.
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 myInt here. If I try to do myInt 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 printName, 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 print it 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. 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.
with 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, print 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'll use double equals 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 zero, 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 my 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. myCat.name equals... Fred in my cat dot 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 dot 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. My cat 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. dingdong method static.
So back here in our main method we could just call cat.dingdong. Notice we're just calling this method on the class 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 cat.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.