<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by NOOBIE on Medium]]></title>
        <description><![CDATA[Stories by NOOBIE on Medium]]></description>
        <link>https://medium.com/@geekprogrammer11?source=rss-e9f839125a96------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*CQM6C0JIMRAWdykDsTvyUw.jpeg</url>
            <title>Stories by NOOBIE on Medium</title>
            <link>https://medium.com/@geekprogrammer11?source=rss-e9f839125a96------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 02:16:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@geekprogrammer11/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[WritingYour First Hello World program on GO]]></title>
            <link>https://medium.com/@geekprogrammer11/writingyour-first-hello-world-program-on-go-5363c731cc21?source=rss-e9f839125a96------2</link>
            <guid isPermaLink="false">https://medium.com/p/5363c731cc21</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[hello-world]]></category>
            <category><![CDATA[beginner]]></category>
            <dc:creator><![CDATA[NOOBIE]]></dc:creator>
            <pubDate>Wed, 12 Apr 2023 02:25:36 GMT</pubDate>
            <atom:updated>2023-04-12T02:25:36.076Z</atom:updated>
            <content:encoded><![CDATA[<p>Go (often called Golang) is an open-source, statically-typed programming language created at Google. It is a compiled language that focuses on simplicity, efficiency, and performance. Go&#39;s syntax is similar to C, but it also includes some features from other programming languages, such as garbage collection and concurrency.</p><p>In this tutorial, we will create a &quot;Hello World&quot; program in Go, which is the traditional first program to write when learning a new programming language.</p><h3>Installing Go</h3><p>Before we start writing our program, we need to make sure that Go is installed on our machine. If you have not installed Go yet, you can download it from the official website: <a href="https://golang.org/dl/">https://golang.org/dl/</a></p><p>Once you have downloaded the appropriate version for your operating system, you can follow the installation instructions to install Go.</p><p>After installing Go, you can verify the installation by opening a terminal and running the following command:</p><pre>$ go version</pre><p>This command will print the version of Go that is currently installed on your machine.</p><h3>Writing the Hello World Program</h3><p>Now that we have installed Go, we can start writing our &quot;Hello World&quot; program. Open a text editor and create a new file named &quot;hello.go&quot;. In this file, we will write the following code:</p><pre>package main<br><br>import &quot;fmt&quot;<br><br>func main() {<br>    fmt.Println(&quot;Hello, World!&quot;)<br>}</pre><p>Let&#39;s go through each line of the code to understand what it does:</p><ul><li>The first line, package main, declares that this file is part of the main package. In Go, every executable program must have a main package.</li><li>The second line, import &quot;fmt&quot;, imports the fmt package, which provides functions for formatted I/O (input/output).</li><li>The third line defines a function named main. This function is the entry point of our program, which means that it is the first function that will be executed when we run our program.</li><li>The fourth line, fmt.Println(&quot;Hello, World!&quot;), calls the Println function from the fmt package to print the &quot;Hello, World!&quot; message to the console.</li></ul><p>That&#39;s it! We have just written our first Go program.</p><h3>Compiling and Running the Program</h3><p>To compile our program, we need to open a terminal, navigate to the directory where our &quot;hello.go&quot; file is located, and run the following command:</p><pre>$ go build</pre><p>This command will create an executable file named &quot;hello&quot; in the same directory as our &quot;hello.go&quot; file.</p><p>To run our program, we can simply type the name of the executable file and press Enter:</p><pre>$ ./hello</pre><p>This command will print the &quot;Hello, World!&quot; message to the console.</p><p>Alternatively, we can run our program directly from the terminal without creating an executable file by using the following command:</p><pre>$ go run hello.go</pre><p>This command will compile and run our program in one step, without creating an executable file.</p><h3>Conclusion</h3><p>In this tutorial, we have learned how to create a &quot;Hello World&quot; program in Go. We have also learned how to compile and run our program using the Go command-line tools.</p><p>Go is a powerful programming language that is well-suited for building efficient and scalable applications. By mastering the basics of Go, we can unlock the full potential of this language and start building amazing projects.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5363c731cc21" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Best Practices for Using Classes in JavaScript: A Comprehensive Guide]]></title>
            <link>https://medium.com/@geekprogrammer11/best-practices-for-using-classes-in-javascript-a-comprehensive-guide-50e53d70a155?source=rss-e9f839125a96------2</link>
            <guid isPermaLink="false">https://medium.com/p/50e53d70a155</guid>
            <dc:creator><![CDATA[NOOBIE]]></dc:creator>
            <pubDate>Thu, 06 Apr 2023 10:20:31 GMT</pubDate>
            <atom:updated>2023-04-06T10:20:31.252Z</atom:updated>
            <content:encoded><![CDATA[<p>JavaScript is a versatile and dynamic programming language that can be used to create complex applications and dynamic web pages. One of the key features of JavaScript is its ability to support object-oriented programming (OOP), which is a programming paradigm that organizes code into reusable, modular components called classes. In this blog, we will discuss the best practices for using classes in JavaScript, with examples that are easy to understand.</p><ol><li><strong>Use Class Syntax</strong></li></ol><p>The class syntax was introduced in ECMAScript 2015 as a cleaner and more concise way to create classes in JavaScript. It is recommended to use this syntax when defining classes in your code. Here’s an example:</p><pre>class Person {<br>  constructor(name, age) {<br>    this.name = name;<br>    this.age = age;<br>  }<br>  sayHi() {<br>    console.log(`Hi, my name is ${this.name} and I am ${this.age} years old.`);<br>  }<br>}</pre><p><strong>2. Use Capital Letters for Class Names</strong></p><p>To make it easier to distinguish classes from other types of objects in your code, it is recommended to use capital letters for class names. This is a convention that is widely followed in the JavaScript community.</p><pre>class Person { // class definition }</pre><p><strong>3. Use Constructors to Initialize Object Properties</strong></p><p>Constructors are special methods that are called when a new object is created from a class. They are used to initialize object properties and perform other setup tasks. It is recommended to use constructors to initialize object properties instead of setting them directly in the class body.</p><pre>class Person {<br>  constructor(name, age) {<br>    this.name = name;<br>    this.age = age;<br>  }<br>}</pre><p><strong>4. Use Methods to Define Class Behavior</strong></p><p>Methods are functions that are defined inside a class and can be called on objects created from that class. They are used to define the behavior of the class. It is recommended to use methods to define class behavior instead of defining functions outside of the class.</p><pre>class Person {<br>  constructor(name, age) {<br>    this.name = name;<br>    this.age = age;<br>  }<br>  sayHi() {<br>    console.log(`Hi, my name is ${this.name} and I am ${this.age} years old.`);<br>  }<br>}</pre><p><strong>5. Use Inheritance to Create Subclasses</strong></p><p>Inheritance is a feature of object-oriented programming that allows you to create new classes based on existing classes. This is useful when you want to reuse code and add new functionality to existing classes. It is recommended to use inheritance to create subclasses when appropriate.</p><pre>class Animal {<br>  constructor(name) {<br>    this.name = name;<br>  }<br>  speak() {<br>    console.log(`${this.name} makes a noise.`);<br>  }<br>}<br><br>class Dog extends Animal {<br>  speak() {<br>    console.log(`${this.name} barks.`);<br>  }<br>}<br><br>let d = new Dog(&#39;Mitzie&#39;);<br>d.speak(); // Mitzie barks.</pre><p><strong>6. Use the Prototype Chain to Access Class Members</strong></p><p>JavaScript uses a prototype-based inheritance model, which means that objects inherit properties and methods from their prototype. It is recommended to use the prototype chain to access class members instead of using the this keyword.avascriptCopy code</p><pre>class Person {<br>  constructor(name, age) {<br>    this.name = name;<br>    this.age = age;<br>  }<br>  sayHi() {<br>    console.log(`Hi, my name is ${this.name} and I am ${this.age} years old.`);<br>  }<br>}<br><br>let p = new Person(&#39;John&#39;, 30);<br>p.sayHi(); // Hi, my name is John and I am 30 years old.</pre><p><strong>7. Use Static Methods for Class-Level Functionality</strong></p><p>Static methods are methods that are defined on the class itself, rather than on individual objects created from the class. They are used for class-level functionality that does not require access to instance-specific data. It is recommended to use static methods for such functionality.</p><pre>class MathUtils {<br>  static square(x) {<br>    return x * x;<br>  }<br>}<br><br>console.log(MathUtils.square(5)); // 25</pre><p><strong>8. Use Getters and Setters for Encapsulation</strong></p><p>Getters and setters are special methods that allow you to access and modify object properties in a controlled manner. They are used for encapsulation, which is the practice of keeping object data private and only exposing it through controlled methods. It is recommended to use getters and setters for encapsulation.</p><pre>class Person {<br>  constructor(name, age) {<br>    this._name = name;<br>    this._age = age;<br>  }<br>  get name() {<br>    return this._name;<br>  }<br>  set name(name) {<br>    this._name = name;<br>  }<br>  get age() {<br>    return this._age;<br>  }<br>  set age(age) {<br>    if (age &lt; 0) {<br>      console.log(&#39;Age cannot be negative.&#39;);<br>    } else {<br>      this._age = age;<br>    }<br>  }<br>}<br><br>let p = new Person(&#39;John&#39;, 30);<br>console.log(p.name); // John<br>p.name = &#39;Bob&#39;;<br>console.log(p.name); // Bob<br>console.log(p.age); // 30<br>p.age = -5; // Age cannot be negative.</pre><p><strong>9. Use Object Destructuring to Simplify Property Access</strong></p><p>Object destructuring is a feature of JavaScript that allows you to extract individual properties from an object and assign them to variables. It is recommended to use object destructuring to simplify property access in your code.</p><pre>class Person {<br>  constructor(name, age) {<br>    this.name = name;<br>    this.age = age;<br>  }<br>}<br><br>let p = new Person(&#39;John&#39;, 30);<br>let {name, age} = p;<br>console.log(name); // John<br>console.log(age); // 30</pre><p><strong>10. Use Object.assign() for Object Composition</strong></p><p>Object composition is a technique in which complex objects are built from simpler objects. Object.assign() is a built-in function in JavaScript that allows you to combine multiple objects into a single object. It is recommended to use Object.assign() for object composition.</p><pre>let person = { name: &#39;John&#39;, age: 30, sayHi() { console.log(`Hi, my name is ${this.name} and I am ${this.age} years old.`); } }; let employee = { jobTitle: &#39;Developer&#39;, salary: 50000 }; let john = Object.assign({}, person, employee); john.sayHi(); // Hi, my name is John and I am 30 years old. console.log(john.jobTitle); // Developer console.log(john.salary); // 50000</pre><p>In conclusion, using classes in JavaScript can make your code more modular, reusable, and maintainable. By following these best practices, you can ensure that your classes are well-structured and easy to use.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=50e53d70a155" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Access Modifiers in JavaScript Classes: Public, Private, and Protected Explained with…]]></title>
            <link>https://medium.com/@geekprogrammer11/understanding-access-modifiers-in-javascript-classes-public-private-and-protected-explained-with-25b41a9904a5?source=rss-e9f839125a96------2</link>
            <guid isPermaLink="false">https://medium.com/p/25b41a9904a5</guid>
            <dc:creator><![CDATA[NOOBIE]]></dc:creator>
            <pubDate>Thu, 06 Apr 2023 09:58:49 GMT</pubDate>
            <atom:updated>2023-04-06T10:10:01.080Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Understanding Access Modifiers in JavaScript Classes: Public, Private, and Protected Explained with Examples.</strong></h3><p>JavaScript classes provide a convenient way to encapsulate data and behavior into reusable modules. One key aspect of encapsulation is controlling access to the properties and methods of a class, so that they can be used safely and efficiently. In JavaScript, there are three access modifiers that can be used to control the visibility of class members: public, private, and protected.</p><ol><li>Public Access Modifier: Public access is the default access modifier in JavaScript classes, which means that all properties and methods are accessible from outside the class, as well as from within the class. Public members can be accessed using the dot notation, like this:</li></ol><pre>class Car {<br>  constructor(make, model) {<br>    this.make = make;<br>    this.model = model;<br>  }<br>  start() {<br>    console.log(&quot;Starting the car...&quot;);<br>  }<br>}<br><br>let myCar = new Car(&quot;Toyota&quot;, &quot;Corolla&quot;);<br>console.log(myCar.make); // &quot;Toyota&quot;<br>myCar.start(); // &quot;Starting the car...&quot;</pre><p>In the example above, the make and model properties and the start() method are all public, because they are not explicitly marked as private or protected. Therefore, they can be accessed and used by any code that has a reference to an instance of the Car class.</p><p>2. Private Access Modifier: Private access allows class members to be accessed only from within the class itself. This can be useful for hiding implementation details or sensitive information, and preventing unintended modification or access by external code. In JavaScript, private members are indicated by using a # prefix before the member name, like this:</p><pre>class BankAccount {<br>  #balance = 0;<br>  #accountNumber;<br>  constructor(accountNumber) {<br>    this.#accountNumber = accountNumber;<br>  }<br>  deposit(amount) {<br>    this.#balance += amount;<br>  }<br>  withdraw(amount) {<br>    if (this.#balance &gt;= amount) {<br>      this.#balance -= amount;<br>    } else {<br>      console.log(&quot;Insufficient balance!&quot;);<br>    }<br>  }<br>  getAccountNumber() {<br>    return this.#accountNumber;<br>  }<br>}<br><br>let myAccount = new BankAccount(&quot;123456&quot;);<br>console.log(myAccount.getAccountNumber()); // &quot;123456&quot;<br>myAccount.deposit(1000);<br>myAccount.withdraw(500);<br>console.log(myAccount.#balance); // Error: SyntaxError: Private field &#39;#balance&#39; must be declared in an enclosing class</pre><p>In the example above, the #balance and #accountNumber properties are both private, which means they can only be accessed from within the BankAccount class. Attempting to access myAccount.#balance from outside the class will result in a SyntaxError since private members cannot be accessed from outside the class.</p><p>Note that private members can still be accessed by methods defined within the class, even if they are not marked as private. For example, the deposit() and withdraw() methods can access the private #balance property since they are defined within the BankAccount class.</p><p>3. Protected Access Modifier: Protected access allows class members to be accessed from within the class itself, as well as from within any subclass that inherits from the class. This can be useful for sharing common functionality or state between related classes, while still maintaining encapsulation and preventing external modification. In JavaScript, protected members are indicated by using a _ prefix before the member name, like this:</p><pre>class Vehicle {<br>  _speed = 0;<br>  accelerate(amount) {<br>    this._speed += amount;<br>  }<br>  brake(amount) {<br>    this._speed -= amount;<br>    if (this._speed &lt; 0) {<br>      this._speed = 0;<br>    }<br>  }<br>  get speed() {<br>    return this._speed;<br>  }<br>}<br><br>class Car extends Vehicle {<br>  #make;<br>  constructor(make) {<br>    super();<br>    this.#make = make;<br>  }<br>  get make() {<br>    return this.#make;<br>  }<br>  honk() {<br>    console.log(&quot;Beep beep!&quot;);<br>  }<br>}<br><br>let myCar = new Car(&quot;Toyota&quot;);<br>console.log(myCar.make); // &quot;Toyota&quot;<br>myCar.accelerate(50);<br>console.log(myCar.speed); // 50<br>myCar.brake(20);<br>console.log(myCar.speed); // 30<br>myCar.honk();  // &quot;Beep beep!&quot;<br>console.log(myCar._speed); // 30 (protected member)</pre><p>In this example, the Vehicle class has a single protected member _speed, which represents the current speed of the vehicle. The Car class extends Vehicle and adds a private member #make to represent the make of the car.</p><p>The Car class also defines a honk() method which is not marked as private, so it is a public method that can be called from outside the class. This method logs a simple message to the console when called.</p><p>The myCar instance is created using the Car constructor with a &quot;Toyota&quot; make parameter. The make property of myCar is accessed using the getter method get make() and returns the value &quot;Toyota&quot;. The accelerate() and brake() methods of myCar are called to modify its speed, and the protected _speed member is accessed directly to log its value to the console.</p><p>By using access modifiers in this way, you can control the visibility and behavior of your class members, and provide a clean and consistent API for external code to interact with.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25b41a9904a5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript Getters and Setters: A Guide to Controlling Access and Modification of Class Properties…]]></title>
            <link>https://medium.com/@geekprogrammer11/javascript-getters-and-setters-a-guide-to-controlling-access-and-modification-of-class-properties-708f6a54768b?source=rss-e9f839125a96------2</link>
            <guid isPermaLink="false">https://medium.com/p/708f6a54768b</guid>
            <dc:creator><![CDATA[NOOBIE]]></dc:creator>
            <pubDate>Thu, 06 Apr 2023 09:53:05 GMT</pubDate>
            <atom:updated>2023-04-06T09:53:05.467Z</atom:updated>
            <content:encoded><![CDATA[<h3>JavaScript Getters and Setters: A Guide to Controlling Access and Modification of Class Properties with Examples</h3><p>JavaScript classes are a powerful feature that allows developers to define reusable code structures that can be instantiated into objects. One of the benefits of classes is the ability to encapsulate data and functionality, meaning that the internal state of a class can be controlled and manipulated in a predictable way. One way to achieve this is through the use of getters and setters, which provide a controlled interface for accessing and modifying class properties. In this article, we’ll explore how getters and setters work in JavaScript classes and provide examples of how they can be used.</p><h3>Understanding Getters and Setters</h3><p>Getters and setters are special methods that can be defined on a class to provide controlled access to its properties. They allow developers to define how a property is accessed or modified, rather than simply exposing the property directly.</p><p>A getter is a method that is called when a property is accessed. It is used to retrieve the current value of the property. A setter, on the other hand, is called when a property is assigned a new value. It is used to validate or transform the new value before assigning it to the property.</p><p>To define a getter or setter, you use the get and set keywords, respectively, followed by the name of the property you want to access or modify. Here&#39;s an example of how to define a getter and setter for a person class:</p><pre>javascriptCopy code</pre><pre>class Person { constructor(firstName, lastName) { this._firstName = firstName;this._lastName = lastName; } get fullName() { return `${this._firstName}${this._lastName}`; } set lastName(newLastName) { this._lastName = newLastName; } }</pre><p>In this example, we define a Person class with a constructor that takes a firstName and lastName parameter. We also define a getter for the fullName property that returns the concatenation of the first and last name. Finally, we define a setter for the lastName property that allows it to be modified.</p><h3>Benefits of Getters and Setters</h3><p>Getters and setters provide several benefits over exposing properties directly. One of the main benefits is that they provide a controlled interface for accessing and modifying class properties. By using getters and setters, developers can ensure that the internal state of a class is accessed or modified in a predictable way, which can help prevent bugs and make the code easier to maintain.</p><p>Another benefit of getters and setters is that they can be used to perform validation or transformation of property values. For example, a setter can be used to ensure that a property is always assigned a valid value, or to transform the value before assigning it to the property. This can help ensure that the internal state of a class is always in a valid state.</p><h3>Examples of Using Getters and Setters</h3><p>Let’s take a look at some examples of how getters and setters can be used in JavaScript classes.</p><h3>Example 1: Temperature Converter</h3><p>Suppose we want to create a class that represents a temperature value in Celsius and provides a way to convert it to Fahrenheit. We can use a getter to calculate the Fahrenheit value based on the Celsius value and a setter to ensure that the Celsius value is always within a certain range. Here’s an example:</p><pre>class Temperature {<br>  constructor(celsius) {<br>    this._celsius = celsius;<br>  }<br><br>  get celsius() {<br>    return this._celsius;<br>  }<br><br>  set celsius(newCelsius) {<br>    if (newCelsius &lt; -273.15) {<br>      throw new Error(&#39;Temperature below absolute zero is not possible&#39;);<br>    }<br>    this._celsius = newCelsius;<br>  }<br><br>  get fahrenheit() {<br>    return (this._celsius * 9/5) + 32;<br>  }<br><br>  set fahrenheit(newFahrenheit) {<br>    this._celsius = (newFahrenheit - 32) * 5/9;<br>  }<br>}</pre><p>In this example, we define a Temperature class that takes a celsius parameter in the constructor. We define a getter for the celsius property that simply returns the value of _celsius, the private variable that stores the temperature in Celsius. We also define a setter for the celsius property that checks if the new value is below absolute zero (-273.15 Celsius) and throws an error if it is.</p><p>We also define a getter for the fahrenheit property that calculates and returns the Fahrenheit equivalent of the temperature in Celsius. Finally, we define a setter for the fahrenheit property that converts the new Fahrenheit value to Celsius and assigns it to _celsius.</p><p>With these getters and setters in place, we can create instances of the Temperature class and access or modify their properties using a controlled interface. For example:</p><pre>const temp = new Temperature(25);<br>console.log(temp.celsius); // Output: 25<br>console.log(temp.fahrenheit); // Output: 77<br>temp.celsius = 30;<br>console.log(temp.celsius); // Output: 30<br>console.log(temp.fahrenheit); // Output: 86<br>temp.fahrenheit = 90;<br>console.log(temp.celsius); // Output: 32.22222222222222<br>console.log(temp.fahrenheit); // Output: 90</pre><h3>Example 2: Bank Account</h3><p>Let’s consider another example where we create a BankAccount class with a balance property. We can use getters and setters to add a transaction history to the account, where each transaction is represented as an object with a type (either &#39;deposit&#39; or &#39;withdraw&#39;) and a amount. Here&#39;s an example:</p><pre>class BankAccount {<br>  constructor(balance) {<br>    this._balance = balance;<br>    this._transactions = [];<br>  }<br><br>  get balance() {<br>    return this._balance;<br>  }<br><br>  set balance(newBalance) {<br>    this._balance = newBalance;<br>  }<br><br>  get transactions() {<br>    return this._transactions;<br>  }<br><br>  deposit(amount) {<br>    this._transactions.push({ type: &#39;deposit&#39;, amount });<br>    this._balance += amount;<br>  }<br><br>  withdraw(amount) {<br>    if (this._balance &lt; amount) {<br>      throw new Error(&#39;Insufficient funds&#39;);<br>    }<br>    this._transactions.push({ type: &#39;withdraw&#39;, amount });<br>    this._balance -= amount;<br>  }<br>}</pre><p>In this example, we define a BankAccount class with a constructor that takes a balance parameter. We also define a getter and setter for the balance property, which simply gets and sets the value of _balance.</p><p>We also define a getter for the transactions property, which returns an array of transaction objects. We then define two methods, deposit and withdraw, which allow the balance to be modified in a controlled way</p><p>he deposit method adds a new transaction object to the _transactions array with a type of &#39;deposit&#39; and the given amount. It then adds the amount to the current _balance.</p><p>The withdraw method checks if the given amount is less than or equal to the current _balance. If not, it throws an error. Otherwise, it adds a new transaction object to the _transactions array with a type of &#39;withdraw&#39; and the given amount. It then subtracts the amount from the current _balance.</p><p>With these getters and setters and methods in place, we can create instances of the BankAccount class and use them to keep track of transactions and balances in a controlled way. For example:</p><pre>const account = new BankAccount(100);<br>console.log(account.balance); // Output: 100<br>account.deposit(50);<br>console.log(account.balance); // Output: 150<br>console.log(account.transactions); // Output: [{ type: &#39;deposit&#39;, amount: 50 }]<br>account.withdraw(25);<br>console.log(account.balance); // Output: 125<br>console.log(account.transactions); // Output: [{ type: &#39;deposit&#39;, amount: 50 }, { type: &#39;withdraw&#39;, amount: 25 }]</pre><p>I hope this example helps to clarify how getters and setters can be used to define methods that allow access to and modification of class properties in a controlled way.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=708f6a54768b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript: Overriding Methods in Subclasses for Customized Behavior]]></title>
            <link>https://medium.com/@geekprogrammer11/javascript-overriding-methods-in-subclasses-for-customized-behavior-502286c962ab?source=rss-e9f839125a96------2</link>
            <guid isPermaLink="false">https://medium.com/p/502286c962ab</guid>
            <dc:creator><![CDATA[NOOBIE]]></dc:creator>
            <pubDate>Thu, 06 Apr 2023 09:45:34 GMT</pubDate>
            <atom:updated>2023-04-06T09:45:34.633Z</atom:updated>
            <content:encoded><![CDATA[<p>In object-oriented programming, inheritance allows us to create a new class based on an existing class, also known as the parent or superclass. When we create a subclass, it inherits all the properties and methods of its parent class, and we can add new properties and methods or modify the behavior of the inherited ones.</p><p>One way to modify the behavior of an inherited method is to override it in the subclass. Overriding a method means that we provide a new implementation of the method in the subclass that replaces the original implementation inherited from the superclass.</p><p>Let’s see how this works in JavaScript. Suppose we have a parent class called Animal that defines a method called speak():</p><pre>class Animal {<br>  speak() {<br>    console.log(&#39;The animal makes a sound&#39;);<br>  }<br>}</pre><p>We can create a subclass called Dog that extends Animal and overrides the speak() method to make it specific to dogs:</p><pre>class Dog extends Animal {<br>  speak() {<br>    console.log(&#39;The dog barks&#39;);<br>  }<br>}</pre><p>In this example, we used the extends keyword to create Dog as a subclass of Animal. Then, we defined a new speak() method in Dog that prints a different message than the speak() method in Animal.</p><p>When we create an instance of Dog and call its speak() method, we get the overridden behavior:</p><pre>const dog = new Dog(); dog.speak(); // outputs &quot;The dog barks&quot;</pre><p>Note that when we call dog.speak(), JavaScript looks for the speak() method in the Dog class first. Since Dog has its own speak() method, it uses that instead of the speak() method in the Animal class. This is what we mean by method overriding.</p><p>We can also access the original implementation of the speak() method in Animal by using the super keyword inside the Dog class:</p><pre>class Dog extends Animal {<br>  speak() {<br>    super.speak(); // calls the speak() method in Animal<br>    console.log(&#39;The dog barks&#39;);<br>  }<br>}</pre><p>Here, we first call super.speak() to invoke the speak() method in Animal. Then, we add our own console.log() statement to print the message specific to dogs. This way, we can reuse some of the behavior from the parent class and customize the rest in the subclass.</p><p>It’s important to note that when we override a method in a subclass, we should ensure that the new implementation is compatible with the original one in terms of the input parameters and return value. Otherwise, we risk breaking the functionality of the parent class or introducing unexpected behavior.</p><p>In conclusion, overriding methods in JavaScript subclasses is a powerful way to customize the behavior of inherited methods and make them specific to the needs of the subclass. By using the extends keyword and the super keyword, we can create subclasses that reuse and modify behavior from their parent classes.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=502286c962ab" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript Class Inheritance: How to Use the Extends Keyword to Create a Subclass That Inherits…]]></title>
            <link>https://medium.com/@geekprogrammer11/javascript-class-inheritance-how-to-use-the-extends-keyword-to-create-a-subclass-that-inherits-73e4f7614ec3?source=rss-e9f839125a96------2</link>
            <guid isPermaLink="false">https://medium.com/p/73e4f7614ec3</guid>
            <dc:creator><![CDATA[NOOBIE]]></dc:creator>
            <pubDate>Thu, 06 Apr 2023 09:21:19 GMT</pubDate>
            <atom:updated>2023-04-06T09:21:19.650Z</atom:updated>
            <content:encoded><![CDATA[<h3>JavaScript Class Inheritance: How to Use the Extends Keyword to Create a Subclass That Inherits Properties and Methods from a Parent Class</h3><p>Class inheritance is an important concept in object-oriented programming that allows you to create new classes that inherit properties and methods from existing classes. This saves you time and effort in writing code, and also helps to organize your code into logical hierarchies.</p><p>In JavaScript, you can create a subclass that inherits from a parent class using the extends keyword. Let&#39;s take a closer look at how this works and how you can use it in your own code.</p><h3>Creating a Parent Class</h3><p>First, let’s create a parent class that we can use as a base for our subclass. We’ll create a simple Person class that has two properties: name and age.</p><pre>class Person {<br>  constructor(name, age) {<br>    this.name = name;<br>    this.age = age;<br>  }<br><br>  sayHello() {<br>    console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);<br>  }<br>}</pre><p>Here, we’ve defined a constructor method that takes in two arguments (name and age) and sets the corresponding properties on the this object. We&#39;ve also defined a sayHello() method that logs a message to the console using the name and age properties.</p><h3>Creating a Subclass</h3><p>Now that we have a parent class, let’s create a subclass that inherits from it. We’ll create a Student class that adds a new property (grade) and a new method (study()).</p><pre>class Student extends Person {<br>  constructor(name, age, grade) {<br>    super(name, age);<br>    this.grade = grade;<br>  }<br><br>  study() {<br>    console.log(`${this.name} is studying hard in grade ${this.grade}.`);<br>  }<br>}</pre><p>Here, we’ve used the extends keyword to indicate that the Student class should inherit from the Person class. We&#39;ve also defined a constructor method that takes in three arguments (name, age, and grade) and calls the parent class&#39;s constructor method using the super() keyword. This sets the name and age properties on the this object, as defined in the parent class. We&#39;ve also set the grade property on the this object.</p><p>Finally, we’ve defined a study() method that logs a message to the console using the name and grade properties.</p><h3>Using the Subclass</h3><p>Now that we have a subclass, we can create instances of it and use its methods just like we would with the parent class. Let’s create a Student object and call its sayHello() and study() methods.</p><pre>const john = new Student(&quot;John&quot;, 18, 12);<br>john.sayHello(); // &quot;Hello, my name is John and I am 18 years old.&quot;<br>john.study(); // &quot;John is studying hard in grade 12.&quot;</pre><p>Here, we’ve created a new Student object with the name &quot;John&quot;, age 18, and grade 12. We&#39;ve then called its sayHello() method, which logs a message to the console using the name and age properties inherited from the parent class. We&#39;ve also called its study() method, which logs a message to the console using the name and grade properties defined in the subclass.</p><h3>Conclusion</h3><p>Class inheritance is a powerful feature of JavaScript that allows you to create new classes that inherit properties and methods from existing classes. By using the extends keyword, you can define a subclass that inherits from a parent class and add new properties and methods as needed. This can save you time and effort in writing code, and also help to organize your code into</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=73e4f7614ec3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript Class Methods: A Comprehensive Guide with Examples]]></title>
            <link>https://medium.com/@geekprogrammer11/javascript-class-methods-a-comprehensive-guide-with-examples-d1a6d4150d9?source=rss-e9f839125a96------2</link>
            <guid isPermaLink="false">https://medium.com/p/d1a6d4150d9</guid>
            <dc:creator><![CDATA[NOOBIE]]></dc:creator>
            <pubDate>Thu, 06 Apr 2023 09:10:28 GMT</pubDate>
            <atom:updated>2023-04-06T09:10:28.750Z</atom:updated>
            <content:encoded><![CDATA[<p>JavaScript classes provide a powerful way to organize code and define objects with properties and methods. Methods are functions that are associated with a class and operate on the class’s properties. In this article, we’ll cover everything you need to know about class methods, including how to define them, how to call them, and how to use them effectively in your code.</p><h3>Defining Class Methods</h3><p>In JavaScript, class methods are defined using the same syntax as regular object methods, but inside the class definition. For example, let’s define a simple class called Person that has a method called greet:</p><pre>class Person {<br>  constructor(name) {<br>    this.name = name;<br>  }<br>  <br>  greet() {<br>    console.log(`Hello, my name is ${this.name}`);<br>  }<br>}</pre><p>In this example, we define a Person class that has a single constructor that sets the name property of the instance. We also define a greet method that logs a greeting to the console that includes the person&#39;s name.</p><p>To call the greet method, we create a new instance of the Person class and call the method on that instance:</p><pre>const person = new Person(&#39;Alice&#39;);<br>person.greet(); // logs &quot;Hello, my name is Alice&quot;</pre><p>Note that the greet method has access to the name property of the instance using the this keyword. This is because the method is defined inside the class and has access to all of the class&#39;s properties and methods.</p><h3>Using Class Methods</h3><p>Class methods can be used to perform operations on the class’s properties or to return values based on those properties. For example, let’s modify our Person class to include a method that calculates the person&#39;s age based on their birth year:</p><pre>class Person {<br>  constructor(name, birthYear) {<br>    this.name = name;<br>    this.birthYear = birthYear;<br>  }<br>  <br>  greet() {<br>    console.log(`Hello, my name is ${this.name}`);<br>  }<br>  <br>  getAge() {<br>    const currentYear = new Date().getFullYear();<br>    return currentYear - this.birthYear;<br>  }<br>}</pre><p>In this example, we’ve added a birthYear property to the Person class and defined a getAge method that calculates the person&#39;s age based on the current year and their birth year. To use this method, we can call it on an instance of the Person class:</p><pre>const person = new Person(&#39;Alice&#39;, 1985);<br>console.log(person.getAge()); // logs 38</pre><p>Here, we create a new instance of the Person class with a birthYear of 1985. We then call the getAge method on that instance, which calculates the person&#39;s age as 38 based on the current year.</p><h3>Using the Static Keyword</h3><p>In addition to instance methods, JavaScript also supports static methods, which are methods that are associated with the class itself rather than with instances of the class. Static methods can be useful for defining utility functions or other operations that don’t depend on the state of the instance.</p><p>To define a static method in a JavaScript class, we use the static keyword before the method definition. For example, let&#39;s modify our Person class to include a static method that returns the current year:</p><pre>class Person {<br>  constructor(name, birthYear) {<br>    this.name = name;<br>    this.birthYear = birthYear;<br>  }<br>  <br>  greet() {<br>    console.log(`Hello, my name is ${this.name}`);<br>  }<br>  <br>  getAge() {<br>    const currentYear = Person.getCurrentYear();<br>    return currentYear - this.birthYear;<br>  }<br>  <br>  static getCurrentYear() {<br>    return new Date().getFullYear();<br>  }<br>}</pre><p>In this example, we’ve added a static method called getCurrentYear to the Person class, which simply returns the current year using the Date object. We then modify the getAge method to call Person.getCurrentYear() instead of creating a new Date object.</p><p>To call the getCurrentYear method, we don&#39;t need to create an instance of the Person class - we can simply call the method on the class itself:</p><pre>console.log(Person.getCurrentYear()); // logs the current year (e.g. 2023)</pre><p>Here, we call the getCurrentYear method directly on the Person class, without creating an instance of the class first.</p><h3>Conclusion</h3><p>Class methods are a powerful feature of JavaScript classes that allow us to define functions that operate on the class’s properties and return values based on those properties. We can define instance methods using the same syntax as regular object methods, and static methods using the static keyword. By understanding how to use class methods effectively, we can write more maintainable, reusable, and efficient code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d1a6d4150d9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript Class Constructors: How to Use the Constructor Method to Initialize Object Properties]]></title>
            <link>https://medium.com/@geekprogrammer11/javascript-class-constructors-how-to-use-the-constructor-method-to-initialize-object-properties-978b5f514dc7?source=rss-e9f839125a96------2</link>
            <guid isPermaLink="false">https://medium.com/p/978b5f514dc7</guid>
            <dc:creator><![CDATA[NOOBIE]]></dc:creator>
            <pubDate>Thu, 06 Apr 2023 08:59:34 GMT</pubDate>
            <atom:updated>2023-04-06T08:59:34.418Z</atom:updated>
            <content:encoded><![CDATA[<p>JavaScript classes are a way to define objects with specific properties and behaviors. They are similar to classes in other programming languages, but they are implemented using prototype-based inheritance. In this blog post, we’ll be discussing one of the most important aspects of JavaScript classes: the constructor method.</p><p>The constructor method is a special method that is called when a new instance of a class is created. It is responsible for initializing the properties of the new object. The constructor method is defined using the constructor keyword, and it can take parameters that are used to set the initial values of the object’s properties.</p><p>Here’s an example of a simple class with a constructor method:</p><pre>class Person {<br>  constructor(name, age) {<br>    this.name = name;<br>    this.age = age;<br>  }<br>}</pre><p>In this example, we define a class called Person with two properties: name and age. The constructor method takes two parameters, name and age, and assigns them to the corresponding properties of the object using the this keyword.</p><p>Now, let’s see how we can create an instance of this class:</p><pre>const person1 = new Person(&#39;Alice&#39;, 30);<br>console.log(person1); // { name: &#39;Alice&#39;, age: 30 }</pre><p>We use the new keyword to create a new instance of the Person class, and pass in two arguments, &#39;Alice&#39; and 30, to the constructor method. This creates a new object with the name property set to &#39;Alice&#39; and the age property set to 30.</p><p>It’s important to note that the constructor method is optional. If you don’t define a constructor method for your class, JavaScript will create a default constructor method that doesn’t do anything. However, if you want to initialize your object with specific values, you’ll need to define a constructor method.</p><p>In addition to setting initial values for properties, the constructor method can also be used to perform other setup tasks, such as setting up event listeners or connecting to a database. Just like any other method in a class, the constructor method can call other methods or access other properties of the object using the this keyword.</p><p>Here’s an example of a more complex class with a constructor method that performs some additional setup tasks:</p><pre>class Car {<br>  constructor(make, model, year) {<br>    this.make = make;<br>    this.model = model;<br>    this.year = year;<br>    this.started = false;<br>    this.engine = new Engine();<br>    this.setupEventListeners();<br>  }<br><br>  start() {<br>    this.engine.start();<br>    this.started = true;<br>  }<br><br>  stop() {<br>    this.engine.stop();<br>    this.started = false;<br>  }<br><br>  setupEventListeners() {<br>    window.addEventListener(&#39;keydown&#39;, (event) =&gt; {<br>      if (event.code === &#39;KeyS&#39;) {<br>        this.start();<br>      } else if (event.code === &#39;KeyQ&#39;) {<br>        this.stop();<br>      }<br>    });<br>  }<br>}<br><br>class Engine {<br>  start() {<br>    console.log(&#39;Engine started&#39;);<br>  }<br><br>  stop() {<br>    console.log(&#39;Engine stopped&#39;);<br>  }<br>}</pre><p>In this example, we define a class called Car with several properties, including make, model, year, started, and engine. The constructor method initializes these properties, creates a new instance of the Engine class, and sets up event listeners to listen for key events that start or stop the engine.</p><p>The Car class also has two methods, start and stop, that control the engine. These methods call the corresponding methods of the Engine class and update the startedproperty of the Car object.</p><p>The Engine class is a separate class that represents the engine of the car. It has two methods, start and stop, that log messages to the console to indicate whether the engine has started or stopped.</p><p>Here’s how we can create a new instance of the Car class:</p><pre>const car1 = new Car(&#39;Toyota&#39;, &#39;Corolla&#39;, 2022);</pre><p>This creates a new instance of the Car class with the make property set to &#39;Toyota&#39;, the model property set to &#39;Corolla&#39;, and the year property set to 2022. The started property is initially set to false, and the engine property is set to a new instance of the Engine class.</p><p>We can then call the start and stop methods of the Car object to start and stop the engine:</p><pre>car1.start(); // logs &quot;Engine started&quot;<br>car1.stop(); // logs &quot;Engine stopped&quot;</pre><p>These methods update the started property of the Car object and call the corresponding methods of the Engine object.</p><p>In summary, the constructor method is a crucial part of JavaScript classes that allows you to initialize the properties of a new object. It can take parameters to set the initial values of the object’s properties and perform other setup tasks. By using the constructor method, you can create more complex objects with specific behaviors and properties.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=978b5f514dc7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript Classes: A Beginner’s Guide to Creating Reusable Code Templates]]></title>
            <link>https://medium.com/@geekprogrammer11/javascript-classes-a-beginners-guide-to-creating-reusable-code-templates-8ec53334bbc3?source=rss-e9f839125a96------2</link>
            <guid isPermaLink="false">https://medium.com/p/8ec53334bbc3</guid>
            <dc:creator><![CDATA[NOOBIE]]></dc:creator>
            <pubDate>Thu, 06 Apr 2023 08:21:36 GMT</pubDate>
            <atom:updated>2023-04-06T08:21:36.041Z</atom:updated>
            <content:encoded><![CDATA[<p>JavaScript classes are a powerful feature of the language that allows you to create reusable code templates for objects. In other words, classes provide a way to define a blueprint or a template for creating objects that share the same properties and methods.</p><p>In this article, we’ll introduce you to the basics of JavaScript classes, why they are useful, and how to use them.</p><h3>What is a class in JavaScript?</h3><p>In JavaScript, a class is a template for creating objects. It defines the properties and methods that the objects will have, but does not create any objects itself. Once a class is defined, you can create as many objects from it as you need.</p><p>To create a class in JavaScript, you use the class keyword followed by the name of the class. For example:</p><pre>class Car {<br>  // class definition<br>}</pre><p>In this example, we have created a class called Car. This class does not define any properties or methods yet, but we can add them later.</p><h3>Why use classes in JavaScript?</h3><p>JavaScript classes are useful for several reasons:</p><h3>1. Encapsulation</h3><p>Classes provide a way to group related data and behavior into a single unit. This is known as encapsulation, and it is a fundamental principle of object-oriented programming. Encapsulation allows you to hide the complexity of the class from the outside world, and only expose a simple interface for interacting with it.</p><p>For example, consider a class that represents a bank account. The class might have properties like balance and accountNumber, and methods like deposit and withdraw. By encapsulating this functionality in a class, you can ensure that the account data is kept private and secure.</p><h3>2. Reusability</h3><p>Classes allow you to define a blueprint for creating multiple objects with the same properties and methods. This makes your code more reusable, since you can create new objects from the same class whenever you need them.</p><p>For example, imagine you are creating a game with multiple enemies. Each enemy might have the same properties like health and position, and methods like move and attack. By creating a class for the enemy, you can define these properties and methods once, and then create as many enemies as you need from the same class.</p><h3>3. Inheritance</h3><p>JavaScript classes support inheritance, which allows you to create new classes that inherit properties and methods from a parent class. This is useful when you want to create a new class that is similar to an existing class, but with some additional or different functionality.</p><p>For example, you might have a class called Animal that defines properties like name and species, and methods like eat and sleep. You could then create a subclass called Dog that inherits these properties and methods, but also has additional properties like breed and methods like bark.</p><h3>How to create a class in JavaScript</h3><p>To create a class in JavaScript, you use the class keyword followed by the name of the class. Inside the class, you define its properties and methods.</p><p>Here’s an example of a simple class that represents a person:</p><pre>class Person {<br>  constructor(name, age) {<br>    this.name = name;<br>    this.age = age;<br>  }<br><br>  sayHello() {<br>    console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);<br>  }<br>}</pre><p>In this example, we have created a class called Person. The class has a constructor method that takes two parameters: name and age. The constructor sets the name and age properties of the new object using the this keyword.</p><p>The class also has a method called sayHello() that logs a greeting message to the console.</p><p>To create a new object from this class, you use the new keyword followed by the name of the class and any parameters required by the constructor. For example:</p><pre>let person1 = new Person(&quot;Alice&quot;, 30);<br>let person2 = new Person(&quot;Bob&quot;, 25);<br><br>person1.sayHello(); // outputs &quot;Hello, my name is Alice and I am 30 years old.&quot;<br>person2.sayHello(); // outputs &quot;Hello, my name is Bob and I am 25 years old.&quot;</pre><p>In this example, we have created two new Person objects, person1 and person2, with different values for the name and age properties. We then called the sayHello() method on each object to log a greeting message to the console.</p><h3>Conclusion</h3><p>JavaScript classes are a powerful feature of the language that allow you to create reusable code templates for objects. They provide a way to group related data and behavior into a single unit, and support encapsulation, reusability, and inheritance. By mastering JavaScript classes, you can write more modular, scalable, and maintainable code.</p><p>Thank you for reading our beginner’s guide to JavaScript classes! We hope you found it informative and helpful. In this series, we will be covering a variety of topics related to JavaScript classes that will help you deepen your understanding of this powerful language feature.</p><p>We hope you’ll continue to follow along with this series and deepen your understanding of JavaScript classes. Stay tuned for the next installment, where we’ll be diving into inheritance and subclasses.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ec53334bbc3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[KOTLIN: All About NULL Safety]]></title>
            <link>https://medium.com/@geekprogrammer11/kotlin-understanding-null-safety-3c82e4b32c3a?source=rss-e9f839125a96------2</link>
            <guid isPermaLink="false">https://medium.com/p/3c82e4b32c3a</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[NOOBIE]]></dc:creator>
            <pubDate>Tue, 14 Mar 2023 07:40:14 GMT</pubDate>
            <atom:updated>2024-06-04T20:07:16.352Z</atom:updated>
            <content:encoded><![CDATA[<h3>KOTLIN: UNDERSTANDING NULL SAFETY</h3><p>Kotlin is a modern programming language developed by JetBrains, which runs on the Java Virtual Machine (JVM) and is gaining popularity among developers. One of the features that makes Kotlin stand out is its strong focus on null safety, which helps prevent common programming errors and makes it easier to write robust, reliable code.</p><p>Null safety refers to the ability of a programming language to detect and handle null values, which are values that represent the absence of a value. Null values can cause problems in programming because they can lead to null pointer exceptions, which can crash the program or cause unexpected behavior.</p><p>In this article, we will explore the various aspects of null safety in Kotlin and provide examples of how to ensure null safety in your Kotlin code.</p><h3>Declaring nullable types</h3><p>In Kotlin, types are either nullable or non-nullable. Non-nullable types cannot hold null values, whereas nullable types can. To declare a nullable type, simply add a question mark to the end of the type.</p><p>For example, the following code declares a nullable string:</p><pre>var nullableString: String? = null</pre><p>Here, the question mark after the String type indicates that the variable can hold a null value.</p><h3>Safe call operator</h3><p>The safe call operator (?.) is used to access properties or methods of an object that may be null. If the object is null, the expression returns null instead of throwing a null pointer exception.</p><p>For example, consider the following code:</p><pre>val str: String? = null val length: Int? = str?.length</pre><p>Here, the safe call operator is used to access the length property of the nullable string variable str. Since str is null, length will be assigned a null value instead of throwing a null pointer exception.</p><h3>Elvis operator</h3><p>The Elvis operator (?:) is used to provide a default value when a nullable variable is null. If the variable is not null, its value is used. If it is null, the default value is used instead.</p><p>For example, consider the following code:</p><pre>val str: String? = null val length: Int = str?.length ?: 0</pre><p>Here, the Elvis operator is used to assign a default value of 0 to the length variable if str is null. Otherwise, the length property of str is assigned to length.</p><h3>Not-null assertion operator</h3><p>The not-null assertion operator (!!) is used to tell the compiler that a nullable variable is not null at a certain point in the code. If the variable is null, a null pointer exception is thrown.</p><p>For example, consider the following code:</p><pre>var str: String? = null val length: Int = str!!.length</pre><p>Here, the not-null assertion operator is used to tell the compiler that str is not null at the point where the length property is accessed. If str is null, a null pointer exception will be thrown.</p><h3>Safe cast operator</h3><p>The safe cast operator (as?) is used to cast a variable to a certain type, returning null if the variable is not of that type.</p><p>For example, consider the following code:</p><pre>val obj: Any = &quot;hello&quot; val str: String? = obj as? String</pre><p>Here, the safe cast operator is used to cast the obj variable to a string. Since obj is a string, str will be assigned its value. If obj was not a string, str would be assigned a null value.</p><h3>Non-null types</h3><p>Non-null types are types that cannot hold null values. They are declared by adding an exclamation mark to the end of the type.</p><p>For example, the following code declares a non-null string:</p><pre>val nonNullString: String = &quot;hello&quot;</pre><p>Here, the exclamation mark after the String type indicates that the variable cannot hold a null value.</p><h3>Late initialization</h3><p>Kotlin also supports late initialization of non-nullable types. This means that you can declare a variable as non-nullable, but delay initializing it until later in the code.</p><p>For example:</p><pre>lateinit var nonNullString: String</pre><p>Here, the lateinit keyword tells the compiler that the variable will be initialized later in the code. This can be useful when you don’t know the value of a variable at the time of declaration, but you know it will be initialized before it is used.</p><h3>Non-null assertions</h3><p>Non-null assertions (!!) can be used to assert that a nullable variable is not null at a particular point in the code. This is similar to the not-null assertion operator, but it is used in the code rather than in the variable declaration.</p><p>For example:</p><pre>val nullableString: String? = &quot;hello&quot; val length: Int = nullableString!!.length</pre><p>Here, the non-null assertion is used to assert that nullableString is not null at the point where the length property is accessed. If nullableString is null, a null pointer exception will be thrown.</p><p>In conclusion, null safety is an important feature of Kotlin that can help prevent common programming errors and make your code more reliable. By declaring nullable types, using safe call and Elvis operators, using not-null assertions and safe casts, and leveraging non-null types and late initialization, you can ensure null safety in your Kotlin code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3c82e4b32c3a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>