Variables And Data Types

Complete Step By Step Java for Testers Java Concepts - Data Types
18 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€85.19
List Price:  €119.27
You save:  €34.08
£74.77
List Price:  £104.68
You save:  £29.91
CA$137.78
List Price:  CA$192.89
You save:  CA$55.11
A$150.31
List Price:  A$210.44
You save:  A$60.13
S$129.19
List Price:  S$180.88
You save:  S$51.68
HK$777.80
List Price:  HK$1,088.96
You save:  HK$311.15
CHF 79.59
List Price:  CHF 111.43
You save:  CHF 31.83
NOK kr1,013.63
List Price:  NOK kr1,419.13
You save:  NOK kr405.49
DKK kr636.11
List Price:  DKK kr890.58
You save:  DKK kr254.47
NZ$172.27
List Price:  NZ$241.19
You save:  NZ$68.91
د.إ367.21
List Price:  د.إ514.11
You save:  د.إ146.90
৳12,200.05
List Price:  ৳17,080.55
You save:  ৳4,880.50
₹9,057.54
List Price:  ₹12,680.92
You save:  ₹3,623.38
RM409.68
List Price:  RM573.58
You save:  RM163.89
₦144,900.08
List Price:  ₦202,865.92
You save:  ₦57,965.83
₨27,977.78
List Price:  ₨39,170.01
You save:  ₨11,192.23
฿3,144.12
List Price:  ฿4,401.89
You save:  ฿1,257.77
₺4,257.57
List Price:  ₺5,960.77
You save:  ₺1,703.19
B$541.88
List Price:  B$758.66
You save:  B$216.77
R1,687.37
List Price:  R2,362.39
You save:  R675.01
Лв166.64
List Price:  Лв233.31
You save:  Лв66.66
₩147,438.25
List Price:  ₩206,419.45
You save:  ₩58,981.20
₪322.26
List Price:  ₪451.18
You save:  ₪128.91
₱5,910.90
List Price:  ₱8,275.50
You save:  ₱2,364.60
¥15,573.94
List Price:  ¥21,804.14
You save:  ¥6,230.19
MX$1,801.20
List Price:  MX$2,521.76
You save:  MX$720.55
QR363.83
List Price:  QR509.38
You save:  QR145.55
P1,322.75
List Price:  P1,851.91
You save:  P529.15
KSh12,873.43
List Price:  KSh18,023.31
You save:  KSh5,149.88
E£4,730.87
List Price:  E£6,623.40
You save:  E£1,892.53
ብር15,598.69
List Price:  ብር21,838.79
You save:  ብር6,240.10
Kz91,690.83
List Price:  Kz128,370.83
You save:  Kz36,680
CLP$91,270.93
List Price:  CLP$127,782.96
You save:  CLP$36,512.02
CN¥705.37
List Price:  CN¥987.55
You save:  CN¥282.18
RD$6,346.47
List Price:  RD$8,885.32
You save:  RD$2,538.84
DA12,964.71
List Price:  DA18,151.11
You save:  DA5,186.40
FJ$227.15
List Price:  FJ$318.02
You save:  FJ$90.87
Q764.64
List Price:  Q1,070.52
You save:  Q305.88
GY$20,885.90
List Price:  GY$29,241.10
You save:  GY$8,355.19
ISK kr12,616.33
List Price:  ISK kr17,663.37
You save:  ISK kr5,047.03
DH918.43
List Price:  DH1,285.85
You save:  DH367.41
L1,687.62
List Price:  L2,362.74
You save:  L675.11
ден5,239.78
List Price:  ден7,335.90
You save:  ден2,096.12
MOP$800.52
List Price:  MOP$1,120.76
You save:  MOP$320.24
N$1,684.29
List Price:  N$2,358.08
You save:  N$673.78
C$3,674.23
List Price:  C$5,144.07
You save:  C$1,469.83
रु14,445.11
List Price:  रु20,223.73
You save:  रु5,778.62
S/336.11
List Price:  S/470.57
You save:  S/134.45
K430.33
List Price:  K602.48
You save:  K172.15
SAR375.18
List Price:  SAR525.27
You save:  SAR150.08
ZK2,303.63
List Price:  ZK3,225.17
You save:  ZK921.54
L433.54
List Price:  L606.98
You save:  L173.43
Kč2,066.70
List Price:  Kč2,893.46
You save:  Kč826.76
Ft32,660.30
List Price:  Ft45,725.73
You save:  Ft13,065.42
SEK kr926.89
List Price:  SEK kr1,297.69
You save:  SEK kr370.79
ARS$143,373.53
List Price:  ARS$200,728.68
You save:  ARS$57,355.14
Bs689.81
List Price:  Bs965.76
You save:  Bs275.95
COP$380,209.72
List Price:  COP$532,308.82
You save:  COP$152,099.10
₡49,937.50
List Price:  ₡69,914.50
You save:  ₡19,977
L2,628.31
List Price:  L3,679.75
You save:  L1,051.43
₲670,577.05
List Price:  ₲938,834.69
You save:  ₲268,257.64
$U3,917.97
List Price:  $U5,485.31
You save:  $U1,567.34
zł359.54
List Price:  zł503.38
You save:  zł143.83
Already have an account? Log In

