Variable Types

Learn the Basic Java Concepts Working with variables and operators
14 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.44
List Price:  €93.49
You save:  €28.05
£56.04
List Price:  £80.06
You save:  £24.02
CA$95.74
List Price:  CA$136.79
You save:  CA$41.04
A$107.11
List Price:  A$153.03
You save:  A$45.91
S$95.36
List Price:  S$136.24
You save:  S$40.87
HK$547.90
List Price:  HK$782.75
You save:  HK$234.85
CHF 64.03
List Price:  CHF 91.47
You save:  CHF 27.44
NOK kr772.60
List Price:  NOK kr1,103.76
You save:  NOK kr331.16
DKK kr487.88
List Price:  DKK kr697.01
You save:  DKK kr209.12
NZ$117.84
List Price:  NZ$168.36
You save:  NZ$50.51
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,700.43
List Price:  ৳11,001.09
You save:  ৳3,300.65
₹5,837.33
List Price:  ₹8,339.41
You save:  ₹2,502.07
RM333.81
List Price:  RM476.90
You save:  RM143.08
₦92,489.99
List Price:  ₦132,134.22
You save:  ₦39,644.23
₨19,474.37
List Price:  ₨27,821.72
You save:  ₨8,347.35
฿2,588.61
List Price:  ฿3,698.17
You save:  ฿1,109.56
₺2,275.23
List Price:  ₺3,250.47
You save:  ₺975.23
B$358.08
List Price:  B$511.57
You save:  B$153.48
R1,312.98
List Price:  R1,875.77
You save:  R562.78
Лв127.91
List Price:  Лв182.73
You save:  Лв54.82
₩96,452.39
List Price:  ₩137,795.03
You save:  ₩41,342.64
₪267.73
List Price:  ₪382.49
You save:  ₪114.75
₱4,034.64
List Price:  ₱5,764.02
You save:  ₱1,729.38
¥11,065.06
List Price:  ¥15,807.91
You save:  ¥4,742.85
MX$1,201.03
List Price:  MX$1,715.83
You save:  MX$514.80
QR255.05
List Price:  QR364.38
You save:  QR109.32
P965.79
List Price:  P1,379.76
You save:  P413.97
KSh9,296.24
List Price:  KSh13,280.92
You save:  KSh3,984.67
E£3,349.82
List Price:  E£4,785.67
You save:  E£1,435.84
ብር3,985.10
List Price:  ብር5,693.24
You save:  ብር1,708.14
Kz58,375.85
List Price:  Kz83,397.65
You save:  Kz25,021.80
CLP$66,375.71
List Price:  CLP$94,826.51
You save:  CLP$28,450.80
CN¥507.11
List Price:  CN¥724.48
You save:  CN¥217.36
RD$4,113.13
List Price:  RD$5,876.15
You save:  RD$1,763.02
DA9,392.53
List Price:  DA13,418.47
You save:  DA4,025.94
FJ$158.35
List Price:  FJ$226.22
You save:  FJ$67.87
Q544.06
List Price:  Q777.27
You save:  Q233.20
GY$14,678.83
List Price:  GY$20,970.66
You save:  GY$6,291.82
ISK kr9,837.79
List Price:  ISK kr14,054.59
You save:  ISK kr4,216.80
DH708.72
List Price:  DH1,012.51
You save:  DH303.78
L1,246.75
List Price:  L1,781.16
You save:  L534.40
ден4,025.16
List Price:  ден5,750.48
You save:  ден1,725.31
MOP$565.76
List Price:  MOP$808.27
You save:  MOP$242.50
N$1,340.22
List Price:  N$1,914.68
You save:  N$574.46
C$2,582.07
List Price:  C$3,688.83
You save:  C$1,106.76
रु9,353.99
List Price:  रु13,363.42
You save:  रु4,009.42
S/263.57
List Price:  S/376.55
You save:  S/112.97
K270.82
List Price:  K386.91
You save:  K116.08
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,859.31
List Price:  ZK2,656.27
You save:  ZK796.96
L325.94
List Price:  L465.65
You save:  L139.71
Kč1,644.39
List Price:  Kč2,349.23
You save:  Kč704.84
Ft25,720.62
List Price:  Ft36,745.32
You save:  Ft11,024.70
SEK kr762.14
List Price:  SEK kr1,088.81
You save:  SEK kr326.67
ARS$61,174.36
List Price:  ARS$87,395.69
You save:  ARS$26,221.33
Bs486.59
List Price:  Bs695.16
You save:  Bs208.56
COP$277,528.94
List Price:  COP$396,486.91
You save:  COP$118,957.97
₡35,172.98
List Price:  ₡50,249.28
You save:  ₡15,076.29
L1,732.56
List Price:  L2,475.19
You save:  L742.63
₲520,857.74
List Price:  ₲744,114.38
You save:  ₲223,256.64
$U2,703.34
List Price:  $U3,862.08
You save:  $U1,158.74
zł282.38
List Price:  zł403.41
You save:  zł121.03
Already have an account? Log In

