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
Colbert's Life in the Swamp | Live Free or Die
[Music] [Music] Every day in the woods is just a constant challenge. It’s urgency after urgency, project after project. Got an otter! It’s a river otter. This is one of my most valuable pelts; it brings a top price, and, uh, not many people do, but I try…
The Largest Housing Crash Is Coming | Why I Sold
What’s up, guys? It’s Graham here. Now, I usually don’t record informal videos without a whole bunch of charts and graphs and fancy research, but something needs to be said about the current state of the housing market and the direction it’s headed. I do…
Khan Academy India Talent Search 2017
Hi, I’m Sal Khan, founder of the Khan Academy, and I just want to let you know about our India talent search. As you might know, Khan Academy, we’re a not-for-profit with a mission of providing a free, world-class education for anyone, anywhere. To us, t…
This Just Ruined Robinhood...
What do the guys? It’s Graham here, so let’s go ahead and spill some drama, or the T, as they say on YouTube, with some of the recent changes that have been going on with Charles Schwab, Robin Hood, TD Ameritrade, and some of the other brokerages that are…
Life's Biggest Paradoxes
In life, anything is possible because we can never fully understand how the world works, and the laws of physics prevent us from being able to tell the future. Everything we predict is a probability; some are a lot more probable, others are less probable,…
Genetics vocabulary | Inheritance and variation | Middle school biology | Khan Academy
We know that any sexually reproducing organism is getting DNA from both its male parent and its female parent, and that’s true also for human beings. You might know we have 23 pairs of chromosomes, but let’s zoom in on one of those pairs. So, let’s say th…