<?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; examples</title>
	<atom:link href="http://ankit.co/tag/examples/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>Basic Programs on Generics</title>
		<link>http://ankit.co/tutorials/java-tutorials/generics/basic-programs-on-generics?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=basic-programs-on-generics</link>
		<comments>http://ankit.co/tutorials/java-tutorials/generics/basic-programs-on-generics#comments</comments>
		<pubDate>Mon, 07 Jan 2013 23:11:11 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Generics]]></category>
		<category><![CDATA[examples]]></category>
		<category><![CDATA[generics]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=218</guid>
		<description><![CDATA[<p>Generic Functionality Generics introduced the concept of type variable. A type variable is an unqualified identifier as per the Java Language Specification, Which can be used in following ways: Generic class declarations Generic interface declarations Generic method declarations Generic constructor declarations. Above code demonstrates the use of Generic class, constructor and method. Here we are [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/generics/basic-programs-on-generics">Basic Programs on Generics</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<h2>Generic Functionality</h2>
<p>Generics introduced the concept of type variable. A type variable is an unqualified identifier as per the Java Language Specification, Which can be used in following ways:</p>
<ul>
<li>Generic class declarations</li>
<li>Generic interface declarations</li>
<li>Generic method declarations</li>
<li>Generic constructor declarations.</li>
</ul>
<pre class="brush: java; title: ; notranslate">
class genericsDemo
{
	public static void main(String[] s)
	{
		test&lt;Integer&gt; t = new test&lt;Integer&gt;(5);
		int i = t.getI();       // No need to type cast!
		System.out.println(i);

		test&lt;String&gt; ts = new test&lt;String&gt;(&quot;This is demo.&quot;);
		String is = ts.getI();   // No need to type cast!
		System.out.println(is);
		
		xyz o = new xyz(7);
		test&lt;xyz&gt; txyz = new test&lt;xyz&gt;(o);
		xyz newo = txyz.getI();   // No need to type cast!
		System.out.println(newo);
	}	
}

 
class test&lt;T&gt;         // Generic class
{
	private T i;
	test(T i)     // Generic Constructor
	{
		this.i = i;	
	}	
	T getI()     // Generic Method
	{
		return i;	
	}
}


class xyz
{
	int x;
	xyz(int x)
	{
		this.x = x;
	}
	
	public String toString()
	{
		return &quot;Value of X is: &quot;+x;
	}
}
</pre>
<p>Above code demonstrates the use of Generic class, constructor and method. Here we are learning one more thing: It&#8217;s not a compulsion that we can only pass wrapper class objects as generic variable. A custom/User defined class&#8217;s object can also be passed as we did with: &#8220;xyz&#8221; class.</p>
<p><strong>Output: </strong></p>
<p><a class="highslide"  onclick="return hs.expand(this)" href="../../../images/generic.jpg" rel="attachment"><img class="size-full aligncenter" alt="Generics Basic" src="../../../images/generic_400.jpg" /></a></p>
<hr/>
<h3>Example on Generic Method</h3>
<pre class="brush: java; title: ; notranslate">
public class GenericMethodTest
{
   // generic method printArray                         
   public static &lt;E&gt; void printArray( E[] inputArray )
   {
      // Display array elements              
         for ( E element : inputArray )
         {        
            System.out.printf( &quot;%s &quot;, element );
         }
         System.out.println();
    }

    public static void main( String args[] )
    {
        // Create arrays of Integer, Double and Character
        Integer[] intArray = { 1, 2, 3, 4, 5 };
        Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
        Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };

        System.out.println( &quot;Array integerArray contains:&quot; );
        printArray( intArray  ); // pass an Integer array

        System.out.println( &quot;\nArray doubleArray contains:&quot; );
        printArray( doubleArray ); // pass a Double array

        System.out.println( &quot;\nArray characterArray contains:&quot; );
        printArray( charArray ); // pass a Character array
    } 
}
</pre>
<p><strong>Output: </strong></p>
<p><a class="highslide"  onclick="return hs.expand(this)" href="../../../images/genericMethod.jpg" rel="attachment"><img class="size-full aligncenter" alt="Generics Basic" src="../../../images/genericMethod_400.jpg" /></a></p>
<hr/>
<h3>Example on Generic Interface</h3>
<pre class="brush: java; title: ; notranslate">
interface MinMax&lt;T extends Comparable&lt;T&gt;&gt; 
{
	T min();
	T max();
}
// Now, implement MinMax
class MyClass&lt;T extends Comparable&lt;T&gt;&gt; implements MinMax&lt;T&gt; 
{
	T[] vals;
	MyClass(T[] o) 
	{ 
		vals = o; 
	}
	// Return the minimum value in vals.
	public T min() 
	{
		T v = vals[0];
		for(int i=1; i &lt; vals.length; i++)
		if(vals[i].compareTo(v) &lt; 0) v = vals[i];
		return v;
	}
	// Return the maximum value in vals.
	public T max() 
	{
		T v = vals[0];
		for(int i=1; i &lt; vals.length; i++)
		if(vals[i].compareTo(v) &gt; 0) v = vals[i];
		return v;
	}
}
class GenIFDemo 
{
	public static void main(String args[]) 
	{
		Integer inums[] = {3, 6, 2, 8, 6 };
		Character chs[] = {'b', 'r', 'p', 'w' };
		MyClass&lt;Integer&gt; iob = new MyClass&lt;Integer&gt;(inums);
		MyClass&lt;Character&gt; cob = new MyClass&lt;Character&gt;(chs);
		System.out.println(&quot;Max value in inums: &quot; + iob.max());
		System.out.println(&quot;Min value in inums: &quot; + iob.min());
		System.out.println(&quot;Max value in chs: &quot; + cob.max());
		System.out.println(&quot;Min value in chs: &quot; + cob.min());
	}
}
</pre>
<p><strong>Output: </strong></p>
<p><a class="highslide"  onclick="return hs.expand(this)" href="../../../images/genIfDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="Generics Basic" src="../../../images/genIfDemo_400.jpg" /></a></p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/generics/basic-programs-on-generics">Basic Programs on 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/basic-programs-on-generics/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