Transcript

Hello all welcome back to the next one. In this one we will talk about variables in Java. So let me first bring up Eclipse or let me show you what I have done till now. So in our introduction package, I have created a class called variables. And that's what it shows package name, class name. And then I added some comments here.

So we can just save a little bit of time instead of writing comments when we talk about the concept. And you can ignore all these other packages, you will see them in all respective lectures. But for now, let's just focus on introduction and variables. So in Java, we need variables to store data, any data that we want to use in this programming, we want to store it in some way, right? Either we can use it directly, or we want to store it in a place so that we can use it. Use the variable name in all the places where we need that value.

So imagine you want to Just a simple example, you want to multiply 100 by 200. Either you can just do 100 multiplied by 200 directly, or you can store 100 in a variable and 200 in another variable and then multiply them. So in this simple example, it doesn't make sense to store them in a variable. But usually in programming, there are a lot of steps. And everywhere, we don't want to use that same hundred thing. We want to store it in a variable, give it a name, and then use that name everywhere else.

So they are the placeholders to store the values or the data that we want to store. When we declare a variable, it creates a space in the memory to store the data. So it assigns the space in the memory of the computer. Every data that we want to store has a type. This is very important and this is what these comments will show you. Every data that we want to store has type.

So imagine If I want to store hundred it is an integer value and we all might have studied it in mathematics at some point, but yes it is an integer value. So, I will define a type where I want to save this hundred and we will talk about that right now, if I want to save that text, let's say your name or address or something that is a text. So, we need a type to store that kind of thing. In Java, there are different data types available to store different types of values. So, for integers, we will have a type for text we will have another type. For now, in Java, we have eight primitive data types to store the primitive data and remaining type of data is stored in reference data types or you can call them non primitive also.

So in this lecture, we will see the primitive data types so that we can declare variables when we want to use them. And in next future lectures, we will talk about reference data types also. So now let's jump on to the programming. The first one is the byte data type if a store a byte value, and how do we know, which is a byte value, so it has a range. If you want to start something from negative 128 to positive 127, you can use byte. But usually in programming, this is also an integer value, we cannot use an integer datatype.

For now, please understand that this is the range. And when we talk about integers, I'll explain more. So it has a default value of zero, which means if we don't assign a value, it's going to have a zero value. So the way we declare it is byte so this is the key word in Java. This tells Java that the variable this person is defining is going to be a byte type. and the value is going to be stored is of a byte type, right?

So this is a key word which tells us that it's going to be a byte variable. byte data byte. And the next is the name, I'm gonna say my byte, you can actually call it anything, you can call it just like my name. But I'm just trying to make sense. So it relates to the type of the data. So I'm just saying is my byte, or maybe we can say byte variable.

