<?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>DEV Community: Gaurav Kukade</title>
    <description>The latest articles on DEV Community by Gaurav Kukade (@gauravkukade).</description>
    <link>https://dev.to/gauravkukade</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%2F232474%2F38b72f4d-d841-4579-8469-4bbf7e342643.jpeg</url>
      <title>DEV Community: Gaurav Kukade</title>
      <link>https://dev.to/gauravkukade</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gauravkukade"/>
    <language>en</language>
    <item>
      <title>How To Convert double To int In Java?</title>
      <dc:creator>Gaurav Kukade</dc:creator>
      <pubDate>Thu, 04 Mar 2021 19:30:27 +0000</pubDate>
      <link>https://dev.to/gauravkukade/how-to-convert-double-to-int-in-java-38mf</link>
      <guid>https://dev.to/gauravkukade/how-to-convert-double-to-int-in-java-38mf</guid>
      <description>&lt;p&gt;&lt;em&gt;This article is originally published at &lt;a href="https://coderolls.com/convert-double-to-int/"&gt;https://coderolls.com/convert-double-to-int/&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In this article, we will see how we can convert a double to an int.&lt;/p&gt;

&lt;p&gt;In java programming, you will have a double primitive value (ex 82.14), but to do the further operations you need an int value (ex. 82) so let's see how to convert double to int in java.&lt;/p&gt;

&lt;p&gt;There are three ways you can convert double to int. I will list them all below and, then we will see them one by one.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;convert double to int - using typecasting&lt;/li&gt;
&lt;li&gt;convert double to int - using &lt;code&gt;Math.round()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;convert double to int - using &lt;code&gt;Double.IntValue()&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;You may like to visit:&lt;br&gt;
&lt;a href="https://coderolls.com/convert-int-to-string/"&gt;How To Convert An Integer To String In Java&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  1. convert double to int - using typecasting
&lt;/h2&gt;

&lt;p&gt;We know  &lt;code&gt;double&lt;/code&gt; is a 64-bit primitive value, and int is a 32-bit primitive value. So, to convert double to int, we can downcast the double value to int.&lt;/p&gt;

&lt;p&gt;I have given a simple example below that shows to convert double to int using typecasting.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * A java program to convert double to int using typecasting 
 * @author Gaurav Kukade at coderolls.com
 **/&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DoubleToIntUsingTypecasting&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;

     &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="o"&gt;[]&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;

        &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;doubleValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;82.14&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 82.14&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"doubleValue: "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;doubleValue&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;//typecase double to int&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;intValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;doubleValue&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 82&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"intValue: "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;intValue&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nl"&gt;doubleValue:&lt;/span&gt; &lt;span class="mf"&gt;82.14&lt;/span&gt;
&lt;span class="nl"&gt;intValue:&lt;/span&gt; &lt;span class="mi"&gt;82&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The problem with the typecasting is that it will truncate the value after the decimal point. It will not round it.&lt;/p&gt;

&lt;p&gt;In the case of 82.14, we will get an int value of 82, which looks ok. But when we have a double value like 82.99, we will get only 82 and loss the 0.99 that is ~1.&lt;/p&gt;

&lt;p&gt;It may create an issue in your calculations.&lt;/p&gt;

&lt;p&gt;In the case of 82.99, it should be rounded to 83 and then converted to int.&lt;/p&gt;

&lt;p&gt;It is not possible with typecasting, but our next solution can achieve it.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. convert double to int  - using &lt;code&gt;Math.round()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Math.round()&lt;/code&gt; method will round the floating-point value to the nearest long value. Then we can typecast it to the int.&lt;/p&gt;

&lt;p&gt;I have given a simple java program below that shows how to convert double to int using the &lt;code&gt;Math.round()&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** 
 * A java program to convert double to int using 
 * Math.round() method 
 * @author Gaurav Kukade at coderolls.com
 **/&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DoubleToIntUsingRoundMethod&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;

     &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="o"&gt;[]&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;

        &lt;span class="c1"&gt;// case 1&lt;/span&gt;
        &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;doubleValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;82.14&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 82.14&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"doubleValue: "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;doubleValue&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;//typecase double to int&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;intValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;round&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doubleValue&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 82&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"intValue: "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;intValue&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// case 2&lt;/span&gt;
        &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;nextDoubleValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;82.99&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// &lt;/span&gt;


        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"nextDoubleValue: "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;nextDoubleValue&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Math.round(nextDoubleValue) returns long value&lt;/span&gt;
        &lt;span class="c1"&gt;//typecase long to int&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;nextIntValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;round&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nextDoubleValue&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 83&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"nextIntValue: "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;nextIntValue&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;              
     &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nl"&gt;doubleValue:&lt;/span&gt; &lt;span class="mf"&gt;82.14&lt;/span&gt;
&lt;span class="nl"&gt;intValue:&lt;/span&gt; &lt;span class="mi"&gt;82&lt;/span&gt;

&lt;span class="nl"&gt;nextDoubleValue:&lt;/span&gt; &lt;span class="mf"&gt;82.99&lt;/span&gt;
&lt;span class="nl"&gt;nextIntValue:&lt;/span&gt; &lt;span class="mi"&gt;83&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. convert double to int - using &lt;code&gt;Double.intValue()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;In this way, we will convert the double primitive value to the &lt;code&gt;Double&lt;/code&gt; wrapper class, and then we can use the &lt;code&gt;intValue()&lt;/code&gt; method of the &lt;code&gt;Double&lt;/code&gt; wrapper class.&lt;/p&gt;

&lt;p&gt;This method does not round the value before converting it to the int value. It will remove the digits after the decimal point.&lt;/p&gt;

&lt;p&gt;I have given a simple java program below that shows how to convert double to int using the &lt;code&gt;Double.IntValue()&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * 
 * A java program to convert double to int using 
 * Double.intValue() method  
 * @author Gaurav Kukade at coderolls.com
 * 
 **/&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DoubleToIntUsingIntValueMethod&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;

     &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="o"&gt;[]&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;

        &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;doubleValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;82.14&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 82.14&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"doubleValue: "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;doubleValue&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;//create Double wrapper object&lt;/span&gt;
        &lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="n"&gt;doubleValueObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Double&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doubleValue&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;


        &lt;span class="c1"&gt;//typecase double to int&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;intValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;doubleValueObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;intValue&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 82&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"intValue: "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;intValue&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
     &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nl"&gt;doubleValue:&lt;/span&gt; &lt;span class="mf"&gt;82.14&lt;/span&gt;
&lt;span class="nl"&gt;intValue:&lt;/span&gt; &lt;span class="mi"&gt;82&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We can convert double to int in java using the three ways given below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. convert double to int - using typecasting&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this method we typecast the double value to int as give below,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;intValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;round&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doubleValue&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But in this way, we will lose the value after the decimal point. It will not do the rounding before converting double to int.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. convert double to int - using &lt;code&gt;Math.round()&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this way, we use the &lt;code&gt;Math.round()&lt;/code&gt; method for the rounding purpose. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;Math.round()&lt;/code&gt; method round the double value to the nearest long, and then we can typecast long to the int as given below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;nextIntValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;round&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nextDoubleValue&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. convert double to int - using &lt;code&gt;Double.IntValue()&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this way, we convert the &lt;code&gt;double&lt;/code&gt; value to the &lt;code&gt;Double&lt;/code&gt; wrapper class, and then we use the &lt;code&gt;Double.intValue()&lt;/code&gt; method to get the int value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;//create Double wrapper object&lt;/span&gt;
&lt;span class="nc"&gt;Double&lt;/span&gt; &lt;span class="n"&gt;doubleValueObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Double&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doubleValue&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//typecase double to int&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;intValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;doubleValueObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;intValue&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this way also we will lose the digits after the decimal points.&lt;/p&gt;

