Transcript for:
Overview of Java Programming Concepts

Last week on the channel. Alright, before this video gets started, I just want to make one thing clear. I am not a JavaScript developer. Stop making jokes about how I only talk about JavaScript. I suck at JavaScript. I am not only about JavaScript. I am a Java developer, if anything. It is not JavaScript. All I wanted to do was express that I don't really use JavaScript innocent enough, until the real ghost said this. Wait until he learns about Tailwind CSS, little Java dev brain will explode. On a more serious note, anyone who use Java can't really be taken seriously or professionally at this point. It's a dead language as far as current business standards go. What? Um, okay, so I responded with this. Is this satire? It feels like satire. But if it's not, Tailwind has absolutely nothing to do with Java. And uh, yeah, Java is... so dead it's only used by 90% of fortune 500 companies. Which is true by the way. Which is why this tutorial thing is important. This video, whatever. Because you're going to be at least getting your feet wet with skills that apparently nobody wants to learn but all the big companies use. And that's not even the only Java hate I got. So just to spite all of y'all who said this crap, here's the world's shortest Java course. Shout out to Juxtapose for the video idea. If any of y'all like UI stuff. Check out that channel. It is flawless. So what the heck is Java? Well Java is an object-oriented programming language used to build... anything, really. Web apps, mobile apps, enterprise systems, scientific applications, stupid AI applications... I really need to get back coding on Arch Linux. Anyway, the reason for this is because Java is compiled into bytecode. This is your.class file, which can be run on any device that has a JVM or Java virtual machine, or something similar like Android Runtime. So if I write this code and compile it with the Java compiler, you'll notice a dot class file has been generated This is what's fed into the JVM to effectively translate the Java program to be understood by whatever hardware It's on and then we run it like this and it spits out our expected output But who the heck wants to write code like this instead most people use IDEs like this one. IntelliJ IDEA Eclipse and my beloved NetBeans that I learned on being the most popular three although I use IntelliJ IDEA now which comes with its own special JDK otherwise you would have to manually install the JDK which is a Java development kit which comes with the compiler, the loader, the Java runtime environment which comes with the JVM and some other dev tools needed to build programs in Java. This is an IDE, an integrated development environment because it has tools integrated into the development environment. Most IDEs are going to look very similar and you can customize it to your liking like moving windows from one side to the next as you can see. This one is the Project Explorer which helps you navigate your project's files and folders. This window is the editor window where you write your code and has things like code completion. So if we wanted to call our function printboard it'll recommend printboard right here because it knows that we have that function somewhere else in our code. This is when you know it's bad. I've been using JavaScript too long. Java doesn't have functions, Java has methods, which are the same as functions in other languages, but in Java they're called methods. Okay, I will not be making that mistake anymore. It also has syntax highlighting as well as real time error detection. You can also see more of your tools right over here in the view tool windows and all of these different tools, including the terminal. And then we also have version control. So you can create a local git repository and have version control and then hook it up to a remote git repository like on GitHub. It's a one-stop shop. So let's create a new project together. Just come up here. There's going to be a file new Project. This is a Java project we can name this learn Java or whatever you want pick your location We can create a git repository right here by checking this box or we can leave it unchecked built system We're going to use IntelliJ. Although you can use maven or Gradle JDK 1.8 because that's all we have installed but if we want to download a new one and use that we could We'll have it add sample code and generate code with onboarding tips and create And as you can see, it has some instructions right here to run the code, press shift F 10, or click this button up here. And I'm not going to go through all of them, you can do it on your own. But let's run it. As you can see, we're using a for loop, which we'll go over in a second looping through i is less than or equal to five. So 12345 is in fact less than or equal to five, and then it stops. Oh, it also says Hello and welcome. That's what I was going to do. So if we wanted to remove this, and we wanted to have a Hello World application, we will just have System Out print F which We can also do print Len, hello world run, hello world. And let me show you one thing real quick, as you can see within out, this is the main class. If I were to delete that file, let's delete the whole directory actually. So now we don't have a main class in there. When we click run, it'll compile and run. So when we click run, you'll see it produce that dot class file right here, like it did before. And then run all with a click of one button. All right, now that you have a loose understanding of JDK and the difference between using it just raw, and then the advantages something like an IDE will give you, let's talk about the Java programming language itself. Data types. Java has two different types of data types, primitive and non-primitive. And this is not an exhaustive list of each, but rather a few to serve as an example. So for primitive data types, they represent the most simple values possible. And they are not composed of other types like non-primitive types are. And also they are built into the language, or in other words, predefined. So we have integer, which is a whole number. We have double, which is a decimal number of a certain size. We have char, which stands for character. And then we have boolean, which is true or false. As for a few non-primitive data types, we have a string and an array. So this string is actually a little bit of a weird one. Because non primitive data types are actually created by the programmer themselves in other words not predefined like primitive data types Except for string because string it actually comes with this java.lang package right here And as you can see it is an array of characters It's just predefined within Java itself, but that's not really information you need to focus on more importantly non primitive data Types are actually objects so you can do things to them So this string that we're calling greeting and we have assigned the value of hello Well, what we can do is take greeting and we can hit the period and you can see all of these different Methods that you can do to greeting built into Java so we can make the entire string Lowercase or uppercase or we can get the length of it But right now we're just running the method length on greeting and not doing anything with it so what we would want to do is store it in an integer because it is the length of greeting which is five hello is five letters and we can call this greet length so greet length equals greeting dot length and then instead of hello world down here which if we run it you can see we output hello world we could throw in greet length and it should output five and there we go it outputs five but you may be wondering why are we doing greeting dot length and then storing it into an integer greet length and then throwing that into our output instead of just doing it directly greeting.length within our output like this. Well you can it does work exactly the same as you can see but the reason why we would want to do it this way is because we can reuse this integer right here. You see if you needed to do greeting.length elsewhere in the code instead of running the length method on greeting five times we do it one time, store that value in greet length, and then now we can call greet length as many times as we want. Oh yeah, to make things simpler, all of these data types that are grayed out means they're not being used. So all of this is not being used. These are the only three lines of code within this main method. And that's it. This is the entire Java program right here. And let's say we wanted to perform some arithmetic and not only that, but create our own method. Well, let's just do the arithmetic first. So what we're going to do, we already have an integer right here, which is the length of this greeting. What I can also do is create, I don't know, let's just create a number, kind of what we had before and call it four. Okay. And then again, what we can do is greet length plus number, and that output should be nine, five plus four equals nine. Beautiful. But instead of having it in here, And I do know we already went over this, okay, but we can do int and then we can have total and it'll equal Greet length number and just like before if we print out total it should still be nine and there's the nine However, there's also another Way to go about this and that's where creating our own method would come into play so within this I'm going to create a method public static int add We'll call it add and we'll throw in int a as parameter 1 and int B as parameter 2 and this is what we're going to Do inside the function. Oh it already is there okay return a plus B What we can do is take our method add and we will pass in greet length as well as number as parameters, A and B, which will return A plus B. And we are still returning total. So what should occur is we get nine again, and there it is. Now you may be wondering, well, how are we going to all this trouble just to add two values together and print out the total? Well, one, this is just example, but two, it's for reusability. These methods are for reusability, for readability. It's pretty simple that we're adding this. greet length and number together, although it is pretty straightforward when all you do is greet length plus number. But what happens if you want to get something like a ratio of likes and dislikes, like on this YouTube video? Of course, there's going to be zero dislikes and a bunch of likes, so make sure you like the video. And the math for this is a little bit more complex. So what you would want to do is likes divided by likes plus dislikes times 100. However, More often than not, this is going to result in some sort of decimal. Remember, integers are whole numbers. Doubles, or floats and things of that nature, doubles are decimals. So we need to change what we're returning to a double, and then we're good, right? No, because actually, we need to add an if statement, because what happens if likes plus dislikes actually equals zero? Well, then we want that to return zero. And then this works. I don't know why this is so big over here anyway. So instead of adding greet length and number, what we want to do is do a ratio of likes to dislikes and make this a double and we can run this. And I don't even know what the actual ratio would be, but it's 99.9. Wow. That's pretty good. So what happens if it's, if it's 435, then that should be 50% even, right? 50 on the dot. So this is a very good use case as to why you would want to do something like create a method for some sort of arithmetic Because if you have to create the ratio five to ten times Well, you don't want to write this five to ten times All you have to do is write it once and then you can call it as many times as you want With as many different like to dislikes for different YouTube videos as you want Now we're going to take this a step further. So what we're in right now is the main class. In the main class, you want to keep as clean as possible. This is the entry point into your application. It coordinates the overall flow rather than you wanting it to contain business logic and complex operations and things of that nature. You don't want that. So one, we would want this out of here anyway, but let's pretend that this was the Chick-fil-A class. And for whatever reason, you're trying to get the ratio of likes to dislikes on a new chicken sandwich, okay? So it makes sense for this method to be in here because this is where you're going to be using it. However, what if you also had a Burger King class? Not the best in the world, but Burger King class. Or anywhere else. Anywhere else where you would want to use the ratio method. You don't have to dive into your Chick-fil-A class every single time in order to... call the ratio method. So what you would want to do is come over to source. We'll right click. That's weird. Okay. Maybe because I'm zoomed in 2200%, but okay. Java class. And we're going to create a class called math utils. That's what we're going to call it. And within this math utils, we're going to have all of our math methods, like the ad, the ratio, the whatever you want. So that way it's nice and clean. We know exactly where it is. Of course, it's going to be in math utils. So I'm going to pull this, paste it over here. And now we have ratio in our math utils. But wait a second. How are we going to use ratio in this Java class if it is in this Java class? Well, instead of just having ratio, we would have math utils dot ratio and it should work. Just like that. Now we skated over a few things that I want to talk about and those are flow control statements like your if statements, if else statements, your switch statements, your for loops. And I want to talk about those a little bit. So you have an if statement right here and it is very straightforward. It's if this, which this right now is likes plus dislikes equals zero, then do what is inside these curly braces, which is return zero. However, if this criteria is not met, It skips over this logic right here and goes directly to the next line because it does not enter this. And that for loop that we saw before was going through. So actually, let me write a new one using greet length, since this is an int and it's greeting length, as you can see, hello, hello, hello. Hello has been growing this entire video. I don't know if you've noticed that, but I just spoiled it for you. So there we go. So if we want to write a for loop, we can do it just like this. And that's not exactly what I want. I want to do int i equals zero. And then how this works is when I is less than greet length. Um, I plus plus, which that is just adding one to I every single time it goes through this for loop. So every single time it goes through one system out print Lynn, this exactly greeting dot char at I and allow me to simplify this real quick, we will not need this for this instance, which means we don't need this, which means we don't need any of these. This is all the code that we're using right now. created this string we got the length of the string and now we're using the length of the string in the for loop to loop through that length and print out each character that is within it and let me explain how that works so we start off with i equaling zero okay so if you want to visualize this i equals zero right now greet length equals 15 because as you can see this is 15 right here and as you can see zero is less than 15 so it is going to do what's inside these curly braces which is print out the character at that I position, which as we discussed is zero of greeting. So this is in fact the zero position, just to clarify, it's not the one position, it's a zero position. And so it'll print out an H and then it'll add one to I making this one. And then one is in fact less than 15 and it'll come through here. This will be one. So then it'll print out E and then it'll add one to one. making it two, come in here, print out the next one. That's how it works. It is just, we are using it like this because we wanted to come up here and change the greeting instead of changing this, you know, from 15. Oh, that's only five letters. Now change it to five. It changes all dynamically. You don't have to worry about it. And then the same breath I changes dynamically as well. So it can be zero and zero. And then when we add one, it can be one. And one and then it could be two and two you see what I'm saying So whatever I is that's what it equals we want all this to be dynamic not hard coded in here That's why we use the variables So what should happen is that this should be printed out in the terminal when we click run and as you can see it is However, I wanted each letter to be on its own line That is why I use println because each time it puts it on a new line So we can save that run it And then each line will have its own character. H-E-L. And it keeps going all the way through the entire string. That's a for loop. So that is Java in a nutshell. I don't think this was the world's shortest Java course. I kind of... I feel like everything I talked about in this video was important. I didn't want to cut all of that out to try to make it the world's shortest Java course. That's fine. However, what I did do is I removed some off the tail end of it. Where I created like a little inventory. System with items and inventory and how to manipulate those and create a collection of items that collection being an array list So we go over a few things there I'll upload that video like tomorrow or something because it's already recorded and basically edited it down I'll upload that in a separate video But if you can't wait until then or you just want to sharpen your computer science skills check out the sponsor today's video Brilliant and my favorite thing about brilliant is that you learn by doing let me show you So you see all of these different learning paths, but when you drill in you can see all of these different courses. Let's go to the algorithms and data structures course, and then you'll see all of these individual lessons. There are thousands of interactive lessons on Brilliant, and these are concepts that you need to understand if you want to become a good software developer. And the courses are designed to develop your critical thinking skills through problem solving, not just memorization. And the best part, you can learn a little every day. building a powerful habit that'll boost both your personal and professional growth. I'd recommend the Thinking and Code course because it builds a solid foundation for computational problem solving. You'll learn about how programs operate. You'll learn about the concept of repetition by writing a program to collect all of the coins using as few blocks as possible. You'll learn about the concept of conditional statements in an interactive way, as well as combining conditionals. And it's not just for beginners. I've taken this course myself just to keep my skills sharp. Not only that, but like, they're kind of fun. So if you've ever wanted to understand how smart systems like these actually work, Brilliant is a perfect place to start. Just visit brilliant.org slash Forrest Knight to get a 30-day free trial and 20% off an annual subscription. Hope you enjoyed it. Y'all have a good one. Have fun learning Java. Not JavaScript.