<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>java-tutorials | Automation Dojos</title>
	<atom:link href="https://www.automationdojos.com/tag/java-tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.automationdojos.com/tag/java-tutorials/</link>
	<description>Applied Automation Ops</description>
	<lastBuildDate>Wed, 05 Jan 2022 21:49:41 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://www.automationdojos.com/wp-content/uploads/2021/06/Adjo_X_fav_32.png</url>
	<title>java-tutorials | Automation Dojos</title>
	<link>https://www.automationdojos.com/tag/java-tutorials/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Java Tutorial #7 Part 1 – Classes &#038; Objects</title>
		<link>https://www.automationdojos.com/java-classes-and-objects-part1/</link>
					<comments>https://www.automationdojos.com/java-classes-and-objects-part1/#respond</comments>
		
		<dc:creator><![CDATA[Tushar Sharma]]></dc:creator>
		<pubDate>Tue, 11 Jun 2019 14:51:00 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Tutorials]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[core-java]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java-class]]></category>
		<category><![CDATA[java-method]]></category>
		<category><![CDATA[java-object]]></category>
		<category><![CDATA[java-tutorials]]></category>
		<guid isPermaLink="false">https://www.automationdojos.com/?p=22488</guid>

					<description><![CDATA[<p><a href="https://www.automationdojos.com/java-classes-and-objects-part1/">Java Tutorial #7 Part 1 – Classes &#038; Objects</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="575" height="345" src="https://www.automationdojos.com/wp-content/uploads/2019/06/post-java-tutorial-7-1.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" fetchpriority="high" srcset="https://www.automationdojos.com/wp-content/uploads/2019/06/post-java-tutorial-7-1.png 575w, https://www.automationdojos.com/wp-content/uploads/2019/06/post-java-tutorial-7-1-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2019/06/post-java-tutorial-7-1-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2019/06/post-java-tutorial-7-1-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2019/06/post-java-tutorial-7-1-400x240.png 400w" sizes="(max-width: 575px) 100vw, 575px" /></p>
<p>Classes are a very important concept in Java. Java code always needs to be written in a class. In this article, we will be taking a very close look at classes and understanding their syntax and semantics. Classes and objects A class defines a new data type. Once defined, this new type can be used [&#8230;]</p>
<p>The post <a href="https://www.automationdojos.com/java-classes-and-objects-part1/">Java Tutorial #7 Part 1 – Classes &#038; Objects</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://www.automationdojos.com/java-classes-and-objects-part1/">Java Tutorial #7 Part 1 – Classes &#038; Objects</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="575" height="345" src="https://www.automationdojos.com/wp-content/uploads/2019/06/post-java-tutorial-7-1.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" srcset="https://www.automationdojos.com/wp-content/uploads/2019/06/post-java-tutorial-7-1.png 575w, https://www.automationdojos.com/wp-content/uploads/2019/06/post-java-tutorial-7-1-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2019/06/post-java-tutorial-7-1-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2019/06/post-java-tutorial-7-1-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2019/06/post-java-tutorial-7-1-400x240.png 400w" sizes="(max-width: 575px) 100vw, 575px" /></p><h2 class="simpletoc-title">Table of Contents</h2>
<ul class="simpletoc-list">
<li><a href="#classes-and-objects">Classes and objects</a>


<ul><li>
<a href="#class-syntax">Class Syntax</a>

</li>
<li><a href="#defining-a-class">Defining a class</a>

</li>
<li><a href="#creating-an-object">Creating an object</a>

</li>
</ul>
<li><a href="#instance-fields">Instance Fields</a>


<ul><li>
<a href="#defining-instance-fields">Defining instance Fields</a>

</li>
<li><a href="#accessing-instance-fields">Accessing instance Fields</a>

</li>
</ul>
<li><a href="#methods">Methods</a>


<ul><li>
<a href="#defining-a-method">Defining a method</a>

</li>
<li><a href="#invoking-a-method">Invoking a method</a>

</li>
</ul>
<li><a href="#conclusion">Conclusion</a>
</li></ul>


<p>Classes are a very important concept in Java. Java code always needs to be written in a class. In this article, we will be taking a very close look at classes and understanding their syntax and semantics.</p>


<h2 class="wp-block-heading" id="classes-and-objects">Classes and objects</h2>


<p>A class defines a new data type. Once defined, this new type can be used to create objects of that type. So, a class acts like a template and provides the structure for an object.</p>


<h3 class="wp-block-heading" id="class-syntax">Class Syntax</h3>


<p>A class generally contains data and the code that operates on the data. The following is the general syntax of a class:</p>



<div style="height:5px" aria-hidden="true" class="wp-block-spacer"></div>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class classname{

	type var1;
	type var2;
	type varn;

type method1(parameters){
}

type method2(parameters){
}

type methodn(parameters){
}

}</pre>



<p>The <strong>class</strong> keyword is followed by the name to be given to the class. The data in the class is present in the form of variables. These are known as <strong>instance fields</strong>. The code in the class is present in the form of <strong>methods</strong>. A class can contain any number of instance fields and methods. Also, both instance fields and methods are optional, a class can contain either instance fields or methods or both. Instance fields and methods are collectively referred to as members of a class.</p>


<div class="su-note"  style="border-color:#d1cec9;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#ebe8e3;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">
<h3 class="wp-block-heading" id="defining-a-class">Defining a class</h3>
<p>A class can be defined by using the <strong>&#8216;class&#8217;</strong> keyword followed by the name to be given to the class. Let us first create a very basic class that does not have instance fields and methods:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">class StudentDemo {

}</pre>
<p>This code defines a class called <strong>StudentDemo</strong> and this <strong>StudentDemo</strong> acts as a template using which Student objects can be created.</p>
</div></div>



<div style="height:10px" aria-hidden="true" class="wp-block-spacer"></div>


<div class="su-note"  style="border-color:#d1cec9;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#ebe8e3;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">
<h3 class="wp-block-heading" id="creating-an-object">Creating an object</h3>
<p>A class can be used to create objects of that type. Multiple objects can be created from the same class. All objects of a class conform to the structure provided by the class. The <strong>new</strong> keyword can be used to create ab object of a class.</p>
<p>The following code demonstrates creating an object of type <strong>Student</strong>:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">public class StudentDemo {

	public static void main(String[] args) {
		
		Student student1 = new Student();
	}

}</pre>
<p>This code defines a class called <strong>StudentDemo</strong>. The <strong>main</strong> method creates a <strong>Student</strong> object called <strong>student1</strong>. The <strong>new</strong> keyword is followed by the class name and parentheses. This is known as a constructor of the class. Constructors will be covered in the next part of this article.</p>
</div></div>



<div style="height:5px" aria-hidden="true" class="wp-block-spacer"></div>




<div class="rt-block">

<h2 class="wp-block-heading" id="instance-fields">Instance Fields</h2>


<p>A class can also contain data in the form of instance fields. Instance fields add state to a class. Each object of a class has its own copy of the instance fields.</p>


<div class="su-spacer" style="height:15px"></div>

<h3 class="wp-block-heading" id="defining-instance-fields">Defining instance Fields</h3>


<p>Instance fields can be defined by specifying their data type followed by a variable name. Let us add some instance fields to the Student class:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">public class Student {
	
	String name;
	double marks;
	
}
</pre>



<p>This code specifies two instance fields in the <strong>Student </strong>class. <strong>name</strong> is of type <strong>String</strong> and <strong>marks</strong> is of type <strong>double</strong>. When a <strong>Student</strong> object is created, Java allocates memory to the <strong>name</strong> and <strong>marks</strong> field.</p>


<div class="su-spacer" style="height:20px"></div>

<h3 class="wp-block-heading" id="accessing-instance-fields">Accessing instance Fields</h3>


<p>Java provides the <strong>dot operator (.)</strong> to access instance fields. The object name should be followed by the dot operator and followed by the field name. The following code demonstrates assigning some values to the name and marks fields in the Student class:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Student student1 = new Student();
		
student1.name = "John Doe";
student1.marks = 93;
</pre>

<div class="su-spacer" style="height:5px"></div>

<p>This code creates a <strong>Student</strong> object called <strong>student1</strong>. It assigns the value <strong>John Doe</strong> to the <strong>name</strong> field of <strong>student1</strong> and the value <strong>93</strong> to the <strong>marks</strong> field of <strong>student1</strong>.</p>



<p>You can create another <strong>Student</strong> object as follows:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Student student2 = new Student();
		
student2.name = "Lucia Kane";
student2.marks = 78.5;
</pre>



<p>This code creates another <strong>Student</strong> object called <strong>student2</strong> with different values for <strong>name</strong> and <strong>marks</strong>.</p>


<div class="su-spacer" style="height:5px"></div>
</div>


<h2 class="wp-block-heading" id="methods">Methods</h2>


<p>As mentioned earlier, a class can contain code in the form of methods. Methods generally operate on the data in the class.</p>


<div class="su-note"  style="border-color:#dcdcdc;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#f6f6f6;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">
<h3 class="wp-block-heading" id="defining-a-method">Defining a method</h3>
<p>A method can be defined by specifying the name of the method followed by the code to be placed in the body of the method. Let us add a method to the <strong>Student</strong> class:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">public class Student {
	
	String name;
	double marks;
	
	void printDetails() {
		System.out.println("Student "+name+" has "+marks+" marks");
	}
	
}
</pre>
<p>This code specifies a method called <strong>printDetails</strong> on the <strong>Student</strong> class. It simply prints the values for the <strong>name</strong> and <strong>marks</strong> field.</p>
</div></div>



<div style="height:10px" aria-hidden="true" class="wp-block-spacer"></div>


<div class="su-note"  style="border-color:#dcdcdc;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#f6f6f6;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">
<h3 class="wp-block-heading" id="invoking-a-method">Invoking a method</h3>
<p>Just like instance fields, a method can also be accessed via the dot operator. The following code demonstrates invoking the <strong>printDetails</strong> method:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Student student1 = new Student();
		
student1.name = "John Doe";
student1.marks = 93;
		
student1.printDetails();
</pre>
<p>This code creates a <strong>Student</strong> object called <strong>student1</strong> and assigns some values to the <strong>name</strong> and <strong>marks</strong> field. It then invokes the <strong>printDetails</strong> method. So, this causes the code in the <strong>printDetails</strong> method to be executed. So, when you run the code snippet above, it will print the following to the console:</p>
<p>Student John Doe has 93.0 marks</p>
</div></div>


<h2 class="wp-block-heading" id="conclusion">Conclusion</h2>


