Learn Kotlin in 12 Minutes - 2021

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

There's some good stuff in here, and definitely high-density. But TBH I don't think a newbie is going to come away with anything. It's just too much.

That's not a bad thing, if that's not your audience, and it means you can take a breath here and there: Programmers already know what compilers and variables and functions are. You can skip the explanations on the fundamental blocks. Then you can (breathe or) tell them what they need to know about them, particularly where Kotlin might behave differently from what they're used to.

For example, I've been programming forever, but I hadn't looked at Kotlin until yesterday. (Hooray, I put off learning Java long enough now I don't have to!) That repeat{} loop is cool, and new to me, and my inner kid who wrote B-Tree data structures in Pascal is looking at those pairs and triples with some suspicion.

👍︎︎ 5 👤︎︎ u/phrits 📅︎︎ Jan 27 2021 🗫︎ replies

I would probably focus on what's unique and interesting about Kotlin if this is for an audience that can already program in another language. The code here isn't really exercising some of the more elegant features of Kotlin.

There was a thread on this sub recently discuss them. But ones that I really appreciate:

  • Type inference
  • Extension methods
  • if and try expressions: i.e.: kotlin val it = if (whatever) { "foo" } else { "whatever" }
  • Trailing lambda syntax and general functional programming support (as opposed to the loops in your example)
  • null safety
👍︎︎ 4 👤︎︎ u/geoffreychallen 📅︎︎ Jan 28 2021 🗫︎ replies

I appreciate this type of instructional video. Just get to the point and give me the information and I'll pause or backup as necessary.

Spend too much time typing, backspacing, then fixing typing errors or tracking down runtime errors, that's when my mind starts to wander.

👍︎︎ 3 👤︎︎ u/baraqiyal 📅︎︎ Jan 28 2021 🗫︎ replies

I have a feeling that this is too much/fast for a newbie and not enough for an experienced coder (ie. has at least a basic knowledge of Java).

Sorry for my bad English.

I've had a learning curve in university with C -> C++ -> Java -> C# and I feel like I just watched a basics of programming in 4x speed.

The ? sign is an interesting concept in Kotlin, but then you cut it short. Just like when you mentioned that Kotlin and Java is very similar. They compile into the same Java byte code and because of that they can be translated into one of the other very easily.

No mention that in Kotlin, everything is an object, unlike Java. So, when you declare a String, you can use it's methods because it's more than just an array of characters. (I should have wrote there are no primitive types in Kotlin.)

And from my viewpoint, saying something like "val is a keyword because the IDE gives it a color" is a no-go. It's like saying "I know the answer to the question because the other guy told me it".

It was a well put together video with it's errors. Nothing is perfect, but we can work towards an "almost-perfect" state and I can see that you are capable of doing so. Don't give up!

Edit: spelling and clarification.

👍︎︎ 1 👤︎︎ u/gergoking 📅︎︎ Jan 28 2021 🗫︎ replies
Captions
in this video i'll teach you the core of the kotlin programming language kotlin is a general purpose statically typed language that has become extremely popular in the last few years kotlin can be used on both the client and the server which means the code can be run either on the user's device the client or on some computer in the cloud called the server which responds to requests from the client on the client kotlin is now widely used for android development to write apps for the 2.5 billion devices running the android operating system kotlin has seen a huge rise in popularity and i can promise you it will only become more popular in the years to come kotlin is an example of a programming language which is a way for me and you to communicate with computers at the end of the day computers only understand binary zeros and ones so over the years we've created many different ways to instruct the computer what we'd like it to do each of these approaches is called a programming language and you've probably heard of some of them such as c or java or javascript since kotlin was created much more recently it has taken the lessons from the earlier languages to make something that is both powerful and easy to use java and kotlin have a special relationship since they're compatible with each other kotlin is what java would look like if it was designed today to write kotlin code i'm going to be using a program called intellij which you can download for free from the internet to start out with i created an empty file called kotlin minutes the file extension for kotlin files is dot kt and the code we write will be inside this special function called main we'll talk more about functions later in classic fashion the first thing we'll do is print out hello world so we'll use this function called println and type in hello world in double quotes now we'll execute our code by hitting this green arrow which will compile and run our code compiling just means taking the symbols and keyboards you write and turning that into something that the computer can actually execute you can see the output in this run tool window that pops up where it says hello world as we expect let's start our tour of kotlin with variables a variable is a piece of data which has a name and a type for example strings are a sequence of characters so my first name would be stored as a as a string type the way we'll declare the variable is with a special keyword called val you can tell it's a keyword since it'll turn a different color in the editor in kotlin every variable must have a type that's why we say kotlin is statically typed so in this example the name of the variable is first name and the type is string because we're setting the value of the variable equal to drawhold right away we actually don't need to specify that this is of type string this is a nice feature of kotlin called type inference where if it's obvious what the type is we don't need to explicitly indicate it there's also an important concept of whether a variable is read-only or readable and writable if the value of a variable can change after it's initialized then we have to declare it with this var keyword since my weight goes up every holiday season because of how many cookies i'm eating we'll use var for that but my first name will never change so that'll be a vowel there are a few other built-in types in kotlin in addition to string and integer which we just saw for example double is for decimals like 2.5 and booleans only have two values either true or false as you write more code it's sometimes helpful to leave yourself a note about why you wrote the code in a certain way these are called comments and you can leave a comment by using a double slash commented lines are ignored by the computer now that we've talked about variables let's look at operators which allow us to manipulate these variables for example we can combine two strings into a longer string with a plus sign which is called concatenation here we're printing out the value of s1 plus s2 in this variable called combined so when we run the program we get the result of call me maybe which is a concatenation of call me and maybe this is an example of a binary operator because it takes in two inputs there are several different binary operators for numbers as you might expect with two integers here having value nine and four we can add them together and get thirteen we can also subtract them multiply them divide them or find the remainder after the division with this percent sign that would be 1 in this case let's talk a bit more about strings strings are a sequence of characters and there are a bunch of useful things you can do with them for example retrieving a character in the series by indexing into the string like this in kotlin like most other programming languages we start counting at zero so the first character k will be at position zero and the second character o will be position one in the output we can see the first two letters of kotlin we can also check if a string is empty using the method is empty which returns a boolean true or false value or we can get the length of a string which is an integer by using the dot length property if we run this we can see that my string kotlin is not empty because it has characters and there are six characters which is why the length is six another method is substring which will extract a portion of the string between the start and end index that we provide so substring with parameters 2 and 4 will output tl because we will start at t which is index 2 include index 3 the l and then go up 2 but not include index 4 the i one really handy tip as you start writing more kotlin is to explore other methods available to you by hitting the dot or period after writing the name of your variable you'll get an auto complete dialog which shows you all the possible things you can do with this variable the period is probably one of the most important symbols in kotlin since it allows us to use the built-in functionality of the language one thing worth pointing out is that the options and methods that you get will depend on the type of the variable for example the options here with the integer will be different than what we had with the string one last point on the topic of strings we'll frequently want to print out the value of a variable inside a string for that we'll use the dollar sign to do string interpolation the dollar sign means that we'll replace the variable here with the contents of it inside the string so if we run this we can see the output is the string is kotlin because the value of my string is kotlin we can use interpolation for variables of any type let's move on to conditionals which are a way to execute certain code depending on a condition an if statement is an example of flow control which will evaluate whether a statement is true or false and execute some code only if it's true for example we can print out a message if this exam score variable is greater than 70. because the value 88 is larger than 70 when we run the code we see you passed because we're running the code inside the if block the statement we're evaluating here can be anything with a true or false return value so for example we could do exams greater than 70 greater than or equal to less than less than or equal to equal to or not equal to a value i'll change this back to exam score greater than 70 but what if you want to do something if that condition fails for that we'll use an else block and the code inside of here will get run if the condition fails in this case that means that the exam score is less than 70 so we'll print out you failed and now let's change the exam score to 55 which is something that will trigger the else block to run and so here in the output you can see the message you failed we often want to hold a bunch of variables at once instead of defining them one at a time for that we can use collections for example instead of having a separate variable here for each of my friends names we can have one variable which is a list of strings containing all my friends names so now if i wanted to print out the third element in this list we could do that by looking at index 2 of our names list remember that kotlin is 0 indexed so ali is at index 0 maya index 1 and chan at index 2. when we run this we do indeed see 10 in the output by default collections in kotlin are immutable which means we can't add or remove elements from the list if we want to change the contents we must declare the list as a mutable list by changing the declaration now we are able to add a fourth element all elements in a collection must have the same type so in this example everything is of type string and we could actually make that explicit by adding this string type parameter to the declaration if we tried adding something else into the list we would get a compile error for example here we're trying to add 80 which is an integer into the list and if i hover over it says the integer literal does not conform to the expected type string it's really common to do something on every element in a collection the most common way of doing that is with a for loop for example if we wanted to print out each element in our list of friends names we'd write the for loop like this for name in names println name if we run this we can see the output is the three names in the list the way i think about this is that in every cycle or iteration of the for loop the variable name will take on one element of the list it'll go in order so first it'll be ali then maya and then chan the other common use of for loops is to do something a certain number of times we can use for i in 1.5 to execute something exactly five times so if you run this we can see that the operation that we're doing five times is println and we're printing out i each time where i will take on each of the values 1 through 5 before it exits the for loop if we don't want to include the last number then we can do for i in 1 until 5 and that will just print out the numbers 1 through 4. next we'll talk about functions functions are a way to combine chunks of your code so we can reuse them throughout our program this makes our code easier to read and think about we can create a function with the fun keyword followed by the function name so in this example it's my function followed by open and close parentheses and then the body of the function goes inside of these curly braces now we can invoke our function from the main function just by calling it with the open and closed parenthesis if we run this the body of my function gets executed so we print out hello if we want to give this function some input we can add the parameter name and type to the function signature so here we have a parameter name of name and the type of string in the body of the function we can treat the parameter as a normal variable so here we're including that input into the print line you'll notice back in the main function that we now have an error when trying to call my function and that's because whoever invokes this function is responsible for passing in the correct number and type of parameters let's pass in the string j for the parameter and if we run this now we can see hello j as the output we can also set the visibility of functions to be private so they can't be accessed from other classes or files people always ask me what kind of private fun are you having but that's the topic for another video finally the concept of null is really important in kotlin null means having no value and if a variable is allowed to have no value then we need to update the type to include a question mark this means that the variable instagram bio is a nullable string so on instagram if you've written a bio then this variable would have a string value but if it's not set it would be null the important thing to remember is that we can't call any methods on something which is null one way to get around this is to check if the variable is not null and then in the body of this if statement we can now safely call any method we want in this case because we're setting instagram bio to null we'll fail the check on line 3 which means when we run the program we get an empty output however if we update instagram bio to be a string then we will get into the body of the if statement and that's how we're printing out uppercase growth the other way of doing this is to use a shorthand for exactly this if check which is the question mark dot operator so we can say instagram bio question mark dot to upper case and this means we'll only call this method on this variable or this object if it's non-null so if we print this out we can see that we now get growth in uppercase printed out twice that's pretty much the core of kotlin we went over variables operators strings if statements for loops and functions if you're interested in learning more about all the good stuff in kotlin i'd love if you hit the subscribe button and like this video thanks for watching and i'll see you in the next one
Info
Channel: Rahul Pandey
Views: 50,027
Rating: 4.9056048 out of 5
Keywords: kotlin, development, programming, code, tutorial, rahulpandey, rahul, pandey, teach, kotlin tutorial, kotlin programming language, kotlin in 12 minutes, learn kotlin in minutes, learn kotlin fast, kotlin nullability, kotlin type inference, kotlin println, kotlin hello world, kotlin java, kotlin related to java, kotlin conditionals, kotlin functions, kotlin for beginners, kotlin introduction, kotlin overview, kotlin strings, kotlin types, kotlin if statment, android, kotlin developer
Id: iYrgWO2oibY
Channel Id: undefined
Length: 12min 0sec (720 seconds)
Published: Wed Jan 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.