Kotlin Variable type String
A free video tutorial from Fahd Sheraz
Lead Web Developer & Software Engineer | Instructor
4.3 instructor rating • 6 courses • 70,659 students
Learn more from the full courseKotlin Android Development Masterclass
Learn Kotlin, Android Development, Android Studio & IntelliJ IDEA from Scratch. Build Android Apps in Kotlin.
32:16:59 of on-demand video • Updated February 2020
- Build sophisticated Android Oreo Apps using Kotlin
- Quickly master Kotlin Programming language
- Become a Confident Kotlin and Android Developer who is hirable
- NEW Content and Updates will be added to the course frequently
- FULL SUPPORT - Your answers will be answered promptly!
English [Auto] All right. So we are making strides here which is wonderful. The next thing we're going to do is to continue talking about variables. Variables in cutline and many other languages they do have types. Now a type essentially means what type of a bucket. And my creating and what kind of things am I supposed to put in that bucket that I created. OK. So let's go ahead and create a new file class here. So we're going to see a new ruling class and going to call this variable. We're going to start with string. OK. I'm going to go and copy all of this information here because we don't need to be constantly writing the same thing. Right. So I'm going to delete all of this. So we know what the variables are. Now we can talk about times we have strings we have and we have seen strings already. So this is a string type string in double We have boolean we have a float have a char and a few others but these are the main ones again a type essentially tells us because we know that a variable is a little bucket in memory that we assign to say hey by get here is your name. And also we are going to add you add this inside of you. OK. So when we define a variable we need to make sure that we are. We also define what kind of data we are going to put inside of it. We don't we don't want to create a bucket that is supposed to hold cats and then we decide that we want to put elephants in it. Right. If just physically it wouldn't work. OK. So let's go back here to create a far country. OK. And we're going to say Spain. OK. So again we know this is a strain because we have double quotes. And inside we have a text that that's history. So this is a string variable. Now this country here is a bucket that contains this main stream. OK. So that means also if I go here and I say country is equal to Mozambique OK. That's also valid because now I'm changing whatever was in the country first was Spain and now I'm saying up now it's going to be Mozambique. Now the reason why I know this is OK and we can see there's no problem here. In fact we can go ahead and say brand the line and say country we're going to go and run. We should see Mozambique there's Mozambique that's all good. Again we say Mozambique here because we changed to boys. Now the reason why I'm ok even to put Mozambique or any other street here is because I know the first thing I did here when I instantiate it or when I first created this bucket I said to the Spain which is a string implicitly we're saying this country back in here will always hold the strings. If we were to add let's say 23 which is an integer We'll talk about next we're going to have an error OK because it says here the integer literal does not conform to the expected type string. And you may say well how again we didn't specify it to be a string. Right but we actually did it the moment you say equals and and put the spaceplane here as it is the string. Then that means this is always going to be a string. This is how you define a variable to be a string implicitly not directly. Now if we want this to always be a string or to explicitly say that this country variable is a string C was a country like that would call in and say string just like that. OK so now we are saying this country here is of type string and then we could say here a country Spain and that would all work. Or we could have said at the top here is Spain like that. That would also work. So there's two ways to define a variable. Wonderful. So we learn a little bit more about how to create a variable of type string. So we learn how to explicitly do that by the same country. Call it strange. And then we can say equal Spain or if we want we don't want to do that. We could just go had at the bottom here say country and then added to say Spain. Same difference. So it's all the same. And also we learn the implicit way of defining a variable type country and we just give it Spain like that. And so that is also another way of us telling our system that country will only accept strings. All right. Perfect So the next video will go ahead and talk about integers.