<p>So, just to summarise, a class acts like a template that can be used to create objects of the class. A class can contain instance fields and methods. In the next part of this article, we will be taking a closer look at methods and also exploring constructors.</p>
<div class='the_champ_sharing_container the_champ_vertical_sharing the_champ_hide_sharing the_champ_bottom_sharing' style='width:42px;left: -10px;top: 222px;-webkit-box-shadow:none;box-shadow:none;' data-super-socializer-href="https://www.automationdojos.com/tag/java-tutorials/feed/"><div class="the_champ_sharing_ul"><a aria-label="Facebook" class="the_champ_facebook" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D22488" title="Facebook" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#0765FE;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path fill="#484848" d="M28 16c0-6.627-5.373-12-12-12S4 9.373 4 16c0 5.628 3.875 10.35 9.101 11.647v-7.98h-2.474V16H13.1v-1.58c0-4.085 1.849-5.978 5.859-5.978.76 0 2.072.15 2.608.298v3.325c-.283-.03-.775-.045-1.386-.045-1.967 0-2.728.745-2.728 2.683V16h3.92l-.673 3.667h-3.247v8.245C23.395 27.195 28 22.135 28 16Z"></path></svg></span></a><a aria-label="Twitter" class="the_champ_button_twitter" href="https://twitter.com/intent/tweet?via=@testoxide&text=java-tutorials&url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D22488" title="Twitter" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_twitter" style="background-color:#55acee;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-4 -4 39 39"><path d="M28 8.557a9.913 9.913 0 0 1-2.828.775 4.93 4.93 0 0 0 2.166-2.725 9.738 9.738 0 0 1-3.13 1.194 4.92 4.92 0 0 0-3.593-1.55 4.924 4.924 0 0 0-4.794 6.049c-4.09-.21-7.72-2.17-10.15-5.15a4.942 4.942 0 0 0-.665 2.477c0 1.71.87 3.214 2.19 4.1a4.968 4.968 0 0 1-2.23-.616v.06c0 2.39 1.7 4.38 3.952 4.83-.414.115-.85.174-1.297.174-.318 0-.626-.03-.928-.086a4.935 4.935 0 0 0 4.6 3.42 9.893 9.893 0 0 1-6.114 2.107c-.398 0-.79-.023-1.175-.068a13.953 13.953 0 0 0 7.55 2.213c9.056 0 14.01-7.507 14.01-14.013 0-.213-.005-.426-.015-.637.96-.695 1.795-1.56 2.455-2.55z" fill="#484848"></path></svg></span></a><a aria-label="Linkedin" class="the_champ_button_linkedin" href="https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D22488" title="Linkedin" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_linkedin" style="background-color:#0077b5;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path d="M6.227 12.61h4.19v13.48h-4.19V12.61zm2.095-6.7a2.43 2.43 0 0 1 0 4.86c-1.344 0-2.428-1.09-2.428-2.43s1.084-2.43 2.428-2.43m4.72 6.7h4.02v1.84h.058c.56-1.058 1.927-2.176 3.965-2.176 4.238 0 5.02 2.792 5.02 6.42v7.395h-4.183v-6.56c0-1.564-.03-3.574-2.178-3.574-2.18 0-2.514 1.7-2.514 3.46v6.668h-4.187V12.61z" fill="#484848"></path></svg></span></a><a aria-label="Pinterest" class="the_champ_button_pinterest" href="https://www.automationdojos.com/tag/java-tutorials/feed/" onclick="event.preventDefault();javascript:void((function(){var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','//assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());" title="Pinterest" rel="noopener" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_pinterest" style="background-color:#cc2329;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-2 -2 35 35"><path fill="#484848" d="M16.539 4.5c-6.277 0-9.442 4.5-9.442 8.253 0 2.272.86 4.293 2.705 5.046.303.125.574.005.662-.33.061-.231.205-.816.27-1.06.088-.331.053-.447-.191-.736-.532-.627-.873-1.439-.873-2.591 0-3.338 2.498-6.327 6.505-6.327 3.548 0 5.497 2.168 5.497 5.062 0 3.81-1.686 7.025-4.188 7.025-1.382 0-2.416-1.142-2.085-2.545.397-1.674 1.166-3.48 1.166-4.689 0-1.081-.581-1.983-1.782-1.983-1.413 0-2.548 1.462-2.548 3.419 0 1.247.421 2.091.421 2.091l-1.699 7.199c-.505 2.137-.076 4.755-.039 5.019.021.158.223.196.314.077.13-.17 1.813-2.247 2.384-4.324.162-.587.929-3.631.929-3.631.46.876 1.801 1.646 3.227 1.646 4.247 0 7.128-3.871 7.128-9.053.003-3.918-3.317-7.568-8.361-7.568z"/></svg></span></a><a aria-label="Reddit" class="the_champ_button_reddit" href="https://reddit.com/submit?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D22488&title=java-tutorials" title="Reddit" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_reddit" style="background-color:#ff5700;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-3.5 -3.5 39 39"><path d="M28.543 15.774a2.953 2.953 0 0 0-2.951-2.949 2.882 2.882 0 0 0-1.9.713 14.075 14.075 0 0 0-6.85-2.044l1.38-4.349 3.768.884a2.452 2.452 0 1 0 .24-1.176l-4.274-1a.6.6 0 0 0-.709.4l-1.659 5.224a14.314 14.314 0 0 0-7.316 2.029 2.908 2.908 0 0 0-1.872-.681 2.942 2.942 0 0 0-1.618 5.4 5.109 5.109 0 0 0-.062.765c0 4.158 5.037 7.541 11.229 7.541s11.22-3.383 11.22-7.541a5.2 5.2 0 0 0-.053-.706 2.963 2.963 0 0 0 1.427-2.51zm-18.008 1.88a1.753 1.753 0 0 1 1.73-1.74 1.73 1.73 0 0 1 1.709 1.74 1.709 1.709 0 0 1-1.709 1.711 1.733 1.733 0 0 1-1.73-1.711zm9.565 4.968a5.573 5.573 0 0 1-4.081 1.272h-.032a5.576 5.576 0 0 1-4.087-1.272.6.6 0 0 1 .844-.854 4.5 4.5 0 0 0 3.238.927h.032a4.5 4.5 0 0 0 3.237-.927.6.6 0 1 1 .844.854zm-.331-3.256a1.726 1.726 0 1 1 1.709-1.712 1.717 1.717 0 0 1-1.712 1.712z" fill="#484848"/></svg></span></a><a aria-label="Whatsapp" class="the_champ_whatsapp" href="https://api.whatsapp.com/send?text=java-tutorials https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D22488" title="Whatsapp" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#55eb4c;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-6 -5 40 40"><path class="the_champ_svg_stroke the_champ_no_fill" stroke="#484848" stroke-width="2" fill="none" d="M 11.579798566743314 24.396926207859085 A 10 10 0 1 0 6.808479557110079 20.73576436351046"></path><path d="M 7 19 l -1 6 l 6 -1" class="the_champ_no_fill the_champ_svg_stroke" stroke="#484848" stroke-width="2" fill="none"></path><path d="M 10 10 q -1 8 8 11 c 5 -1 0 -6 -1 -3 q -4 -3 -5 -5 c 4 -2 -1 -5 -1 -4" fill="#484848"></path></svg></span></a><a class="the_champ_more" title="More" rel="nofollow noopener" style="font-size:24px!important;border:0;box-shadow:none;display:inline-block!important;font-size:16px;padding:0 4px;vertical-align: middle;display:inline;" href="https://www.automationdojos.com/?p=22488" onclick="event.preventDefault()"><span class="the_champ_svg" style="background-color:#ee8e2d;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block!important;opacity:1;float:left;font-size:32px!important;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;display:inline;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box;" onclick="theChampMoreSharingPopup(this, 'https://www.automationdojos.com/?p=22488', 'java-tutorials', '' )"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-.3 0 32 32" version="1.1" width="100%" height="100%" style="display:block;" xml:space="preserve"><g><path fill="#484848" d="M18 14V8h-4v6H8v4h6v6h4v-6h6v-4h-6z" fill-rule="evenodd"></path></g></svg></span></a></div></div><p>The post <a href="https://www.automationdojos.com/java-classes-and-objects-part1/">Java Tutorial #7 Part 1 – Classes &#038; Objects</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.automationdojos.com/java-classes-and-objects-part1/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Java Tutorial #6 – Jump Statements</title>
		<link>https://www.automationdojos.com/java-jump-statements/</link>
					<comments>https://www.automationdojos.com/java-jump-statements/#respond</comments>
		
		<dc:creator><![CDATA[Tushar Sharma]]></dc:creator>
		<pubDate>Sat, 11 May 2019 12:09:00 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Tutorials]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[core-java]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java-jump-statements]]></category>
		<category><![CDATA[java-tutorials]]></category>
		<guid isPermaLink="false">https://www.automationdojos.com/?p=22473</guid>

					<description><![CDATA[<p><a href="https://www.automationdojos.com/java-jump-statements/">Java Tutorial #6 – Jump Statements</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="575" height="345" src="https://www.automationdojos.com/wp-content/uploads/2019/05/post-java-tutorial-6.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" srcset="https://www.automationdojos.com/wp-content/uploads/2019/05/post-java-tutorial-6.png 575w, https://www.automationdojos.com/wp-content/uploads/2019/05/post-java-tutorial-6-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2019/05/post-java-tutorial-6-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2019/05/post-java-tutorial-6-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2019/05/post-java-tutorial-6-400x240.png 400w" sizes="(max-width: 575px) 100vw, 575px" /></p>
<p>Introduction The break statement is used to stop further execution. It can be used either in a loop or within a switch statement. Break Statement The break statement is used to stop further execution. It can be used either in a loop or within a switch statement. If the break statement is not specified, then [&#8230;]</p>
<p>The post <a href="https://www.automationdojos.com/java-jump-statements/">Java Tutorial #6 – Jump Statements</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://www.automationdojos.com/java-jump-statements/">Java Tutorial #6 – Jump Statements</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="575" height="345" src="https://www.automationdojos.com/wp-content/uploads/2019/05/post-java-tutorial-6.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2019/05/post-java-tutorial-6.png 575w, https://www.automationdojos.com/wp-content/uploads/2019/05/post-java-tutorial-6-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2019/05/post-java-tutorial-6-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2019/05/post-java-tutorial-6-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2019/05/post-java-tutorial-6-400x240.png 400w" sizes="auto, (max-width: 575px) 100vw, 575px" /></p><h2 class="simpletoc-title">Table of Contents</h2>
<ul class="simpletoc-list">
<li><a href="#introduction">Introduction</a>

</li>
<li><a href="#break-statement">Break Statement</a>


<ul><li>
<a href="#break-in-a-loop">Break in a loop</a>

</li>
<li><a href="#break-in-switch-statement">Break in Switch statement</a>

</li>
</ul>
<li><a href="#continue-statement">Continue Statement</a>

</li>
<li><a href="#return-statement">Return Statement</a>

</li>
<li><a href="#conclusion">Conclusion</a>
</li></ul>

<h2 class="wp-block-heading" id="introduction">Introduction</h2>


<p>The break statement is used to stop further execution. It can be used either in a loop or within a switch statement.</p>


<h2 class="wp-block-heading" id="break-statement">Break Statement</h2>


<p>The break statement is used to stop further execution. It can be used either in a loop or within a switch statement.</p>



<div style="height:10px" aria-hidden="true" class="wp-block-spacer"></div>


<div class="su-note"  style="border-color:#dcdcdc;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#f6f6f6;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">
<h3 class="wp-block-heading" id="break-in-a-loop">Break in a loop</h3>
<p>When the break statement is used within a loop, it terminates the loop. So, the rest of the body of the loop is skipped and control is transferred to the first statement outside the loop even if the condition part of the loop is true. The break statement can be used with all Java loops like while, do-while, for and for-each. The following code demonstrates a break statement within a for loop:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">int[] input = { 10, 20, 30,40,50 };

for (int num : input) {
if(num > 25) {
		break;
	}
System.out.println("Num:"+num);
}
System.out.println("Outside loop");
</pre>
<p>This code declares an array and uses a <strong>for-each</strong> loop to iterate through the array. The loop executes normally for the values <strong>10</strong> and <strong>20</strong>. When the value <strong>30</strong> is encountered, the <strong>if</strong> condition that checks if the number is greater than <strong>25</strong> becomes true and so the <strong>break</strong> gets executed. This terminates the loop and control is transferred outside the loop. So, this code produces the following output:</p>
<p>
Num:10<br />
Num:20<br />
Outside loop
</p>
</div></div>



<div style="height:15px" aria-hidden="true" class="wp-block-spacer"></div>


<div class="su-note"  style="border-color:#dcdcdc;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#f6f6f6;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">
<h3 class="wp-block-heading" id="break-in-switch-statement">Break in Switch statement</h3>
<p>The break statement can also be used within a case statement in a switch block. In such a scenario, it terminates the case statement when a match is found, so no further case statements are checked. The following code demonstrates a break within a switch statement:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">String dept = "IT";
switch(dept) {
case "HR": 
	System.out.println("Human Resources");
	break;
case "IT": 
	System.out.println("Information Technology");
	break;
case "Admin": 
	System.out.println("Administration");
	break;
default: 
	System.out.println("Invalid dept");
	break;
}
System.out.println("Outside switch");
</pre>
<p>This code uses a <strong>switch</strong> block.  It compares the value in the <strong>dept</strong> variable with each <strong>case</strong> value. A <strong>break</strong> is specified in each case statement. The variable <strong>dept</strong> matches the second case statement and so the value <strong>Information Technology</strong> gets printed to the console. Since a <strong>break</strong> statement is present, no further case statements are checked and control transfers outside the switch block. So, this code produces the following output:</p>
<p>
Information Technology<br />
Outside switch
</p>
</div></div>



<p>If the break statement is not specified, then each case statement is checked even after a successful match.</p>



<div class="rt-block">
<h2 class="wp-block-heading" id="continue-statement">Continue Statement</h2>


<p>The continue statement is used to stop the current iteration of a loop. As soon as the continue statement is encountered the rest of the body of the loop is skipped and control transfers to the top of the loop. The condition is checked again and the loop is continued as long as the condition is true. The following code demonstrates the continue statement:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">int[] input = { 10, 20, 30,40,50 };

for (int num : input) {
if(num == 30) {
continue;
}
System.out.println("Num:"+num);
}
System.out.println("Outside loop");
</pre>



<p>This code declares an array and uses a <strong>for-each</strong> loop to iterate through the array. The loop executes normally for the values <strong>10</strong> and <strong>20.</strong> When the value <strong>30</strong> is encountered, the if condition that checks if the number is equal to <strong>30</strong> becomes true and so the continue gets executed. This skips the <strong>Sysout</strong> statement for the value <strong>30</strong> and control is transferred to the top of the loop. The loop then executes for the other values in the array. So, this code produces the following output:</p>



<pre class="wp-block-code"><code>Num:10
Num:20
Num:40
Num:50
Outside loop
</code></pre>

</div>



<div class="rt-block">
<h2 class="wp-block-heading" id="return-statement">Return Statement</h2>


<p>The return statement is used to return control from a method to an invoking method. It can optionally return a value. As soon as return is encountered, control is transferred to the invoking method.</p>



<p>The following code demonstrates the return statement:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">public void greet() {
String greeting = sayHello("Earth");
	System.out.println(greeting);
}
	
public String sayHello(String name) {
return "Hello "+name;
}
</pre>



<p>This code declares a method called <strong>greet(</strong>) which invokes the <strong>sayHello()</strong> method. The <strong>sayHello()</strong> method uses the <strong>return</strong> statement to return a String value. As soon as return is encountered, control is transferred to the invoking method, in this case, the <strong>greet()</strong> method.</p>



<p>So, this code produces the following output: Hello Earth</p>

</div>


<h2 class="wp-block-heading" id="conclusion">Conclusion</h2>


<p>Java jump statements help to transfer control to a different part of the code. The break statement terminates a loop and transfers control outside the loop. It can also be used to terminate a case statement in a switch block. The continue statement skips the current iteration of the loop, transfers control to the top of the loop and continues the next iteration. The return statement is used to return control from a method to an invoking method.</p>
<div class='the_champ_sharing_container the_champ_vertical_sharing the_champ_hide_sharing the_champ_bottom_sharing' style='width:42px;left: -10px;top: 222px;-webkit-box-shadow:none;box-shadow:none;' data-super-socializer-href="https://www.automationdojos.com/tag/java-tutorials/feed/"><div class="the_champ_sharing_ul"><a aria-label="Facebook" class="the_champ_facebook" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D22473" title="Facebook" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#0765FE;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path fill="#484848" d="M28 16c0-6.627-5.373-12-12-12S4 9.373 4 16c0 5.628 3.875 10.35 9.101 11.647v-7.98h-2.474V16H13.1v-1.58c0-4.085 1.849-5.978 5.859-5.978.76 0 2.072.15 2.608.298v3.325c-.283-.03-.775-.045-1.386-.045-1.967 0-2.728.745-2.728 2.683V16h3.92l-.673 3.667h-3.247v8.245C23.395 27.195 28 22.135 28 16Z"></path></svg></span></a><a aria-label="Twitter" class="the_champ_button_twitter" href="https://twitter.com/intent/tweet?via=@testoxide&text=java-tutorials&url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D22473" title="Twitter" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_twitter" style="background-color:#55acee;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-4 -4 39 39"><path d="M28 8.557a9.913 9.913 0 0 1-2.828.775 4.93 4.93 0 0 0 2.166-2.725 9.738 9.738 0 0 1-3.13 1.194 4.92 4.92 0 0 0-3.593-1.55 4.924 4.924 0 0 0-4.794 6.049c-4.09-.21-7.72-2.17-10.15-5.15a4.942 4.942 0 0 0-.665 2.477c0 1.71.87 3.214 2.19 4.1a4.968 4.968 0 0 1-2.23-.616v.06c0 2.39 1.7 4.38 3.952 4.83-.414.115-.85.174-1.297.174-.318 0-.626-.03-.928-.086a4.935 4.935 0 0 0 4.6 3.42 9.893 9.893 0 0 1-6.114 2.107c-.398 0-.79-.023-1.175-.068a13.953 13.953 0 0 0 7.55 2.213c9.056 0 14.01-7.507 14.01-14.013 0-.213-.005-.426-.015-.637.96-.695 1.795-1.56 2.455-2.55z" fill="#484848"></path></svg></span></a><a aria-label="Linkedin" class="the_champ_button_linkedin" href="https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D22473" title="Linkedin" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_linkedin" style="background-color:#0077b5;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path d="M6.227 12.61h4.19v13.48h-4.19V12.61zm2.095-6.7a2.43 2.43 0 0 1 0 4.86c-1.344 0-2.428-1.09-2.428-2.43s1.084-2.43 2.428-2.43m4.72 6.7h4.02v1.84h.058c.56-1.058 1.927-2.176 3.965-2.176 4.238 0 5.02 2.792 5.02 6.42v7.395h-4.183v-6.56c0-1.564-.03-3.574-2.178-3.574-2.18 0-2.514 1.7-2.514 3.46v6.668h-4.187V12.61z" fill="#484848"></path></svg></span></a><a aria-label="Pinterest" class="the_champ_button_pinterest" href="https://www.automationdojos.com/tag/java-tutorials/feed/" onclick="event.preventDefault();javascript:void((function(){var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','//assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());" title="Pinterest" rel="noopener" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_pinterest" style="background-color:#cc2329;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-2 -2 35 35"><path fill="#484848" d="M16.539 4.5c-6.277 0-9.442 4.5-9.442 8.253 0 2.272.86 4.293 2.705 5.046.303.125.574.005.662-.33.061-.231.205-.816.27-1.06.088-.331.053-.447-.191-.736-.532-.627-.873-1.439-.873-2.591 0-3.338 2.498-6.327 6.505-6.327 3.548 0 5.497 2.168 5.497 5.062 0 3.81-1.686 7.025-4.188 7.025-1.382 0-2.416-1.142-2.085-2.545.397-1.674 1.166-3.48 1.166-4.689 0-1.081-.581-1.983-1.782-1.983-1.413 0-2.548 1.462-2.548 3.419 0 1.247.421 2.091.421 2.091l-1.699 7.199c-.505 2.137-.076 4.755-.039 5.019.021.158.223.196.314.077.13-.17 1.813-2.247 2.384-4.324.162-.587.929-3.631.929-3.631.46.876 1.801 1.646 3.227 1.646 4.247 0 7.128-3.871 7.128-9.053.003-3.918-3.317-7.568-8.361-7.568z"/></svg></span></a><a aria-label="Reddit" class="the_champ_button_reddit" href="https://reddit.com/submit?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D22473&title=java-tutorials" title="Reddit" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_reddit" style="background-color:#ff5700;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-3.5 -3.5 39 39"><path d="M28.543 15.774a2.953 2.953 0 0 0-2.951-2.949 2.882 2.882 0 0 0-1.9.713 14.075 14.075 0 0 0-6.85-2.044l1.38-4.349 3.768.884a2.452 2.452 0 1 0 .24-1.176l-4.274-1a.6.6 0 0 0-.709.4l-1.659 5.224a14.314 14.314 0 0 0-7.316 2.029 2.908 2.908 0 0 0-1.872-.681 2.942 2.942 0 0 0-1.618 5.4 5.109 5.109 0 0 0-.062.765c0 4.158 5.037 7.541 11.229 7.541s11.22-3.383 11.22-7.541a5.2 5.2 0 0 0-.053-.706 2.963 2.963 0 0 0 1.427-2.51zm-18.008 1.88a1.753 1.753 0 0 1 1.73-1.74 1.73 1.73 0 0 1 1.709 1.74 1.709 1.709 0 0 1-1.709 1.711 1.733 1.733 0 0 1-1.73-1.711zm9.565 4.968a5.573 5.573 0 0 1-4.081 1.272h-.032a5.576 5.576 0 0 1-4.087-1.272.6.6 0 0 1 .844-.854 4.5 4.5 0 0 0 3.238.927h.032a4.5 4.5 0 0 0 3.237-.927.6.6 0 1 1 .844.854zm-.331-3.256a1.726 1.726 0 1 1 1.709-1.712 1.717 1.717 0 0 1-1.712 1.712z" fill="#484848"/></svg></span></a><a aria-label="Whatsapp" class="the_champ_whatsapp" href="https://api.whatsapp.com/send?text=java-tutorials https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D22473" title="Whatsapp" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#55eb4c;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-6 -5 40 40"><path class="the_champ_svg_stroke the_champ_no_fill" stroke="#484848" stroke-width="2" fill="none" d="M 11.579798566743314 24.396926207859085 A 10 10 0 1 0 6.808479557110079 20.73576436351046"></path><path d="M 7 19 l -1 6 l 6 -1" class="the_champ_no_fill the_champ_svg_stroke" stroke="#484848" stroke-width="2" fill="none"></path><path d="M 10 10 q -1 8 8 11 c 5 -1 0 -6 -1 -3 q -4 -3 -5 -5 c 4 -2 -1 -5 -1 -4" fill="#484848"></path></svg></span></a><a class="the_champ_more" title="More" rel="nofollow noopener" style="font-size:24px!important;border:0;box-shadow:none;display:inline-block!important;font-size:16px;padding:0 4px;vertical-align: middle;display:inline;" href="https://www.automationdojos.com/?p=22473" onclick="event.preventDefault()"><span class="the_champ_svg" style="background-color:#ee8e2d;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block!important;opacity:1;float:left;font-size:32px!important;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;display:inline;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box;" onclick="theChampMoreSharingPopup(this, 'https://www.automationdojos.com/?p=22473', 'java-tutorials', '' )"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-.3 0 32 32" version="1.1" width="100%" height="100%" style="display:block;" xml:space="preserve"><g><path fill="#484848" d="M18 14V8h-4v6H8v4h6v6h4v-6h6v-4h-6z" fill-rule="evenodd"></path></g></svg></span></a></div></div><p>The post <a href="https://www.automationdojos.com/java-jump-statements/">Java Tutorial #6 – Jump Statements</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.automationdojos.com/java-jump-statements/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Java Tutorial #5 – Loop Statements</title>
		<link>https://www.automationdojos.com/java-loop-statements/</link>
					<comments>https://www.automationdojos.com/java-loop-statements/#respond</comments>
		
		<dc:creator><![CDATA[Tushar Sharma]]></dc:creator>
		<pubDate>Thu, 11 Apr 2019 09:47:00 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Tutorials]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[core-java]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java-iterations]]></category>
		<category><![CDATA[java-loops]]></category>
		<category><![CDATA[java-tutorials]]></category>
		<guid isPermaLink="false">https://www.automationdojos.com/?p=21233</guid>

					<description><![CDATA[<p><a href="https://www.automationdojos.com/java-loop-statements/">Java Tutorial #5 – Loop Statements</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="575" height="345" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post-java-tutorial-5.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2021/10/post-java-tutorial-5.png 575w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-java-tutorial-5-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-java-tutorial-5-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-java-tutorial-5-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-java-tutorial-5-400x240.png 400w" sizes="auto, (max-width: 575px) 100vw, 575px" /></p>
<p>Iteration statements are used to repeat a particular block of code until a certain condition is true. In this article, we will be taking a look at Java’s iteration statements. While Statement The while statement repeats a block of code as long as a condition is true. Do-while Statement A do-while loop is similar to [&#8230;]</p>
<p>The post <a href="https://www.automationdojos.com/java-loop-statements/">Java Tutorial #5 – Loop Statements</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://www.automationdojos.com/java-loop-statements/">Java Tutorial #5 – Loop Statements</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="575" height="345" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post-java-tutorial-5.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2021/10/post-java-tutorial-5.png 575w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-java-tutorial-5-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-java-tutorial-5-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-java-tutorial-5-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2021/10/post-java-tutorial-5-400x240.png 400w" sizes="auto, (max-width: 575px) 100vw, 575px" /></p><h2 class="simpletoc-title">Table of Contents</h2>
<ul class="simpletoc-list">
<li><a href="#while-statement">While Statement</a>


<ul><li>
<a href="#syntax">Syntax</a>

</li>
<li><a href="#example">Example</a>

</li>
</ul>
<li><a href="#dowhile-statement">Do-while Statement</a>


<ul><li>
<a href="#syntax">Syntax</a>

</li>
<li><a href="#example">Example</a>

</li>
</ul>
<li><a href="#for-statement">For Statement</a>


<ul><li>
<a href="#syntax">Syntax</a>

</li>
<li><a href="#example">Example</a>

</li>
</ul>
<li><a href="#foreach-statement">For-Each Statement</a>


<ul><li>
<a href="#syntax">Syntax</a>

</li>
<li><a href="#example">Example</a>

</li>
</ul>
<li><a href="#conclusion">Conclusion</a>
</li></ul>


<p>Iteration statements are used to repeat a particular block of code until a certain condition is true. In this article, we will be taking a look at Java’s iteration statements.</p>


<h2 class="wp-block-heading" id="while-statement">While Statement</h2>


<p>The while statement repeats a block of code as long as a condition is true.</p>


<div class="su-note"  style="border-color:#dcdcdc;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#f6f6f6;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">
<h3 class="wp-block-heading" style="font-size:20px" id="syntax">Syntax</h3>
<p>A <strong>while</strong> statement has the following syntax:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">while (condition){
//body of the loop
}
</pre>
<p>The while keyword is followed by a condition that evaluates to a boolean value. If the boolean value is <strong>true</strong>, the while loop is entered. Once the code in the loop is completed, the condition is checked again and this continues till the condition becomes <strong>false</strong>.</p>
</div></div>



<div style="height:15px" aria-hidden="true" class="wp-block-spacer"></div>


<div class="su-note"  style="border-color:#dcdcdc;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#f6f6f6;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">
<h3 class="wp-block-heading" style="font-size:20px" id="example">Example</h3>
<p>The following code demonstrates a basic <strong>while</strong> statement:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">int i = 0;

while (i &lt; 3) {
System.out.println("Loop Iteration: " + i);
	i++;
}
System.out.println("Done!");
</pre>
<p>Here, the while statement checks if <strong>i</strong> is less than <strong>3</strong>. Since this is true, the loop is entered and the <strong>System.out</strong> statement is executed. <strong>i</strong> is then incremented in the while loop and the condition is checked again. This goes on till the value of <strong>i </strong>becomes <strong>3</strong> after which the loop is exited. So, this code produces the following output:</p>
<p>
Loop Iteration: 0<br />
Loop Iteration: 1<br />
Loop Iteration: 2<br />
Done!
</p>
</div></div>



<div style="height:5px" aria-hidden="true" class="wp-block-spacer"></div>


<h2 class="wp-block-heading" id="dowhile-statement">Do-while Statement</h2>


<p>A do-while loop is similar to a while loop except that it checks the condition at the end of the loop. This ensures that the body of the loop is executed at least once.</p>


<div class="su-note"  style="border-color:#dcdcdc;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#f6f6f6;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">
<h3 class="wp-block-heading" style="font-size:20px" id="syntax">Syntax</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">do{
// body of the loop
} while(condition);
</pre>
<p>The <strong>do</strong> keyword is followed by some code followed by a <strong>while</strong> statement. A condition that evaluates to a boolean value is specified in the <strong>while</strong> statement. The loop first executes and then checks the condition. If the condition is true, the loop is repeated. The loop is exited when the condition becomes false.</p>
</div></div>



<div style="height:15px" aria-hidden="true" class="wp-block-spacer"></div>


<div class="su-note"  style="border-color:#dcdcdc;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#f6f6f6;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">
<h3 class="wp-block-heading" style="font-size:20px" id="example">Example</h3>
<p>The following code demonstrates a basic <strong>do-while</strong> statement:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">int i = 0;

do {
System.out.println("Loop Iteration: " + i);
	i++;
} while (i &lt; 3);
		
System.out.println("Done!");
This code is similar to the code above except that it uses a do-while loop. So, it produces the same output as before. 
However, consider the following code:
int i = 5;

do {
System.out.println("Loop Iteration: " + i);
	i++;
} while (i &lt; 3);
		
System.out.println("Done!");
</pre>
<p>In this case, <strong>i</strong> is initialized to the value <strong>5</strong>. Since the do-while loop checks the condition at the end, the body of the loop is executed once. After that, the condition is checked and since it is false, the loop is not entered again. So, this code produces the following output:</p>
<p>
Loop Iteration: 5<br />
Done!
</p>
<p>
If a while loop had been used here, the body of the loop would not have been executed even once since the condition is false.
</p>
</div></div>



<div style="height:5px" aria-hidden="true" class="wp-block-spacer"></div>


<h2 class="wp-block-heading" id="for-statement">For Statement</h2>


<p>A for loop can be used to iterate over a range of values. It continues the iteration until a condition is true, after which the loop is exited.</p>


<div class="su-note"  style="border-color:#dcdcdc;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#f6f6f6;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">
<h3 class="wp-block-heading" style="font-size:20px" id="syntax">Syntax</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">for(initialization;condition;iteration){
//body of the loop
}
</pre>
<p>The <strong>for</strong> keyword is followed by an initialization statement, a condition and an iteration statement. The initialization statement executes only once, at the start of the loop. The condition and iteration parts are executed each time the loop is repeated. The condition statement evaluates to a boolean value. If it is true, the body of the loop is executed. Once the body completes, the iteration part is executed and the condition is again checked. This continues till the condition evaluates to false.&nbsp;</p>
</div></div>