&lt;p&gt;So this is how we do convert a double to int in java. You can check all three (&lt;a href="https://github.com/coderolls/blogpost-coding-examples/blob/main/java-basic/DoubleToIntUsingTypecasting.java"&gt;DoubleToIntUsingTypecasting.java&lt;/a&gt;, &lt;a href="https://github.com/coderolls/blogpost-coding-examples/blob/main/java-basic/DoubleToIntUsingRoundMethod.java"&gt;DoubleToIntUsingRoundMethod.java&lt;/a&gt;, &lt;a href="https://github.com/coderolls/blogpost-coding-examples/blob/main/java-basic/DoubleToIntUsingIntValueMethod.java"&gt;DoubleToIntUsingIntValueMethod.java&lt;/a&gt;) programs on GitHub &lt;/p&gt;

&lt;p&gt;You can read more about &lt;a href="https://coderolls.com/convert-int-to-string/"&gt;string to int&lt;/a&gt; and &lt;a href="https://coderolls.com/convert-string-to-int/"&gt;int to string&lt;/a&gt; conversion.&lt;/p&gt;




&lt;p&gt;You can visit my &lt;a href="https://www.youtube.com/channel/UCl31HHUdQbSHOQfc9L-wo3w?view_as=subscriber?sub_confirmation=1"&gt;YouTube channel 'coderolls'&lt;/a&gt; to find more video tutorials.&lt;/p&gt;

&lt;p&gt;If you found this article worth, support me by  &lt;a href="https://www.paypal.me/GauravKukade"&gt;giving a cup of Coffee ☕&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Related Articles
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://coderolls.com/convert-int-to-string/"&gt;How To Convert An Integer To String In Java&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://coderolls.com/convert-string-to-int/"&gt;How to convert String to Integer in Java&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://coderolls.com/convert-stringbuilder-to-string-in-java/"&gt;How To Convert StringBuilder To String In Java?&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://coderolls.com/compare-strings-in-java/"&gt;How Do I Compare Strings In Java&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://coderolls.com/reverse-a-string-in-java/"&gt;How To Reverse A String In Java (5 ways)&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>How To Parse JSON In Java?</title>
      <dc:creator>Gaurav Kukade</dc:creator>
      <pubDate>Sat, 09 Jan 2021 06:04:54 +0000</pubDate>
      <link>https://dev.to/gauravkukade/how-to-parse-json-in-java-378j</link>
      <guid>https://dev.to/gauravkukade/how-to-parse-json-in-java-378j</guid>
      <description>&lt;p&gt;In this article we will see, how to parse JSON in java. We will be using the JSON Libraries like JSON-java, GSON and json-simple&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JSON&lt;/strong&gt; stands for JavaScript Object Notation. It is easy to use and lightweight open standard file format for storing and transporting data.&lt;/p&gt;

&lt;p&gt;As per the &lt;a href="https://www.json.org/json-en.html"&gt;json.org&lt;/a&gt;,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is easy for humans to read and write. It is easy for machines to parse and generate.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Example JSON:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"coderolls"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"blog"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"address"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"street"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1600 Pennsylvania Avenue NW"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"city"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Washington"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"state"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"DC"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"employees"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"firstName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"lastName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Doe"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"firstName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Anna"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"lastName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Smith"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"firstName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Peter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"lastName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Jones"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above JSON, &lt;code&gt;name&lt;/code&gt; is key and &lt;code&gt;coderolls&lt;/code&gt; is it's value. For &lt;code&gt;address&lt;/code&gt; key, value is another JSON object which contains key and values.&lt;/p&gt;

&lt;p&gt;For &lt;code&gt;employees&lt;/code&gt; key, it's value is array of JSON objects.&lt;/p&gt;

&lt;p&gt;Today we will see three json libraries in java to parse a JSON string. These are listed below,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;JSON-Java&lt;/li&gt;
&lt;li&gt;GSON&lt;/li&gt;
&lt;li&gt;json-simple&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now we will see one by one example of all three libraries  for parsing a JSON in java.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to parse JOSN in Java using JSON-Java
&lt;/h2&gt;

&lt;p&gt;JSON-java is one of the most simple JSON library for Java.&lt;/p&gt;

&lt;p&gt;Here, we will be using the &lt;code&gt;JSONObject&lt;/code&gt; class of the JSON-java library.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;JSONObject&lt;/code&gt; &lt;a href="https://stleary.github.io/JSON-java/index.html"&gt;has a constructor which accepts string&lt;/a&gt;. We will be using ths constructor to parse a JSON string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public JSONObject(String source) throws JSONException

Construct a JSONObject from a source JSON text string. This is the most commonly used JSONObject constructor.

Parameters:

source  - A string beginning with  `{` (left brace)  and ending with  `}`  (right brace).

Throws:

JSONException  - If there is a syntax error in the source string or a duplicated key.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can refer &lt;a href="https://stleary.github.io/JSON-java/index.html"&gt;JSON-java Docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Maven:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dependency&amp;gt;
    &amp;lt;groupId&amp;gt;org.json&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;json&amp;lt;/artifactId&amp;gt;
    &amp;lt;version&amp;gt;20201115&amp;lt;/version&amp;gt;
