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
Why I won't be getting the Apple Credit Card...
Alright guys, so we got to have a heart-to-heart and talk about the Apple credit card and why I won’t be getting one. They know a lot of you have asked for my opinion when it comes to this, especially coming from the perspective of someone who’s a bit of …
Impacts of Agricultural Practices| Land and water use| AP Environmental science| Khan Academy
Hey there! Today I’m going to cover the impacts of agricultural practices. To do so, I’m going to take you through my morning ritual. It sounds weird, but my bowl of multigrain Cheerios and rice milk and relaxing in my super comfy pajamas are all connecte…
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…
This Empowering Memorial Honors the Legacies of Military Women | National Geographic
I remember vividly at the dedication 20 years ago of the memorial. There was a World War I veteran in her uniform who spoke. She said, “When I served in the Navy, women were not even allowed to vote.” I thought, what a brave woman! So in that hundred year…
The Seventh Amendment | US government and civics | Khan Academy
Hi, this is Kim from Khan Academy. Today, we’re learning more about the Seventh Amendment to the U.S. Constitution. The Seventh Amendment guarantees the right to juries in civil cases when the value in controversy is greater than twenty dollars. To learn…
Scaling functions horizontally: examples | Transformations of functions | Algebra 2 | Khan Academy
We are told this is the graph of function f. Fair enough. Function g is defined as g of x is equal to f of 2x. What is the graph of g? So, pause this video and try to figure that out on your own. All right, now let’s work through this. The way I will thi…