<div style="height:15px" aria-hidden="true" class="wp-block-spacer"></div>


<div class="su-note"  style="border-color:#dcdcdc;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#f6f6f6;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">
<h3 class="wp-block-heading" style="font-size:20px" id="example">Example</h3>
<p>The following code demonstrates a basic <strong>for </strong>statement:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">for(int i = 0;i &lt; 3;i++) {
System.out.println("Loop Iteration: " + i);
}
System.out.println("Done!");
</pre>
<p>Here <strong>i=0</strong> is the initialization part, <strong>i &lt; 3</strong> is the condition and <strong>i++</strong> is the iteration part. The for loop initializes <strong>i</strong> to <strong>0</strong> at the start of the loop. It then checks if <strong>i&lt;3</strong> is true and since this is true, it enters the loop. Once the code it in the loop completes, it increments <strong>i</strong> and again checks if the condition <strong>i&lt;3</strong> is true. This continues till <strong>i</strong> becomes <strong>3</strong> after which the loop is exited. So, this code produces the following output:</p>
<p>
Loop Iteration: 0<br />
Loop Iteration: 1<br />
Loop Iteration: 2<br />
Done!
</p>
</div></div>



<div style="height:5px" aria-hidden="true" class="wp-block-spacer"></div>


<h2 class="wp-block-heading" id="foreach-statement">For-Each Statement</h2>


<p>The for-each loop is used to cycle through an array or a collection of objects.</p>


<div class="su-note"  style="border-color:#dcdcdc;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#f6f6f6;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">
<h3 class="wp-block-heading" style="font-size:20px" id="syntax">Syntax</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">for(datatype var : collection) {
//body of the loop
}
</pre>
<p>The <strong>for</strong> keyword is followed by a variable and the <strong>collection</strong> over which the loop should iterate. In each iteration of the loop, it fetches the next element from the array or collection and executes the body of the loop for that element. This continues till all the elements in the collection are exhausted after which the loop is terminated.</p>
</div></div>



<div style="height:15px" aria-hidden="true" class="wp-block-spacer"></div>


<div class="su-note"  style="border-color:#dcdcdc;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#f6f6f6;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">
<h3 class="wp-block-heading" style="font-size:20px" id="example">Example</h3>
<p>The following code demonstrates a basic <strong>for-each </strong>statement:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="true" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">int[] input = { 10, 20, 30 };

for (int num : input) {
	System.out.println("Number is " + num);
}
</pre>
<p>This code first declares an integer array called <strong>input</strong>. It then uses a <strong>for-each</strong> loop to iterate through this array. For each iteration, the next value from the array input is fetched and assigned to the variable <strong>num</strong>. The body of the loop is then executed with this value. The loop is exited once all the elements in the array are exhausted. So, this code produces the following output:</p>
<p>
Number is 10<br />
Number is 20<br />
Number is 30
</p>
<p>
When you want to iterate over an array or a collection a for-each loop is better than a for loop since it automatically fetches the next element and assigns to a variable
</p>
</div></div>


<h2 class="wp-block-heading" id="conclusion">Conclusion</h2>