&amp;lt;/dependency&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Gradle:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;compile group: 'org.json', name: 'json', version: '20201115'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.coderolls.JSONExample&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.json.JSONArray&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.json.JSONObject&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * A program to parse JSON strin in Java using JSON-Java
 * @author Gaurav Kukade at coderolls.com
 */&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ParseJSONUsingJSONJava&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;jsonString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"{"&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"  \"name\": \"coderolls\","&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"  \"type\": \"blog\","&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"  \"address\": {"&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    \"street\": \"1600 Pennsylvania Avenue NW\","&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    \"city\": \"Washington\","&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    \"state\": \"DC\""&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"  },"&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"  \"employees\": ["&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    {"&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"      \"firstName\": \"John\","&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"      \"lastName\": \"Doe\""&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    },"&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    {"&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"      \"firstName\": \"Anna\","&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"      \"lastName\": \"Smith\""&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    },"&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    {"&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"      \"firstName\": \"Peter\","&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"      \"lastName\": \"Jones\""&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    }"&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"  ]"&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"}"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Parsing the json string in java using JSON-Java......\n"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;//add jsonString to the constructor&lt;/span&gt;
        &lt;span class="nc"&gt;JSONObject&lt;/span&gt; &lt;span class="n"&gt;coderollsJSONObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;JSONObject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jsonString&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;//now we can access the values &lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;coderollsJSONObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Name: "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="s"&gt;"\n"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;//we can get the JSON object present as value of any key in the parent JSON&lt;/span&gt;
        &lt;span class="nc"&gt;JSONObject&lt;/span&gt; &lt;span class="n"&gt;addressJSONObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;coderollsJSONObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getJSONObject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"address"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;//access the values of the addressJSONObject &lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;street&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;addressJSONObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"street"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Street: "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;street&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="s"&gt;"\n"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;


        &lt;span class="c1"&gt;//we can get the json array present as value of any key in the parent JSON&lt;/span&gt;
        &lt;span class="nc"&gt;JSONArray&lt;/span&gt; &lt;span class="n"&gt;employeesJSONArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;coderollsJSONObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getJSONArray&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"employees"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Printing the employess json array: \n"&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;employeesJSONArray&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()+&lt;/span&gt;&lt;span class="s"&gt;"\n"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;//we can get individual json object at an index from the employeesJSONArray&lt;/span&gt;
        &lt;span class="nc"&gt;JSONObject&lt;/span&gt; &lt;span class="n"&gt;employeeJSONObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;employeesJSONArray&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getJSONObject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;employeeJSONObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"firstName"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"First Name of the employee at index 0: "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Get the code for &lt;a href="https://github.com/coderolls/blogpost-coding-examples/blob/main/java-json/parse-json-in-java/ParseJSONUsingJSONJava.java"&gt;ParseJSONUsingJSON.Java&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Parsing&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt; &lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="no"&gt;JSON&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Java&lt;/span&gt;&lt;span class="o"&gt;......&lt;/span&gt;

&lt;span class="nl"&gt;Name:&lt;/span&gt; &lt;span class="n"&gt;coderolls&lt;/span&gt;

&lt;span class="nl"&gt;Street:&lt;/span&gt; &lt;span class="mi"&gt;1600&lt;/span&gt; &lt;span class="nc"&gt;Pennsylvania&lt;/span&gt; &lt;span class="nc"&gt;Avenue&lt;/span&gt; &lt;span class="no"&gt;NW&lt;/span&gt;

&lt;span class="nc"&gt;Printing&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;employess&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt; &lt;span class="nl"&gt;array:&lt;/span&gt; 
&lt;span class="o"&gt;[{&lt;/span&gt;&lt;span class="s"&gt;"firstName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"John"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"lastName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"Doe"&lt;/span&gt;&lt;span class="o"&gt;},{&lt;/span&gt;&lt;span class="s"&gt;"firstName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"Anna"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"lastName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"Smith"&lt;/span&gt;&lt;span class="o"&gt;},{&lt;/span&gt;&lt;span class="s"&gt;"firstName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"Peter"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"lastName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"Jones"&lt;/span&gt;&lt;span class="o"&gt;}]&lt;/span&gt;

&lt;span class="nc"&gt;First&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;employee&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;John&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to parse JOSN in Java using Gson
&lt;/h2&gt;

&lt;p&gt;Gson is an open-source Java library to serialize and deserialize Java objects to JSON developed at Google.&lt;/p&gt;

&lt;p&gt;So we can use it to convert a JSON string to an equivalent Java object.&lt;/p&gt;

&lt;p&gt;Here, we will be using the &lt;code&gt;JsonObject&lt;/code&gt; class of the Gson library.&lt;/p&gt;

&lt;p&gt;You can refer the &lt;a href="https://www.javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/JsonObject.html"&gt;docs of JsonObject&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Maven:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dependency&amp;gt;
    &amp;lt;groupId&amp;gt;com.google.code.gson&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;gson&amp;lt;/artifactId&amp;gt;
    &amp;lt;version&amp;gt;2.8.6&amp;lt;/version&amp;gt;
&amp;lt;/dependency&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Gradle:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.coderolls.JSONExample&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.google.gson.Gson&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.google.gson.JsonArray&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.google.gson.JsonObject&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * A program to parse JSON strin in Java using Gson
 * @author Gaurav Kukade at coderolls.com
 */&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ParseJSONUsingGSON&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="c1"&gt;//take json as string&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;jsonString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"{"&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"  \"name\": \"coderolls\","&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"  \"type\": \"blog\","&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"  \"address\": {"&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    \"street\": \"1600 Pennsylvania Avenue NW\","&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    \"city\": \"Washington\","&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    \"state\": \"DC\""&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"  },"&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"  \"employees\": ["&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    {"&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"      \"firstName\": \"John\","&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"      \"lastName\": \"Doe\""&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    },"&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    {"&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"      \"firstName\": \"Anna\","&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"      \"lastName\": \"Smith\""&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    },"&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    {"&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"      \"firstName\": \"Peter\","&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"      \"lastName\": \"Jones\""&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    }"&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"  ]"&lt;/span&gt;
                &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"}"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Parsing the json string in java using Gson......\n"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;Gson&lt;/span&gt; &lt;span class="n"&gt;gson&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Gson&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;//get json object from the json string&lt;/span&gt;
        &lt;span class="nc"&gt;JsonObject&lt;/span&gt; &lt;span class="n"&gt;coderollsJsonObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gson&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;fromJson&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jsonString&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;JsonObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;//now we can access the values &lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;coderollsJsonObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;getAsString&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Name: "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="s"&gt;"\n"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;//we can get the JSON object present as value of any key in the parent JSON&lt;/span&gt;
        &lt;span class="nc"&gt;JsonObject&lt;/span&gt; &lt;span class="n"&gt;addressJsonObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;coderollsJsonObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"address"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;getAsJsonObject&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;//access the values of the addressJSONObject &lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;street&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;addressJsonObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"street"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;getAsString&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Street: "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;street&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="s"&gt;"\n"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;


        &lt;span class="c1"&gt;//we can get the json array present as value of any key in the parent JSON&lt;/span&gt;
        &lt;span class="nc"&gt;JsonArray&lt;/span&gt; &lt;span class="n"&gt;employeesJsonArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;coderollsJsonObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"employees"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;getAsJsonArray&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Printing the employess json array: \n"&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;employeesJsonArray&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()+&lt;/span&gt;&lt;span class="s"&gt;"\n"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;//we can get individual json object at an index from the employeesJSONArray&lt;/span&gt;
        &lt;span class="nc"&gt;JsonObject&lt;/span&gt; &lt;span class="n"&gt;employeeJsonObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;employeesJsonArray&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;getAsJsonObject&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;employeeJsonObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"firstName"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;getAsString&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"First Name of the employee at index 0: "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Get the code for &lt;a href="https://github.com/coderolls/blogpost-coding-examples/blob/main/java-json/parse-json-in-java/ParseJSONUsingGSON.java"&gt;ParseJSONUsingGSON.java&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Parsing&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt; &lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;Gson&lt;/span&gt;&lt;span class="o"&gt;......&lt;/span&gt;

&lt;span class="nl"&gt;Name:&lt;/span&gt; &lt;span class="n"&gt;coderolls&lt;/span&gt;

&lt;span class="nl"&gt;Street:&lt;/span&gt; &lt;span class="mi"&gt;1600&lt;/span&gt; &lt;span class="nc"&gt;Pennsylvania&lt;/span&gt; &lt;span class="nc"&gt;Avenue&lt;/span&gt; &lt;span class="no"&gt;NW&lt;/span&gt;

