yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Java Lesson 5


3m read
·Nov 3, 2024

Processing might take a few minutes. Refresh later.

Hey guys, this is Mids on one with Java lesson five. In this Java lesson, you're going to learn about if statements, which are a very important part of programming.

So first of all, I'm going to create a new Java project, Java tool, and I'm going to call it if statements. Save it to my desktop. Now, I'm going to go under Source, if statements.java, and edit this file. So I'm going to get rid of all this and put in some code.

So first let me explain what an if statement does. An if statement compares two things or checks if something is true or false. In this case, we're going to do both. So here's what we're going to do. First, we're going to compare two ints. So I'm going to make int i equals 2 and int i1 equals 2.

Now we're going to do system.out.print line checking if, and now I'm going to do this and say "r equal." Okay, so now I'll say if i equals equals i1. This is the code right in between these two curly braces that will happen if this condition is true.

So if i is the same as i1, represented by this double equals, that's comparing them, then it'll go to this code. So let me just go over this one more time. One equals assigns a value, two equals compares a value. So I'm doing if i equals equals i1.

Now this is the code that will happen if they are equal. Right here, I'm going to say system.out.print line, and actually I'll just copy this and I'll just get rid of this. So there we go. So now it'll print out that they are in fact equal.

Now if they're not equal, I'm going to say they're not equal. So I'm going to say else. Now all the code between this curly brace and this curly brace is going to happen otherwise. So if i does not equal i1, right here they are not equal. And that's how to compare integers.

So if I run this... Sorry, I accidentally put an 'L' there instead of a semicolon. So if I run this and I go into the console, here we go, checking if two and two are equal. Two and two are equal.

So if I change i1 to be 3 and I run the console... Oh, no. I'll go back to 2.

So now I'll repeat this process for Strings. Now, comparing a string is different. I'm going to declare String a equals "I" and I'm going to do String A1 equals "high." Now here's an interesting thing: I'm going to print out the same thing checking if a equals A1, but I decide maybe I want to put a and A1 in quotes so that way people know they’re strings.

So here's a question: how do I put a quote in a string? That's a good question. What you do for that is backslash quote. Just like backslash n, backslash backslash, all those things. Backslash quote will put a quote inside of quotes.

So backslash quote, and then I'll put another quote do a, and now I'll do another backslash quote. There we go! Also, another thing is double backslash puts one backslash in because, um, otherwise it wouldn't work perfectly. Because yeah, so I'm checking if a and A1 are equal.

So I'm going to say if... and here's how to compare two strings: you can't do if a equals equals A1. That doesn't work. You have to do a.equals(left parentheses A1 right parentheses).

Now here's an interesting thing: classes such as a string have functions on them, or methods, whatever you want to call them. Now the string class happens to have a function on it called equals, and if you pass equals a string, it'll return a Boolean for whether they are equal or not.

Now a Boolean is either true or false. So we're calling a function on a to see if it's equal to A1. So we're passing equals A1, that's how you pass stuff. Then if it's true, which if checks if it's true, then it'll go to this.

So I'll say system... Or no, I'll copy this again. There we go. Now I'll say else, and here's the code here. So this will work too. I'll run it.

Run my console, checking if checking if high and high are equal. High and high are equal. So now if I change this to high one, high and high one are not equal.

So there you have it. So now I'm going to do another thing for booleans. I'm going to say Boolean b equals true. So now I'll do system.out.print line checking if... now add b, so it'll either put true or false in this. This is true. So I'm going to check if b is true, and the way you do t...

More Articles

View All
Cellular respiration | Energy and matter in biological systems | High school biology | Khan Academy
In this video, we’re going to talk about cellular respiration, which sounds like a very fancy thing, but it’s really just about the biochemical processes that can take things that we find in food and convert it into forms of energy that we can use to do t…
Cavalieri's principle in 3D | Solid geometry | High school geometry | Khan Academy
So we have two cylinders here, and let’s say we know that they have the exact same volume, and that makes sense because it looks like they have the same area of their base, and they have the same height. Now, what I’m going to do is start cutting up this…
Picture of Everything? -- DONG
This website lets you create a custom message that takes up the entire page. You can then share the custom URL with friends to say something loudly, bigly. But for more things you can do online now, guys, this is DONG. The Sound Walk is like Guitar Hero …
... and why!
The reason this trick works every single time is elegantly simple. It has everything to do with the fact that their chosen card will always be in a pack that is third from the top. That’s because we had them take the pack containing their card, see? Ther…
Zeros of polynomials: plotting zeros | Polynomial graphs | Algebra 2 | Khan Academy
We’re told we want to find the zeros of this polynomial, and they give us the polynomial right over here, and it’s in factored form. They say plot all the zeros or the x-intercepts of the polynomial in the interactive graph. This is a screenshot from Khan…
Safari Live - Day 340 | National Geographic
This program features live coverage of an African safari and may include animal kills and carcasses. Viewer discretion is advised. A very good afternoon to you all far and wide from the Maasai Mara here in Kenya. We have a lioness over there. My name’s L…