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
How To Manage Your Money Like The 1%
What’s the guys? It’s Graham here. So CNBC just posted an article saying that 60% of Americans would go into debt if a thousand-dollar emergency came up. I read that and I thought to myself, this is absolutely unacceptable, and this has to change. Hearin…
How to renovate your private jet
I was going to say, like, what’s a turnaround time for the change of an interior? Also, for example, if you put half a million into renovating the interior, there are three things you could do to the interior: the soft goods, which is all the fabrics, whe…
How to see with sound - Jacques S. Abramowicz
In a pitch-black cave, bats can’t see much. But even with their eyes shut, they can navigate rocky topography at incredible speeds. This is because a bat’s flight isn’t just guided by its eyes, but rather, by its ears. It may seem impossible to see with s…
Best Crypto To Buy Right Now | Kitco NEWS
[Music] So in this environment, Roy, give me your top DeFi or crypto investment. Is there an altcoin that you think has room to rally? Give me, in two sentences, your top investment in the space right now. “I’m going to disappoint you right now because I…
I Struck A Match With a Bullet (380,117 frames per second SlowMo) - Smarter Every Day 294
Hey, it’s me, Destin. Welcome back to Smarter Every Day. Goggle up because science is about to happen. This is my buddy John Henry. Hey, how’s it going? Way back before, Smarter Every Day was Smarter Every Day; it was just a couple dudes hanging out in th…
2017 Berkshire Hathaway Annual Meeting (Full Version)
Thank you and good morning. Duh, that’s Charlie. I’m Warren. You can tell us apart because, uh, he can hear and I can see. That’s why we, uh, work together so well. We usually have our specialty. Uh, I’d like to welcome you to, uh, we got a lot of out-of…