tag:blogger.com,1999:blog-77363420766624738452024-02-08T08:48:25.244-08:00Java BreadA cup of Java is always soothing to your mind !!!Anonymoushttp://www.blogger.com/profile/08652669950315650050noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-7736342076662473845.post-20285331228637847102014-11-16T22:47:00.000-08:002014-11-16T22:47:14.265-08:00Polymorphism or simply In many forms<div dir="ltr" style="text-align: left;" trbidi="on">
<b>Polymorphism</b> means many forms.<br />
In computer terms we relate polymorphism as one of the founding stones of OOP and many Programming Languages. <br />
<br />
When an Object has the ability to take more than one form it is referred to as Polymorphic. When a parent class reference is used to point to an object of it's child it is termed as a polymorphic reference.<br />
<br />
When we talk about method polymorphism in Java, Compile Time and Run Time are two of its types.<br />
<br />
Compile Time Polymorphism or Method Overloading is achieved when the methods have the same name and quite possibly process the input in a similar manner but take different inputs; either the number or the types of input are different. Method Overloading works when all the methods that you wish to overload are in the exact same class. <br />
<blockquote class="tr_bq">
Let's take an example ...<br />public class JavaBlink {<br /> // This method takes two arguments and return there sum<br /> public int sum(int o1, int o2) {<br /> return o1 + o2;<br /> }<br /> <br /> // This method takes three arguments and return there sum<br /> public int sum(int o1, int o2, int o3) {<br /> return o1 + o2 + o3;<br /> }<br />} </blockquote>
In the example above you could see that I have used different number of arguments and overloaded the method sum. This is one of the simplest forms of method overloading. <br />
<br />
Run Time Polymorphism on the other hand, as the name suggests, happens when the program is actually executed after compilation. This requires at least two classes or one class and one interface, both of which should be in a hierarchy. <br />
<blockquote class="tr_bq">
Let's take an example ...<br />public class Earth {<br /> public float checkGravity() {<br /> return 9.78f;<br /> }<br />}<br /><br />public class Moon extends Earth {<br /> public float checkGravity() {<br /> return 1.622f;<br /> }<br />}<br /><br />public class Tester {<br /> public static void main(String[] a) {<br /> // creates an object of type Earth with reference type Earth<br /> Earth earth = new Earth(); <br /> <br /> // creates an object of type Moon with reference type Moon<br /> Moon moon = new Moon(); <br /> <br /> // creates an object of type Moon with reference type Earth<br /> // This is where the polymorphism kicks in; At runtime the call<br /> // is sent to Moon's checkGravity()<br /> Earth polyMoon = new Moon(); <br /> <br /> System.out.println(earth.checkGravity());<br /> System.out.println(moon.checkGravity());<br /> System.out.println(polyMoon.checkGravity());<br /> }<br />}</blockquote>
<br />
In Java, specifically, we can use polymorphism in a variety of ways ...<br />
<br />
1. Methods can be made polymorphic to cater to a wide variety of inputs<br />
2. Polymorphic references as method arguments can be used where a variety of SubTypes could be passed; though care should be taken when calling methods on received objects<br />
3. Polymorphism is the driving force behind Strategy Design Pattern</div>
Anonymoushttp://www.blogger.com/profile/08652669950315650050noreply@blogger.com0tag:blogger.com,1999:blog-7736342076662473845.post-78509414846694711592013-07-07T05:15:00.000-07:002013-07-07T05:15:01.283-07:00Encapsulation - Let's Hide<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Arial, Helvetica, sans-serif;"><b>Encapsulation ... </b></span><br />
<br />
<span style="font-family: Arial, Helvetica, sans-serif;">In Simple words it means <b>"the process of enclosing" </b>which further means <b>"surround completely". </b></span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">Encapsulation hides the implementation details from the outside code which in turn provides high flexibility and maintainability. The classes thus written are loosely coupled and change in one class doesn't accounts for a change in some other class that access it. Due to this we can rewrite our class to append any new change without affecting any other code.</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">Now. How can we do that ?</span><br />
<ol style="text-align: left;">
<li><span style="font-family: Arial, Helvetica, sans-serif;">By keeping Instance variables protected from outside code (usually making it private)</span></li>
<li><span style="font-family: Arial, Helvetica, sans-serif;">By creating accessor methods and restrict outside code from directly accessing the instance variables (use JavaBeans naming convention to create accessors).</span></li>
</ol>
<span style="font-family: Arial, Helvetica, sans-serif;"> </span></div>
Anonymoushttp://www.blogger.com/profile/08652669950315650050noreply@blogger.com0tag:blogger.com,1999:blog-7736342076662473845.post-17505969731186912162013-05-08T11:21:00.002-07:002013-05-08T11:39:22.014-07:00Difference between an Interface and an Abstract Class ???<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: "Courier New",Courier,monospace;">Let's first summarize the two of them ...</span><br />
<br style="font-family: "Courier New",Courier,monospace;" />
<span style="font-family: "Courier New",Courier,monospace;"><b>An Interface</b> - is much like an agreement that declares the characteristics and capabilities of a Class that implements it.</span><br />
<br style="font-family: "Courier New",Courier,monospace;" />
<span style="font-family: "Courier New",Courier,monospace;"><b>An Abstract Class</b> - is more like a conceptual subset of a class that extends it.</span><br />
<br style="font-family: "Courier New",Courier,monospace;" />
<span style="font-family: "Courier New",Courier,monospace;">Now, Lets talk about them in depth ...</span><br />
<br />
<b style="font-family: "Courier New",Courier,monospace;">Abstract Classes ...</b><br />
<ul style="text-align: left;">
<li><span style="font-family: "Courier New",Courier,monospace;">cannot be instantiated </span></li>
<li><span style="font-family: "Courier New",Courier,monospace;">have abstract methods i.e only declaration part of a method</span></li>
<li><span style="font-family: "Courier New",Courier,monospace;">can also have non-abstract methods</span></li>
<li><span style="font-family: "Courier New",Courier,monospace;">can have data members as well</span></li>
<li><span style="font-family: "Courier New",Courier,monospace;">must be subclassed to be used</span></li>
<li><span style="font-family: "Courier New",Courier,monospace;">must be declared as abstract</span></li>
<li><span style="font-family: "Courier New",Courier,monospace;">can extend other classes</span></li>
<li><span style="font-family: "Courier New",Courier,monospace;">can implement interfaces </span></li>
</ul>
<blockquote class="tr_bq">
<div style="text-align: left;">
<span style="font-family: "Courier New",Courier,monospace;">public abstract Class Automobile {</span></div>
<div style="text-align: left;">
<span style="font-family: "Courier New",Courier,monospace;"> private int wheels;</span></div>
<div style="text-align: left;">
<span style="font-family: "Courier New",Courier,monospace;"> public abstract travel();</span></div>
<div style="text-align: left;">
<span style="font-family: "Courier New",Courier,monospace;">} </span></div>
</blockquote>
<div style="text-align: left;">
<br style="font-family: "Courier New",Courier,monospace;" />
<b><span style="font-family: "Courier New",Courier,monospace;">Interface ...</span></b></div>
<ul style="text-align: left;">
<li><span style="font-family: "Courier New",Courier,monospace;">is much like 100% abstract classes</span></li>
<li><span style="font-family: "Courier New",Courier,monospace;">cannot be instantiated</span></li>
<li><span style="font-family: "Courier New",Courier,monospace;">data members are implicitly public static final</span></li>
<li><span style="font-family: "Courier New",Courier,monospace;">methods are implicitly public abstract</span></li>
<li><span style="font-family: "Courier New",Courier,monospace;">can only extend other interfaces, it cannot extend a class or implement an interface</span></li>
<li><span style="font-family: "Courier New",Courier,monospace;">can be used polymorphically</span></li>
<li><span style="font-family: "Courier New",Courier,monospace;">can be implemented by any class within any hierarchy </span><br /><div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span style="font-family: "Courier New",Courier,monospace;">public interface moveable {</span></div>
<div style="text-align: left;">
<span style="font-family: "Courier New",Courier,monospace;"> int wheels;</span></div>
<div style="text-align: left;">
<span style="font-family: "Courier New",Courier,monospace;"> travel();<br />
</span></div>
<div style="text-align: left;">
<span style="font-family: "Courier New",Courier,monospace;">} </span></div>
</li>
</ul>
<div style="text-align: left;">
</div>
</div>
Anonymoushttp://www.blogger.com/profile/08652669950315650050noreply@blogger.com0tag:blogger.com,1999:blog-7736342076662473845.post-15862498076728449312013-05-05T00:52:00.000-07:002013-05-05T00:52:01.734-07:00What is Casting ???<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="font-family: "Courier New",Courier,monospace;">
<b>Casting lets you convert one type of value into another.</b></div>
<div style="font-family: "Courier New",Courier,monospace;">
<b>In general terms casts can be implicit as well as explicit.</b></div>
<div style="font-family: "Courier New",Courier,monospace;">
<b>Let's dig some more into it ...</b></div>
<div style="font-family: "Courier New",Courier,monospace;">
<b><br /></b></div>
<div style="font-family: "Courier New",Courier,monospace;">
Implicit casts are those for which you do not have to write the code yourself i.e. it happens automatically. for Explicit casts code is required to be written by the developer.</div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
Implicit casts are the often termed as widening conversions, for a smaller value can always be put into a larger container if the semantics of the data types involved are appropriate.</div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
Explicit casts are used for narrowing conversions where a larger value is to be fit into a smaller container. Again the semantics of the involved data types must be appropriate.</div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
Casting can be described in terms of</div>
<div style="font-family: "Courier New",Courier,monospace;">
1. Primitive Data Types</div>
<div style="font-family: "Courier New",Courier,monospace;">
2. Reference Variables</div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
<b>Let's talk about Primitive Data Type Casting first...</b></div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;">int a = 100;</span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;">long aa = a; // Implicit cast, an int can always fit in a long </span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;">float f = 100.234;</span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;">int i = (float)f; // Explicit cast, float will loose it's decimal notation</span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;"><span style="font-size: small;">In the above two examples Implicit and Explicit casts are shown in their simplest forms... let's see a few more examples to understand.</span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;">int i = 100.234; // error; a cast is very much required.</span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;">int i = (int) 100.234; // works</span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: x-small;"></span><br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><b>Let's take a look at casting involving Reference Variables</b></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;">Suppose, we have this hierarchy ... </span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<b><span style="font-size: small;">Animal >>> Mammal >>> Dolphin</span></b></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><br /></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;">Dolphin dolphin = new Dolphin();</span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;">Mammal mammal = dolphin;</span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;">Animal animal = mammal;</span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;">Above written code is a classic example of an Implicit casting or sometimes also referred to as Upcasting.</span></span></span></div>
<br />
<br />
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;">if(animal instanceof Mammal) {</span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"> mammal = (Mammal) animal;</span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;">}</span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;">Above code is an example of an Explicit cast or a Downcast.</span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;">In case of an Upcast, an error can only arise if the two reference variables do not fall in the same hierarchy or if the container reference falls below in the same hierarchy.</span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;">Implicit cast also comes with a small cost. When you upcast a reference variable, it can then only call those methods and only access those data members which are a part of the new Reference type.</span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;">Class Animal { // Animal Class</span></span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;"> public int getLegs() {</span></span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;"> return 4;</span></span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;"> }</span></span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;">}</span></span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;">Class Mammal extends Animal { // Mammal Class</span></span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;"> public int getEyes() {</span></span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;"> return 2;</span></span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;"> }</span></span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;">}</span></span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<br /></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;">Class Test {</span></span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;"> public static void main(String... s) { </span></span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;"> Mammal mammal = new Mammal();</span></span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;"> Animal animal = mammal; // correct, legal</span></span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;"> </span></span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;"> mammal.getLegs(); // correct </span></span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;"> mammal.getEyes(); // incorrect, Eyes() is a part of the mammal class</span></span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;"> } </span></span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;">} </span></span> </span></span></div>
<br />
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-family: "Courier New", Courier, monospace;">In the above example mammal.getEyes() will throw an Exception at runtime because Animal class has no knowledge of the getEyes() present in the Mammal class. Reference variable "animal" can only be used to call those methods which are present in the Animal Class only.</span></span></span></span></span></span><br />
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><span style="font-size: x-small;"><span style="font-size: small;"><br /> </span></span></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><br /></span></div>
<div style="font-family: "Courier New",Courier,monospace;">
<span style="font-size: small;"><br /></span></div>
</div>
Anonymoushttp://www.blogger.com/profile/08652669950315650050noreply@blogger.com0tag:blogger.com,1999:blog-7736342076662473845.post-90221964356733822013-04-30T08:58:00.002-07:002013-04-30T09:01:31.981-07:00Access Specifiers and Visibility of data member <div dir="ltr" style="text-align: left;" trbidi="on">
<table>
<tr><th>Visibility</th><th>Public</th><th>Protected</th><th>Default</th><th>Private</th></tr>
<tr><td>Within the Same Class</td><td>Yes</td><td>Yes</td><td>Yes</td><td>Yes</td></tr>
<tr><td>From a class within the same package</td><td>Yes</td><td>Yes</td><td>Yes</td><td>No</td></tr>
<tr><td>From a subclass within the same package</td><td>Yes</td><td>Yes</td><td>Yes</td><td>No</td></tr>
<tr><td>From a subclass outside the package</td><td>Yes</td><td>Yes, through Inheritance</td><td>No</td><td>No</td></tr>
<tr><td>From a non-subclass outside the package</td><td>Yes</td><td>No</td><td>No</td><td>No</td></tr>
</table>
<br /></div>
Anonymoushttp://www.blogger.com/profile/08652669950315650050noreply@blogger.com0tag:blogger.com,1999:blog-7736342076662473845.post-50195574824390601982013-04-28T06:15:00.001-07:002013-04-28T06:17:04.463-07:00Difference between equals() and ==<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="font-family: Arial,Helvetica,sans-serif;">
<b>"equals()"</b> and <b>"==</b><b>"</b> are both used for checking the equality of two variables. </div>
<span style="font-family: Arial,Helvetica,sans-serif;">A small difference is there though. </span><br />
<br />
<ol style="text-align: left;">
<li><span style="font-family: Arial,Helvetica,sans-serif;"></span><b>equals() </b>checks for "value based equality" while<b> "==" </b>checks for reference equality.</li>
<li><b>equals() </b>is a method and can only be used in conjunction with an object while <b>"==" </b>is an operator and can be used for primitives and objects both</li>
<li><b>equals() </b>is a part of the Object class</li>
<li><b>equals() </b>can be overloaded and you can give your own meaning to it which is not possible with the <b>"==" </b>operator</li>
<li><b>"==" </b>when used with a primitive checks for the value help by the variables. On the other side when <b>"==" </b>is used with a reference variable it checks if the two reference variables point to the same object.</li>
</ol>
</div>
Anonymoushttp://www.blogger.com/profile/08652669950315650050noreply@blogger.com0tag:blogger.com,1999:blog-7736342076662473845.post-78696764523791024042013-04-27T03:14:00.000-07:002013-04-27T03:18:46.785-07:00How Pass by Value works in Java ???<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Arial, Helvetica, sans-serif;">In Java <b>Pass by Value </b>is everywhere ... whenever you pass an argument to a method in Java it is passed as a <b>value. </b></span><br />
<br />
<span style="font-family: Arial, Helvetica, sans-serif;">In Java, the primitives datatypes as well as the Class objects are passed by <b>value </b>only. </span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">By <b>value </b>i mean <b>a copy </b>of the original. </span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">Let's take up a small example ...</span><br />
<br />
<blockquote class="tr_bq">
<span style="font-family: Arial, Helvetica, sans-serif;">public int doSomething(int val1, int val2) {</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"> val1 += 5;</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"> val2 += 10;</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"> return val1 + val2;</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">}</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif;">public static void main(String... args) {</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"> int val1 = 5;</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"> int val2 = 10;</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"> System.out.println("val1 = " + val1 + " , val2 = " + val2);</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"> int val3 = doSomething(val1,val2);</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;"><br /> </span><span style="font-family: Arial, Helvetica, sans-serif;">System.out.println("val1 = " + val1 + " , val2 = " + val2 + " , val3 = " + val3);</span><br />
<span style="font-family: Arial, Helvetica, sans-serif;">}</span></blockquote>
<br />
<span style="font-family: Arial, Helvetica, sans-serif;"> As you can see in this example the two syso's print the same value of <b>val1 </b>and <b>val2 </b>before and after they are passed to the <b>doSomething()</b></span><br />
<br />
<span style="font-family: Arial, Helvetica, sans-serif;">Now, if we talk about Class Objects, a <b>copy </b>of the object is <b>passed </b>as an <b>argument </b>to the method and the original object remains unchanged.<b> </b></span></div>
Anonymoushttp://www.blogger.com/profile/08652669950315650050noreply@blogger.com0tag:blogger.com,1999:blog-7736342076662473845.post-2313934418481142902013-04-24T08:41:00.002-07:002013-04-27T03:18:09.385-07:00Difference between 'pass by value' and 'pass by reference'<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: small;"><b><span style="font-family: Arial, Helvetica, sans-serif;">Pass by Value - </span></b></span><span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size: small;">This means passing the actual value of the variable as an argument to some method.</span></span><br />
<br />
<span style="font-family: Arial, Helvetica, sans-serif; font-size: small;"><b>Pass by Reference - </b>This means passing the reference to the variable as an argument to some method.</span><br />
<br />
<span style="font-family: Arial, Helvetica, sans-serif; font-size: small;">Now the question ??? What does Java Use from among the two ??? </span><br />
<span style="font-family: Arial, Helvetica, sans-serif; font-size: small;"><br /></span>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: small;"><b>Java is actually pass by value for all the variables within a single VM.</b></span><br />
<br />
</div>
Anonymoushttp://www.blogger.com/profile/08652669950315650050noreply@blogger.com0tag:blogger.com,1999:blog-7736342076662473845.post-59544898507955391902013-04-23T09:47:00.000-07:002013-04-27T03:17:35.387-07:00How JavaBeans differ from POJO's ???<div dir="ltr" style="text-align: left;" trbidi="on">
<b>JavaBeans</b> are governed by certain Java Specifications which a <b>POJO </b>is free from.<br />
<br />
The JavaBean specification requires a java class to ...<br />
<ol style="text-align: left;">
<li>be <b>Serializable</b></li>
<li>have <b>getters </b>and <b>setters</b></li>
<li>have a no argument</li>
</ol>
JavaBean Property Naming Rules ...<br />
<ol style="text-align: left;">
<li>If the property is not a <b>boolean</b>, the <b>getter </b>method's prefix must be <b>get</b> e.g. getSize()</li>
<li>If the property is a <b>boolean</b>, the <b>getter </b>method's prefix should be either get or is e.g. <b>getEmpty()</b>, <b>isEmpty()</b></li>
<li>The <b>setter </b>method's prefix must be set e.g. <b>setSize()</b></li>
<li>Name of the <b>getter </b>or <b>setter </b>method is completed by changing the case of the first letter of the property name to <b>UPPERCASE </b>e.g. if property is size then methods are <b>getSize()</b> and <b>getSize()</b></li>
<li><b>setter </b>methods should be marked <b>public </b>with <b>void </b>return type with an <b>argument </b>that represents the property type</li>
<li><b>getter </b>methods should be marked <b>public</b>, take no arguments, and have a return type that matches the argument type of the setter method</li>
</ol>
</div>
Anonymoushttp://www.blogger.com/profile/08652669950315650050noreply@blogger.com0tag:blogger.com,1999:blog-7736342076662473845.post-81806933678457626282013-04-21T23:17:00.001-07:002013-04-27T03:16:53.172-07:00What is a Plain Old Java Object or POJO ???<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
A "POJO" is an ordinary Java Object which does not follow any of the major Java object models, conventions, or frameworks. <span style="line-height: 19.1875px;"> </span><br />
<br />
A "POJO" however is bound by the restrictions forced by the Java Language Specification.</div>
<br />
<span style="font-family: sans-serif; font-size: medium;"><span style="line-height: 19.1875px;">Some of these restrictions are ... </span></span><br />
<br />
<ol style="text-align: left;">
<li><span style="font-family: sans-serif; font-size: small;"><span style="line-height: 19.1875px;">A <b>POJO </b>cannot extend any Class</span></span></li>
<li><span style="font-family: sans-serif; font-size: small;"><span style="line-height: 19.1875px;">A <b>POJO </b>cannot implement any Interface</span></span></li>
<li><span style="font-family: sans-serif; font-size: small;"><span style="line-height: 19.1875px;">A <b>POJO </b>cannot contain any prespecified annotation </span></span></li>
</ol>
<div>
<span style="font-family: sans-serif; font-size: small;"><span style="line-height: 19.1875px;">An example of a simple POJO may be ...</span></span></div>
<div>
<span style="font-family: sans-serif; font-size: x-small;"><span style="line-height: 19.1875px;"><br /></span></span></div>
<blockquote class="tr_bq">
<pre class="de1" style="background-color: #f9f9f9; background-image: none; border: 0px none white; font-family: monospace, monospace; font-size: 13px; line-height: 1.2em; padding: 0px; vertical-align: top;"><span class="kw1" style="font-weight: bold;">public</span> <span class="kw1" style="font-weight: bold;">class</span> POJO <span class="br0" style="color: #009900;">{</span>
<span class="kw1" style="font-weight: bold;">private</span> <span class="kw3" style="color: #003399;">String</span> someProperty<span class="sy0" style="color: #339933;">;</span>
<span class="kw1" style="font-weight: bold;">public</span> <span class="kw3" style="color: #003399;">String</span> getSomeProperty<span class="br0" style="color: #009900;">(</span><span class="br0" style="color: #009900;">)</span> <span class="br0" style="color: #009900;">{</span>
<span class="kw1" style="font-weight: bold;">return</span> someProperty<span class="sy0" style="color: #339933;">;</span>
<span class="br0" style="color: #009900;">}</span>
<span class="kw1" style="font-weight: bold;">public</span> <span class="kw4" style="color: #000066; font-weight: bold;">void</span> setSomeProperty<span class="br0" style="color: #009900;">(</span><span class="kw3" style="color: #003399;">String</span> someProperty<span class="br0" style="color: #009900;">)</span> <span class="br0" style="color: #009900;">{</span>
<span class="kw1" style="font-weight: bold;">this</span>.<span class="me1" style="color: #006633;">someProperty</span> <span class="sy0" style="color: #339933;">=</span> someProperty<span class="sy0" style="color: #339933;">;</span>
<span class="br0" style="color: #009900;">}</span>
<span class="br0" style="color: #009900;">}</span></pre>
</blockquote>
</div>
Anonymoushttp://www.blogger.com/profile/08652669950315650050noreply@blogger.com0