AIM: Develop programs based on variation in methods i.e. passing by value, returning values and returning objects from methods.
PassByValue.java
Output
PassByReference.java
Output
ReturningValue.java
Output
ReturningObject.java
Output
Happy Coding :)
PassByValue.java
class PassByValue { String name="java"; void display(String name) { name="Java Programming"; } public static void main(String arg[]) { PassByValue p=new PassByValue(); System.out.println("Before changes : "+p.name); p.display("C++"); System.out.println("After changes : "+p.name); } }
Output
Pass by value in java by practical server |
PassByReference.java
class PassByReference { String name="Java"; void display(PassByReference p) { name="Java Programming"; } public static void main(String arg[]) { PassByReference p=new PassByReference(); System.out.println("Before changes : "+p.name); p.display(p); System.out.println("After changes : "+p.name); } }
Output
pass by reference in java by practical server |
ReturningValue.java
class ReturningValue { int n1,n2; void getValue(int a,int b) { n1=a; n2=b; } int returnMax() { return n1>n2?n1:n2; } public static void main(String arg[]) { ReturningValue R=new ReturningValue(); R.getValue(12,44); System.out.println("Maximum is : "+R.returnMax()); } }
Output
Returning value in java by practical server |
ReturningObject.java
class ReturningObject { int n1,n2; ReturningObject(int a,int b) { n1=a; n2=b; } ReturningObject minNumber() { ReturningObject min=new ReturningObject(55,35); return min; } public static void main(String arg[]) { ReturningObject R1=new ReturningObject(66,234); ReturningObject R2=R1.minNumber(); int minR1=R1.n1<R1.n2?R1.n1:R1.n2; System.out.println("Minimum of (R1) "+R1.n1+" & "+R1.n2+" is "+minR1); int minR2=R2 .n1<R2.n2?R2.n1:R2.n2; System.out.println("Minimum of (R2) "+R2.n1+" & "+R2.n2+" is "+minR2); } }
Output
returning object in java by practical server |
Happy Coding :)
0 Comments
Post a Comment