&lt;span class="nc"&gt;Printing&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;employess&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt; &lt;span class="nl"&gt;array:&lt;/span&gt; 
&lt;span class="o"&gt;[{&lt;/span&gt;&lt;span class="s"&gt;"firstName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"John"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"lastName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"Doe"&lt;/span&gt;&lt;span class="o"&gt;},{&lt;/span&gt;&lt;span class="s"&gt;"firstName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"Anna"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"lastName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"Smith"&lt;/span&gt;&lt;span class="o"&gt;},{&lt;/span&gt;&lt;span class="s"&gt;"firstName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"Peter"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"lastName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"Jones"&lt;/span&gt;&lt;span class="o"&gt;}]&lt;/span&gt;

&lt;span class="nc"&gt;First&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;employee&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;John&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to parse JOSN in Java using json-simple
&lt;/h2&gt;

&lt;p&gt;JSON.simple is a simple Java toolkit for JSON. You can use JSON.simple to encode or decode JSON text.&lt;/p&gt;

&lt;p&gt;Maven:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dependency&amp;gt;
    &amp;lt;groupId&amp;gt;com.googlecode.json-simple&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;json-simple&amp;lt;/artifactId&amp;gt;
    &amp;lt;version&amp;gt;1.1.1&amp;lt;/version&amp;gt;
&amp;lt;/dependency&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Gradle:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can refer the &lt;a href="https://code.google.com/archive/p/json-simple/"&gt;json-simple project page.java&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;com.coderolls.JSONExample&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.json.simple.JSONArray&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.json.simple.JSONObject&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.json.simple.parser.JSONParser&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.json.simple.parser.ParseException&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * A program to parse JSON strin in Java using json-simple
 * @author Gaurav Kukade at coderolls.com
 */&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ParseJSONUsingJsonSimple&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//take json as string&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;jsonString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"{"&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"  \"name\": \"coderolls\","&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"  \"type\": \"blog\","&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"  \"address\": {"&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    \"street\": \"1600 Pennsylvania Avenue NW\","&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    \"city\": \"Washington\","&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    \"state\": \"DC\""&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"  },"&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"  \"employees\": ["&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    {"&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"      \"firstName\": \"John\","&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"      \"lastName\": \"Doe\""&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    },"&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    {"&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"      \"firstName\": \"Anna\","&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"      \"lastName\": \"Smith\""&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    },"&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    {"&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"      \"firstName\": \"Peter\","&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"      \"lastName\": \"Jones\""&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"    }"&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"  ]"&lt;/span&gt;
                        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"}"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Parsing the json string in java using json-simple......\n"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;JSONParser&lt;/span&gt; &lt;span class="n"&gt;parser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;JSONParser&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;JSONObject&lt;/span&gt; &lt;span class="n"&gt;coderollsJSONObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;JSONObject&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;coderollsJSONObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;JSONObject&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;parser&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jsonString&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ParseException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printStackTrace&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;//now we can access the values &lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;coderollsJSONObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Name: "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="s"&gt;"\n"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;//we can get the JSON object present as value of any key in the parent JSON&lt;/span&gt;
        &lt;span class="nc"&gt;JSONObject&lt;/span&gt; &lt;span class="n"&gt;addressJSONObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;JSONObject&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;coderollsJSONObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"address"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;//access the values of the addressJSONObject &lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;street&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;addressJSONObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"street"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Street: "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;street&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="s"&gt;"\n"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;


        &lt;span class="c1"&gt;//we can get the json array present as value of any key in the parent JSON&lt;/span&gt;
        &lt;span class="nc"&gt;JSONArray&lt;/span&gt; &lt;span class="n"&gt;employeesJSONArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;JSONArray&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;coderollsJSONObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"employees"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Printing the employess json array: \n"&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;employeesJSONArray&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()+&lt;/span&gt;&lt;span class="s"&gt;"\n"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;//we can get individual json object at an index from the employeesJSONArray&lt;/span&gt;
        &lt;span class="nc"&gt;JSONObject&lt;/span&gt; &lt;span class="n"&gt;employeeJSONObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;JSONObject&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;employeesJSONArray&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;employeeJSONObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"firstName"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"First Name of the employee at index 0: "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Get the code for &lt;a href="https://github.com/coderolls/blogpost-coding-examples/blob/main/java-json/parse-json-in-java/ParseJSONUsingJsonSimple.java"&gt;ParseJSONUsingJsonSimple.java&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Parsing&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt; &lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;simple&lt;/span&gt;&lt;span class="o"&gt;......&lt;/span&gt;

&lt;span class="nl"&gt;Name:&lt;/span&gt; &lt;span class="n"&gt;coderolls&lt;/span&gt;

&lt;span class="nl"&gt;Street:&lt;/span&gt; &lt;span class="mi"&gt;1600&lt;/span&gt; &lt;span class="nc"&gt;Pennsylvania&lt;/span&gt; &lt;span class="nc"&gt;Avenue&lt;/span&gt; &lt;span class="no"&gt;NW&lt;/span&gt;

&lt;span class="nc"&gt;Printing&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;employess&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt; &lt;span class="nl"&gt;array:&lt;/span&gt; 
&lt;span class="o"&gt;[{&lt;/span&gt;&lt;span class="s"&gt;"firstName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"John"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"lastName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"Doe"&lt;/span&gt;&lt;span class="o"&gt;},{&lt;/span&gt;&lt;span class="s"&gt;"firstName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"Anna"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"lastName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"Smith"&lt;/span&gt;&lt;span class="o"&gt;},{&lt;/span&gt;&lt;span class="s"&gt;"firstName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"Peter"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"lastName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;"Jones"&lt;/span&gt;&lt;span class="o"&gt;}]&lt;/span&gt;

&lt;span class="nc"&gt;First&lt;/span&gt; &lt;span class="nc"&gt;Name&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;employee&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;John&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;All three libraries allow you create a JSON object from the json string but I found the JSON-Java is easy to use.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This blogpost is already published at : &lt;a href="https://coderolls.com/parse-json-in-java/"&gt;https://coderolls.com/parse-json-in-java/&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You can visit my &lt;a href="https://www.youtube.com/channel/UCl31HHUdQbSHOQfc9L-wo3w?view_as=subscriber?sub_confirmation=1"&gt;YouTube channel 'coderolls'&lt;/a&gt; to find more video tutorials.&lt;/p&gt;




&lt;p&gt;You can support me by &lt;a href="https://www.paypal.me/GauravKukade"&gt;giving a cup of Coffee ☕&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Related Articles
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://coderolls.com/basic-git-commands/"&gt;8 Basic GIT Commands Every Newbie Developer Must Know&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://coderolls.com/reverse-a-string-in-java/"&gt;How To Reverse A String In Java (5 ways)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://coderolls.com/create-uuid-in-java/"&gt;How To Create UUID in Java?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://coderolls.com/java-string-pool-and-intern-method/"&gt;Learn About Java String Pool And intern() Method&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://coderolls.com/compare-strings-in-java/"&gt;How Do I Compare Strings In Java&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://coderolls.com/compare-two-strings-lexicographically-in-java/"&gt;Compare Two Strings Lexicographically In Java&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://coderolls.com/difference-between-stringbuffer-and-stringbuilder/"&gt;Difference Between StringBuffer and StringBuilder class&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>json</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>How To Create UUID in Java?</title>
      <dc:creator>Gaurav Kukade</dc:creator>
      <pubDate>Thu, 24 Dec 2020 03:52:16 +0000</pubDate>
      <link>https://dev.to/gauravkukade/how-to-create-uuid-in-java-317l</link>
      <guid>https://dev.to/gauravkukade/how-to-create-uuid-in-java-317l</guid>
      <description>&lt;p&gt;In this article you will see, how to create UUID in Java.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;UUID, a universally unique identifier is a 128-bit number used to identify information in computer systems.&lt;/p&gt;

