<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sai Laasya Vabilisetty</title>
    <description>The latest articles on Forem by Sai Laasya Vabilisetty (@laasyasetty).</description>
    <link>https://forem.com/laasyasetty</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F409241%2F6fbf122b-f39a-4fae-b1f1-0c8d7a3512e7.jpeg</url>
      <title>Forem: Sai Laasya Vabilisetty</title>
      <link>https://forem.com/laasyasetty</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/laasyasetty"/>
    <language>en</language>
    <item>
      <title>Explain to me like I am Five: Time Complexity</title>
      <dc:creator>Sai Laasya Vabilisetty</dc:creator>
      <pubDate>Sun, 20 Sep 2020 08:36:32 +0000</pubDate>
      <link>https://forem.com/laasyasetty/explain-to-me-like-i-am-five-time-complexity-h3l</link>
      <guid>https://forem.com/laasyasetty/explain-to-me-like-i-am-five-time-complexity-h3l</guid>
      <description>&lt;h2&gt;
  
  
  Explain To Me Like I Am Five:
&lt;/h2&gt;

&lt;p&gt;Time Complexity&lt;/p&gt;

&lt;p&gt;Hello Everyone!!💛&lt;/p&gt;

&lt;p&gt;Time Complexity is simply a measure of time it takes for a program to complete its task. Time complexity plays a crucial role in every spot while programming. So, let's try to simplify and learn now.&lt;/p&gt;

&lt;h2&gt;
  
  
  CONTENTS
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;How to find time complexity?&lt;/li&gt;
&lt;li&gt;Understanding asymptotic analysis&lt;/li&gt;
&lt;li&gt;Analysis of Big-O complexities&lt;/li&gt;
&lt;li&gt;Big-O cheatsheet&lt;/li&gt;
&lt;li&gt;Do we consider time complexity of in-built functions?&lt;/li&gt;
&lt;li&gt;Time complexity of conditional statements&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How to find time complexity?
&lt;/h3&gt;

&lt;p&gt;To check which algorithm is better for our task, one normal way is to run both algorithms on our computer and note which one is taking less time. But, this way of finding time complexity is not effective as results depend on the factors like the performance of the device, inputs given, etc. So, we check the time complexity using &lt;strong&gt;asymptotic analysis&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;Asymptotic Analysis&lt;/strong&gt;, we evaluate the performance based on the input size we give. In other words, time complexity is estimated by counting the number of elementary steps performed by an algorithm to finish execution. &lt;br&gt;
Let's see an example to understand this in a better way, let us try to find using two different cases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1st Case&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//psuedocode
int i = 1 to N
N = N + N
print N
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2nd Case&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//pseudocode
return N * N
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the first case, time will depend on N, as N increases time also increases. In the second case, any value of N we take we will get the result in one step(independent of N).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cN5yqUUF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1600490203522/UGbSwTrBx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cN5yqUUF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1600490203522/UGbSwTrBx.png" alt="timecomplex.PNG"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Understanding asymptotic analysis
&lt;/h3&gt;

