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
An organism's niche | Ecology and natural systems | High school biology | Khan Academy
In this video, we’re going to talk about an organism’s niche, and all a niche is, it’s a fancy way of saying where you will actually see an organism: the environmental conditions where you are going to actually see organisms of a certain kind. To underst…
Watchers of the Land | Short Film Showcase | National Geographic
[Music] And you can’t ever lose your history or your stories; otherwise, you’ll lose who you are. It’s the Den way to pass on your teachings to younger [Music] people. A lot has changed since the 50s, and now that we do have a say, you know, we’re going t…
Would You Risk Venomous Insect Stings for Your Job? | National Geographic
Harvest rants are intriguing because not only are they among the most painful of all stinging insects, their venom is 30-40 times more toxic than, say, rattlesnake venom. If you pick them up by hand, well, you might just get stung. The biggest risk around…
McCulloch v. Maryland | National Constitution Center | Khan Academy
Hey, this is Kim from Khan Academy, and today we’re learning about McCulloch versus Maryland, a Supreme Court case decided in 1819 that helped to define the relationship between the federal government and the states. The question at issue in this case was…
LearnStorm Growth Mindset: Khan Academy's humanities content creator on social belonging
Hey, I’m Kim Kutz Elliott and I work on humanities content at Khan Academy. So yeah, I thought about things that were really difficult for me. One thing, um, that was hard for me was class discussion because I went to this history class, and I swear that…
Ecosystem dynamics: Clark’s nutcrackers and the white bark pine | Khan Academy
What’s that? That sound, that call, sounds like something a crow would make but not quite. That’s actually the call of a really interesting bird called Clark’s nutcracker. These birds are cousins of the American crow, which you might see and hear around …