Kotlin Crash Course 2025

Kotlin Crash Course 2025

Installation and Setup of IntelliJ IDEA

Downloading IntelliJ IDEA

  • The installation process begins with searching for "IntelliJ" and selecting the first link.
  • Two versions are available: Ultimate (feature-rich, not needed now) and Community Edition (free). The speaker opts for the Community Edition.

Installing IntelliJ IDEA

  • After downloading, the installation involves moving the application to the Applications folder on a Mac with an M1 chip.
  • Once installed, the user opens IntelliJ IDEA and agrees to terms and conditions to complete setup.

Creating a Basic Kotlin Program

Setting Up Kotlin Files

  • A new project is created with a source directory containing a Kotlin package where all programs will be declared.
  • The speaker creates a basic file named main.kt, noting that all Kotlin files end with .kt.

Writing Hello World in Kotlin

  • The entry point of every program in Kotlin is defined by the main function using the keyword fun.
  • A simple "Hello World" program is written using print method; it successfully prints "Hello".

Using Print Statements in Kotlin

Printing Multiple Lines

  • To print multiple lines, println can be used instead of print, which keeps outputs on separate lines.
  • Variables can be declared to store values like "Android", which can then be printed using $ symbol for interpolation.

Understanding Variables in Kotlin

Variable Declaration

  • In Kotlin, variables are locations for storing data identified by names. They can be declared using either val or var.

Immutable vs Mutable Variables

  • Using val declares immutable variables (cannot change value), while var allows reassignment.

Examples of Variable Usage

  • An example shows defining an integer variable with val, demonstrating that its value cannot be changed without error.

Data Types in Kotlin

Object-Oriented Approach

  • Unlike Java's primitive types, everything in Kotlin is treated as an object. This includes numbers and strings.

Defining Different Data Types

  • To define an integer variable specifically, use syntax like:

val x: Int = 20

Long and Double Types

  • For long variables, append 'L' (e.g., val y: Long = 1234L). For double values, simply declare them as:

val z: Double = 5.6

Float Type Declaration

  • When declaring float types, append 'F' (e.g., val f: Float = 3.4F) to avoid errors related to type mismatch.

Understanding Variable Types and Type Inference in Kotlin

Defining Variables

  • In Kotlin, variables can be defined using specific types such as Float for floating-point numbers and Boolean, which can hold values of either true or false.
  • When dealing with different variable types, explicit conversion is necessary. For instance, converting an integer to a string requires the use of member functions.

Type Conversion

  • To convert an integer (x) to a string, you can use the member function x.toString(). Similarly, converting a double (y) to a string is done using y.toString().
  • Kotlin's compiler has the ability to infer types based on assigned values, eliminating the need for explicit type definitions in many cases.

Type Inference

  • This feature allows developers to define variables without specifying their types explicitly; for example, defining a variable z as a Boolean without declaring its type.
  • The ability of Kotlin's compiler to automatically detect variable types is referred to as type inference and is considered one of its key features.

Working with Strings in Kotlin

String Definition and Properties

  • Strings in Kotlin are used to represent combinations of characters. They can be created using either single quotes or double quotes.
  • The String class provides various properties such as .length for obtaining the length of the string and indexing operators for accessing individual characters.