<p>Java iteration statements help to repeat a block of code. The while and do-while repeat a block of code as long as a condition is true. The do-while loop should be used when you want the body of the loop to be executed at least once. The for loop is used to iterate over a range of values. The for-each loop is used to iterate over an array or a collection.</p>
<div class='the_champ_sharing_container the_champ_vertical_sharing the_champ_hide_sharing the_champ_bottom_sharing' style='width:42px;left: -10px;top: 222px;-webkit-box-shadow:none;box-shadow:none;' data-super-socializer-href="https://www.automationdojos.com/tag/java-tutorials/feed/"><div class="the_champ_sharing_ul"><a aria-label="Facebook" class="the_champ_facebook" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D21233" title="Facebook" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#0765FE;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path fill="#484848" d="M28 16c0-6.627-5.373-12-12-12S4 9.373 4 16c0 5.628 3.875 10.35 9.101 11.647v-7.98h-2.474V16H13.1v-1.58c0-4.085 1.849-5.978 5.859-5.978.76 0 2.072.15 2.608.298v3.325c-.283-.03-.775-.045-1.386-.045-1.967 0-2.728.745-2.728 2.683V16h3.92l-.673 3.667h-3.247v8.245C23.395 27.195 28 22.135 28 16Z"></path></svg></span></a><a aria-label="Twitter" class="the_champ_button_twitter" href="https://twitter.com/intent/tweet?via=@testoxide&text=java-tutorials&url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D21233" title="Twitter" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_twitter" style="background-color:#55acee;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-4 -4 39 39"><path d="M28 8.557a9.913 9.913 0 0 1-2.828.775 4.93 4.93 0 0 0 2.166-2.725 9.738 9.738 0 0 1-3.13 1.194 4.92 4.92 0 0 0-3.593-1.55 4.924 4.924 0 0 0-4.794 6.049c-4.09-.21-7.72-2.17-10.15-5.15a4.942 4.942 0 0 0-.665 2.477c0 1.71.87 3.214 2.19 4.1a4.968 4.968 0 0 1-2.23-.616v.06c0 2.39 1.7 4.38 3.952 4.83-.414.115-.85.174-1.297.174-.318 0-.626-.03-.928-.086a4.935 4.935 0 0 0 4.6 3.42 9.893 9.893 0 0 1-6.114 2.107c-.398 0-.79-.023-1.175-.068a13.953 13.953 0 0 0 7.55 2.213c9.056 0 14.01-7.507 14.01-14.013 0-.213-.005-.426-.015-.637.96-.695 1.795-1.56 2.455-2.55z" fill="#484848"></path></svg></span></a><a aria-label="Linkedin" class="the_champ_button_linkedin" href="https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D21233" title="Linkedin" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_linkedin" style="background-color:#0077b5;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path d="M6.227 12.61h4.19v13.48h-4.19V12.61zm2.095-6.7a2.43 2.43 0 0 1 0 4.86c-1.344 0-2.428-1.09-2.428-2.43s1.084-2.43 2.428-2.43m4.72 6.7h4.02v1.84h.058c.56-1.058 1.927-2.176 3.965-2.176 4.238 0 5.02 2.792 5.02 6.42v7.395h-4.183v-6.56c0-1.564-.03-3.574-2.178-3.574-2.18 0-2.514 1.7-2.514 3.46v6.668h-4.187V12.61z" fill="#484848"></path></svg></span></a><a aria-label="Pinterest" class="the_champ_button_pinterest" href="https://www.automationdojos.com/tag/java-tutorials/feed/" onclick="event.preventDefault();javascript:void((function(){var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','//assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());" title="Pinterest" rel="noopener" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_pinterest" style="background-color:#cc2329;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-2 -2 35 35"><path fill="#484848" d="M16.539 4.5c-6.277 0-9.442 4.5-9.442 8.253 0 2.272.86 4.293 2.705 5.046.303.125.574.005.662-.33.061-.231.205-.816.27-1.06.088-.331.053-.447-.191-.736-.532-.627-.873-1.439-.873-2.591 0-3.338 2.498-6.327 6.505-6.327 3.548 0 5.497 2.168 5.497 5.062 0 3.81-1.686 7.025-4.188 7.025-1.382 0-2.416-1.142-2.085-2.545.397-1.674 1.166-3.48 1.166-4.689 0-1.081-.581-1.983-1.782-1.983-1.413 0-2.548 1.462-2.548 3.419 0 1.247.421 2.091.421 2.091l-1.699 7.199c-.505 2.137-.076 4.755-.039 5.019.021.158.223.196.314.077.13-.17 1.813-2.247 2.384-4.324.162-.587.929-3.631.929-3.631.46.876 1.801 1.646 3.227 1.646 4.247 0 7.128-3.871 7.128-9.053.003-3.918-3.317-7.568-8.361-7.568z"/></svg></span></a><a aria-label="Reddit" class="the_champ_button_reddit" href="https://reddit.com/submit?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D21233&title=java-tutorials" title="Reddit" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_reddit" style="background-color:#ff5700;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-3.5 -3.5 39 39"><path d="M28.543 15.774a2.953 2.953 0 0 0-2.951-2.949 2.882 2.882 0 0 0-1.9.713 14.075 14.075 0 0 0-6.85-2.044l1.38-4.349 3.768.884a2.452 2.452 0 1 0 .24-1.176l-4.274-1a.6.6 0 0 0-.709.4l-1.659 5.224a14.314 14.314 0 0 0-7.316 2.029 2.908 2.908 0 0 0-1.872-.681 2.942 2.942 0 0 0-1.618 5.4 5.109 5.109 0 0 0-.062.765c0 4.158 5.037 7.541 11.229 7.541s11.22-3.383 11.22-7.541a5.2 5.2 0 0 0-.053-.706 2.963 2.963 0 0 0 1.427-2.51zm-18.008 1.88a1.753 1.753 0 0 1 1.73-1.74 1.73 1.73 0 0 1 1.709 1.74 1.709 1.709 0 0 1-1.709 1.711 1.733 1.733 0 0 1-1.73-1.711zm9.565 4.968a5.573 5.573 0 0 1-4.081 1.272h-.032a5.576 5.576 0 0 1-4.087-1.272.6.6 0 0 1 .844-.854 4.5 4.5 0 0 0 3.238.927h.032a4.5 4.5 0 0 0 3.237-.927.6.6 0 1 1 .844.854zm-.331-3.256a1.726 1.726 0 1 1 1.709-1.712 1.717 1.717 0 0 1-1.712 1.712z" fill="#484848"/></svg></span></a><a aria-label="Whatsapp" class="the_champ_whatsapp" href="https://api.whatsapp.com/send?text=java-tutorials https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D21233" title="Whatsapp" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#55eb4c;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-6 -5 40 40"><path class="the_champ_svg_stroke the_champ_no_fill" stroke="#484848" stroke-width="2" fill="none" d="M 11.579798566743314 24.396926207859085 A 10 10 0 1 0 6.808479557110079 20.73576436351046"></path><path d="M 7 19 l -1 6 l 6 -1" class="the_champ_no_fill the_champ_svg_stroke" stroke="#484848" stroke-width="2" fill="none"></path><path d="M 10 10 q -1 8 8 11 c 5 -1 0 -6 -1 -3 q -4 -3 -5 -5 c 4 -2 -1 -5 -1 -4" fill="#484848"></path></svg></span></a><a class="the_champ_more" title="More" rel="nofollow noopener" style="font-size:24px!important;border:0;box-shadow:none;display:inline-block!important;font-size:16px;padding:0 4px;vertical-align: middle;display:inline;" href="https://www.automationdojos.com/?p=21233" onclick="event.preventDefault()"><span class="the_champ_svg" style="background-color:#ee8e2d;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block!important;opacity:1;float:left;font-size:32px!important;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;display:inline;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box;" onclick="theChampMoreSharingPopup(this, 'https://www.automationdojos.com/?p=21233', 'java-tutorials', '' )"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-.3 0 32 32" version="1.1" width="100%" height="100%" style="display:block;" xml:space="preserve"><g><path fill="#484848" d="M18 14V8h-4v6H8v4h6v6h4v-6h6v-4h-6z" fill-rule="evenodd"></path></g></svg></span></a></div></div><p>The post <a href="https://www.automationdojos.com/java-loop-statements/">Java Tutorial #5 – Loop Statements</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.automationdojos.com/java-loop-statements/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Java Tutorial #4 – Control Statements</title>
		<link>https://www.automationdojos.com/java-control-statements/</link>
					<comments>https://www.automationdojos.com/java-control-statements/#respond</comments>
		
		<dc:creator><![CDATA[Tushar Sharma]]></dc:creator>
		<pubDate>Sat, 30 Mar 2019 18:17:00 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Tutorials]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[core-java]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java-control-statement]]></category>
		<category><![CDATA[java-decision-statement]]></category>
		<category><![CDATA[java-tutorials]]></category>
		<guid isPermaLink="false">https://www.automationdojos.com/?p=20707</guid>

					<description><![CDATA[<p><a href="https://www.automationdojos.com/java-control-statements/">Java Tutorial #4 – Control Statements</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="575" height="345" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post_JAVA-Tutorial-4.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2021/10/post_JAVA-Tutorial-4.png 575w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_JAVA-Tutorial-4-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_JAVA-Tutorial-4-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_JAVA-Tutorial-4-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_JAVA-Tutorial-4-400x240.png 400w" sizes="auto, (max-width: 575px) 100vw, 575px" /></p>
<p>Introduction Control statements are used to change the flow of execution based on changes to certain variables in the code. One of the types of control statements are the selection statements or decision statements. These should be used when you want to check some condition and take a decision based on the condition. Java supports [&#8230;]</p>
<p>The post <a href="https://www.automationdojos.com/java-control-statements/">Java Tutorial #4 – Control Statements</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://www.automationdojos.com/java-control-statements/">Java Tutorial #4 – Control Statements</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="575" height="345" src="https://www.automationdojos.com/wp-content/uploads/2021/10/post_JAVA-Tutorial-4.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2021/10/post_JAVA-Tutorial-4.png 575w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_JAVA-Tutorial-4-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_JAVA-Tutorial-4-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_JAVA-Tutorial-4-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2021/10/post_JAVA-Tutorial-4-400x240.png 400w" sizes="auto, (max-width: 575px) 100vw, 575px" /></p><h2 class="simpletoc-title">Table of Contents</h2>
<ul class="simpletoc-list">
<li><a href="#introduction">Introduction</a>

</li>
<li><a href="#the-if-statement">The &#8216;If&#8217; Statement</a>


<ul><li>
<a href="#syntax">Syntax</a>

</li>
<li><a href="#basic-if-statement">Basic If Statement</a>

</li>
<li><a href="#ifelse-statement">If-else Statement</a>

</li>
<li><a href="#nested-if-statement">Nested If Statement</a>

</li>
</ul>
<li><a href="#the-switch-statement">The Switch Statement</a>


<ul><li>
<a href="#syntax">Syntax</a>

</li>
<li><a href="#example">Example</a>

</li>
</ul>
<li><a href="#conclusion">Conclusion</a>
</li></ul>

<h2 class="wp-block-heading" id="introduction">Introduction</h2>


<p>Control statements are used to change the flow of execution based on changes to certain variables in the code. One of the types of control statements are the selection statements or decision statements. These should be used when you want to check some condition and take a decision based on the condition. Java supports the if and switch statement decision statements</p>


<h2 class="wp-block-heading" id="the-if-statement">The &#8216;If&#8217; Statement</h2>


<p>The <strong>if </strong>statement is the most widely used control statement. It is used to check a condition. If the condition is true, it executes some code. You can also have an optional else part that executes if the condition is false. An if statement has the following syntax:</p>


<div class="su-note"  style="border-color:#dcdcdc;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#f6f6f6;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">
<h3 class="wp-block-heading" id="syntax">Syntax</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">if(condition){
  //some code here
}
else { // this part is optional
    //some code here
}</pre>
<p>So, the <strong>if</strong> keyword is followed by a condition that evaluates to a <strong>boolean</strong> value. If the boolean value is <strong>true</strong>, the code within the if block is executed. If the boolean value is <strong>false</strong>, the code within the else block is executed. The else block is optional.</p>
</div></div>



<div style="height:5px" aria-hidden="true" class="wp-block-spacer"></div>


<h3 class="wp-block-heading" id="basic-if-statement">Basic If Statement</h3>


<p>The <strong>if </strong>statement can be used by itself, without having an else statement. In such a scenario, if the condition specified within the <strong>if</strong> statement is true, the code within the <strong>if</strong> block is executed after which control transfers outside the if block. If the condition in the <strong>if</strong> statement is false, the <strong>if</strong> block is not entered and control directly transfers outside the if block. The following code demonstrates this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">System.out.println("Enter a Value:");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
if(i > 5) {
System.out.println("Value is greater than 5");
}
System.out.println("Outside if");
scanner.close();</pre>



<p>Here, the <strong>if</strong> statement checks if the value entered by the user is greater than 5. If so, it executes a Sysout statement. &nbsp;If the value entered by the user is less than 5, the <strong>if</strong> block is not entered. So, if the user enters the value 10, this code prints the following output:</p>



<pre class="wp-block-code"><code>Enter a Value:
10
Value is greater than 5
Outside if
If the user enters the value 2, this code prints the following output:
Enter a Value:
2
Outside if
</code></pre>



<div style="height:5px" aria-hidden="true" class="wp-block-spacer"></div>


<h3 class="wp-block-heading" id="ifelse-statement">If-else Statement</h3>


<p>The <strong>else</strong> statement can also be used with the <strong>if</strong> statement. If the condition within the <strong>if</strong> statement is <strong>false</strong>, the code within the <strong>else</strong> block gets executed. The following code demonstrates this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">System.out.println("Enter a Value:");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
if(i > 5) {
System.out.println("Value is greater than 5");
}
else {
	System.out.println("Value is less than 5");
}
scanner.close();</pre>



<p>Here, the <strong>if</strong> statement checks if the value entered by the user is greater than 5. If so, it executes a Sysout statement, otherwise, it executes a different Sysout statement. So, if the user enters the value 10, this code prints the following output:</p>



<pre class="wp-block-code"><code>Enter a Value:
10
Value is greater than 5
If the user enters the value 2, this code prints the following output:
Enter a Value:
2
Value is less than 5
</code></pre>



<div style="height:5px" aria-hidden="true" class="wp-block-spacer"></div>


<h3 class="wp-block-heading" id="nested-if-statement">Nested If Statement</h3>


<p>A <strong>nested if </strong>is simply an <strong>if</strong> statement within another <strong>if</strong> statement. Only if the outer if statement is true, the condition within the inner if statement is checked. Each if statement can have an optional else statement. The following code demonstrates this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">System.out.println("Enter a Value:");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
if(i >= 5) {
if(i &lt; 10) {
		System.out.println("Value is between 5 to 10");
	}
}
else {
	System.out.println("Value is less than 5");
}
scanner.close();</pre>



<p>Here, the <strong>if</strong> statement checks if the value entered by the user is greater than or equal to 5. If so, there is another <strong>if</strong> statement within this <strong>if</strong> statement that checks if the value is less than 10. The inner <strong>if</strong> does not have an else statement while the outer <strong>if</strong> has an else statement. So, if the user enters the value 6, this code prints the following output:</p>



<pre class="wp-block-code"><code>Enter a Value:
6
Value is between 5 to 10
If the user enters the value 2, this code prints the following output:
Enter a Value:
2
Value is less than 5
</code></pre>



<div style="height:10px" aria-hidden="true" class="wp-block-spacer"></div>


<h2 class="wp-block-heading" id="the-switch-statement">The Switch Statement</h2>


<p>Sometimes, you will need to write code that checks for several conditions. In such a scenario using multiple if-else statements makes the code difficult to read. Java provides the switch statement as an alternative. It checks a value and executes one of several code blocks based on the value. A switch statement has the following syntax:</p>


<div class="su-note"  style="border-color:#dcdcdc;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#f6f6f6;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">
<h3 class="wp-block-heading" id="syntax">Syntax</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">switch(expression) {
		case value1: 
			//some code
			break; //optional
		case value2: 
			//some code
			break; //optional
		…..
case valuen: 
			//some code
			break; //optional

		default: 
			//some code
			break; //optional
		}
	}
</pre>
<p>The <strong>switch</strong> keyword is followed by an expression. This expression is compared with the value specified with every <strong>case</strong> statement. As soon as a match is found, the code following that <strong>case</strong> statement is executed. If there is a <strong>break</strong> statement in the <strong>case</strong> statement, no further <strong>case</strong> statements are checked and control transfers outside the <strong>switch</strong> statement. In case the expression does not match any <strong>case</strong> statement, the code following the <strong>default</strong> keyword is executed.</p>
</div></div>



<div style="height:10px" aria-hidden="true" class="wp-block-spacer"></div>


<div class="su-note"  style="border-color:#dcdcdc;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"><div class="su-note-inner su-u-clearfix su-u-trim" style="background-color:#f6f6f6;border-color:#ffffff;color:#444444;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;">
<h3 class="wp-block-heading" id="example">Example</h3>
<p>The following code demonstrates a basic <strong>switch</strong> block:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="java" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">System.out.println("Enter a Value:");
Scanner scanner = new Scanner(System.in);
int month = scanner.nextInt();
switch(month) {
case 1: 
	System.out.println("January");
	break;
case 2: 
	System.out.println("February");
	break;
case 3: 
	System.out.println("March");
	break;
//code for other months here
default: 
	System.out.println("Invalid month");
	break;
}
scanner.close();
</pre>
</div></div>



<pre class="wp-block-code"><code>Enter a Value:
2
February
If the user enters a value greater than 12, this code prints the following output:
Enter a Value:
14
Invalid month
</code></pre>


<h2 class="wp-block-heading" id="conclusion">Conclusion</h2>