&lt;p&gt;UUID is made of hex digits along with 4 hyphen ("-") symbols. The length of a UUID is 36 characters.&lt;/p&gt;

&lt;p&gt;There are 5 types of UUID but mostly version 4 i.e. Randomly Generated UUID is used.&lt;/p&gt;

&lt;p&gt;We are going to create the version 4 UUID here.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example UUID
&lt;/h3&gt;

&lt;p&gt;df6fdea1-10c3-474c-ae62-e63def80de0b&lt;/p&gt;

&lt;h2&gt;
  
  
  How to create UUID in Java?
&lt;/h2&gt;

&lt;p&gt;Creating a Randomly Generated UUID (version 4) is really easy in Java.&lt;/p&gt;

&lt;p&gt;UUID Class is present in  &lt;code&gt;java.util&lt;/code&gt; package. And it has the static method &lt;code&gt;randomUUID()&lt;/code&gt; which returns the randomly generated UUID.&lt;/p&gt;

&lt;p&gt;The example is given below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.UUID&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * A Java program to create a Randomly Generated UUID i.e. Version 4 UUID
 * @author Gaurav Kukade at coderolls.com
 *
 */&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CreateUUID&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="c1"&gt;// creating random uuid i.e. version 4 UUID&lt;/span&gt;
        &lt;span class="no"&gt;UUID&lt;/span&gt; &lt;span class="n"&gt;uuid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;UUID&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;randomUUID&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Printing the randomly generated UUID value......\n"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"uuid: "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;uuid&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Printing&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;randomly&lt;/span&gt; &lt;span class="n"&gt;generated&lt;/span&gt; &lt;span class="no"&gt;UUID&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;......&lt;/span&gt;

&lt;span class="nl"&gt;uuid:&lt;/span&gt; &lt;span class="n"&gt;e3aed661&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;ccc2&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="n"&gt;fd&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;ad69&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;734&lt;/span&gt;&lt;span class="n"&gt;fee350cd2&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Get the &lt;a href="https://gist.github.com/gauravkukade/395f314c549969bd300d72c7e032dbcb"&gt;above code as GitHub Gist&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This article was originally published at &lt;a href="https://coderolls.com/create-uuid-in-java/"&gt;https://coderolls.com/create-uuid-in-java/&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;You can visit my &lt;a href="https://www.youtube.com/channel/UCl31HHUdQbSHOQfc9L-wo3w?view_as=subscriber?sub_confirmation=1"&gt;YouTube channel 'coderolls'&lt;/a&gt; to find more video tutorials.&lt;/p&gt;

&lt;h4&gt;
  
  
  Related Articles
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://coderolls.com/reverse-a-string-in-java/"&gt;How To Reverse A String In Java (5 ways)&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://coderolls.com/basic-git-commands/"&gt;8 Basic GIT Commands Every Newbie Developer Must Know&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>How do you name your GIT branch?</title>
      <dc:creator>Gaurav Kukade</dc:creator>
      <pubDate>Mon, 13 Jan 2020 11:44:48 +0000</pubDate>
      <link>https://dev.to/gauravkukade/how-do-you-name-your-git-branch-4bcj</link>
      <guid>https://dev.to/gauravkukade/how-do-you-name-your-git-branch-4bcj</guid>
      <description>&lt;p&gt;Hi developers,&lt;/p&gt;

&lt;p&gt;I will like to ask you a simple question.&lt;/p&gt;

&lt;p&gt;How do you name your GIT branch?&lt;/p&gt;

&lt;p&gt;A. b/your_new_branch_name&lt;br&gt;
B. your_new_branch_name&lt;/p&gt;

&lt;p&gt;I am using the first way at my organization. Why I am doing this? 🤔 Because I have observed all are doing the same. 😐&lt;/p&gt;

&lt;p&gt;My colleague told me that we are using 'b/' to identify it as a branch easily.&lt;/p&gt;

&lt;p&gt;What do you think about it?&lt;/p&gt;

&lt;p&gt;Please write down your opinion on the branch names in the comment section below.&lt;/p&gt;

&lt;p&gt;Thanks!&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>8 Basic GIT Commands Every Newbie Developer Must Know</title>
      <dc:creator>Gaurav Kukade</dc:creator>
      <pubDate>Mon, 30 Dec 2019 07:39:28 +0000</pubDate>
      <link>https://dev.to/gauravkukade/8-basic-git-commands-every-newbie-developer-must-know-1jli</link>
      <guid>https://dev.to/gauravkukade/8-basic-git-commands-every-newbie-developer-must-know-1jli</guid>
      <description>&lt;p&gt;GIT is one of the most important parts of the developer's day to day work. So learning GIT is a must for a newbie developer. In this article, you are going to learn the 8 most important basic GIT commands.&lt;/p&gt;

&lt;p&gt;Below, I have listed down all the 8 commands and then, we will have a look at them one by one.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;git init&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;git clone&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;git add&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;git commit&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;git status&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;git branch&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;git pull&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;git push&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  1. &lt;code&gt;git init&lt;/code&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;git init&lt;/code&gt; command initializes brand new GIT repository (locally) and begins tracking the existing directory.&lt;/p&gt;

&lt;p&gt;When you hit the &lt;code&gt;git init&lt;/code&gt; command, git adds a subfolder withing an existing directory that manages all required files for version controlling.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HP@Gaurav MINGW64 /e/example
$ git init
Initialized empty Git repository in E:/example/.git/

HP@Gaurav MINGW64 /e/example (master)
$
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You are hitting &lt;code&gt;git init&lt;/code&gt; means you want to initialize the current directory as GIT repository.&lt;/p&gt;

&lt;p&gt;The following GIF files show initializing a new repository and a hidden sub-folder containing all data structure required for version control. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CHDLmN13--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1414/1%2A6HIO5V4MpkgsQ4xGllGk-Q.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CHDLmN13--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1414/1%2A6HIO5V4MpkgsQ4xGllGk-Q.gif" alt="Initializing a git repository locally"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. &lt;code&gt;git clone&lt;/code&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;git clone&lt;/code&gt; creates a local copy of a repository that already exists remotely. The local copy is the exact copy of the remote repository, it contains the same files, history, and branches.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git clone &amp;lt;remote-repository-link&amp;gt;
&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;HP@Gaurav MINGW64 /e/directory
$ git clone https://github.com/gauravkukade/example.git
Cloning into 'example'...
remote: Enumerating objects: 16, done.
remote: Counting objects: 100% (16/16), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 16 (delta 3), reused 7 (delta 2), pack-reused 0
Unpacking objects: 100% (16/16), done.