Transcript

Hello there, and welcome back to the Java development course. So last time we went over working with variables, and you had some homework to do, your job was to create three variables of type integer, assign them just some values, and then print the sum of those values. So this is what you should have done. So here I have three values A, B, and C, I find them the value 1210, and 10. And then I just print the sum of those values a plus b plus c. And let's run this application. And there we go, we get 32.

Now, if we change a to 13, and must have run this application, we get 33. So it automatically changes according to what how it changes these values. So that is essentially how we can add three numbers together in Java. So if you got this that's very good. If not, don't worry. This is that like the first Java homework that you've ever done, most likely, so Don't you know, don't beat yourself up about it understand what this code does.

And if you can understand what it does that then you're good to go for the next lessons. So without further ado, then let's get into this lesson. Alright, so today we're going to be going over variable types in Java. And we actually already went over our very first date variable type, which is called integer. And we're going to go over exactly what that means in just a minute here. But first of all, let's go.

Let's go and create a new class. All right, so the class name will be variable types. There we go, as always, public static, void, main. And finish. And there we go. Now we have a variable types Java.

So now let's go ahead and delete this comment here. And actually, let's very quickly go over comments. So comments are things just something that Java will ignore. So if you go to actually write some anything here, we can write anything. And then we actually we could actually just create an multi line comment by doing this. There we go.

And right we can write anything here as well. And so this will this will just be ignored by the Java compiler. So we can now run this application. And as you can see, nothing happens since this is all commented. So even if we put, I don't know, maybe system out print ln, print ln hello world into here, nothing will happen since it is commented. So this is just a single line comments, everything we put after double slashes will be a comment.

And then it's just a multi line comment. So comments are actually pretty important since they help us you know, write code to ourselves, messages to ourselves, sort of or other developers. So if you have a very big application we can do, we can actually write a comment that's like this application, or this part of the application does something so it adds two numbers together, for example, and then later on, another developer will stumble upon what you wrote written, and he'll just look at it and he'll Be like, Oh, that's what it does. And so yeah, so comments are actually considered a very good programmer will write good comments. So that's just, you know, usually that's usually considered the truth sort of. Okay, so without further ado, then let's get into today's lesson, we're going to be going over data types.

So Java actually has seven or eight data types. I can never really remember them number I know them. Let's take a look at them. So let me go to write a muslin common here. There we go. Okay, so first of all, we have int, then we have long then before we actually have short, and then before short, yeah, bite.

Okay, and then we also have float and float and double. And don't worry, we're gonna go over exactly what they mean in a second here. And then we also have Boolean and chart as well. So I guess I guess this is eight. So it's eight after all, not, not, not not not seven. Okay, so these are the primitive data types in Java.

So primitive data types just mean that, you know, they're sort of built in, let's just let's just call it, let's just call it like that for now, you'll understand exactly what that means in a couple lessons here. But for now, just understand these are built in. So what is the difference between them? So why do we need a bunch of different data types? So first of all, let's go over these four symbols to understand and we've already gone over it. So an int is just a number so we can actually go out and do int a equals 12.

Okay. Short, essentially also a number. So we can do short, b equals 13. And then a bite, same thing. Bite is also a number. It's a whole number.

And then we're actually no we got we actually need to do 12. And then we actually need to add this B right here. B What's the problem? Oh, right. I didn't put a name to the bite. What a Dumbo.

Okay, there we go. Bite k equals 12. And then I was thinking, What's the problem? What's the problem there? I didn't print it. Okay, bite shorten and then long.

So all of these all of these are just numbers, whole numbers. So why in the heck do we need four different types of whole numbers? Well, I'll tell you why, because of memory. So these four numbers have both at all of them can hold different amounts of numbers. So different maximum number so we can actually print System dot out dot print ln. Print ln, byte dot max value.

Let's actually just do the same thing for all four of them. So byte short, long. No, it's gonna be integer, integer and long. So Elisa does, you're gonna put the maximum value that all of these data types can hold. Let's go and take a look. So there we go.

So a byte can hold 127 values. So what does that mean? So that means at the maximum, it could hold at 127. If we do 128, here, we get an error. So 127 is the maximum number of bytes can hold, the maximum number of shorts can hold is 32,767. There we go.