And I'm just going to assign a value, maybe hundred. And I can just show you right away, if I try something beyond the range, maybe if I try like, or 200, it's gonna give me an error. It says type mismatch cannot convert from end to byte. So it is the default integer type. So anything beyond this range, it thinks of as an integer, and it tells us that I think it's an integer, and I'm not able to convert it to byte. So make sure you have it in the proper range.

So these things are usually used when we are very common. concerned about the memory that I'm trying to use something between minus 128 to 127. So let's use byte only when, when I'm really concerned about the memory. But otherwise, even if I'm declaring something like 10, I would always use in int, and I'm going to talk about into value just now. But if you are like really concerned about the memory, then we use it otherwise, we really do not use by it a lot. So let me just print it out.

So see, so is the shortcut. And if I do this, system, dot out dot print ln is the statement which helps us in printing anything. So I'm here I'm gonna say byte value. And plus is used to join two values. So I'm going to say byte variable. So now this byte variable holds this value hundred, wherever I'm going to use this byte variable, it's going to print hundred or it's going to use hundred Right.

Now let's move to the next one. Finally we will print everything together. So the next one is shot the key word for this shot and I can just say maybe shot variable. And the range here is minus 32 67682 plus 32767, which is quite huge. So maybe I can use something like more than hundred, maybe let's use this thousand and should be happy if I go more than this it then it will start complaining. So now let's just try to print this as well so we know what we are doing.

See, so and I'm gonna say it as like short value is short variable. So it is exactly the same way that we have done above. Then here is the entity type. So this is the Default integer type in Java, we usually use this it has a quite big range. This is quite good enough for most of the stuff that we do. And the default for all the integer types like the byte is integer type short is integer type integer is int, isn't it an integer type, the default for all of them zero.

And we will define it in the similar way, the key word is in IMT. And I'm going to say in the future variable. So, when you define your names or anything, please always make sure that you do not use any keywords from Java do not use byte short, do not use class also do not use public do not use these words, when you are giving some names to anything. These are reserved words in Java, if you use them it will create problems. So when you define a class name also do not use something which is already there in Java, do not use a package, do not use a class. Do not use public.

Do not use It or anything, so do not use anything which is already in Java. So integer variable, and maybe I can give it a little bit of higher value. Something like that should be okay. And now let's print it out. So see so and in integer value or no value is fine, good enough. And I'm gonna say, in the future value variable.

So that's for the integer variable. Now there's another end keyword word for long. So this is also an integer type. It has a really huge range from minus two raised to the bar 63 to tourists to the past 63 minus one. This is like huge range. We only use it when we know we cannot really deal with big numbers.

We usually don't use it, so It's good to know about it, but we usually we will actually usually not use short and bite also, most likely always will be using end. So but let's declare this as well. So I'm gonna say, long, and then I can say long variable. And maybe I'll give some huge number. And when we define long, what we can do here is we can at the end, we can add a URL, and then put the semicolon. So it is perfectly valid for this index.

It is only used for long. Just notice that we are not using this kind of syntax in front of by Jordan. And so now when we can actually just print it, and I can say, long value, and here I can see long variable. So that's about it. Most of them these are done. Now it's the time for float.

So float is the floating point. The default is 0.0 float and double other floating point. And double is actually the default floating point the default decimal digit thing in Java. And the difference is in integers, you will see that we are not using the dots we are not using decimals, like we're using full values hundred thousand dollars like 2030 or 2221. But full values. What if I want to use something like 21.2 so 21.2 it's not gonna be covered in integer, if I give $1 to here, it will complain it will say cannot convert from double to end because this thing dot point is a double.

So float is a lesser precision thing. It's like only up to one digit. And this is not the default in Java the default floating point the default decimal is the double. So but we can also use float if we are not doing like very precise calculations. So whenever there is a precise calculation like financial stuff and anything like banking applications, they mostly use double. So let's define a float first.