HP@Gaurav MINGW64 /e/directory
$
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can clone any public repository from the platforms like GitHub,  BitBucket, GitLab, and other GIT hosting platforms.&lt;/p&gt;

&lt;p&gt;The following GIF shows the &lt;code&gt;git clone&lt;/code&gt; command&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ycIrWiDp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1445/1%2ATzRS2D9M523xG5opciHjrQ.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ycIrWiDp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1445/1%2ATzRS2D9M523xG5opciHjrQ.gif" alt="Cloning the remote repository"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. &lt;code&gt;git add&lt;/code&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;git add&lt;/code&gt; stages a change.&lt;/p&gt;

&lt;p&gt;If you are done with changes in your code then its necessary to stage that changes and take a snapshot of it to include it in the repository's history.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git add&lt;/code&gt; does the first step, it stages a change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git add &amp;lt;path-of-the-file-you-made-changes-in&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you made changes in the multiple files and you want to stage all files in the same command then add the file path of all files separated y a single space.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git add &amp;lt;first-filepath-you-want-to-stage&amp;gt; &amp;lt;second-filepath-you-want-to-stage&amp;gt; &amp;lt;nth-filepath-you-want-to-stage&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to stage all the files, then write '.' (dot) after &lt;code&gt;git add&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;$ git add .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Any staged changes will become the part of the next snapshot and a part of the repository's history.&lt;/p&gt;

&lt;p&gt;You can stage and take a snapshot of the current changes in a single command also but is not recommended.&lt;/p&gt;

&lt;p&gt;Staging your changes first and then taking snapshot gives you complete control over the repository's history. &lt;/p&gt;

&lt;p&gt;The following GIF shows the &lt;code&gt;git add&lt;/code&gt; command&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xqdwC8Hy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1566/1%2AdZWhxnpnPx96TUCIHYAg5w.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xqdwC8Hy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1566/1%2AdZWhxnpnPx96TUCIHYAg5w.gif" alt="Staging the changes"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. &lt;code&gt;git commit&lt;/code&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;git commit&lt;/code&gt; save the snapshot to the repository's history.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git add&lt;/code&gt; does the first step i.e. staging the changes and &lt;code&gt;git commit&lt;/code&gt; does the final step i.e. it saves the snapshot to the repository's history. In GIT these two-step completes the changes tracking process.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git commit -m "&amp;lt;meaningful-git-commit-message&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can write a meaningful message to the commit. It is recommended to write a commit message in the 'Simple Present Tense'.&lt;/p&gt;

&lt;p&gt;If you are committing a new feature to your project then your commit message should be &lt;code&gt;"Add &amp;lt;feature-name&amp;gt; feature"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The following GIF shows the &lt;code&gt;git commit&lt;/code&gt; command&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JPN3X8PV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1345/1%2A5MrNANFag1YZbA6XOmgVhQ.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JPN3X8PV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1345/1%2A5MrNANFag1YZbA6XOmgVhQ.gif" alt="Committing the staged changes"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is the simple way to write the commit message but there is a more in-depth way to write a commit message with title and description. We will see that in a separate blogpost.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. &lt;code&gt;git status&lt;/code&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;git status&lt;/code&gt; shows the status of the changes as untracked, modified or staged.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HP@Gaurav MINGW64 /e/directory/example (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD &amp;lt;file&amp;gt;..." to unstage)

        modified:   README.md

Untracked files:
  (use "git add &amp;lt;file&amp;gt;..." to include in what will be committed)

        ex.txt


HP@Gaurav MINGW64 /e/directory/example (master)
$

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

&lt;/div&gt;



&lt;p&gt;The following GIF shows the &lt;code&gt;git status&lt;/code&gt; command&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GbSZYAGD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1345/1%2A2ZF0bd-wuIVG-3WSXwmByQ.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GbSZYAGD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1345/1%2A2ZF0bd-wuIVG-3WSXwmByQ.gif" alt="Showing status of the current change process"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  6. &lt;code&gt;git branch&lt;/code&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;git branch&lt;/code&gt; list the existing branches from the local repository. The current branch will be highlighted in green and marked with an asterisk.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HP@Gaurav MINGW64 /e/directory/example (master)
$ git branch
* master
  new_branch

HP@Gaurav MINGW64 /e/directory/example (master)
$
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The followig gif shows the &lt;code&gt;git branch&lt;/code&gt; command&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8gDRzJ3J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1345/1%2As1AoKmagaDhcazUktiuUWQ.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8gDRzJ3J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1345/1%2As1AoKmagaDhcazUktiuUWQ.gif" alt="GIF showing git branch command"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  7. &lt;code&gt;git pull&lt;/code&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;git pull&lt;/code&gt; update the local repository with updates from its remote counterpart. i.e. remote repository.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HP@Gaurav MINGW64 /e/directory/example (master)
$ git pull
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/gauravkukade/example
   15e8755..d7aefb1  master     -&amp;gt; origin/master
Updating 15e8755..d7aefb1
Fast-forward
 new-file-added-to-remote-repository.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 new-file-added-to-remote-repository.txt

HP@Gaurav MINGW64 /e/directory/example (master)
$

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

&lt;/div&gt;



&lt;p&gt;If your teammate made commits to the remote branch and you want to reflect these changes in your local environment then you will hit the command &lt;code&gt;git pull&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This command will check if there are any updates on the remote branch as compared to your local environment, if yes, then it will update your local environment with these changes. If no, then it will do nothing.&lt;/p&gt;

&lt;p&gt;The following GIF shows the &lt;code&gt;git pull&lt;/code&gt; command&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2fQBk-1a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1406/1%2AuxGQYgKj6fVX8_aOYm7itw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2fQBk-1a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1406/1%2AuxGQYgKj6fVX8_aOYm7itw.gif" alt="Pulling the changes committed by others on remote repository&amp;lt;br&amp;gt;
"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  8. &lt;code&gt;git push&lt;/code&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;git push&lt;/code&gt; updates the remote repository with any commits made locally to a brach&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git push origin &amp;lt;branch-name-you-have made commits on&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the branch does not exist on a remote repository then the whole branch with its commits will be pushed to the remote repository.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git push origin &amp;lt;newly-locally-created-branch-name&amp;gt;
&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;HP@Gaurav MINGW64 /e/directory/example (master)
$ git push origin master
fatal: HttpRequestException encountered.
   An error occurred while sending the request.
Username for 'https://github.com': gauravkukade
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 255 bytes | 255.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/gauravkukade/example.git
   d7aefb1..dc3d3ef  master -&amp;gt; master

HP@Gaurav MINGW64 /e/directory/example (master)
$
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following GIF shows the &lt;code&gt;git push&lt;/code&gt; command&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TdsGxx5_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1406/1%2AiTrmzsNdQdFigdVfrTH0Rg.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TdsGxx5_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1406/1%2AiTrmzsNdQdFigdVfrTH0Rg.gif" alt="Pushing locally committed changes to the remote repository"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You are here means you like the post and hence here is your bonus content.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to create a new branch locally
&lt;/h3&gt;