The maximum number an integer can hold is 2 billion as we went over it already. And the maximum number along can hold is actually this right here. Or no, for type int, is that a range? Okay, so we'll go over exactly why this why we're getting this error in a second here. So this is just the different amounts that a different data types can hold. So a byte can hold a maximum of 127 shorts can hold them Some 32,207, a integer can hold a maximum of 2 billion and so on, and so on could hold a maximum of this number.

I don't really know how to describe it. But yeah, so just this number. So that's essentially how we work with different variable types and how that's how different variable types work and why we have different variable types. Now, I'm sure you're wondering, why in the heck Can't we just have one variable type that has the maximum so just long, so why do we need all these? Well, in short, is because memory, so in memory, essentially, is just the thing that holds the numbers themselves. And in memory, the higher a number is, the higher it sort of, the higher it can hold.

So if we have let's just say, I don't know, maybe 80 slots of memory, it can hold a certain numbers, let's say like a billion, it's a lot higher, but let's just say a billion and so on. If we put the number two into the ad slots, then what's the point of you know, having the ad slots, we don't need them when we need to. So as a result, we have this these different data types. So it doesn't really work that way that we can, you know, maybe assign 40 data, data 40 memory slots, and then just, you know, get take more as needed, it doesn't really work that way, we would then have to delete the 40 and then copy that number into a completely different memory slots. So that's just sort of how it works. When result we have these four data types, they can hold different amounts of memory.

So byte can hold a byte short can hold, I think two bytes into can hold four bytes, and then long could hold eight bytes, I think. So that is essentially how it how it works. Now, why are we getting this error here? Well, because we actually need to cast this down to one Right now it's actually trying to say that this right here is an int, integer and not a long. So in order to make it a long, we just have to do l at the end here. And it'll understand that this is actually a long and not an integer.

So if you ever get an error like that, just put an L at the end of it. And so that's essentially how we work with numeric data types. Those are the number data types integer data types at least. Okay, so what if we need to hold decimal points? So numbers with decimal points, what do we do then? Well, for that we need this float and double as well.

So a float and we'll both hold just decimal points, so number of decimal points so we can have float, G equals 3.4. All right, there we go. Of course we get an error since it's actually trying to create a double so when we create a number with a decimal point in Java, it will automatically think that it is a double so Need to just put an F at the end? And that will just tell it you know, this is not a double, this is a float. And then same thing with double, so Double D equals 4.5 4.5. Okay, so I'm sure wondering at this point, why do we need to separate two separate types for decimal points?

Well, for the same reason that we need to separate types, for separate types for numbers, because of memory. So a float can hold a maximum of a certain number of values. And then a double can also hold a maximum of a certain number of values. So we're going to get the maximum exponent so system, dot out dot print ln float dot max exponent. So what does the max exponent mean? Well, that just means that it's the maximum number of values we can have on both sides.

So in this case, we have two values on both sides. And if we add two more, three more now we have five values 12345 Five values, that's just the maximum number of exponent it can hold. So we can actually do float dot max exponent. And then we can do the same thing for double as well. double dot max exponent, let's run this. And as you can see, so the maximum exponent for float is 127.

So that's the maximum numbers, it can hold. amount of numbers you can hold. And then the maximum exponent for double is 1002 23. So that is essentially how we work with different data types. So we'll actually go over Boolean and char in the next lesson. Since in this lesson is about it's best to just keep it with numbers, right?

So we're going to go over one last thing about how we can add different numbers together. So what we can actually do is we could do float. Let's just say sum equals 4.5 plus 656 plus, let's say G. Okay, and so right now we're going to get an error. So Type mismatch cannot convert from double to float. So what has happened? Well, you know, here how we, when we do this, it automatically has, you know, tells you that this is a double and not a float.

Well, that's because that's just the way Java works. So whenever we create a number with a decimal, it automatically created as a double. And so that's Central, but to do it what it's doing here. So for that, we actually just need a casting as well. But we can actually just do F at the end here, because that just just won't really work. In this case, what we actually need to do is we just need to do, actually, Eclipse has a very handy here add cast to float right here.

So there's this we'll just solve our problem. And there we go. So we just added the cast of float. So it's going to add this together, and then whatever result it gets, it's just going to change it to float. And so now we can actually print the sum. System dot out dot print ln sum.

All right, there we go. And let's run this There we go. And so it prints a sum as 63.959656. And there we go. Okay, so that's essentially how we can work with different types of numeric data types in Java. So, without further ado, then let's assign you some homework.

Okay, so for homework, your job is to create a variable of every single type. So of int, short of a single numeric type, I mean, so int, short, byte, long, float and double. And then finally, create one last double, which will contain the sum of all those variable types. And then finally, just print that sum of all those variable types. So just assign some random numbers to it. It doesn't actually have to be anything.

So just like the last lesson pretty much except with that different variable types. So without further ado then I wish you luck and I'll see you next time soon.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.