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

Java Lesson 2 | Variables


3m read
·Nov 3, 2024

Hey, this is Jake. I'm a special guest on Mac Heads 101, and today I'm going to be talking about variables in Java.

So, I'm going to open up Eclipse. What a variable is, is it's basically something that stores some information. There are two parts to it essentially: the data type and then the name. The name is just to reference it, and you can pretty much use any name you want, except it can't have spaces in it, and you can't start with a capital letter.

I'm going to be talking about four different data types in this tutorial. The first is int. So, to do that, you type int, and I'll call it whole number because that's essentially what an INT is. Int can only store the values of whole numbers; it can't store values with decimals. So, to give it a value, I use equals 10, and I could make that equal any integer I wanted.

The next one I'm going to show is double. You write double, and I'll call that decimal number because that is a decimal. I can set that equal to something like 5.32, and that’s fine. If you set it to 5.32, you would get an error; I'm just going to show you that right now. See? Because it has to be a whole number.

The next one I'm going to be showing you is char, which is a single character like a letter. So I'll call it char letter equals, and then in single quotes, whatever you want. So I'll make that equal to 'B'. Then, if you want to do multiple letters, like if you want to do a phrase, you have to do string. String starts with a capital 'S'. So, string I'll call it phrase equals, and then in double quotes "maads 101".

All right, so now there's the form I'm going to be showing you. If you want to print out a variable, what you have to do is it's pretty simple. If you're just printing out a variable, by the way, always end a line of code with a semicolon.

So, let's say I wanted to print out the phrase "Mac has 101". So I just put phrase in there, and that will print out the value because if I do it without quotes, it'll print out the value of that variable. See? "Mac has 101".

And if I wanted to print out a phrase and a variable, you have to do this. So, I'll just, in these quotes, I'll put "Hello, this is", and then I'll put a space because there's going to be another word. I have to put plus, and that will print out "Hello, this is" plus "maads 101".

And that's how you do some text and variables. So, here: "Hello, this is maads 101". There we go!

You can also do math here. So I could have int wholeNumber equals 10, double decimalNumber equals 5.32, and then I could have another double called sum equals wholeNumber plus decimalNumber. If I print that out, it'll print out the value of wholeNumber plus decimalNumber, which is 15.32.

So now, you can do plus, you can do minus. If you want to multiply, you have to use an asterisk, and that would multiply the two. So I'll just print that out; I'll show you then multiply the numbers.

To divide, you use a slash like that, and that's division. There's a lot more math stuff you can do, and I'll be making another tutorial on that. But you actually have to import something; you have to import java.lang.Math to use a lot of them.

But that is all for variables. So, see you next time!

More Articles

View All
Example: Graphing y=3⋅sin(½⋅x)-2 | Trigonometry | Algebra 2 | Khan Academy
So we’re asked to graph ( y ) is equal to three times sine of one half ( x ) minus two in the interactive widget. And this is the interactive widget that you would find on Khan Academy. It first bears mentioning how this widget works. So this point right …
Formulas and units: Comparing rates | Working with units | Algebra I | Khan Academy
We’re told that Hannah and Martine each got a plant for their home. Hannah measured that her plant grows on average two centimeters per week. Martine measured that her plant grows on average three millimeters per day. Which plant grows faster? Pause this…
BEHIND THE SCENES Of Shark Tank During COVID | Kevin O'Leary
I’m um in Las Vegas somewhere in quarantine getting ready to shoot Shark Tank, in the bubble, as they say. [Music] So anyways, I’m um in Las Vegas somewhere in quarantine somewhere and, uh, getting ready to shoot Shark Tank real soon in the bubble, as the…
Hanging out with a monitor lizard | Primal Survivor: Extreme African Safari
There’s a monitor lizard right there, right on that termite mound, just basking in the sun. They can either play dead or they can run like grease lightning. Let me see if I can get a better look at it. I can see that this monitor could use a little hel…
World's First Electric Generator
[Applause] I have a pipe. Yeah, do you want to hold it? Do you know what it’s made of? Metal. Is it brass? Copper? Coer? Is copper magnetic? No? Uh-oh. I’m going to go. No, I didn’t think it was. Go, yes! I’m going to go. Yes! Well, why don’t we check? Th…
My Response to the NAR Lawsuit (Real Estate Is Changing Forever)
What’s up, you guys? It’s Graham here. So this is something I wasn’t sure I wanted to address or even if there’d be enough interest for me to talk about this. But after repeatedly being asked to comment on the recent class action lawsuit against the Nati…