&lt;p&gt;You can create a new branch locally using the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git checkout -b &amp;lt;your-new-branch-name&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6IEjTXIV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1345/1%2A7Hr7-jga164qb5xWB_UuMA.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6IEjTXIV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1345/1%2A7Hr7-jga164qb5xWB_UuMA.gif" alt="Creating a new branch locally"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you found this article worth, please &lt;a href="https://www.paypal.me/GauravKukade"&gt;Give me a cup of Coffee ☕&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have queries please let me know in the comment section below.&lt;/p&gt;




&lt;p&gt;This article originally published at &lt;a href="https://coderolls.com/basic-git-commads/"&gt;https://coderolls.com/basic-git-commads/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hi 👋🏼, I am Gaurav Kukade, a Java Developer and author at &lt;a href="https://codeRolls.com"&gt;https://codeRolls.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>git</category>
      <category>github</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>How Do I Compare Strings In Java</title>
      <dc:creator>Gaurav Kukade</dc:creator>
      <pubDate>Fri, 20 Dec 2019 12:16:37 +0000</pubDate>
      <link>https://dev.to/gauravkukade/how-do-i-compare-strings-in-java-53m6</link>
      <guid>https://dev.to/gauravkukade/how-do-i-compare-strings-in-java-53m6</guid>
      <description>&lt;p&gt;&lt;em&gt;This article is originally published at &lt;a href="https://coderolls.com/compare-strings-in-java/" rel="noopener noreferrer"&gt;https://coderolls.com/compare-strings-in-java/&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In this article you are going to learn how to compare strings. What problem occurs when you compare string using &lt;code&gt;equals to&lt;/code&gt; (&lt;code&gt;=&lt;/code&gt;)operator.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The  &lt;code&gt;String&lt;/code&gt;  is a special Class in Java. We use String regularly in Java programs, so comparing two string is a common practice in Java. In this article I tried to answer the most common questions about the string, ‘How do I compare strings in Java?’&lt;/p&gt;

&lt;p&gt;Comparing strings is very helpful the processes like authentication, sorting, reference matching, etc.&lt;/p&gt;

&lt;p&gt;I have listed 3 ways to compare strings in Java.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Using  &lt;code&gt;equals()&lt;/code&gt;  method ( comparing the content)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using  &lt;code&gt;==&lt;/code&gt;  operator (comparing the object reference)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using  &lt;code&gt;compareTo()&lt;/code&gt;  method (comparing strings lexicographically)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  1. Compare strings using  &lt;code&gt;equals()&lt;/code&gt;  method
&lt;/h2&gt;

&lt;p&gt;In this way, I am using  &lt;code&gt;.equals()&lt;/code&gt;  instance method of the String class. Originally  &lt;code&gt;.equals()&lt;/code&gt;  method is the  &lt;code&gt;Object&lt;/code&gt;  class method, String class overrides it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;**equals()**&lt;/code&gt; &lt;strong&gt;method the compare two strings for value equality, weather they are logically equal.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;equals()&lt;/code&gt;  method in String class takes another string a parameter and compare it with the specified string, it returns  &lt;code&gt;true&lt;/code&gt;  if and only if the parameter string is not null and contains the same characters as the specified string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public boolean equals(Object anObject)

It compare this string with the argument strings and return true if the argument is not null and contains the same character as the specified string.

param -
another string

returns -

true - if argument is not null and it contains same characters as the specified string
false - if the argument is null or it does not contain same characters as the specified string

ex. firstString.equals(secondString)
// returns true if and only if the secondString is not null and contains the same characters as firstString.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have given the program to compare string using  &lt;code&gt;equals()&lt;/code&gt;  method below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * A Java program to compare two strings using equsls()
 * and equalsIgnoreCase() method of the String.
 * 
 * @author Gaurav Kukade at coderolls.com
 */

public class CompareUsingEquals {

  public static void main(String[] args) {
    String firstString = "coderolls";
    String secondString = "javablog";
    String thirdString = "coderolls";
    String fourthString = "CodeRolls";

    System.out.println("Comparing strings using equals() and equalsIgnoreCase() method\n");

    // Using equals() method
    System.out.print("firstString.equals(secondString) : ");
    System.out.println(firstString.equals(secondString));

    System.out.print("firstString.equals(thirdString) : ");
    System.out.println(firstString.equals(thirdString));

    /*
     * Using equalsIgnoreCase() method to ignore
     * case consideration (i.e. Capital or small) of both the strings.
     */
    System.out.print("firstString.equalsIgnoreCase(fourthString) : ");
    System.out.println(firstString.equalsIgnoreCase(fourthString));
  }

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

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Comparing strings using equals() and equalsIgnoreCase() method

firstString.equals(secondString) : false
firstString.equals(thirdString) : true
firstString.equalsIgnoreCase(fourthString) : true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Compare strings using  &lt;code&gt;==&lt;/code&gt;  operator
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;In String,&lt;/strong&gt; &lt;code&gt;**==**&lt;/code&gt; &lt;strong&gt;operator is used to comparing the reference of the given strings, whether they are referring to the same objects.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you compare two strings using  &lt;code&gt;==&lt;/code&gt;  operator, it will return  &lt;code&gt;true&lt;/code&gt;  if the string variables are pointing toward the same java object, else it will return  &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I have given a Java program to compare using  &lt;code&gt;==&lt;/code&gt;  operator below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * A Java program to compare strings using == operator.
 * 
 * == operator ckecks weather both the strings referring
 * to the same String Object.
 * 
 * @author Gaurav Kukade at coderolls.com
 */

public class CompareUsingEqualsToOperator {