So I'm gonna say FL o 80, and float variable. And what we can do here is maybe 20.0 and then let's put semicolon and then I'm gonna copy this guy to print and I'm going to show you one thing we are seeing an error and I'll explain why we're seeing that. So float variable and here we can say float variable. Now, if you see it says, type mismatch cannot convert from double to float. So wherever Java sees this dot thing, it thinks that it is a double type of variable. So but since we are only using one decimal point, we can tell Java or we can just use to 20.1 that it is a floating point by giving f here so forgive FL Java understands that it is a float variable similar to the last Well, now the next thing we will do is the default type, which is the double default type for the decimal.

So I'm going to say double and double variable equals to maybe something like 60.33, something like this, and semicolon. So now there is no error because this is by default, the double variable, and we can also print it out. So I'm gonna say double value. And here I'm gonna say double variable, right. So most of the stuff is done, we have covered six of the data types, and mostly related to the integers and the double floats. Now this is one interesting one, this is Boolean.

So this is the shortest of all the smallest because it's like one bit It is it can, it can have only two values, either false or true. And the default value is false. And this is the easiest variable. So we can define it by saying Boolean, and I'm gonna say like Boolean variable. I cannot use Boolean again, right? It will confuse because Boolean is a key word.

That's what I'm trying to say never use any keyboard when you give any names. So Boolean Boolean variable, and the value that I can give right now here is maybe true. So it can either have true or false. And this true is also a key word. So Java recognizes it. If I try to type throw in capital T, it will not understand it, because that's not a keyword.

So it is very specific. It's case sensitive. We have to type true in small and false also in small so I can show you exactly so false. It isn't small. So whatever key word we use, Java recognizes them. So this is the keyboard this There's a long list of keywords, we don't really have to remember everything because I know some of you may ask like, how do I remember all of the keywords.

So, all the variable types, you will know when we when we use them when you use them, you will know that but other variable types, these are only eight. So definitely not use them. true false, very easy not use them. public static void these things class, these things, obviously, these are key words, not use them. So as we go through you will, you will actually know them. And there is a list also but it's I would never recommend to remember it don't muck that up.

So it's just fine. You will you will actually know it when you use it. So it's perfectly fine to not give stress and try to remember everything. It's it's not a very meaningful thing to remember. So I would say boolean value, and here I can see a boolean variable and the last one is cash. Which is a character.

Let me just bring it up so that we can see it. I can just put some space here. Yeah. So CAD. So some people can call it char also some call it care. It is actually a short form for characters.

So that's why I'm calling it cat. So we define it using ch AR. And I can see like CAD variable and the ranges from zero to 65535. It only accepts in the single core, it only accepts one character, like ABCD, or any character. And it is a little interesting when I'll show you when I run it. So that's about it.

And this we can see cash value, and here we can see variable, right. So that's about it. We have defined all the data types, all the data types. And now let me just run it. So you guys will see how it looks when we run it. And you can see the output here byte value hundred, that's what we assigned.

So we assign the value to byte variable. And now we use byte variable in the print statement. So it is printing the value that we have assigned, right? You can see all the values being printed here. For the car care value. It's a that's what we printed.

And this is a little interesting one, because I wanted to show you this thing. Some of you may think, man, the values are from 0265535. Like, why are we using A or B or C. So these are like using ASCII values. So if I type here, something like zero, it will print the ASCII value of that. So let me just run it and let me just show you. So it's maybe it's space or something.

Let me just use something else. So 65535, maybe just, let's use the math. Similar value. And let's see what it prints and it prints a question marks. So which means the value the in the sky system, the value for question mark is six by 535. Let's just remove couple of them.

And let's see what it prints. So 4655 it prints some the symbol it, I don't think it's a why it looks like symbol to me. So let's print maybe 20. And let's see what it prints. So it doesn't print anything. So nothing in 20 or maybe a space or something, maybe let's try 100 over here.

So D is the hundred here. So again, you don't need to remember these ASCII values. nobody uses them. I'm just showing it because so that you understand what this minimum and maximum value means. When we use a car variable. We always use it like a character like this.

If I'm trying to print like something so reassemble, I'm going to do something like this. But always, character value within a single coat is a cat variable. So that's about it. Please download this code and run it. Do let me know if you have any questions and I'll see you in the next lectures. Thanks for attending this one.

Thank you.

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.