&lt;p&gt;Let's consider an example, there is a time complexity function obtained as T(n)=n^2+2n+8. Here for large values of n, (2n+8) becomes insignificant when compared to n^2.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We also neglect constant terms of higher-order coefficients. If we have 250n^2 and 300n^3, we only consider n^2 and n^3 ignoring constants.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--H3nVJvEQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1600491769980/F6zjALllJ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--H3nVJvEQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1600491769980/F6zjALllJ.png" alt="graph.PNG"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Analysis of Big-O complexities
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Time complexity is said to be O(1) when it does not contain loop(which varies with input), recursion, or function call.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for(int i=0; i &amp;lt; 25; i++){
//statments
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;O(N)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Time complexity is said to be O(N) when the variables in a loop are incremented or decremented.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for(int i= n; i &amp;gt; 0; i--){
//statments
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;O(N^k)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Time complexity can be said O(N^k)(k can be 1,2,3...) by the number of times the innermost loop of the nested loop is executed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for(int i= 0; i &amp;lt; n; i++){
for(j= 0 ;j &amp;lt; n; j++){
//statments
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;O(logN)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Time complexity is considered as O(logN) if the loop variables are divided or multiplied by a constant.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for(int i = 0; i &amp;lt; n; i = i * c){
//statments
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Big-O cheatsheet
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pmPOOJAx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1600497558125/O9si_ZBhc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pmPOOJAx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1600497558125/O9si_ZBhc.png" alt="cheatsheet.PNG"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Do we consider time complexity of in-built functions?
&lt;/h3&gt;

&lt;p&gt;Yes, we do consider time complexity of in-built functions which effect time like &lt;code&gt;Collections.sort()&lt;/code&gt; in java uses merge sort and has the complexity of O(NlogN), &lt;code&gt;sort()&lt;/code&gt; in python which uses Timsort and has complexity as O(NlogN), etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  Time Complexity of conditional statements
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//pseudocode
input n
if n&amp;lt;7
print "n is less than 7"
else
for int i = 0 to n
print i
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, observe the above code if the input we give is less than 7 then we execute only a print statement so time complexity is O(1). If the input is greater than 7 then there is a for loop which executes for n times. So complexity is n. Therefore best case time complexity is O(1) and worst case is O(N).&lt;/p&gt;

&lt;p&gt;That's it. Now you can find time complexity of code by your own.😍&lt;/p&gt;

&lt;h4&gt;
  
  
  Other articles you may like
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://laasyasettyblog.hashnode.dev/markdown-cheatsheet-to-write-a-stunning-article"&gt;Markdown Cheatsheet to write stunning articles!&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://laasyasettyblog.hashnode.dev/mastering-this-keyword-in-java"&gt;Mastering this keyword in JAVA&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://laasyasettyblog.hashnode.dev/scanner-class-nextline-issue-resolved-in-java"&gt;Scanner class nextLine() issue resolved in JAVA&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://laasyasettyblog.hashnode.dev/introduction-to-git-adding-an-ssh-key"&gt;Adding an SSH key in GitHub&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>computerscience</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Scanner class nextLine() issue resolved in JAVA</title>
      <dc:creator>Sai Laasya Vabilisetty</dc:creator>
      <pubDate>Fri, 18 Sep 2020 07:10:48 +0000</pubDate>
      <link>https://forem.com/laasyasetty/scanner-class-nextline-issue-resolved-in-java-4nc8</link>
      <guid>https://forem.com/laasyasetty/scanner-class-nextline-issue-resolved-in-java-4nc8</guid>
      <description>&lt;p&gt;Hello everyone!😋 &lt;/p&gt;

&lt;p&gt;This article highlights the basic theme of the scanner and a common error while using nextLine() method. So why late, let's dive into the topic!🤿&lt;/p&gt;

&lt;h2&gt;
  
  
  CONTENTS
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What is the Scanner class?&lt;/li&gt;
&lt;li&gt;Some basic methods in Scanner class&lt;/li&gt;
&lt;li&gt;Why nextLine() method skips input?&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is Scanner class?
&lt;/h3&gt;

&lt;p&gt;The scanner class belongs to &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html"&gt;java.util&lt;/a&gt; package. It is used for obtaining input for data types like int, char, double (all primitive types), string, used to read a file by passing file object. It can also be used for &lt;a href="https://www.w3schools.com/java/java_wrapper_classes.asp"&gt;wrapper&lt;/a&gt; class objects. &lt;/p&gt;

&lt;p&gt;We can access the methods of scanner class by a scanner object. While creating an object we pass &lt;code&gt;System.in&lt;/code&gt; as a parameter. This tells the java compiler that the input will be provided through &lt;strong&gt;standard input&lt;/strong&gt; (keyboard).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//creating a scanner object
Scanner scan=new Scanner(System.in);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Some basic methods in Scanner class
&lt;/h3&gt;

&lt;p&gt;Some of the basic methods you need to know while using scanner class are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;nextInt():&lt;/strong&gt; It scans the next token of the input as an int value.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; int a=scan.nextInt();  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;nextDouble():&lt;/strong&gt; It scans the next token of the input as double value.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;double a=scan.nextDouble();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;nextLine():&lt;/strong&gt; It scans from the current position until it finds a line seperator.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;String s=scan.nextLine();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will be explained more clearly in the next section. There are various other methods in Scanner class, you can check it &lt;a href="https://www.javatpoint.com/Scanner-class"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why nextLine() method skips input?
&lt;/h3&gt;

&lt;p&gt;So, to understand this clearly, let's start with an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class ScannerExample {
    public static void main(String args[]){
     Scanner sc=new Scanner(System.in);
     System.out.println("Enter account number");
       int account_num=sc.nextInt();
     System.out.println("Enter account holder's name");
       String name=sc.nextLine();
     System.out.println("Enter the amount to deposit");
       double amount=sc.nextInt();
     System.out.print("Account number: "+account_num);
     System.out.print("Name: "+name);
     System.out.print("Amount: "+amount);
    }
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, we are taking input for 3 variables &lt;code&gt;account_num&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;amount&lt;/code&gt; using respective scanner methods, which we have learnt earlier. Let's check the output now.&lt;/p&gt;

&lt;h4&gt;
  
  
  Expected Output:
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Account number: 154623
 Name: john doe
 Amount: 4000.0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Output we get:
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Account number: 154623
 Name:
 Amount: 4000.0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Did you observe the difference?🤔The input we give using &lt;code&gt;nextLine()&lt;/code&gt;for &lt;code&gt;name&lt;/code&gt; is skipped and continued with the next one.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why did this happen?
&lt;/h4&gt;

&lt;p&gt;So, when we used &lt;code&gt;nextInt()&lt;/code&gt; to get &lt;code&gt;account_num&lt;/code&gt; it scans till the end of input 154623 and the cursor remains there after reading the number like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;154623👆   //cursor position after reading input
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&amp;gt;Now, we know &lt;code&gt;nextLine()&lt;/code&gt; is used to take a string as an input which there by accepts spaces too.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, when we use &lt;code&gt;nextLine()&lt;/code&gt;, it scans from where the cursor is and takes empty space as input. That's the reason why we got an empty line for &lt;code&gt;name&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  How to resolve this?
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;We can just use a &lt;code&gt;nextLine()&lt;/code&gt; method right after taking int as input, so it takes the empty space away and allows us to give input for &lt;code&gt;name&lt;/code&gt;. The code looks something like this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  System.out.println("Enter account number");
     int account_num=sc.nextInt();
     sc.nextLine() //this takes away the empty space as input
  System.out.println("Enter account holder's name");
     String name=sc.nextLine();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We can also scan the complete line for integer using &lt;code&gt;nextLine()&lt;/code&gt; and convert it to int using &lt;code&gt;Integer.parseInt()&lt;/code&gt; , so &lt;code&gt;nextLine()&lt;/code&gt; reads till end of line and &lt;code&gt;parseInt()&lt;/code&gt; converts data into int.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;System.out.println("Enter account number");
  int account_num=Integer.parseInt(sc.nextLine());
 System.out.println("Enter account holder's name");
       String name=sc.nextLine();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So, whenever you are using &lt;code&gt;nextLine()&lt;/code&gt; after using any methods to get input like int, double, float etc, make sure that you apply these approaches.&lt;/p&gt;

&lt;p&gt;If you are reading this line, you have reached the end of the article and you have learnt a new thing today.😍&lt;/p&gt;

&lt;p&gt;Happy Java'ing!😊&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>webdev</category>
      <category>java</category>
      <category>computerscience</category>
    </item>
    <item>
      <title> Mastering 'this' keyword in Java.</title>
      <dc:creator>Sai Laasya Vabilisetty</dc:creator>
      <pubDate>Tue, 01 Sep 2020 15:58:58 +0000</pubDate>
      <link>https://forem.com/laasyasetty/mastering-this-keyword-in-java-4jao</link>
      <guid>https://forem.com/laasyasetty/mastering-this-keyword-in-java-4jao</guid>
      <description>&lt;p&gt;We mostly use &lt;strong&gt;this&lt;/strong&gt; keyword as a reference to the current object in a method or a constructor. In simple words, it is a reference variable that points to the current object. In the below example, &lt;strong&gt;this&lt;/strong&gt; keyword is used to refer to current class object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Example{
int a;
Example(int a){
this.a=a;           
}
public static void main(String args[]){
Example obj=new Example("John Doe");
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, there are many other cases where we can use &lt;strong&gt;this&lt;/strong&gt; keyword efficiently. Let's explore them now.🤩&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Using this keyword to eliminate confusion between class and instance variables.
&lt;/h3&gt;

&lt;p&gt;If the variable declared in class and method/constructor share the same name then we use this keyword to refer class instance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; As you can see in the below code, we have declared 2 global variables as &lt;code&gt;place&lt;/code&gt; and &lt;code&gt;pin&lt;/code&gt;, the same variable names are used for constructor parameters too. So, we use &lt;strong&gt;this&lt;/strong&gt; to tell the program that the arguments we pass for the constructor variables are for the global variables too. Therefore, when we try to print pin and place in &lt;code&gt;meth()&lt;/code&gt; it prints the values we passed during object creation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Example{
    String place;
    int pin;
    One(String place,int pin){
     this.pin=pin;                  
    this.place=place;
    }
     void meth(){
      System.out.println(place+" "+pin);
    }
    public static void main(String args[]){
      Example obj=new Example("chennai",603103);                                          
       obj.meth();
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output: chennai 603103
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. this keyword is used to call other constructors of the same class.
&lt;/h3&gt;

&lt;p&gt;Here, we are using &lt;strong&gt;this&lt;/strong&gt; to invoke a parameterized constructor inside a default constructor.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JgKOtbcd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1598866046045/28aCgWQNf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JgKOtbcd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1598866046045/28aCgWQNf.png" alt="this_two.PNG"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Example {
    Two(int a,int b){
    System.out.println("Parameterized Constructor");  
    }
    Two(){
        this(25,32);  
    System.out.println("Default Constructor");    
    }
    public static void main(String args[]){
    Example obj= new Example();   
    }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output:   Parameterized Constructor
          Default Constructor
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Constructor Chaining
&lt;/h3&gt;

&lt;p&gt;This is a process of calling one constructor from another constructor of the same class. The idea of this concept is already covered in the previous step, but let's explore it more clearly now.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4uQJ4hYV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1598938885296/6FvCdhUhv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4uQJ4hYV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1598938885296/6FvCdhUhv.png" alt="this_three.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;To understand clearly I have commented constructors with numbers in the below code. Now, when the object is created, constructor-1 is invoked which leads to constructor-2 as we used &lt;code&gt;this()&lt;/code&gt; to call and thereby constructor-2 uses &lt;code&gt;this()&lt;/code&gt; to call constructor-3. Therefore, the order of execution will be constructor 3,2 and 1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Example {
 /*1*/ Example(){
        this(10,"laasya");
        System.out.println("Constructor 1");
        }
 /*2*/ Example(int a,String s){
        this(10);
        System.out.println("Constructor 2");
    }
/*3*/ Example(int a){
        System.out.println("Constructor 3");
    }
     public static void main(String args[]){
        new Example();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output: Constructor 3
        Constructor 2
        Constructor 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Using this to invoke the current class method.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; Here we are using &lt;strong&gt;this&lt;/strong&gt; to call one method from another. Instead of writing &lt;code&gt;this.check()&lt;/code&gt; we can simply use &lt;code&gt;check()&lt;/code&gt; which works completely fine. But it is recommended to use &lt;strong&gt;this&lt;/strong&gt; as it ensures code readability in long run.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Example{
    void check(){
     System.out.println("This method is called from another method");
    }
    void checkTwo(){
        check();
    System.out.println("I called another method");      
    }
    public static void main(String args[]){
    Example obj=new Example();
    obj.checkTwo();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;output: This method is called from another method
        I called another method
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Passing this keyword as a method argument
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; In the below example, we have declared &lt;code&gt;methodOne()&lt;/code&gt; which has a class object as a parameter. So, while calling &lt;code&gt;methodOne()&lt;/code&gt; we can pass &lt;strong&gt;this&lt;/strong&gt; as an argument which prints the value of 'a' which was passed to constructor during object creation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Example {
int a;
Example(int b){
a=b;
}
void methodOne(Example object){
 System.out.println("I was called by using this keyword as arg "+a);
    }
void methodTwo(){
   methodOne(this);
    }
    public static void main(String args[]){
     Example obj= new Example(20);
     obj.methodTwo();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output: I was called by using this keyword as arg 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. Using this keyword to return the current class object.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; In the below code, when &lt;code&gt;meth()&lt;/code&gt; method which is defined by class-name is called it returns the current class instance. Remember &lt;code&gt;meth()&lt;/code&gt; returns but doesn't print. So, to print values we are using &lt;code&gt;display()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Example {
   int age;
   Example(){
     age =20;
   }
    Example meth(){
        return this;
    }
    void display(){
       System.out.println(age);
    }
    public static void main(String args[]){
Example obj=new Example();
obj.meth().display();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output: 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  7. Using this keyword as a constructor argument
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;In this below example, we have 2 classes A and T. When a new object of A is created, the default constructor of A creates a new object of class T passing &lt;strong&gt;this&lt;/strong&gt; as an argument. Now, the default constructor of T stores the details of object A in object T as we did &lt;code&gt;this.obj=obj&lt;/code&gt;. Hence, when we call `display()' it gets the value of the variable created in class A and prints it. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; public class A{
     int age=10;
     A(){
         T t=new T(this);
         t.display();
     }
     class T {
      A obj;
       T(A obj){
         this.obj=obj;
       }
       void display(){
           System.out.println(obj.age);
       }
   }
     public static void main(String args[]){
      new A();
    }

 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output: 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;That's it😍. You have reached the end of the article.&lt;br&gt;
Happy Java'ing people🤗&lt;/p&gt;




&lt;p&gt;Thank you for reading till the end. I would love to connect with you through &lt;a href="https://twitter.com/LaasyaSetty"&gt;Twitter&lt;/a&gt;,  &lt;a href="https://www.linkedin.com/in/sai-laasya-vabilisetty-305bba17a/"&gt;LinkedIn&lt;/a&gt;,  &lt;a href="https://github.com/laasya-setty"&gt;Github&lt;/a&gt;.😍&lt;/p&gt;

</description>
      <category>java</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