<p>Java decision statements help to execute code based on some conditions. Java has two main decision statements, if and switch. The if statement can have an optional else part. The switch statement can be used when you want to check for multiple values.</p>
<div class='the_champ_sharing_container the_champ_vertical_sharing the_champ_hide_sharing the_champ_bottom_sharing' style='width:42px;left: -10px;top: 222px;-webkit-box-shadow:none;box-shadow:none;' data-super-socializer-href="https://www.automationdojos.com/tag/java-tutorials/feed/"><div class="the_champ_sharing_ul"><a aria-label="Facebook" class="the_champ_facebook" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D20707" title="Facebook" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#0765FE;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path fill="#484848" d="M28 16c0-6.627-5.373-12-12-12S4 9.373 4 16c0 5.628 3.875 10.35 9.101 11.647v-7.98h-2.474V16H13.1v-1.58c0-4.085 1.849-5.978 5.859-5.978.76 0 2.072.15 2.608.298v3.325c-.283-.03-.775-.045-1.386-.045-1.967 0-2.728.745-2.728 2.683V16h3.92l-.673 3.667h-3.247v8.245C23.395 27.195 28 22.135 28 16Z"></path></svg></span></a><a aria-label="Twitter" class="the_champ_button_twitter" href="https://twitter.com/intent/tweet?via=@testoxide&text=java-tutorials&url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D20707" title="Twitter" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_twitter" style="background-color:#55acee;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-4 -4 39 39"><path d="M28 8.557a9.913 9.913 0 0 1-2.828.775 4.93 4.93 0 0 0 2.166-2.725 9.738 9.738 0 0 1-3.13 1.194 4.92 4.92 0 0 0-3.593-1.55 4.924 4.924 0 0 0-4.794 6.049c-4.09-.21-7.72-2.17-10.15-5.15a4.942 4.942 0 0 0-.665 2.477c0 1.71.87 3.214 2.19 4.1a4.968 4.968 0 0 1-2.23-.616v.06c0 2.39 1.7 4.38 3.952 4.83-.414.115-.85.174-1.297.174-.318 0-.626-.03-.928-.086a4.935 4.935 0 0 0 4.6 3.42 9.893 9.893 0 0 1-6.114 2.107c-.398 0-.79-.023-1.175-.068a13.953 13.953 0 0 0 7.55 2.213c9.056 0 14.01-7.507 14.01-14.013 0-.213-.005-.426-.015-.637.96-.695 1.795-1.56 2.455-2.55z" fill="#484848"></path></svg></span></a><a aria-label="Linkedin" class="the_champ_button_linkedin" href="https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D20707" title="Linkedin" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_linkedin" style="background-color:#0077b5;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path d="M6.227 12.61h4.19v13.48h-4.19V12.61zm2.095-6.7a2.43 2.43 0 0 1 0 4.86c-1.344 0-2.428-1.09-2.428-2.43s1.084-2.43 2.428-2.43m4.72 6.7h4.02v1.84h.058c.56-1.058 1.927-2.176 3.965-2.176 4.238 0 5.02 2.792 5.02 6.42v7.395h-4.183v-6.56c0-1.564-.03-3.574-2.178-3.574-2.18 0-2.514 1.7-2.514 3.46v6.668h-4.187V12.61z" fill="#484848"></path></svg></span></a><a aria-label="Pinterest" class="the_champ_button_pinterest" href="https://www.automationdojos.com/tag/java-tutorials/feed/" onclick="event.preventDefault();javascript:void((function(){var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','//assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());" title="Pinterest" rel="noopener" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_pinterest" style="background-color:#cc2329;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-2 -2 35 35"><path fill="#484848" d="M16.539 4.5c-6.277 0-9.442 4.5-9.442 8.253 0 2.272.86 4.293 2.705 5.046.303.125.574.005.662-.33.061-.231.205-.816.27-1.06.088-.331.053-.447-.191-.736-.532-.627-.873-1.439-.873-2.591 0-3.338 2.498-6.327 6.505-6.327 3.548 0 5.497 2.168 5.497 5.062 0 3.81-1.686 7.025-4.188 7.025-1.382 0-2.416-1.142-2.085-2.545.397-1.674 1.166-3.48 1.166-4.689 0-1.081-.581-1.983-1.782-1.983-1.413 0-2.548 1.462-2.548 3.419 0 1.247.421 2.091.421 2.091l-1.699 7.199c-.505 2.137-.076 4.755-.039 5.019.021.158.223.196.314.077.13-.17 1.813-2.247 2.384-4.324.162-.587.929-3.631.929-3.631.46.876 1.801 1.646 3.227 1.646 4.247 0 7.128-3.871 7.128-9.053.003-3.918-3.317-7.568-8.361-7.568z"/></svg></span></a><a aria-label="Reddit" class="the_champ_button_reddit" href="https://reddit.com/submit?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D20707&title=java-tutorials" title="Reddit" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_reddit" style="background-color:#ff5700;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-3.5 -3.5 39 39"><path d="M28.543 15.774a2.953 2.953 0 0 0-2.951-2.949 2.882 2.882 0 0 0-1.9.713 14.075 14.075 0 0 0-6.85-2.044l1.38-4.349 3.768.884a2.452 2.452 0 1 0 .24-1.176l-4.274-1a.6.6 0 0 0-.709.4l-1.659 5.224a14.314 14.314 0 0 0-7.316 2.029 2.908 2.908 0 0 0-1.872-.681 2.942 2.942 0 0 0-1.618 5.4 5.109 5.109 0 0 0-.062.765c0 4.158 5.037 7.541 11.229 7.541s11.22-3.383 11.22-7.541a5.2 5.2 0 0 0-.053-.706 2.963 2.963 0 0 0 1.427-2.51zm-18.008 1.88a1.753 1.753 0 0 1 1.73-1.74 1.73 1.73 0 0 1 1.709 1.74 1.709 1.709 0 0 1-1.709 1.711 1.733 1.733 0 0 1-1.73-1.711zm9.565 4.968a5.573 5.573 0 0 1-4.081 1.272h-.032a5.576 5.576 0 0 1-4.087-1.272.6.6 0 0 1 .844-.854 4.5 4.5 0 0 0 3.238.927h.032a4.5 4.5 0 0 0 3.237-.927.6.6 0 1 1 .844.854zm-.331-3.256a1.726 1.726 0 1 1 1.709-1.712 1.717 1.717 0 0 1-1.712 1.712z" fill="#484848"/></svg></span></a><a aria-label="Whatsapp" class="the_champ_whatsapp" href="https://api.whatsapp.com/send?text=java-tutorials https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D20707" title="Whatsapp" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#55eb4c;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-6 -5 40 40"><path class="the_champ_svg_stroke the_champ_no_fill" stroke="#484848" stroke-width="2" fill="none" d="M 11.579798566743314 24.396926207859085 A 10 10 0 1 0 6.808479557110079 20.73576436351046"></path><path d="M 7 19 l -1 6 l 6 -1" class="the_champ_no_fill the_champ_svg_stroke" stroke="#484848" stroke-width="2" fill="none"></path><path d="M 10 10 q -1 8 8 11 c 5 -1 0 -6 -1 -3 q -4 -3 -5 -5 c 4 -2 -1 -5 -1 -4" fill="#484848"></path></svg></span></a><a class="the_champ_more" title="More" rel="nofollow noopener" style="font-size:24px!important;border:0;box-shadow:none;display:inline-block!important;font-size:16px;padding:0 4px;vertical-align: middle;display:inline;" href="https://www.automationdojos.com/?p=20707" onclick="event.preventDefault()"><span class="the_champ_svg" style="background-color:#ee8e2d;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block!important;opacity:1;float:left;font-size:32px!important;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;display:inline;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box;" onclick="theChampMoreSharingPopup(this, 'https://www.automationdojos.com/?p=20707', 'java-tutorials', '' )"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-.3 0 32 32" version="1.1" width="100%" height="100%" style="display:block;" xml:space="preserve"><g><path fill="#484848" d="M18 14V8h-4v6H8v4h6v6h4v-6h6v-4h-6z" fill-rule="evenodd"></path></g></svg></span></a></div></div><p>The post <a href="https://www.automationdojos.com/java-control-statements/">Java Tutorial #4 – Control Statements</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.automationdojos.com/java-control-statements/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Java Tutorial #3 – Java Arrays</title>
		<link>https://www.automationdojos.com/java-tutorial-java-arrays/</link>
					<comments>https://www.automationdojos.com/java-tutorial-java-arrays/#respond</comments>
		
		<dc:creator><![CDATA[Tushar Sharma]]></dc:creator>
		<pubDate>Thu, 21 Mar 2019 15:40:22 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Tutorials]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[core-java]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java-operators]]></category>
		<category><![CDATA[java-tutorials]]></category>
		<guid isPermaLink="false">https://www.automationdojos.com/?p=13365</guid>

					<description><![CDATA[<p><a href="https://www.automationdojos.com/java-tutorial-java-arrays/">Java Tutorial #3 – Java Arrays</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="280" height="168" src="https://www.automationdojos.com/wp-content/uploads/2020/05/post_JAVA-Tutorial3-1-280x168.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2020/05/post_JAVA-Tutorial3-1-280x168.png 280w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_JAVA-Tutorial3-1-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_JAVA-Tutorial3-1-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_JAVA-Tutorial3-1-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_JAVA-Tutorial3-1-400x240.png 400w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_JAVA-Tutorial3-1.png 575w" sizes="auto, (max-width: 280px) 100vw, 280px" /></p>
<p>Table of Contents One Dimensional Array Declaring Array Allocting Memory to Array Accessing Array Elements Initializing Array Length of Array Multi Dimensional Array Creating 3 Dimensional Array Introduction Arrays are used to store a group of values of the same data type. So a single variable can be used to refer to the group of [&#8230;]</p>
<p>The post <a href="https://www.automationdojos.com/java-tutorial-java-arrays/">Java Tutorial #3 – Java Arrays</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://www.automationdojos.com/java-tutorial-java-arrays/">Java Tutorial #3 – Java Arrays</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="280" height="168" src="https://www.automationdojos.com/wp-content/uploads/2020/05/post_JAVA-Tutorial3-1-280x168.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2020/05/post_JAVA-Tutorial3-1-280x168.png 280w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_JAVA-Tutorial3-1-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_JAVA-Tutorial3-1-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_JAVA-Tutorial3-1-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_JAVA-Tutorial3-1-400x240.png 400w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_JAVA-Tutorial3-1.png 575w" sizes="auto, (max-width: 280px) 100vw, 280px" /></p>
<h2 class="simpletoc-title">Table of Contents</h2>
<ul class="simpletoc">
<li> <a href="#one-dimension">One Dimensional Array</a>
<ul>
<li><a href="#declaring-array">Declaring Array</a></li>
<li> <a href="#allocating-memory">Allocting Memory to Array</a></li>
<li> <a href="#accessing-element">Accessing Array Elements</a></li>
<li> <a href="#initialize-array">Initializing Array</a></li>
<li> <a href="#length-array">Length of Array</a></li>
</ul>
</li>
<li> <a href="#multi-dimension">Multi Dimensional Array</a></li>
<li> <a href="#creating-using-3dimension">Creating 3 Dimensional Array</a></li>
</ul>


<h3>Introduction</h3>
<p>Arrays are used to store a group of values of the <strong><em>same data type</em></strong>. So a single variable can be used to refer to the group of values. Arrays can store data of <strong><em>any data type</em></strong> and can have one or more dimensions as explained below.</p>
<h3>1. One-Dimensional Array</h3>
<p>A one-dimensional array is essentially a list of values referred to by a common name.</p>
<div class="su-box su-box-style-default" id="" style="border-color:#bfbfbf;border-radius:3px;"><div class="su-box-title" style="background-color:#f2f2f2;color:#6b6b6b;border-top-left-radius:1px;border-top-right-radius:1px">(a) Declaring an array</div><div class="su-box-content su-u-clearfix su-u-trim" style="border-bottom-left-radius:1px;border-bottom-right-radius:1px">
<p>In order to create an array, you first need to declare a variable of the desired type corresponding to the array. The following code demonstrates this:</p>
<pre class="lang:java decode:true ">int myArray[]; // creates myArray to store int values</pre>
<p>Notice that the name of the array is followed by square brackets. This indicates that we are trying to create an array. Since we have declared myArray to be of type <strong>int</strong>, each element in the array can only be of <strong>int</strong> data type.</p>
</div></div>
<div class="su-spacer" style="height:20px"></div>
<div class="su-box su-box-style-default" id="" style="border-color:#bfbfbf;border-radius:3px;"><div class="su-box-title" style="background-color:#f2f2f2;color:#6b6b6b;border-top-left-radius:1px;border-top-right-radius:1px">(b) Allocating memory to the array</div><div class="su-box-content su-u-clearfix su-u-trim" style="border-bottom-left-radius:1px;border-bottom-right-radius:1px">
<p>Although the code snippet above declares an array, you need to allocate memory to it using the <strong>new</strong> keyword as follows:</p>
<pre class="lang:java decode:true ">myArray = new int[10]; // creates array of size 10</pre>
<p>Here, the keyword <strong>new</strong> is used to allocate memory. This is a special keyword that is used to allocate memory. This will be covered in detail when we take a closer look at classes.  Notice that the size (10 in this case) needs to be specified within square brackets as well. This indicates that the array is capable of storing 10 values.</p>
<p>The declaration and memory allocation can also be combined into a single statement as follows:</p>
<pre class="lang:java decode:true ">int myArray[] = new int[10]; //creates myArray with size 10</pre>
</div></div>
<div class="su-spacer" style="height:20px"></div>
<div class="su-box su-box-style-default" id="" style="border-color:#bfbfbf;border-radius:3px;"><div class="su-box-title" style="background-color:#f2f2f2;color:#6b6b6b;border-top-left-radius:1px;border-top-right-radius:1px">(c) Accessing an element in an array</div><div class="su-box-content su-u-clearfix su-u-trim" style="border-bottom-left-radius:1px;border-bottom-right-radius:1px">
<p>Each <strong>value</strong> within the array is called as an <strong>element</strong> in the array. An element in an array can be accessed via its <strong>index</strong>. Index is simply an integer that specifies a position in the array. The index needs to be specified within square brackets too. Array index begins at 0, so the 1<sup>st</sup> element is at position 0, the 2<sup>nd</sup> at position 1 and so on.</p>
<p>Consider the following code snippet that assigns a value to a particular element:</p>
<pre class="lang:java decode:true ">myArray[3] = 5; // element at index 3, i.e.4th element will be assigned 5</pre>
<p>Similarly, you can also access an element at a particular position using its index:</p>
<pre class="lang:java decode:true ">int value = myArray[2]; 
// value at index 2 i.e. 3rd position is assigned to the variable value</pre>
</div></div>
<div class="su-spacer" style="height:20px"></div>
<div class="su-box su-box-style-default" id="" style="border-color:#bfbfbf;border-radius:3px;"><div class="su-box-title" style="background-color:#f2f2f2;color:#6b6b6b;border-top-left-radius:1px;border-top-right-radius:1px">(d) Initializing an array</div><div class="su-box-content su-u-clearfix su-u-trim" style="border-bottom-left-radius:1px;border-bottom-right-radius:1px">
<p>Arrays can also be initialized with some values when they are declared. You need to specify the values to be stored in the array within curly braces as a list of comma separated values. The following code demonstrates this:</p>
<pre class="lang:java decode:true ">int myArray[] = {2,4,6,8,10}; 
//creates new array of size 5 with specified values</pre>
<p>Note that here the size is not specified in the square brackets explicitly; it is the same as the number of elements in the curly brackets.</p>
</div></div>
<div class="su-spacer" style="height:20px"></div>
<div class="su-box su-box-style-default" id="" style="border-color:#bfbfbf;border-radius:3px;"><div class="su-box-title" style="background-color:#f2f2f2;color:#6b6b6b;border-top-left-radius:1px;border-top-right-radius:1px">(e) Obtaining the length of an array</div><div class="su-box-content su-u-clearfix su-u-trim" style="border-bottom-left-radius:1px;border-bottom-right-radius:1px">
<p>There is a <strong>length</strong> property provided on the array object. This can be used to determine the number of elements in the array.  The following code demonstrates this:</p>
<pre class="lang:java decode:true ">int myArray[] = {2,4,6,8,10};
int size = myArray.length; // size will be 5
</pre>
<p>Since there are 5 values in the array myArray, size will be assigned the value 5.</p>
</div></div>
<div class="su-spacer" style="height:20px"></div>
<h3>2. Multi-Dimensional Arrays</h3>
<p>Multidimensional arrays are basically arrays of arrays. They are used to store data in tabular form. Each additional index needs to be specified using additional square brackets.</p>
<div class="su-box su-box-style-default" id="" style="border-color:#bfbfbf;border-radius:3px;"><div class="su-box-title" style="background-color:#f2f2f2;color:#6b6b6b;border-top-left-radius:1px;border-top-right-radius:1px">(a) Creating a Two-dimensional array</div><div class="su-box-content su-u-clearfix su-u-trim" style="border-bottom-left-radius:1px;border-bottom-right-radius:1px">
<p>The following code creates a two-dimensional array:</p>
<pre class="lang:java decode:true ">int myArray[][] = new int[4][6];</pre>
<p>This code creates a new two-dimensional array with the dimensions as 4 and 6. So basically, myArray is an array having 4 elements. Each element in the array is an array of 6 elements.</p>
<p>So it will store data as follows:<br />{(2,4,6,8,10,12),  // 4 arrays, each having 6 elements<br />(3,6,9,12,15,18),<br />(4,8,12,16,20,24),<br />(5,10,15,20,25,30)}</p>
</div></div>
<div class="su-spacer" style="height:20px"></div>
<div class="su-box su-box-style-default" id="" style="border-color:#bfbfbf;border-radius:3px;"><div class="su-box-title" style="background-color:#f2f2f2;color:#6b6b6b;border-top-left-radius:1px;border-top-right-radius:1px">(b) Accessing elements in a Two-dimensional array</div><div class="su-box-content su-u-clearfix su-u-trim" style="border-bottom-left-radius:1px;border-bottom-right-radius:1px">
<p>In order to access an element in a multi-dimensional array, you need to specify the position for each dimension as follows:</p>
<pre class="lang:java decode:true ">int val = myArray[2][4]; 
// a will be assigned the element at row index 2 and column index 4
</pre>
<p>Just like one dimensional arrays, array index begins at 0 even for multi-dimensional arrays. So if we consider the data above, the value 20 will be assigned to the variable val</p>
</div></div>
<div class="su-spacer" style="height:10px"></div>
<h3>3. Creating and Using a 3-Dimensional Array</h3>
<p>Similarly, you can create a 3-dimensional array as follows:</p>
<pre class="lang:java decode:true ">int myArray[][][] = new int [2][3][4];
myArray[1][0][3] = 5;</pre>
<p>This code declares a 3-dimensional array with 2,3,4 as the dimensions. It sets the element at position 1,0,3 to 5.</p>
<div class="su-spacer" style="height:10px"></div>
<h3>Conclusion</h3>
<p>So this article covers what arrays are and how you can use them. It also walks you through multi-dimensional arrays.</p>
<div class="su-spacer" style="height:10px"></div><div class='the_champ_sharing_container the_champ_vertical_sharing the_champ_hide_sharing the_champ_bottom_sharing' style='width:42px;left: -10px;top: 222px;-webkit-box-shadow:none;box-shadow:none;' data-super-socializer-href="https://www.automationdojos.com/tag/java-tutorials/feed/"><div class="the_champ_sharing_ul"><a aria-label="Facebook" class="the_champ_facebook" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13365" title="Facebook" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#0765FE;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path fill="#484848" d="M28 16c0-6.627-5.373-12-12-12S4 9.373 4 16c0 5.628 3.875 10.35 9.101 11.647v-7.98h-2.474V16H13.1v-1.58c0-4.085 1.849-5.978 5.859-5.978.76 0 2.072.15 2.608.298v3.325c-.283-.03-.775-.045-1.386-.045-1.967 0-2.728.745-2.728 2.683V16h3.92l-.673 3.667h-3.247v8.245C23.395 27.195 28 22.135 28 16Z"></path></svg></span></a><a aria-label="Twitter" class="the_champ_button_twitter" href="https://twitter.com/intent/tweet?via=@testoxide&text=java-tutorials&url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13365" title="Twitter" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_twitter" style="background-color:#55acee;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-4 -4 39 39"><path d="M28 8.557a9.913 9.913 0 0 1-2.828.775 4.93 4.93 0 0 0 2.166-2.725 9.738 9.738 0 0 1-3.13 1.194 4.92 4.92 0 0 0-3.593-1.55 4.924 4.924 0 0 0-4.794 6.049c-4.09-.21-7.72-2.17-10.15-5.15a4.942 4.942 0 0 0-.665 2.477c0 1.71.87 3.214 2.19 4.1a4.968 4.968 0 0 1-2.23-.616v.06c0 2.39 1.7 4.38 3.952 4.83-.414.115-.85.174-1.297.174-.318 0-.626-.03-.928-.086a4.935 4.935 0 0 0 4.6 3.42 9.893 9.893 0 0 1-6.114 2.107c-.398 0-.79-.023-1.175-.068a13.953 13.953 0 0 0 7.55 2.213c9.056 0 14.01-7.507 14.01-14.013 0-.213-.005-.426-.015-.637.96-.695 1.795-1.56 2.455-2.55z" fill="#484848"></path></svg></span></a><a aria-label="Linkedin" class="the_champ_button_linkedin" href="https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13365" title="Linkedin" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_linkedin" style="background-color:#0077b5;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path d="M6.227 12.61h4.19v13.48h-4.19V12.61zm2.095-6.7a2.43 2.43 0 0 1 0 4.86c-1.344 0-2.428-1.09-2.428-2.43s1.084-2.43 2.428-2.43m4.72 6.7h4.02v1.84h.058c.56-1.058 1.927-2.176 3.965-2.176 4.238 0 5.02 2.792 5.02 6.42v7.395h-4.183v-6.56c0-1.564-.03-3.574-2.178-3.574-2.18 0-2.514 1.7-2.514 3.46v6.668h-4.187V12.61z" fill="#484848"></path></svg></span></a><a aria-label="Pinterest" class="the_champ_button_pinterest" href="https://www.automationdojos.com/tag/java-tutorials/feed/" onclick="event.preventDefault();javascript:void((function(){var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','//assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());" title="Pinterest" rel="noopener" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_pinterest" style="background-color:#cc2329;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-2 -2 35 35"><path fill="#484848" d="M16.539 4.5c-6.277 0-9.442 4.5-9.442 8.253 0 2.272.86 4.293 2.705 5.046.303.125.574.005.662-.33.061-.231.205-.816.27-1.06.088-.331.053-.447-.191-.736-.532-.627-.873-1.439-.873-2.591 0-3.338 2.498-6.327 6.505-6.327 3.548 0 5.497 2.168 5.497 5.062 0 3.81-1.686 7.025-4.188 7.025-1.382 0-2.416-1.142-2.085-2.545.397-1.674 1.166-3.48 1.166-4.689 0-1.081-.581-1.983-1.782-1.983-1.413 0-2.548 1.462-2.548 3.419 0 1.247.421 2.091.421 2.091l-1.699 7.199c-.505 2.137-.076 4.755-.039 5.019.021.158.223.196.314.077.13-.17 1.813-2.247 2.384-4.324.162-.587.929-3.631.929-3.631.46.876 1.801 1.646 3.227 1.646 4.247 0 7.128-3.871 7.128-9.053.003-3.918-3.317-7.568-8.361-7.568z"/></svg></span></a><a aria-label="Reddit" class="the_champ_button_reddit" href="https://reddit.com/submit?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13365&title=java-tutorials" title="Reddit" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_reddit" style="background-color:#ff5700;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-3.5 -3.5 39 39"><path d="M28.543 15.774a2.953 2.953 0 0 0-2.951-2.949 2.882 2.882 0 0 0-1.9.713 14.075 14.075 0 0 0-6.85-2.044l1.38-4.349 3.768.884a2.452 2.452 0 1 0 .24-1.176l-4.274-1a.6.6 0 0 0-.709.4l-1.659 5.224a14.314 14.314 0 0 0-7.316 2.029 2.908 2.908 0 0 0-1.872-.681 2.942 2.942 0 0 0-1.618 5.4 5.109 5.109 0 0 0-.062.765c0 4.158 5.037 7.541 11.229 7.541s11.22-3.383 11.22-7.541a5.2 5.2 0 0 0-.053-.706 2.963 2.963 0 0 0 1.427-2.51zm-18.008 1.88a1.753 1.753 0 0 1 1.73-1.74 1.73 1.73 0 0 1 1.709 1.74 1.709 1.709 0 0 1-1.709 1.711 1.733 1.733 0 0 1-1.73-1.711zm9.565 4.968a5.573 5.573 0 0 1-4.081 1.272h-.032a5.576 5.576 0 0 1-4.087-1.272.6.6 0 0 1 .844-.854 4.5 4.5 0 0 0 3.238.927h.032a4.5 4.5 0 0 0 3.237-.927.6.6 0 1 1 .844.854zm-.331-3.256a1.726 1.726 0 1 1 1.709-1.712 1.717 1.717 0 0 1-1.712 1.712z" fill="#484848"/></svg></span></a><a aria-label="Whatsapp" class="the_champ_whatsapp" href="https://api.whatsapp.com/send?text=java-tutorials https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13365" title="Whatsapp" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#55eb4c;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-6 -5 40 40"><path class="the_champ_svg_stroke the_champ_no_fill" stroke="#484848" stroke-width="2" fill="none" d="M 11.579798566743314 24.396926207859085 A 10 10 0 1 0 6.808479557110079 20.73576436351046"></path><path d="M 7 19 l -1 6 l 6 -1" class="the_champ_no_fill the_champ_svg_stroke" stroke="#484848" stroke-width="2" fill="none"></path><path d="M 10 10 q -1 8 8 11 c 5 -1 0 -6 -1 -3 q -4 -3 -5 -5 c 4 -2 -1 -5 -1 -4" fill="#484848"></path></svg></span></a><a class="the_champ_more" title="More" rel="nofollow noopener" style="font-size:24px!important;border:0;box-shadow:none;display:inline-block!important;font-size:16px;padding:0 4px;vertical-align: middle;display:inline;" href="https://www.automationdojos.com/?p=13365" onclick="event.preventDefault()"><span class="the_champ_svg" style="background-color:#ee8e2d;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block!important;opacity:1;float:left;font-size:32px!important;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;display:inline;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box;" onclick="theChampMoreSharingPopup(this, 'https://www.automationdojos.com/?p=13365', 'java-tutorials', '' )"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-.3 0 32 32" version="1.1" width="100%" height="100%" style="display:block;" xml:space="preserve"><g><path fill="#484848" d="M18 14V8h-4v6H8v4h6v6h4v-6h6v-4h-6z" fill-rule="evenodd"></path></g></svg></span></a></div></div><p>The post <a href="https://www.automationdojos.com/java-tutorial-java-arrays/">Java Tutorial #3 – Java Arrays</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.automationdojos.com/java-tutorial-java-arrays/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Java Tutorial #2 – Operators in Java</title>
		<link>https://www.automationdojos.com/operators-in-java/</link>
					<comments>https://www.automationdojos.com/operators-in-java/#respond</comments>
		
		<dc:creator><![CDATA[Tushar Sharma]]></dc:creator>
		<pubDate>Sat, 26 Jan 2019 01:36:14 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Tutorials]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[core-java]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java-operators]]></category>
		<category><![CDATA[java-tutorials]]></category>
		<guid isPermaLink="false">https://www.automationdojos.com/?p=13331</guid>

					<description><![CDATA[<p><a href="https://www.automationdojos.com/operators-in-java/">Java Tutorial #2 – Operators in Java</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="280" height="168" src="https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-02-280x168.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-02-280x168.png 280w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-02-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-02-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-02-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-02-400x240.png 400w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-02.png 575w" sizes="auto, (max-width: 280px) 100vw, 280px" /></p>
<p>Table of Contents Introduction 1. Assignment Operator 2. Arithmetic Operators 3. Compound Operators 4. Increment &#038; Decrement Operators 5. Relational Operators 6. Logical Operators Introduction Operators are an essential part of any programming language. In-fact it is not possible not to write a decent piece of code without making use of &#8216;Operators&#8217;. Java operators can [&#8230;]</p>
<p>The post <a href="https://www.automationdojos.com/operators-in-java/">Java Tutorial #2 – Operators in Java</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://www.automationdojos.com/operators-in-java/">Java Tutorial #2 – Operators in Java</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="280" height="168" src="https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-02-280x168.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-02-280x168.png 280w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-02-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-02-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-02-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-02-400x240.png 400w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-02.png 575w" sizes="auto, (max-width: 280px) 100vw, 280px" /></p>
<h2 class="simpletoc-title">Table of Contents</h2>
<ul class="simpletoc">
<li> <a href="#introduction">Introduction</a></li>
<li> <a href="#assignment-operator">1. Assignment Operator</a></li>
<li> <a href="#arithmetic-operators">2. Arithmetic Operators</a></li>
<li> <a href="#compund-operators">3. Compound Operators</a></li>
<li><a href="#increment-and-decrement-operators">4. Increment &#038; Decrement Operators</a></li>
<li><a href="#relational-operators">5. Relational Operators</a></li>
<li><a href="#logical-operators">6. Logical Operators</a></li>
</ul>


<h2>Introduction</h2>
<p>Operators are an essential part of any programming language. In-fact it is not possible not to write a decent piece of code without making use of &#8216;Operators&#8217;. Java operators can be categorized mainly into <strong>arithmetic</strong>, <strong>relational</strong> and <strong>logical operators</strong>. In addition, there is a special assignment operator that is used to assign values to variables. Each of these is covered in a separate section below.</p>
<h2>1. Assignment Operator</h2>
<p>The <strong>“=”</strong> sign is known as the assignment operator.  It assigns whatever is to its right to the variable on its left. The variable type must be compatible with the value being assigned. The following code demonstrates this:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="java">int a = 10; //assigns the value 10 to the variable a

int b = 5+8; // assigns the value 13 to the variable b</pre>
<h2>2. Arithmetic Operators</h2>
<p>Arithmetic operators are those that are used in ordinary mathematics, that is addition, subtraction, multiplication and division. These can only be applied to numerical operands. The Arithmetic operators can be further broken down into <em>Basic Operators, Compound Assignment Operators and Increment/Decrement operators</em>.</p>
<div class="su-table su-table-alternate">
<table width="642">
<tbody>
<tr>
<td width="70"><strong>Symbol</strong></td>
<td width="119"><strong>Name</strong></td>
<td width="258"><strong>Description</strong></td>
<td width="196"><strong>Example</strong></td>
</tr>
<tr>
<td width="70">+</td>
<td width="119">Addition</td>
<td width="258">Adds the value on the left to the value on the right</td>
<td width="196">int a=30, b=20;
<p> </p>
<p>System.out.println(a+b); //prints 50</p>
</td>
</tr>
<tr>
<td width="70">&#8211;</td>
<td width="119">Subtraction</td>
<td width="258">Subtracts value on the right from the value on the left</td>
<td width="196">int a=30, b=20;
<p> </p>
<p>System.out.println(a-b);</p>
<p>//prints 10</p>
</td>
</tr>
<tr>
<td width="70">*</td>
<td width="119">Multiplication</td>
<td width="258">Multiplies the value on the left to the value on the right</td>
<td width="196">int a=30, b=2;
<p> </p>
<p>System.out.println(a*b);</p>
<p>//prints 60</p>
</td>
</tr>
<tr>
<td width="70">/</td>
<td width="119">Division</td>
<td width="258">Divides the value on the left with the value on the Right</td>
<td width="196">int a=30, b=10;
<p> </p>
<p>System.out.println(a/b);</p>
<p>//prints 3</p>
</td>
</tr>
<tr>
<td width="70">%</td>
<td width="119">Modulus</td>
<td width="258">Returns the remainder of dividing the value on the left with the value on the right</td>
<td width="196">int a=35, b=10;
<p> </p>
<p>System.out.println(a%b);</p>
<p>//prints 5</p>
</td>
</tr>
</tbody>
</table>
</div>
<h2>3. Compound Assignment</h2>
<p>Java provides special operators which can be used to combine an operation with an assignment.  These are known as compound assignment operators. The compound operators are just shortcut operators which can be used when you want to change the value of a particular variable. Java supports compound assignment operators for each of the arithmetic operators as listed below:</p>
<div class="su-table su-table-alternate">
<table width="642">
<tbody>
<tr>
<td width="70"><strong>Symbol</strong></td>
<td width="119"><strong>Name</strong></td>
<td width="258"><strong>Description</strong></td>
<td width="196"><strong>Example</strong></td>
</tr>
<tr>
<td width="70">+=</td>
<td width="119">Addition assignment</td>
<td width="258">Adds the value on the right to the variable on the left and assigns the result to the variable on the left</td>
<td width="196">int a=30;
<p> </p>
<p>a += 10; //same as a=a+10;</p>
</td>
</tr>
<tr>
<td width="70">-=</td>
<td width="119">Subtraction assignment</td>
<td width="258">Subtracts the value on the right from the variable on the left and assigns the result to the variable on the left</td>
<td width="196">int a=30;
<p> </p>
<p>a -= 20; // same as a=a-20;</p>
</td>
</tr>
<tr>
<td width="70">*=</td>
<td width="119">Multiplication assignment</td>
<td width="258">Multiplies the value on the right to the variable on the left and assigns the result to the variable on the left</td>
<td width="196">int a=30;
<p> </p>
<p>a *= 2; // same as a=a*2;</p>
</td>
</tr>
<tr>
<td width="70">/=</td>
<td width="119">Division assignment</td>
<td width="258">Divides the variable on right with the value on the left and assigns the result to the variable on the left</td>
<td width="196">int a=30, b=10;
<p> </p>
<p>a/=b; same as a=a/10;</p>
</td>
</tr>
<tr>
<td width="70">%=</td>
<td width="119">Modulus assignment</td>
<td width="258">Obtains the remainder of dividing the variable on the left with the value on the right and assigns the result to the variable on the left</td>
<td width="196">int a=35, b=10;
<p> </p>
<p>int a %= b; // same as a=a%10;</p>
</td>
</tr>
</tbody>
</table>
</div>
<h2>4. Increment &amp; Decrement Operators</h2>
<p>Java provides two additional operators known as increment and decrement operators. These basically increment or decrement the variable on which they are invoked.</p>
<div class="su-table su-table-alternate">
<table width="642">
<tbody>
<tr>
<td width="66"><strong>Symbol</strong></td>
<td width="151"><strong>Name</strong></td>
<td width="227"><strong>Description</strong></td>
<td width="198"><strong>Example</strong></td>
</tr>
<tr>
<td width="66">+ +</td>
<td width="151">Increment</td>
<td width="227">Increments the value on which it is invoked by 1</td>
<td width="198">int a = 5;
<p> </p>
<p>a++; // same as a=a+1</p>
</td>
</tr>
<tr>
<td width="66">&#8211; &#8211;</td>
<td width="151">Decrement</td>
<td width="227">Decrements the value on which it is invoked by 1</td>
<td width="198">int b = 5;
<p> </p>
<p>b&#8211;; // same as</p>
<p>b=b-1;</p>
</td>
</tr>
</tbody>
</table>
</div>
<p>Both operators come in two variants, prefix and postfix. In the prefix form the operator precedes the operand and the operation is performed before the assignment. In the postfix form, the operator succeeds the operand and the operation is performed after the assignment. The following code demonstrates this:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="java">int i = 5;
//postfix form, will print i, then increment, so will output 5
System.out.println(i++); 
int j = 10;
//prefix form, will decrement j first then print it, so will output 9
System.out.println(--j);</pre>
<h2>5. Relational Operators</h2>
<p>Relational operators are used to determine the relationship between operands. They return a boolean value. Java has the following relationship operators:</p>
<div class="su-table su-table-alternate">
<table width="690">
<tbody>
<tr>
<td width="66"><strong>Symbol</strong></td>
<td width="151"><strong>Name</strong></td>
<td width="227"><strong>Description</strong></td>
<td width="246"><strong>Example</strong></td>
</tr>
<tr>
<td width="66">&gt;</td>
<td width="151">Greater than</td>
<td width="227">Returns true if the value on the left is greater than the value on the right, otherwise returns false</td>
<td width="246">int a=30, b=20;
<p> </p>
<p>boolean flag = a &gt; b; // flag=true</p>
</td>
</tr>
<tr>
<td width="66">&lt;</td>
<td width="151">Less than</td>
<td width="227">Returns true if the value on the left is less than the value on the right, otherwise returns false</td>
<td width="246">int a=30, b=20;
<p> </p>
<p>boolean flag = a &lt; b; // flag=false</p>
</td>
</tr>
<tr>
<td width="66">==</td>
<td width="151">Equal To</td>
<td width="227">Returns true if the value on the left is equal to the value on the right, otherwise returns false</td>
<td width="246">int a=30, b=20;
<p> </p>
<p>boolean flag = a == b; // flag=false</p>
</td>
</tr>
<tr>
<td width="66">!=</td>
<td width="151">Not Equal To</td>
<td width="227">Returns true if the value on the left is not equal to the value on the right, otherwise returns false</td>
<td width="246">int a=30, b=20;
<p> </p>
<p>boolean flag = a != b; // flag=true</p>
</td>
</tr>
<tr>
<td width="66">&gt;=</td>
<td width="151">Greater than or Equal to</td>
<td width="227">Returns true if the value on the left is greater than or equal to the value on the right, otherwise returns false</td>
<td width="246">int a=30, b=20;
<p> </p>
<p>boolean flag = a &gt;= b; // flag=true</p>
</td>
</tr>
<tr>
<td width="66">&lt;=</td>
<td width="151">Less than or Equal to</td>
<td width="227">Returns true if the value on the left is less than or equal to the value on the right, otherwise returns false</td>
<td width="246">int a=20, b=20;
<p> </p>
<p>boolean flag = a &lt;= b; // flag=true</p>
</td>
</tr>
</tbody>
</table>
</div>
<h2>6. Logical Operators</h2>
<p>Logical operators operate on boolean values and are used to combine relational comparisons. Java has the following logical operators:</p>
<div class="su-table su-table-alternate">
<table width="690">
<tbody>
<tr>
<td width="66"><strong>Symbol</strong></td>
<td width="142"><strong>Name</strong></td>
<td width="236"><strong>Description</strong></td>
<td width="246"><strong>Example</strong></td>
</tr>
<tr>
<td width="66">&amp;&amp;</td>
<td width="142">Logical And</td>
<td width="236">Returns true if both its operands are true</td>
<td width="246">boolean a = true,b=false;
<p> </p>
<p>boolean c = a &amp;&amp; b;</p>
<p>// c=false</p>
</td>
</tr>
<tr>
<td width="66">||</td>
<td width="142">Logical Or</td>
<td width="236">Returns true if any one of its operands are true</td>
<td width="246">boolean a = true,b=false;
<p> </p>
<p>boolean c = a || b;</p>
<p>// c=true</p>
</td>
</tr>
<tr>
<td width="66">!</td>
<td width="142">Not</td>
<td width="236">Reverses its operand</td>
<td width="246">boolean a = true;
<p> </p>
<p>boolean c = !a;</p>
<p>//c=false</p>
</td>
</tr>
</tbody>
</table>
</div>
<p>Logical operators can be used to combine relational comparisons as follows:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="java">int a=30, b=20, c=10;
if(a &gt; b &amp;&amp; b &gt; c){
// do something
}</pre>
<div class="su-spacer" style="height:10px"></div>
<div class="su-spacer" style="height:10px"></div><div class='the_champ_sharing_container the_champ_vertical_sharing the_champ_hide_sharing the_champ_bottom_sharing' style='width:42px;left: -10px;top: 222px;-webkit-box-shadow:none;box-shadow:none;' data-super-socializer-href="https://www.automationdojos.com/tag/java-tutorials/feed/"><div class="the_champ_sharing_ul"><a aria-label="Facebook" class="the_champ_facebook" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13331" title="Facebook" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#0765FE;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path fill="#484848" d="M28 16c0-6.627-5.373-12-12-12S4 9.373 4 16c0 5.628 3.875 10.35 9.101 11.647v-7.98h-2.474V16H13.1v-1.58c0-4.085 1.849-5.978 5.859-5.978.76 0 2.072.15 2.608.298v3.325c-.283-.03-.775-.045-1.386-.045-1.967 0-2.728.745-2.728 2.683V16h3.92l-.673 3.667h-3.247v8.245C23.395 27.195 28 22.135 28 16Z"></path></svg></span></a><a aria-label="Twitter" class="the_champ_button_twitter" href="https://twitter.com/intent/tweet?via=@testoxide&text=java-tutorials&url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13331" title="Twitter" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_twitter" style="background-color:#55acee;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-4 -4 39 39"><path d="M28 8.557a9.913 9.913 0 0 1-2.828.775 4.93 4.93 0 0 0 2.166-2.725 9.738 9.738 0 0 1-3.13 1.194 4.92 4.92 0 0 0-3.593-1.55 4.924 4.924 0 0 0-4.794 6.049c-4.09-.21-7.72-2.17-10.15-5.15a4.942 4.942 0 0 0-.665 2.477c0 1.71.87 3.214 2.19 4.1a4.968 4.968 0 0 1-2.23-.616v.06c0 2.39 1.7 4.38 3.952 4.83-.414.115-.85.174-1.297.174-.318 0-.626-.03-.928-.086a4.935 4.935 0 0 0 4.6 3.42 9.893 9.893 0 0 1-6.114 2.107c-.398 0-.79-.023-1.175-.068a13.953 13.953 0 0 0 7.55 2.213c9.056 0 14.01-7.507 14.01-14.013 0-.213-.005-.426-.015-.637.96-.695 1.795-1.56 2.455-2.55z" fill="#484848"></path></svg></span></a><a aria-label="Linkedin" class="the_champ_button_linkedin" href="https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13331" title="Linkedin" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_linkedin" style="background-color:#0077b5;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path d="M6.227 12.61h4.19v13.48h-4.19V12.61zm2.095-6.7a2.43 2.43 0 0 1 0 4.86c-1.344 0-2.428-1.09-2.428-2.43s1.084-2.43 2.428-2.43m4.72 6.7h4.02v1.84h.058c.56-1.058 1.927-2.176 3.965-2.176 4.238 0 5.02 2.792 5.02 6.42v7.395h-4.183v-6.56c0-1.564-.03-3.574-2.178-3.574-2.18 0-2.514 1.7-2.514 3.46v6.668h-4.187V12.61z" fill="#484848"></path></svg></span></a><a aria-label="Pinterest" class="the_champ_button_pinterest" href="https://www.automationdojos.com/tag/java-tutorials/feed/" onclick="event.preventDefault();javascript:void((function(){var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','//assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());" title="Pinterest" rel="noopener" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_pinterest" style="background-color:#cc2329;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-2 -2 35 35"><path fill="#484848" d="M16.539 4.5c-6.277 0-9.442 4.5-9.442 8.253 0 2.272.86 4.293 2.705 5.046.303.125.574.005.662-.33.061-.231.205-.816.27-1.06.088-.331.053-.447-.191-.736-.532-.627-.873-1.439-.873-2.591 0-3.338 2.498-6.327 6.505-6.327 3.548 0 5.497 2.168 5.497 5.062 0 3.81-1.686 7.025-4.188 7.025-1.382 0-2.416-1.142-2.085-2.545.397-1.674 1.166-3.48 1.166-4.689 0-1.081-.581-1.983-1.782-1.983-1.413 0-2.548 1.462-2.548 3.419 0 1.247.421 2.091.421 2.091l-1.699 7.199c-.505 2.137-.076 4.755-.039 5.019.021.158.223.196.314.077.13-.17 1.813-2.247 2.384-4.324.162-.587.929-3.631.929-3.631.46.876 1.801 1.646 3.227 1.646 4.247 0 7.128-3.871 7.128-9.053.003-3.918-3.317-7.568-8.361-7.568z"/></svg></span></a><a aria-label="Reddit" class="the_champ_button_reddit" href="https://reddit.com/submit?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13331&title=java-tutorials" title="Reddit" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_reddit" style="background-color:#ff5700;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-3.5 -3.5 39 39"><path d="M28.543 15.774a2.953 2.953 0 0 0-2.951-2.949 2.882 2.882 0 0 0-1.9.713 14.075 14.075 0 0 0-6.85-2.044l1.38-4.349 3.768.884a2.452 2.452 0 1 0 .24-1.176l-4.274-1a.6.6 0 0 0-.709.4l-1.659 5.224a14.314 14.314 0 0 0-7.316 2.029 2.908 2.908 0 0 0-1.872-.681 2.942 2.942 0 0 0-1.618 5.4 5.109 5.109 0 0 0-.062.765c0 4.158 5.037 7.541 11.229 7.541s11.22-3.383 11.22-7.541a5.2 5.2 0 0 0-.053-.706 2.963 2.963 0 0 0 1.427-2.51zm-18.008 1.88a1.753 1.753 0 0 1 1.73-1.74 1.73 1.73 0 0 1 1.709 1.74 1.709 1.709 0 0 1-1.709 1.711 1.733 1.733 0 0 1-1.73-1.711zm9.565 4.968a5.573 5.573 0 0 1-4.081 1.272h-.032a5.576 5.576 0 0 1-4.087-1.272.6.6 0 0 1 .844-.854 4.5 4.5 0 0 0 3.238.927h.032a4.5 4.5 0 0 0 3.237-.927.6.6 0 1 1 .844.854zm-.331-3.256a1.726 1.726 0 1 1 1.709-1.712 1.717 1.717 0 0 1-1.712 1.712z" fill="#484848"/></svg></span></a><a aria-label="Whatsapp" class="the_champ_whatsapp" href="https://api.whatsapp.com/send?text=java-tutorials https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13331" title="Whatsapp" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#55eb4c;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-6 -5 40 40"><path class="the_champ_svg_stroke the_champ_no_fill" stroke="#484848" stroke-width="2" fill="none" d="M 11.579798566743314 24.396926207859085 A 10 10 0 1 0 6.808479557110079 20.73576436351046"></path><path d="M 7 19 l -1 6 l 6 -1" class="the_champ_no_fill the_champ_svg_stroke" stroke="#484848" stroke-width="2" fill="none"></path><path d="M 10 10 q -1 8 8 11 c 5 -1 0 -6 -1 -3 q -4 -3 -5 -5 c 4 -2 -1 -5 -1 -4" fill="#484848"></path></svg></span></a><a class="the_champ_more" title="More" rel="nofollow noopener" style="font-size:24px!important;border:0;box-shadow:none;display:inline-block!important;font-size:16px;padding:0 4px;vertical-align: middle;display:inline;" href="https://www.automationdojos.com/?p=13331" onclick="event.preventDefault()"><span class="the_champ_svg" style="background-color:#ee8e2d;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block!important;opacity:1;float:left;font-size:32px!important;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;display:inline;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box;" onclick="theChampMoreSharingPopup(this, 'https://www.automationdojos.com/?p=13331', 'java-tutorials', '' )"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-.3 0 32 32" version="1.1" width="100%" height="100%" style="display:block;" xml:space="preserve"><g><path fill="#484848" d="M18 14V8h-4v6H8v4h6v6h4v-6h6v-4h-6z" fill-rule="evenodd"></path></g></svg></span></a></div></div><p>The post <a href="https://www.automationdojos.com/operators-in-java/">Java Tutorial #2 – Operators in Java</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.automationdojos.com/operators-in-java/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Java Tutorial #1 &#8211; Variables and Data Types</title>
		<link>https://www.automationdojos.com/java-tutorial-1-variables-and-data-types/</link>
					<comments>https://www.automationdojos.com/java-tutorial-1-variables-and-data-types/#respond</comments>
		
		<dc:creator><![CDATA[Tushar Sharma]]></dc:creator>
		<pubDate>Thu, 20 Sep 2018 22:42:16 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Tutorials]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[core-java]]></category>
		<category><![CDATA[java-datatype]]></category>
		<category><![CDATA[java-tutorials]]></category>
		<category><![CDATA[java-variables]]></category>
		<guid isPermaLink="false">https://www.automationdojos.com/?p=13205</guid>

					<description><![CDATA[<p><a href="https://www.automationdojos.com/java-tutorial-1-variables-and-data-types/">Java Tutorial #1 &#8211; Variables and Data Types</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="280" height="168" src="https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-01-280x168.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-01-280x168.png 280w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-01-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-01-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-01-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-01-400x240.png 400w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-01.png 575w" sizes="auto, (max-width: 280px) 100vw, 280px" /></p>
<p>Table of Contents Introduction 1. Variables 2. Data Types Primitive Data Types Integer Data Types Decimal Data Types Character Data Types Boolean Data Types Reference Data Types Introduction Variables are names that are used to identify memory locations that store values. Each variable belongs to a particular data type. Data types determine the size of [&#8230;]</p>
<p>The post <a href="https://www.automationdojos.com/java-tutorial-1-variables-and-data-types/">Java Tutorial #1 &#8211; Variables and Data Types</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://www.automationdojos.com/java-tutorial-1-variables-and-data-types/">Java Tutorial #1 &#8211; Variables and Data Types</a> | As Published On <a href="https://www.automationdojos.com">Automation Dojos</a></p>
<p><img width="280" height="168" src="https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-01-280x168.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" decoding="async" loading="lazy" srcset="https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-01-280x168.png 280w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-01-300x180.png 300w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-01-150x90.png 150w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-01-200x120.png 200w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-01-400x240.png 400w, https://www.automationdojos.com/wp-content/uploads/2020/05/post_java-tutorial-01.png 575w" sizes="auto, (max-width: 280px) 100vw, 280px" /></p>
<h2 class="simpletoc-title">Table of Contents</h2>
<ul class="simpletoc">
<li> <a href="#introduction">Introduction</a></li>
<li> <a href="#variables">1. Variables</a></li>
<li> <a href="#data-types">2. Data Types</a>
<ul>
<li><a href="#primitive-types">Primitive Data Types</a>
<ul>
<li> <a href="#integer-types">Integer Data Types</a></li>
<li> <a href="#decimal-types">Decimal Data Types</a></li>
<li> <a href="#character-types">Character Data Types</a></li>
<li> <a href="#boolean-types">Boolean Data Types</a></li>
</ul>
</li>
<li> <a href="#reference-types">Reference Data Types</a></li>
</ul>
</li>
</ul>


<h3>Introduction</h3>
<p>Variables are names that are used to identify memory locations that store values. Each variable belongs to a particular data type. Data types determine the size of the memory location.In this article, we will be exploring Java variables and data types in detail.</p>
<h3>1. Variables</h3>
<p>A variable can be used to refer to a <em>value </em>at a <strong>memory location</strong>. The code can use and modify the value at the memory location using the variable.   A variable needs to be declared and initialized before it can be used.</p>
<div class="su-box su-box-style-default" id="" style="border-color:#bfbfbf;border-radius:3px;"><div class="su-box-title" style="background-color:#f2f2f2;color:#6b6b6b;border-top-left-radius:1px;border-top-right-radius:1px">Declaring a Variable</div><div class="su-box-content su-u-clearfix su-u-trim" style="border-bottom-left-radius:1px;border-bottom-right-radius:1px">
<p>In order to declare a variable, you need to specify its data type and a name for the variable as shown below:</p>
<pre class="lang:java decode:true ">int i; //declares a variable called i of int data type</pre>
<p>Here, <strong>int</strong> is the data type. It is used to store a non-negative positive or negative whole number. Just like <strong>int</strong>, there are several other data types supported by Java which are covered in the next section.<strong>i</strong> is the variable name. It points to the memory location which the operating system has allocated.<br />You can also declare several variables at once as follows:</p>
<pre class="lang:java decode:true ">int i,j,k; //declares 3 variables called i,j,k of int data type</pre>
<p>Note that the variable names are separated using commas.</p>
</div></div>
<div class="su-spacer" style="height:20px"></div>
<div class="su-box su-box-style-default" id="" style="border-color:#bfbfbf;border-radius:3px;"><div class="su-box-title" style="background-color:#f2f2f2;color:#6b6b6b;border-top-left-radius:1px;border-top-right-radius:1px">Initializing a Variable</div><div class="su-box-content su-u-clearfix su-u-trim" style="border-bottom-left-radius:1px;border-bottom-right-radius:1px">
<p>When a value is assigned to a variable, the variable is said to be initialized.  There are several ways you can initialize a variable:</p>
<div class="componentheading">Initializing at the time of declaration</div>
<p>You can initialize a variable when it is declared as follows:</p>
<pre class="lang:java decode:true ">int i = 100; // creates a variable i and sets it’s value to 100</pre>
<div class="componentheading">Initializing a variable later on</div>
<p>You can declare a variable first and initialize it later on as follows:</p>
<pre class="lang:java decode:true ">int i; // declares int variable i
//do something else
i = 100; // sets i to 100</pre>
<div class="componentheading">Initializing a variable using an expression</div>
<p>You can also use an expression to initialize as variable as follows:</p>
<pre class="lang:java decode:true ">int sum = 10+6; //sets sum to 16
int sum2 = a+b; // adds the values in a and b and assigns the result to sum2</pre>
</div></div>
<div class="su-spacer" style="height:20px"></div>
<div class="su-box su-box-style-default" id="" style="border-color:#bfbfbf;border-radius:3px;"><div class="su-box-title" style="background-color:#f2f2f2;color:#6b6b6b;border-top-left-radius:1px;border-top-right-radius:1px">Using a Variable</div><div class="su-box-content su-u-clearfix su-u-trim" style="border-bottom-left-radius:1px;border-bottom-right-radius:1px">
<p>Once you declare and initialize a variable, you can use it in your code as follows:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="java">int a = b+27; // uses b in an expression
System.out.println(“a = “+a); // prints value of a
if(a &amp;gt; 20) { // uses a in an if statement
// do something
}</pre>
</div></div>
<div class="su-spacer" style="height:20px"></div>
<div class="rt-block">
<h3>2. Data Types</h3>
<p>Data types determine the type of data that a variable can contain and its size. The operating system allocates memory to a variable based its data type. Java supports several data types which can be used to store different type of data. Data types can be categorized as follows:</p>
<div class="su-spacer" style="height:15px"></div>
<h4>1) Primitive Data Types</h4>
<p>Primitive data types are types used to store a <strong>single value</strong>. They are basically used to store numbers and characters.  Primitive data types can be further classified into the following groups:</p>
<div class="su-spacer" style="height:10px"></div>
<h5>(a) Integer Data Types</h5>
<p>Integer data types can be used to store whole numbers, that is non-fractional positive or negative numbers. Java supports <strong>byte</strong>, <strong>short</strong>, <strong>int</strong> and <strong>long</strong> integer data types as detailed below:</p>
<div class="su-table su-table-alternate">
<table width="630">
<tbody>
<tr>
<td width="62"><strong>Name</strong></td>
<td width="63"><strong>Width (Bits)</strong></td>
<td width="178"><strong>Range</strong></td>
<td width="327"><strong>Example</strong></td>
</tr>
<tr>
<td width="62">byte</td>
<td width="63">8</td>
<td width="178">–128 to 127</td>
<td width="327">byte b = 100; // creates variable b of byte data type with value 100
<p> </p>
<p>byte b1 = 128; //will cause error as maximum value is 127</p>
</td>
</tr>
<tr>
<td width="62">short</td>
<td width="63">16</td>
<td width="178">–32,768 to 32,767</td>
<td width="327">short s = 1000; // creates variable s of short data type with value 1000</td>
</tr>
<tr>
<td width="62">int</td>
<td width="63">32</td>
<td width="178">–2,147,483,648 to 2,147,483,647</td>
<td width="327">int i = -4507; // creates variable i of int data type with value -4507</td>
</tr>
<tr>
<td width="62">long</td>
<td width="63">64</td>
<td width="178">–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807</td>
<td width="327">long l = 125000000; // create variable l of long data type with value 125000000</td>
</tr>
</tbody>
</table>
</div>
<p>As can be seen from the table above, the difference between them is the range of values that they can store. While byte can be used to store very small numbers, long can be used to store bigger numbers.</p>
<div class="su-spacer" style="height:10px"></div>
<h5>(b) Decimal Data Types</h5>
<p>Decimal data types can be used to store decimal numbers that is numbers having a fractional component. Java supports the <strong>float and double</strong> data types to store decimal numbers as detailed below:</p>
<div class="su-table su-table-alternate">
<table width="630">
<tbody>
<tr>
<td width="69"><strong>Name</strong></td>
<td width="67"><strong>Width (Bits)</strong></td>
<td width="164"><strong>Range</strong></td>
<td width="329"><strong>Example</strong></td>
</tr>
<tr>
<td width="69">float</td>
<td width="67">32</td>
<td width="164">1.4e–045 to 3.4e+038</td>
<td width="329">float f = 2.5; // compilation error
<p> </p>
<p>float f1 = 3.5f; //creates a float variable f1 with value as 3.5</p>
</td>
</tr>
<tr>
<td width="69">double</td>
<td width="67">64</td>
<td width="164">4.9e–324 to 1.8e+308</td>
<td width="329">double d = 100.25;   // creates a double variable d with value as 100.25</td>
</tr>
</tbody>
</table>
</div>
<p>The difference between the two is the precision level. Float specifies a single precision value whereas double specifies a double precision value. Single precision takes less space as compared to double precision but are not as accurate as double precision, particularly for very large or very small numbers.By default, when you create a decimal number, Java expects it to be of double data type, so if you want to explicitly create a float value, you need to append an ‘f’ at the end as done above.</p>
<div class="su-spacer" style="height:10px"></div>
<h5>(c) Characters</h5>
<p>Java supports the <strong>char</strong> data type to store characters. This can be used to store characters like letters, numbers and symbols as shown below:</p>
<div class="su-table su-table-alternate">
<table width="630">
<tbody>
<tr>
<td width="67"><strong>Name</strong></td>
<td width="68"><strong>Width (Bits)</strong></td>
<td width="159"><strong>Range</strong></td>
<td width="337"><strong>Example</strong></td>
</tr>
<tr>
<td width="67">char</td>
<td width="68">16</td>
<td width="159">0 to 65536</td>
<td width="337">char c = &#8216;a&#8217;; // creates a char variable with value ‘a’</td>
</tr>
</tbody>
</table>
</div>
<p>Java uses Unicode to store characters. Unicode is an international character set that can store characters from many languages. So in addition to the English language character like ‘a’, b’, etc, the char data type can be used to store characters from other languages too.</p>
<div class="su-spacer" style="height:10px"></div>
<h5>(d) Boolean</h5>
<p>Java supports the <strong>boolean</strong> data type to store ‘<strong>true</strong>’ or ‘<strong>false’ </strong>as demonstrated below:</p>
<div class="su-table su-table-alternate">
<table width="630">
<tbody>
<tr>
<td width="67"><strong>Name</strong></td>
<td width="68"><strong>Width (Bits)</strong></td>
<td width="159"><strong>Range</strong></td>
<td width="337"><strong>Example</strong></td>
</tr>
<tr>
<td width="67">boolean</td>
<td width="68">1</td>
<td width="159">true/false</td>
<td width="337">boolean b = true; // assigns the value true to b</td>
</tr>
</tbody>
</table>
</div>
<p>The boolean data type is basically used for comparisons or to test conditions.</p>
<div class="su-spacer" style="height:20px"></div>
<h3>3. Reference Data Types</h3>
<p>In addition to primitive data types, Java supports an additional data type called reference data type. These are used to hold more complex data created using the primitive data types in the form of objects. This will be covered later on.</p>
</div>
<h3>Conclusion</h3>
<p>So this article covers declaring, initializing and using Java variables. It also walks you through the various data types supported by Java.</p><div class='the_champ_sharing_container the_champ_vertical_sharing the_champ_hide_sharing the_champ_bottom_sharing' style='width:42px;left: -10px;top: 222px;-webkit-box-shadow:none;box-shadow:none;' data-super-socializer-href="https://www.automationdojos.com/tag/java-tutorials/feed/"><div class="the_champ_sharing_ul"><a aria-label="Facebook" class="the_champ_facebook" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13205" title="Facebook" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#0765FE;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path fill="#484848" d="M28 16c0-6.627-5.373-12-12-12S4 9.373 4 16c0 5.628 3.875 10.35 9.101 11.647v-7.98h-2.474V16H13.1v-1.58c0-4.085 1.849-5.978 5.859-5.978.76 0 2.072.15 2.608.298v3.325c-.283-.03-.775-.045-1.386-.045-1.967 0-2.728.745-2.728 2.683V16h3.92l-.673 3.667h-3.247v8.245C23.395 27.195 28 22.135 28 16Z"></path></svg></span></a><a aria-label="Twitter" class="the_champ_button_twitter" href="https://twitter.com/intent/tweet?via=@testoxide&text=java-tutorials&url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13205" title="Twitter" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_twitter" style="background-color:#55acee;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-4 -4 39 39"><path d="M28 8.557a9.913 9.913 0 0 1-2.828.775 4.93 4.93 0 0 0 2.166-2.725 9.738 9.738 0 0 1-3.13 1.194 4.92 4.92 0 0 0-3.593-1.55 4.924 4.924 0 0 0-4.794 6.049c-4.09-.21-7.72-2.17-10.15-5.15a4.942 4.942 0 0 0-.665 2.477c0 1.71.87 3.214 2.19 4.1a4.968 4.968 0 0 1-2.23-.616v.06c0 2.39 1.7 4.38 3.952 4.83-.414.115-.85.174-1.297.174-.318 0-.626-.03-.928-.086a4.935 4.935 0 0 0 4.6 3.42 9.893 9.893 0 0 1-6.114 2.107c-.398 0-.79-.023-1.175-.068a13.953 13.953 0 0 0 7.55 2.213c9.056 0 14.01-7.507 14.01-14.013 0-.213-.005-.426-.015-.637.96-.695 1.795-1.56 2.455-2.55z" fill="#484848"></path></svg></span></a><a aria-label="Linkedin" class="the_champ_button_linkedin" href="https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13205" title="Linkedin" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_linkedin" style="background-color:#0077b5;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 32 32"><path d="M6.227 12.61h4.19v13.48h-4.19V12.61zm2.095-6.7a2.43 2.43 0 0 1 0 4.86c-1.344 0-2.428-1.09-2.428-2.43s1.084-2.43 2.428-2.43m4.72 6.7h4.02v1.84h.058c.56-1.058 1.927-2.176 3.965-2.176 4.238 0 5.02 2.792 5.02 6.42v7.395h-4.183v-6.56c0-1.564-.03-3.574-2.178-3.574-2.18 0-2.514 1.7-2.514 3.46v6.668h-4.187V12.61z" fill="#484848"></path></svg></span></a><a aria-label="Pinterest" class="the_champ_button_pinterest" href="https://www.automationdojos.com/tag/java-tutorials/feed/" onclick="event.preventDefault();javascript:void((function(){var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','//assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());" title="Pinterest" rel="noopener" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_pinterest" style="background-color:#cc2329;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-2 -2 35 35"><path fill="#484848" d="M16.539 4.5c-6.277 0-9.442 4.5-9.442 8.253 0 2.272.86 4.293 2.705 5.046.303.125.574.005.662-.33.061-.231.205-.816.27-1.06.088-.331.053-.447-.191-.736-.532-.627-.873-1.439-.873-2.591 0-3.338 2.498-6.327 6.505-6.327 3.548 0 5.497 2.168 5.497 5.062 0 3.81-1.686 7.025-4.188 7.025-1.382 0-2.416-1.142-2.085-2.545.397-1.674 1.166-3.48 1.166-4.689 0-1.081-.581-1.983-1.782-1.983-1.413 0-2.548 1.462-2.548 3.419 0 1.247.421 2.091.421 2.091l-1.699 7.199c-.505 2.137-.076 4.755-.039 5.019.021.158.223.196.314.077.13-.17 1.813-2.247 2.384-4.324.162-.587.929-3.631.929-3.631.46.876 1.801 1.646 3.227 1.646 4.247 0 7.128-3.871 7.128-9.053.003-3.918-3.317-7.568-8.361-7.568z"/></svg></span></a><a aria-label="Reddit" class="the_champ_button_reddit" href="https://reddit.com/submit?url=https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13205&title=java-tutorials" title="Reddit" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg the_champ_s__default the_champ_s_reddit" style="background-color:#ff5700;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-3.5 -3.5 39 39"><path d="M28.543 15.774a2.953 2.953 0 0 0-2.951-2.949 2.882 2.882 0 0 0-1.9.713 14.075 14.075 0 0 0-6.85-2.044l1.38-4.349 3.768.884a2.452 2.452 0 1 0 .24-1.176l-4.274-1a.6.6 0 0 0-.709.4l-1.659 5.224a14.314 14.314 0 0 0-7.316 2.029 2.908 2.908 0 0 0-1.872-.681 2.942 2.942 0 0 0-1.618 5.4 5.109 5.109 0 0 0-.062.765c0 4.158 5.037 7.541 11.229 7.541s11.22-3.383 11.22-7.541a5.2 5.2 0 0 0-.053-.706 2.963 2.963 0 0 0 1.427-2.51zm-18.008 1.88a1.753 1.753 0 0 1 1.73-1.74 1.73 1.73 0 0 1 1.709 1.74 1.709 1.709 0 0 1-1.709 1.711 1.733 1.733 0 0 1-1.73-1.711zm9.565 4.968a5.573 5.573 0 0 1-4.081 1.272h-.032a5.576 5.576 0 0 1-4.087-1.272.6.6 0 0 1 .844-.854 4.5 4.5 0 0 0 3.238.927h.032a4.5 4.5 0 0 0 3.237-.927.6.6 0 1 1 .844.854zm-.331-3.256a1.726 1.726 0 1 1 1.709-1.712 1.717 1.717 0 0 1-1.712 1.712z" fill="#484848"/></svg></span></a><a aria-label="Whatsapp" class="the_champ_whatsapp" href="https://api.whatsapp.com/send?text=java-tutorials https%3A%2F%2Fwww.automationdojos.com%2F%3Fp%3D13205" title="Whatsapp" rel="nofollow noopener" target="_blank" style="font-size:24px!important;box-shadow:none;display:inline-block;vertical-align:middle"><span class="the_champ_svg" style="background-color:#55eb4c;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block;opacity:1;float:left;font-size:24px;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box"><svg style="display:block;" focusable="false" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="-6 -5 40 40"><path class="the_champ_svg_stroke the_champ_no_fill" stroke="#484848" stroke-width="2" fill="none" d="M 11.579798566743314 24.396926207859085 A 10 10 0 1 0 6.808479557110079 20.73576436351046"></path><path d="M 7 19 l -1 6 l 6 -1" class="the_champ_no_fill the_champ_svg_stroke" stroke="#484848" stroke-width="2" fill="none"></path><path d="M 10 10 q -1 8 8 11 c 5 -1 0 -6 -1 -3 q -4 -3 -5 -5 c 4 -2 -1 -5 -1 -4" fill="#484848"></path></svg></span></a><a class="the_champ_more" title="More" rel="nofollow noopener" style="font-size:24px!important;border:0;box-shadow:none;display:inline-block!important;font-size:16px;padding:0 4px;vertical-align: middle;display:inline;" href="https://www.automationdojos.com/?p=13205" onclick="event.preventDefault()"><span class="the_champ_svg" style="background-color:#ee8e2d;width:38px;height:38px;border-radius:3px;margin:0;display:inline-block!important;opacity:1;float:left;font-size:32px!important;box-shadow:none;display:inline-block;font-size:16px;padding:0 4px;vertical-align:middle;display:inline;background-repeat:repeat;overflow:hidden;padding:0;cursor:pointer;box-sizing:content-box;" onclick="theChampMoreSharingPopup(this, 'https://www.automationdojos.com/?p=13205', 'java-tutorials', '' )"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="-.3 0 32 32" version="1.1" width="100%" height="100%" style="display:block;" xml:space="preserve"><g><path fill="#484848" d="M18 14V8h-4v6H8v4h6v6h4v-6h6v-4h-6z" fill-rule="evenodd"></path></g></svg></span></a></div></div><p>The post <a href="https://www.automationdojos.com/java-tutorial-1-variables-and-data-types/">Java Tutorial #1 &#8211; Variables and Data Types</a> appeared first on <a href="https://www.automationdojos.com">Automation Dojos</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.automationdojos.com/java-tutorial-1-variables-and-data-types/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