String Manipulation

  • You can combine strings by using concatenation (e.g., Avenger + rotation) or through string templates that allow embedding variables within strings.
  • Multi-line strings can be defined using triple double quotes ("""). This allows writing large blocks of text easily.

Using Pairs and Triples in Kotlin

Introduction to Pairs

  • Pairs are predefined classes in Kotlin that enable storing two related values together. They can hold values of the same or different types.
  • An example includes storing student names (first name and last name). You can create a pair without explicitly defining its type since Kotlin infers it.

Accessing Pair Values

  • To access elements within a pair, you use .first for the first value and .second for the second value. Both values will print correctly when accessed.

Working with Different Types

  • Pairs can also store different data types; for instance, combining a string (name) with an integer (age).

Exploring Triples

Understanding Triples Functionality

  • Similar to pairs but designed for three related values. A common application could involve storing date components: day, month, and year.

Understanding Date Values and Conditional Statements in C

Defining Date Values

  • The concept of using a string to define date values such as day, month, and year is introduced. A date value triple can be defined (e.g., "7 Jan 2000").
  • Printing the components of the date involves accessing each part through a template (e.g., date.value.first, date.value.second, etc.).
  • The output confirms that the structure allows for clear representation of dates, displaying "day 7, month Jan, year 2000".

Introduction to Special Types in C

  • The 'any' type is discussed as a parent type for all other types in C, including integers, strings, doubles, etc.
  • It is emphasized that 'any' serves as the root of the class hierarchy in C programming.

Understanding Unit Type

  • The unit type is compared to Java's void type; it signifies functions that do not return any value.
  • An example function demonstrates addition without returning a value but can specify 'unit' as its return type.
  • The compiler indicates redundancy when specifying 'unit', suggesting that functions without explicit return types default to unit.

Exploring Conditional Statements

  • Conditional statements are essential for executing code based on specific conditions. An example with an if statement checks if a number is greater than zero.
  • A basic if expression is demonstrated with variable x set to 10; it prints "number is greater than zero" when true.

Implementing If Else Expressions

  • An else block is added to handle cases where the condition fails (e.g., negative numbers).
  • Unlike traditional ternary operators, C's if else expressions can also yield outputs based on conditions.

Advanced Condition Checking

  • Another example checks whether a number (set at four initially) is odd or even using modulus operation within an if else structure.
  • Outputs from both blocks are stored in a result variable which can be printed later. Changing input values dynamically alters results shown by print statements.

This structured approach provides clarity on how date values and conditional logic operate within C programming while illustrating practical examples throughout.

Understanding Temperature-Based Clothing Recommendations

Initial Variable Declaration and Conditions

  • A variable temperature is declared and initialized to 20Β°. Another variable closethToBuy will store results from an if-else expression based on temperature conditions.
  • The first condition checks if the temperature is greater than 0 and less than or equal to 10, suggesting purchasing winter coats if true.
  • Additional conditions are added for temperatures between 10 to 20 (suggesting jackets) and between 20 to 30 (suggesting shirts). If above 30, T-shirts are recommended.

Program Execution and Output

  • A print statement outputs the clothing recommendation based on the evaluated conditions stored in closethToBuy.
  • When the temperature is set to 20Β°, the output correctly suggests purchasing jackets as it meets the condition of being between 10 and 20.
  • Increasing the temperature to 34Β° leads to a new output indicating T-shirts, demonstrating how multiple if-else conditions can be used effectively.

Refactoring with When Expression

  • To enhance clarity, a when expression is introduced. This allows for cleaner code by consolidating all conditions into one structure with respective outputs.
  • The output from this when block is stored in closethToBuy, maintaining functionality while improving readability.

Utilizing Ranges in Conditions

  • The concept of ranges is introduced; instead of checking each condition separately, ranges can simplify expressions.
  • Using range syntax (0..10) allows defining when temperatures fall within specific limits more cleanly compared to traditional comparisons.

Final Implementation of Range Expressions

  • Each condition now uses range definitions: temperatures between specified values lead directly to corresponding outputs without cluttered logic.
  • Testing with a temperature of 25Β° confirms that it falls within the defined range for shirts, showcasing effective use of conditional logic through ranges.

Understanding Closed vs Half Open Ranges

  • Explanation of closed ranges indicates they include all numbers within specified limits (e.g., 0..5 includes both endpoints).
  • Introduction of half-open ranges using syntax like 0 until 5, which excludes the upper limit, differentiates it from closed ranges.

Understanding Kotlin's Null Safety and Half-Open Ranges

Half-Open Ranges in Kotlin

  • To declare a half-open range in Kotlin, the until keyword is used. This means the range starts from an initial value (e.g., 0) and increases but does not include the last defined value.
  • An example of a half-open range is from 0 to 4, which includes values 0, 1, 2, and 3 but excludes 4.

Null Safety Concept in Kotlin

  • The null pointer exception is referred to as a "billion-dollar problem" in programming due to its prevalence, especially in Java where any variable can be assigned a null value without restrictions.
  • In this section, the speaker explains how Kotlin addresses null safety to reduce occurrences of null pointer exceptions by enforcing stricter type definitions.

Variable Declaration and Nullability

  • Variables can be declared using var or val. By default, variables are non-nullable unless explicitly defined as nullable types.
  • For instance, if you declare an integer variable x with a value of 10 and later attempt to assign it a null value directly (e.g., x = null), it will result in an error because it's not defined as nullable.

Nullable Types in Kotlin

  • To allow for null assignments, variables must be declared as nullable types by appending a question mark (?) after the type (e.g., var x: Int?).
  • Once declared as nullable, you can assign null values without errors. This distinction helps prevent accidental crashes due to unhandled null references.

Accessing Properties of Nullable Variables

Safe Calls and Member Functions

  • When working with non-nullable string variables (e.g., var fruit: String = "apple"), all member functions can be accessed directly without issues.
  • However, when accessing properties of nullable variables (e.g., var fruit: String? = "apple"), direct access leads to compilation errors. Safe calls must be used instead.

Implementing Safe Calls

  • To safely access properties on nullable types without crashing the program when encountering null values, use safe call operators (?.).
  • If a variable is set to null during execution and you try to access its length using safe calls, it will return 'null' instead of causing a crash.

Conditional Checks with Nullable Variables

Using Elvis Operator for Null Checks

  • The Elvis operator provides an elegant way to handle conditions involving nullable variables. It evaluates expressions on both sides; if the left side is true (not null), it returns that value; otherwise, it defaults to the right side expression.

This structured approach highlights key concepts related to Kotlin's handling of ranges and its innovative solutions for managing potential pitfalls associated with null values.

Understanding the Elvis Operator and Arrays in Kotlin

The Elvis Operator

  • The Elvis operator is a combination of the question mark (?) and colon (:) used to simplify conditional expressions. It allows for concise null checks, returning a default value if the left-hand side is null.
  • An example demonstrates using the Elvis operator to print a car's name or indicate that it is null. This reduces boilerplate code compared to traditional if-else statements.
  • When executing an expression with the Elvis operator, if the left side is null, it returns the right side value. In this case, assigning "null" as a default value illustrates its functionality.
  • Another example involves checking a nullable string variable named "name." If assigned a valid value, it prints correctly; otherwise, it can return zero instead of null when using the Elvis operator.
  • The use of the Elvis operator helps avoid NullPointerExceptions by providing safe defaults in cases where variables may be unintentionally set to null.

Working with Arrays in Kotlin

  • Kotlin supports arrays similar to other programming languages. Arrays are used to store multiple values of a single type (e.g., strings or integers).
  • A basic integer array can be created using arrayOf(), allowing for easy initialization with specified elements. Type specification is optional as Kotlin infers types automatically.
  • Accessing array elements requires referencing their index, starting from zero. For instance, printing the first element involves accessing index 0 directly.
  • To update an array element, reference its index and assign a new value. This process demonstrates how values within an array can be modified dynamically during execution.
  • Conditional checks on arrays can determine whether specific elements exist within them using methods like contains(), enhancing data validation capabilities in applications.

Defining Different Types of Arrays

  • String arrays can also be defined similarly by listing favorite items (e.g., superheroes), showcasing flexibility in storing various data types together.
  • Other variable types such as float or double arrays can be created following similar principles for diverse data storage needs within Kotlin programs.
  • A unique feature in Kotlin allows for mixed-type arrays using Any, enabling storage of different variable types (e.g., integers, strings). This versatility enhances coding efficiency and adaptability across various scenarios.

Understanding Loops and Classes in Kotlin

Introduction to For Loops

  • The video begins by discussing the use of loops, specifically the for loop, to iterate through array elements in Kotlin.
  • A for loop can be utilized with various data structures such as arrays, lists, or collections. The example focuses on an integer array.
  • The syntax involves using a variable (e.g., digit) along with the in operator followed by the array name to print each element.

Iterating Through Arrays

  • An example is provided where a for loop iterates over an integer array named numbers, printing each digit contained within it.
  • Transitioning to string arrays, a similar approach is demonstrated where a variable (hero) is used to access and print values from a string array.

Using Ranges in For Loops

  • The concept of ranges is introduced; specifically, closed ranges are discussed (e.g., 0 to 5).
  • A for loop is applied over this range, demonstrating how it prints numbers sequentially from 0 to 5.
  • Half-open ranges are also explored, showing that they exclude the upper limit while still iterating through values.

Introduction to Object-Oriented Programming

  • The discussion shifts towards object-oriented programming concepts in Kotlin, starting with classes and objects.
  • To define a class in Kotlin, one must use the class keyword followed by the class name and its body enclosed in braces.

Creating and Renaming Classes

  • Best practices suggest that class names should start with capital letters. An example class named Laptop is created.
  • Properties of the laptop class are defined: brand (string), RAM (integer), and build year (string), showcasing how properties belong exclusively to their respective classes.

Accessing Class Properties

  • Two methods are mentioned for accessing or assigning values to class properties: creating an object or using parameters.
  • An object of the Laptop class is instantiated within another file named "example," emphasizing that main functions serve as entry points for programs.

Accessing Class Properties in Laptop Class

Accessing and Printing Properties

  • To access properties of the Laptop class, use a print statement with the object reference (e.g., laptop.brand) to retrieve values like brand and RAM.
  • The program prints default values assigned during the creation of the Laptop class when executed.

Updating Object Values

  • You can update properties of a laptop object by directly assigning new values (e.g., laptop.brand = "Apple").
  • After updating, running the program will display these updated values, demonstrating how to create and modify class objects effectively.

Creating Main Function Inside Class

Integrating Main Functionality

  • A main function can be created within the class by simply writing Main, which automatically generates a default main function.
  • This allows for better organization of code as it keeps related functionalities together within the same file.

Adding Parameters to Class

Defining Constructor Parameters

  • To add parameters to a class, include them after the class name; this is known as the primary constructor.
  • For example, you can define parameters for brand (string), RAM (integer), and build year (string), allowing for dynamic object creation without manual assignments.

Object Creation with Parameters

  • When creating an object, you can pass parameter values directly during instantiation instead of assigning them manually later on.

Defining Functions in Kotlin

Understanding Functions in Kotlin

  • Functions are defined using the fun keyword and execute only when called. They can take parameters and return data or perform operations.

Creating Custom Functions

  • A custom function named provideInformation is created within the Laptop class to print property details. It includes a function body that executes specific tasks when invoked.

Executing Custom Functionality

Running Custom Functions

  • The custom function is called using an instance of the laptop object (laptop.provideInformation()), demonstrating how functions encapsulate behavior related to their respective classes.

Creating a Basic Calculator Class in Kotlin

Defining the Calculator Class

  • A new class named Calculator is introduced, with variables x, y, and result, all initialized to zero.
  • The main function creates an instance of the Calculator class and discusses passing parameters directly to functions.

Functionality of Addition

  • The process of adding parameters directly within the function is explained, emphasizing how to define integer types for x and y.
  • The addition operation is performed using the method from the calculator object, demonstrating how to pass values (e.g., 1 for both x and y).

Returning Results from Functions

  • The result of the addition is printed, showing that it correctly computes "1 + 1 = 2".
  • Discussion on local variables highlights that results can be returned instead of being stored locally, explaining variable scope.

Specifying Return Types

  • It’s noted that since both inputs are integers, the return type should also be specified as an integer after the function declaration.
  • The use of a return statement allows output from functions to be captured in a variable for further use.

Optimizing Function Outputs

Streamlining Return Statements

  • Simplification of code by returning results directly without intermediate variables is discussed. This enhances clarity and efficiency in coding practices.

Understanding Inheritance in Kotlin

Introduction to Inheritance Concepts

  • An overview of inheritance in Kotlin introduces its ability to reuse properties or methods from one class into another.

Creating a Fruit Class Example

  • A basic class named Fruit is created with properties like name, color, and taste. A method prints these properties for information purposes.

Implementing Inheritance with Apple Class

  • A new class called Apple extends the functionality of the Fruit class without rewriting existing code.

Understanding Superclass and Subclass Dynamics

  • Explanation about superclass (parent class being inherited from) versus subclass (child class inheriting), clarifying their roles in inheritance.

Final Classes Limitation

  • It’s highlighted that every class in Kotlin is final by default, meaning they cannot be inherited unless explicitly declared otherwise.

Understanding Inheritance and Interfaces in Codin

Inheritance in Codin

  • Classes are final by default, meaning their properties and methods cannot be inherited. To allow inheritance, use the open keyword to make a class non-final.
  • To inherit a class, define a parent class with base properties or functions, append the open keyword, and then extend it in a child class using the colon (:) syntax.
  • For example, an Apple class can have properties like name, color, and taste without redefining them in child classes. You can create an object of Apple and access these properties directly.
  • The child class can call functions from the parent class. This demonstrates code reusability as all properties and functions come from the parent class.
  • If another fruit class (e.g., Grape) is created, it can also inherit from the same parent without needing to redefine any properties or methods.

Introduction to Interfaces

  • An interface allows defining function signatures without implementation details. It serves as a contract for classes that implement it.
  • To create an interface in Codin, use the interface keyword followed by a name (e.g., MyCalculator). Functions within this interface do not require bodies at this stage.
  • Functions such as addition or division can be declared using the fun keyword without defining their implementations immediately.
  • When implementing an interface in a class (e.g., CalculatorExample), use the colon (:) followed by the interface name. This will prompt errors if abstract members are not implemented yet.
  • Implementing missing members is straightforward; you can select options to auto-generate method stubs based on your defined interface.

This structured overview captures key concepts regarding inheritance and interfaces within Codin while providing timestamps for easy reference back to specific parts of the transcript.

Understanding Interfaces and Collections in Kotlin

Implementing Interfaces

  • The concept of interfaces allows for defining a structure that can be implemented in various ways. Errors related to implementation are resolved, enabling flexibility in adding functionalities.
  • Classes can implement multiple interfaces using the colon syntax followed by interface names. This allows for diverse functionality within a single class.
  • To implement multiple interfaces, separate them with commas after the colon keyword. This enables overriding methods from all specified interfaces.
  • A class must create an object to access its properties or functions, emphasizing the need for instantiation before usage.

Singleton Class Implementation

  • In Kotlin, a singleton class is created by removing the variable declaration and using the object keyword. This eliminates the need for creating instances manually.
  • Accessing properties and functions of a singleton class is straightforward; you can directly use the class name without instantiation.

Collections in Kotlin

  • Collections in Kotlin include lists, sets, and arrays. The focus here is on list collections which can be defined using var or val.
  • Lists can be immutable (default behavior), meaning elements cannot be added or modified once declared. Attempting to add elements will result in errors.

Mutable vs Immutable Lists

  • To create a mutable list that allows updates, use mutableListOf(). This enables adding or modifying elements post-declaration.
  • Elements can be added at specific indices within mutable lists, providing flexibility compared to immutable lists.

Iterating Through Collections

  • Various methods exist for iterating through collections: traditional loops and enhanced forEach loops utilizing lambda expressions for cleaner code.
  • Running programs will demonstrate collection size and element insertion results effectively showcasing how data manipulation works within these structures.

This structured overview captures key concepts discussed regarding interfaces and collections in Kotlin programming as presented in the transcript.

Video description

( Kotlin Tutorial for beginners with all essential concepts) πŸ˜πŸš€ NativeMobileBits complete free crash course for beginners . To watch all the exclusive Production level content, be a member by Joining this channel (using this below link) : https://www.youtube.com/c/NativeMobileBits/join If you want to learn Jetpack Compose, you can checkout Our Jetpack Compose playlist : https://www.youtube.com/playlist?list=PLLwcRoh3a6u6IsUrXoKaoZNb5LvhOhV8J If you are new to Android, and want to learn Android in 2023, you can checkout this playlist: https://www.youtube.com/playlist?list=PLLwcRoh3a6u60ZEVdcKijpnRFTALeJwL- If you want to learn Kotlin in a reel way (if you are reel addict dev) then checkout this Kotlin Interview Questions https://youtube.com/playlist?list=PLLwcRoh3a6u5XHTHnI0NWOm4-WqUoFY_3 This video covers all the necessary concepts in KOTLIN programming language, this will be helpful for you : - if you are exploring #android or #kotlin or maybe you are a complete beginner and want to learn Kotlin completely - or if you are experienced #kotlindeveloper then it will help you brush up Kotlin concepts. "Please Subscribe & share this video so that others who are learning Kotlin can find this video as well." Kotlin crash-course content: 0:00 - Introduction 0:36 - Kotlin origin & advantages 01:09 - IntelliJ installation 02:05 - Project structure 02:19 - How to create a Hello World's program in Kotlin 04:03 - How to print something & add some comments in Kotlin 04:28 - Variables in Kotlin (Var & Val) 06:34 - DataTypes in Kotlin 08:39 - Type Conversion in Kotlin 09:35 - Type Inference in Kotlin 10:42 - Strings in Kotlin 12:13 - String templates & Multi line strings 12:36 - Pairs in Kotlin 14:48 - Triples in Kotlin 16:39 - Any type in Kotlin 17:29 - Unit type in Kotlin 18:28 - Conditional expressions in Kotlin 18:48 - If else expression 20:28 - How to get some output from if else expression in Kotlin 21:56 - Multiple if-else expression ladder in Kotlin 24:29 - When expression in Kotlin 26:01 - Enhancements in when expression code in Kotlin 26:18 - Ranges inside When expression in Kotlin 28:30 - Different types of Ranges in details in Kotlin 29:58 - Null Safety concept (Nullable Variables Types) in Kotlin 34:45 - How to handle nullable types with Safe Calls check in Kotlin 36:38 - Elvis operator in Kotlin 39:44 - Arrays in Kotlin 41:00 - Example of different types of array programs & its member functions in Kotlin 44:17 - For Loop in Kotlin 47:51 - Object Oriented Programming in Kotlin 47:57 - Class in Kotlin 48:24 - Class structure and how to declare a class in Kotlin 49:25 - Class Variables/ Properties in Kotlin 50:54 - Object in Kotlin 52:01 - How to work with class object and access class's properties and functions in Kotlin 53:13 - Class Constructors & parameters in class in Kotlin 55:24 - Functions in Kotlin 56:17 - How to create our own custom Functions in Kotlin 01:00:00 - How to pass parameters inside a function in Kotlin 01:00:57 - Return some value from a function in Kotlin 01:03:02 - Inheritance in Kotlin 01:05:00 - open keyword in Kotlin 01:08:46 - Interfaces in Kotlin 01:12:40 - Multiple Interfaces in Kotlin 01:13:58 - Singleton class in Kotlin 01:14:42 - object keyword in Kotlin 01:15:09 - Collection in Kotlin 01:15:54 - List & Mutable List in Kotlin 01:18:21 - For each loop in collections in Kotlin 01:19:20 - Thanks from chief I hope you found this crash course helpful happy coding:) be you be happy "If You Like this Video Don't Forget To Share With Others & Also Share Your Views in comments."