  public static void main(String[] args) {

    String firstString = "coderolls";
    String secondString = "javablog";
    String thirdString = "coderolls";

    // creating new String object with the same value as firstString or thirdString
    String fourthString =  new String("coderolls");

    System.out.println("Comparing strings using == operator \n");

    System.out.print("firstString == secondString : ");
    System.out.println(firstString == secondString);

    /*
     * Here firstString and thirdString is referring to the same String object
     * hence it will print 'true'.
     */
    System.out.print("firstString == thirdString : ");
    System.out.println(firstString == thirdString);

    /*
     * Here firstString and fourthString have same value
     * but they are referring to the different String object.
     * 
     * hence it will print 'false'
     */
    System.out.print("firstString == fourthString : ");
    System.out.println(firstString == fourthString);
  }

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

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Comparing strings using == operator 

firstString == secondString : false
firstString == thirdString : true
firstString == fourthString : false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Problem with using  &lt;code&gt;==&lt;/code&gt;  operator for string comparison
&lt;/h2&gt;

&lt;p&gt;Most of the beginner Java developers commit this mistake by comparing two strings using the == operator.&lt;/p&gt;

&lt;p&gt;Logically, they have to check whether both the string contains the same character sequence or not.&lt;/p&gt;

&lt;p&gt;In Java String, the  &lt;code&gt;==&lt;/code&gt;  operator used to check the reference of both the string objects and  &lt;code&gt;equals()&lt;/code&gt;  method used to check the value equality of both strings.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;==&lt;/code&gt;  – checks reference equality&lt;/p&gt;

&lt;p&gt;&lt;code&gt;equals()&lt;/code&gt;  – checks the value equality&lt;/p&gt;

&lt;p&gt;When we assign a string value to the string variable JVM will check if the string with the equal value already present in the string pool or not. If it is not present in the string pool, it will be added to the constant pool and the reference to that string object is returned.&lt;/p&gt;

&lt;p&gt;If it is present in the string pool, the reference to the memory address of that string object is returned.&lt;/p&gt;

&lt;p&gt;The following image shows the pictorial explanation of the same.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderolls.com%2Fassets%2Fimages%2F2019-12-10%2FfirstString-pointing-to-coderolls-in-string-pool.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderolls.com%2Fassets%2Fimages%2F2019-12-10%2FfirstString-pointing-to-coderolls-in-string-pool.png" alt="'firstString' pointing towards the "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;‘firstString’ pointing towards the “coderolls” string in string pool&lt;/p&gt;

&lt;p&gt;If we are assigning the equal value to another string variable, JVM checks if the string with that value is present in the string constant pool or not.&lt;/p&gt;

&lt;p&gt;Since the string object with that value is already created in the previous step. Another string variable starts referring to the previously created string object instance.&lt;/p&gt;

&lt;p&gt;The following image shows the pictorial explanation for the same&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderolls.com%2Fassets%2Fimages%2F2019-12-10%2FsecondString-pointing-to-coderolls-in-string-pool.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderolls.com%2Fassets%2Fimages%2F2019-12-10%2FsecondString-pointing-to-coderolls-in-string-pool.png" alt="'firstString' and 'secondString' pointing towards the "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;‘firstString’ and ‘secondString’ pointing towards the “coderolls” string in string pool&lt;/p&gt;

&lt;p&gt;When we create string using the  &lt;code&gt;new&lt;/code&gt;  operator, a new string object is created and stored in the Java heap space.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderolls.com%2Fassets%2Fimages%2F2019-12-10%2FthirdString-pointing-to-coderolls-in-heap.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderolls.com%2Fassets%2Fimages%2F2019-12-10%2FthirdString-pointing-to-coderolls-in-heap.png" alt="'firstString' and 'secondString' pointing towards the "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;‘firstString’ and ‘secondString’ pointing towards the “coderolls” string in string pool and ‘thirdString’ pointing towards the “coderolls” in java heap space.t&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Compare strings using  &lt;code&gt;compareTo()&lt;/code&gt;  method
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;compareTo()&lt;/code&gt;  method is used to compare two strings lexicographically. i.e. Alphabetically.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;compareTo()&lt;/code&gt;  method compares the character sequence of the argument string with the character sequence of the specified string.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderolls.com%2Fassets%2Fimages%2F2019-12-10%2Fshowing-argument-string-and-specified-string.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcoderolls.com%2Fassets%2Fimages%2F2019-12-10%2Fshowing-argument-string-and-specified-string.png" alt="Showing argument string and specified string"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Showing argument string and a specified string.&lt;/p&gt;

&lt;p&gt;It returns a negative integer if the argument string is lexicographically greater than the specified string. i.e if the argument string follows the specified string. ( argument String &amp;gt; specified String )&lt;/p&gt;

&lt;p&gt;It returns positive integer if the argument string is lexicographically smaller than the specified string. i.e. If the argument string precedes the specified string. ( argument String &amp;lt; specified String )&lt;/p&gt;

&lt;p&gt;It returns zero if both the strings are lexicographical equals. ( argument String = specified String )&lt;/p&gt;

&lt;p&gt;If you want to ignore the cases of both the string use  &lt;code&gt;compareToIgnoreCase()&lt;/code&gt;  method.&lt;/p&gt;

&lt;p&gt;I have given a program for comparing strings using the  &lt;code&gt;compareTo()&lt;/code&gt;  method. It also consists a case for ignoring the cases with  &lt;code&gt;compareToIgnoreCase()&lt;/code&gt;  method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; * A Java program to compare strings using compareTo()
 * and compareToIgnoreCase() method.
 * 
 * compareTo() compare strings lexicograpgically.
 * 
 * @author Gaurav Kukade at coderolls.com
 */

public class CompareUsingCompareTo {

  public static void main(String[] args) {

    String firstString = "java";
    String secondString = "coderolls";
    String thirdString = "sql";
    String fourthString = "CodeRolls";

    System.out.println("Comparing strings using compareTo() and compareToIgnoreCase() method\n");

    // Using compareTo() method
    System.out.print("firstString.compareTo(secondString) : ");
    System.out.println(firstString.compareTo(secondString));

    System.out.print("firstString.compareTo(thirdString) : ");
    System.out.println(firstString.compareTo(thirdString));

    /*
     * Using compareToIgnoreCase() method to ignore
     * case consideration (i.e. Capital or small) of both the strings.
     */
    System.out.print("secondString.compareToIgnoreCase(fourthString) : ");
    System.out.println(secondString.compareToIgnoreCase(fourthString));
  }

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

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Comparing strings using compareTo() and compareToIgnoreCase() method

firstString.compareTo(secondString) : 7
firstString.compareTo(thirdString) : -9
secondString.compareToIgnoreCase(fourthString) : 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have written a detailed article on  &lt;a href="https://coderolls.com/compare-two-strings-lexicographically-in-java/" rel="noopener noreferrer"&gt;how to compare strings lexicographically in java&lt;/a&gt;. In this article, I have also created a user-defined method to compare two strings lexicographically.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We can compare strings using the ways given below&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Using  &lt;code&gt;equals()&lt;/code&gt;  method :  &lt;code&gt;equals()&lt;/code&gt;  method in the strings used to check the string value equality whether they contain the same character sequence.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using  &lt;code&gt;==&lt;/code&gt;  operator :  &lt;code&gt;==&lt;/code&gt;  operator used to check the reference equality of the two strings, whether they are pointing towards the same string object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using  &lt;code&gt;compareTo()&lt;/code&gt;  method : &lt;code&gt;compareTo()&lt;/code&gt;  method used to check the strings lexicographically. I.e alphabetically. Check the detailed articles on  &lt;a href="https://coderolls.com/compare-two-strings-lexicographically-in-java/" rel="noopener noreferrer"&gt;How to compare strings lexicographically&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Most of the beginner java developers do mistakes while comparing strings. They actually want to check the content of the string but they use  &lt;code&gt;==&lt;/code&gt;  operator to check it.&lt;/p&gt;

&lt;p&gt;It is always advised to use &lt;code&gt;equals()&lt;/code&gt; method to compare the string on the basis of its content.&lt;/p&gt;

&lt;p&gt;If you have any queries about the code blocks given above, please write it down in the comment section below. Also. let me know if you have any other way to compare two strings in java in the comment section.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article is originally published at &lt;a href="https://coderolls.com/compare-strings-in-java/" rel="noopener noreferrer"&gt;https://coderolls.com/compare-strings-in-java/&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Related Article
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://coderolls.com/reverse-a-string-in-java/" rel="noopener noreferrer"&gt;How To Reverse A String In Java (5 ways)&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://coderolls.com/compare-two-strings-lexicographically-in-java/" rel="noopener noreferrer"&gt;How To Compare Two Strings Lexicographically In Java&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://coderolls.com/java-introduction/" rel="noopener noreferrer"&gt;Introduction to Java Technology (Language and Platform)&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>string</category>
      <category>corejava</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
