<?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>Ankit Virparia &#187; What is</title>
	<atom:link href="http://ankit.co/tag/what-is/feed" rel="self" type="application/rss+xml" />
	<link>http://ankit.co</link>
	<description>A Programmer, Designer and Trainer</description>
	<lastBuildDate>Sun, 11 May 2014 04:15:47 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>Introduction to Generics</title>
		<link>http://ankit.co/tutorials/java-tutorials/generics/introduction-to-generics?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=introduction-to-generics</link>
		<comments>http://ankit.co/tutorials/java-tutorials/generics/introduction-to-generics#comments</comments>
		<pubDate>Mon, 07 Jan 2013 16:34:03 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Generics]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[What is]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=211</guid>
		<description><![CDATA[<p>What is Generics? Till now whatever classes/methods/constructors/variables we have created were datatype specific i.e. having a fixed datatype which was decided by the owner of the class and cannot be changed once the class is compiled. For example, Above method takes an int value as a parameter which do not support any other datatype. But [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/generics/introduction-to-generics">Introduction to Generics</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<h3>What is Generics?</h3>
<p>Till now whatever classes/methods/constructors/variables we have created were datatype specific i.e. having a fixed datatype which was decided by the owner of the class and cannot be changed once the class is compiled. For example,</p>
<pre class="brush: java; title: ; notranslate">
public void disp(int x)
{
}
</pre>
<p>Above method takes an int value as a parameter which do not support any other datatype. But let&#8217;s say now we are considering a scenario where by there are 2 developers: 1) developer1 and 2) developer2.</p>
<p>Developer1 has written following class which is used by developer2 to find the maximum number from the given set of numbers as discussed below:</p>
<p><strong>Developer1 Wrote:</strong></p>
<pre class="brush: java; title: ; notranslate">
public class findMax
{
	public int maxFrom(int[] arr)
	{	
		int max = 0;
		//code to find max out of given arr and assing it to max variable
		return max;
	
	}
}
</pre>
<p>Now, Developer2 wants to use the class which is created by developer1. Remember that developer2 cannot modify the class <strong>findMax</strong> which is created by developer1.<br />
<strong>Developer2 Wrote:</strong></p>
<pre class="brush: java; title: ; notranslate">
public class abc
{
	public static void main(String[] args)
	{	
		int[] iarr = {1,2,3,4,5,6,7,8,9,0};
		findMax fm = new findMax();
		int max = fm.maxFrom(iarr);
	}
}
</pre>
<p>Correct! Developer2 can use it easily by creating an object and doing a method call.<br />
<strong>Then what is the problem? </strong>Problem is when the developer2 want to find max number out of given set of float numbers. He is planning to pass float[] as an argument and wants a float value back. But there is no such method available in the class findMax which is developed by developer1. </p>
<p>Let&#8217;s say on special request of developer2 even if developer1 agrees to add a overloaded method then that can be said as a temporary solution!<br />
<strong>Developer1 Wrote:</strong></p>
<pre class="brush: java; title: ; notranslate">
public class findMax
{
	public int maxFrom(int[] arr)
	{	
		int max = 0;
		//code to find max out of given arr and assing it to max variable
		return max;
	
	}
	public float maxFrom(float[] arr)
	{	
		float max = 0;
		//code to find max out of given arr and assing it to max variable
		return max;
	
	}

}
</pre>
<p>It&#8217;s a temporary solution because it cannot be said that there won&#8217;t be any future requirement (of developer2) to find a maximum double value out of the given double[]. In that case we have to write one more overloaded method! But Howmany? Whatever number of overloaded method developer1 writes are fixed while writing the class. So, developer2 will be always dependent on developer1! So, This is the problem which can be resolved by the use of Generics&#8230;</p>
<h2>Generics</h2>
<p>Generics is a Java feature that was introduced with Java SE 5.0. It&#8217;s an easy to use feature and many resources are available for Generics but lot many developers face problem in understanding generics concept in java. Anyway, Let&#8217;s start!</p>
<p><strong>Variable:</strong><br />
A variable is normally used by a developer to store some literals right? But lets say a variable which holds the java class/wrapper class that can be said as a generic variable.</p>
<p><strong>How developer1 can write a Generic calss &#8220;findMax&#8221;?</strong><br />
<strong>Developer1 Wrote:</strong></p>
<pre class="brush: java; title: ; notranslate">
public class findMax&lt;T&gt;
{
	public T maxFrom(T[] arr)
	{	
		T max = 0;
		//code to find max out of given arr and assing it to max variable
		return max;
	
	}
}
</pre>
<p>So, As shown in the code above our method maxForm() not takes T type of array as an argument and returns T type of data. Here &#8220;T&#8221; is the variable which will hold the datatype and that can be decided by develope2! How?</p>
<p><strong>Developer2 Wrote:</strong></p>
<pre class="brush: java; title: ; notranslate">
public class abc
{
	public static void main(String[] args)
	{	
		Integer[] iarr = {1,2,3,4,5,6,7,8,9,0};
		findMax&lt;Integer&gt; fm = new findMax&lt;Integer&gt;();
		int max = fm.maxFrom(iarr);
		System.out.println(max);
	}
}
</pre>
<p>Note: Make sure you do not pass any datatype as a generic value, Which is not allowed. Here I passed Integer instead of int whereby Integer is the wrapper class of int datatype.</p>
<p>Full Program by Developer1:</p>
<pre class="brush: java; title: ; notranslate">
import java.util.*;
class findMax&lt;T&gt;
{
	public T maxFrom(T[] arr)
	{	
		T max = arr[0];

		for(int i=0;i&lt;arr.length-1;i++)
		{
			Comparable one = (Comparable)arr[i];
			Comparable two = (Comparable)arr[i+1];

			if(one.compareTo(two) &lt; 0)
			{
				max = arr[i+1];
			}	
		}
		//code to find max out of given arr and assing it to max variable
		return max;
	
	}
}
</pre>
<p>Developer2 may use it with different different datatypes by using appropriate wrapper class.</p>
<pre class="brush: java; title: ; notranslate">
public class abc
{
	public static void main(String[] args)
	{	
		Integer[] iarr = {1,2,3,4,5,6,7,8,9,0};
		findMax&lt;Integer&gt; fm = new findMax&lt;Integer&gt;();
		int max = fm.maxFrom(iarr);
		System.out.println(max);

		Float[] farr = {1.2,2.3,3.4,4.5,5.6,6.9,7.2,8.1,9.0,0.2};
		findMax&lt;Float&gt; ffm = new findMax&lt;Float&gt;();
		float fmax = ffm.maxFrom(farr);
		System.out.println(fmax);

	}
}
</pre>
<p>Again, A major change that you will find is: Comparable? What is that? Ammm&#8230; Comparable is an interface which belongs to java.util package which can be used to compare any object. If we need to compare a normal variable, we might have used <, >, == operators but in our case developer1 (who is writing the logic) does not know anything about what kind of value T will hold. So, We are casting our array indexes to Comparable and comparing it so that we can find the maximum value.</p>
<p>Hope you have understood what is Generics! Write a comment or ask a question. Feel free to contact me.</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/generics/introduction-to-generics">Introduction to Generics</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/generics/introduction-to-generics/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>What is an Applet?</title>
		<link>http://ankit.co/tutorials/java-tutorials/applet/what-is-an-applet?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-is-an-applet</link>
		<comments>http://ankit.co/tutorials/java-tutorials/applet/what-is-an-applet#comments</comments>
		<pubDate>Thu, 03 Jan 2013 17:38:25 +0000</pubDate>
		<dc:creator><![CDATA[Sonali Virparia]]></dc:creator>
				<category><![CDATA[Applet]]></category>
		<category><![CDATA[What is]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=83</guid>
		<description><![CDATA[<p>An applet is a small and portable application that runs under the restricted scope provided by JRE. It has dual compatibility as it can be executed on a web browser and also by applet viewer. JVM is required on the client machine to run an applet program. Five steps to be kept in mind when [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/what-is-an-applet">What is an Applet?</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>An applet is a small and portable application that runs under the restricted scope provided by JRE. It has dual compatibility as it can be executed on a web browser and also by applet viewer. JVM is required on the client machine to run an applet program.</p>
<p><strong>Five steps to be kept in mind when writing an applet program:</strong></p>
<p>1. import java.applet.*  (Need Applet class from java.applet package)</p>
<p>2. create a public class (Class name and File name must be same in case of public class)</p>
<p>3. extend the class from the base class Applet </p>
<p>4. do not write main method </p>
<p>5. write the applet life cycle methods (i.e.  init, start, stop, destroy, paint)</p>
<p><strong>File name:</strong> abc.java</p>
<pre class="brush: java; title: ; notranslate">
import java.applet.*;

public class abc extends Applet
{
}
</pre>
<p><strong>Steps to compile and Run Applet program:</strong></p>
<ol>
<li>Compile your java code using javac.exe on console/command prompt.</li>
<li>To run the generated class file, we have to write &lt;applet&gt; tag in an additional file(e.g. run.txt). Later on that file name is to be passed as an argument to appletviewer.exe as shown below</li>
</ol>
<p><strong>File name:</strong> run.txt</p>
<pre class="brush: xml; title: ; notranslate">
&lt;applet code=&quot;abc&quot; width=&quot;500&quot; height=&quot;500&quot;&gt;&lt;/applet&gt;
</pre>
<p><strong>Commands to be executed:</strong> run.txt</p>
<pre class="brush: xml; title: ; notranslate">
c:&gt; javac abc.java
c:&gt; appletviewer run.txt
</pre>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/what-is-an-applet">What is an Applet?</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/applet/what-is-an-applet/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
