Thursday, 8 October 2015

Java Program - Relationship between Objects


public class First {

public static void main(String[] args) {
// TODO Auto-generated method stub
Sec obj1 = new Sec(20);
Demo obj2 = new Demo(obj1);
obj2.display();
}
}


public class Demo {
Sec t;
int x;
Demo(Sec t){
this.t=t;
x=10;
}
void display(){
System.out.println("The value of x is " +x);
System.out.println("The value of y in Other object is " +t.i);
}
}


public class Sec {
int i;
Sec(int i){
this.i=i;
}
void display(){
System.out.println("The value of i is " +i);
}
}


output:-
The value of x is 10
The value of y in Other object is 20

Wednesday, 7 October 2015

Java - Swapping values within Object

package passing_32_blog;

public class Pass {

public static void main(String[] args) {
// TODO Auto-generated method stub
First obj1 = new First(10,20);
First obj2 = new First(30,40);
System.out.println("The values before swap are " +obj1.i+"\t" +obj1.j);
obj2.swap(obj1);
System.out.println("The values after swap are " +obj1.i+"\t" +obj1.j);
}
}


package passing_32_blog;

public class First {
int i,j;
First (int k,int l){
i=k;
j=l;
}
void swap(First s){
int temp;
temp = s.i;
s.i=s.j;
s.j=temp;
}
}

Output:-
The values before swap are 10 20
The values after swap are    20 10

Java Program - Passing values to methords


public class Passing {

public static void main(String[] args) {
// TODO Auto-generated method stub
Demo obj1 = new Demo(10);
Demo obj2 = new Demo(20);
Demo obj3 = new Demo(30);
System.out.println("The values before calling swap " +obj1.i + "\t" +obj2.i);
obj3.swap(obj1.i, obj2.i);
System.out.println("The values after calling swap " +obj1.i + "\t"  +obj2.i);
}

}


public class Demo {
int i;
Demo(int i){
this.i=i;
}

void swap(int i,int j){
int temp;
temp=i;
i=j;
j=temp;
}
}

Output:-
The values before calling swap 10 20
The values after calling swap 10 20

Java Program - Passing Objects to the methords

import java.io.*;
import java.util.*;
import java.lang.*;
class Acceptt{
public static void main(String args[]) throws IOException{
First obj1 = new First(10);
First obj2 = new First(20);
Second obj3 = new Second();
System.out.println("The HashCode value of Obj1 is " +obj1.hashCode());
System.out.println("The HashCode Value of Obj2 is " +obj2.hashCode());
System.out.println("The value of obj1 is " +obj1.i);
System.out.println("The value of obj2 is " +obj2.i);
obj3.check(obj1, obj2);
System.out.println("In the main Function");
System.out.println("The HashCode value of Obj1 is " +obj1.hashCode());
System.out.println("The HashCode Value of Obj2 is " +obj2.hashCode());
System.out.println("The value of obj1 is " +obj1.i);
System.out.println("The value of obj2 is " +obj2.i);

}
}

class First{

int i;

First(int j){

this.i=j;
}
}

class Second{
void check(First k,First d){
int temp;
temp=k.i;
k.i=d.i;
d.i=temp;
System.out.println("In the sub Function");
System.out.println("The HashCode value of Obj1 is " +k.hashCode());
System.out.println("The HashCode Value of Obj2 is " +d.hashCode());
System.out.println("In the sub Function");

System.out.println("The value of obj1 is " +k.i);
System.out.println("The value of obj2 is " +d.i);
}

}


Output :-
The HashCode value of Obj1 is 366712642
The HashCode Value of Obj2 is 1829164700
The value of obj1 is 10
The value of obj2 is 20
In the sub Function
The HashCode value of Obj1 is 366712642
The HashCode Value of Obj2 is 1829164700
In the sub Function
The value of obj1 is 20
The value of obj2 is 10
In the main Function
The HashCode value of Obj1 is 366712642
The HashCode Value of Obj2 is 1829164700
The value of obj1 is 20
The value of obj2 is 10

Monday, 5 October 2015

All Objects share the same copy of a static variable and Static variables are stored on method area


public class Acceptt {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Accepttt.k++;
  Accepttt.display();
  Accepttt obj1 = new Accepttt();
  Accepttt obj2 = new Accepttt();
  obj1.k++;
  obj1.display();
  obj2.display();
 }

}


class Accepttt {
 static int k=10;
static void display()
{
System.out.println("The value of k is " +k);
}
}


Output:-
The value of k is 11
The value of k is 12
The value of k is 12


Static method example in java

/**
 *
 */

/**
 *
 *
 */
public class Staticmethords {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
      int m = Staticm.sum(10,20);
      System.out.println("The value is " +m);
}
}



public class Staticm {
static int l;
static int sum(int i,int k){
//return (k+i);
l=i+k;
return (l);
}
}

Output:-
The value is 30

Setter and Getter methods example in java

/**
 *
 */

/**
 *
 *
 */
public class Demo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
First age = new First();
age.setage(20);
age.setname("Sai Nagendra Bhavani Prasad battipati");
int k = age.getage();
String name = age.getname();
System.out.println("age is " +k);
System.out.println("name " +name);
}

}




public class First {
int age;
String name;

void setage(int age){
this.age=age;
}
void setname(String name){
this.name=name;
}
int getage(){
return age;
}
String getname(){
return name;
}
}


Output:-
age is 20
name Sai Nagendra Bhavani Prasad battipati