RSS

Monthly Archives: December 2014

Understanding Java pass by value


I think most of the people they are confused with this concept because it is appearing in different angles. First thing I have to say that Java is pass by Value not pass by reference. I’ll show the example to clear the concept.

First of all I’ll show the sample code, then you can see the confused point that you will drive to think that java is pass by reference. you can checkout the full source here.

package sample.passby.value;

/**
 * Point class
 * 
 * @author malalanayake
 *
 */
public class Point {
	private int x;
	private int y;

	public Point(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

}
package sample.passby.value;

/**
 * Demonstration of Java pass by value
 * 
 */
public class App {
	public static void main(String[] args) {
		int xOrginal = 0, yOriginal = 0;
		System.out.println("Original Coordinates:" + xOrginal + "," + yOriginal);
		App.getNextCoordinates(xOrginal, yOriginal);
		System.out.println("After modification of Coordinates:" + xOrginal
				+ "," + yOriginal);

		Point pOriginal = new Point(0, 0);
		System.out.println("Original Point:" + pOriginal.getX() + ","
				+ pOriginal.getY());
		App.getNextCoordinates(pOriginal);
		System.out.println("After modification of Point:" + pOriginal.getX()
				+ "," + pOriginal.getY());

	}

	public static void getNextCoordinates(int x, int y) {
		x = 12;
		y = 15;
	}

	public static void getNextCoordinates(Point point) {
		point.setX(12);
		point.setY(15);
	}
}

Output:
Original Coordinates:0,0
After modification of Coordinates:0,0
Original Point:0,0
After modification of Point:12,15

So you can see in the above example if I pass the primitives then it is not going to change the values. But if I pass the object type then if i change something on that it will reflect. The understanding point is Java every time pass by value and if it is primitive then it is just pass the value to the given argument and nothing will reflect. See the following image for understanding.
PassByValue (1)

But if we pass the Object type then java pass the “reference by value”. This is the tricky point that we need to understand.
PassByValue (2)

So you can see if we pass some Object type to some function actually its going to pass the pointer to that Object. That means if we change some thing on that it will be reflect on that. But be aware about this, You cannot change the pointer which is not in the scope. As an example if you do following coding in getNextCoordinates(Point point) method it will not reflecting anything on the original object because it is going to change the pointer of the object which is under that scope not the original object that passed to the function.

public static void getNextCoordinates(Point point) {
                point = new Point(12,15);
}
 
Leave a comment

Posted by on December 8, 2014 in java

 

Tags: , ,

Comparator and Comparable interfaces in Java


c4c3

Comparator and Comparable interfaces are heavily used in the Java development to manage the sorting of the collections. This approach will achieve the software engineering goals such as extensibility, reusability, modularity etc. In this post I like to explain this mechanism of how to use comparator and comparable interfaces. First thing is we are going to use the Collections API for sorting which is provided by Java. So we have to expose sorting comparison logic without exposing the internal structure of our custom implementation.Here you can download the source.

Lets look at how to use Comparable interface.

As an example I have Employee class which is created by me and need to sort the collection of employees according to the employee age.

  • First we need to implement the Comparable interface in Employee class and override the compareTo method
  • Then we can pass the employee collection to the Collections.sort() API to do the sorting.
  • Underneath what will happen Collections.sort() method will access the compareTo implementation and get the decision according to the return value.

See the code sample below to get clear understanding.

package sample.common;

/**
 * Employee Class
 *
 * @author malalanayake
 *
 */
public class Employee implements Comparable<Employee> {
	private int id;
	private String name;
	private int age;
	private int salary;

	public Employee(int id, String name, int age, int salary) {
		this.id = id;
		this.name = name;
		this.age = age;
		this.salary = salary;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public int getSalary() {
		return salary;
	}

	public void setSalary(int salary) {
		this.salary = salary;
	}

	public int compareTo(Employee o) {
		if (o.getAge() == this.age) {
			return 0;
		} else if (o.getAge() > this.age) {
			return -1;
		} else {
			return 1;
		}
	}

	@Override
	public String toString() {
		return "[Employee ID:" + this.getId() + " Name:" + this.getName()
				+ " Age:" + this.getAge() + " Salary:" + this.getSalary() + "]";
	}

}
package sample.app;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import comparator.sample.app.SalaryComparator;
import sample.common.Employee;

/**
 * Application Class
 *
 * @author malalanayake
 *
 */
public class App {
	public static void main(String[] args) {
		List<Employee> employees = new ArrayList<Employee>();

		Employee emp_dinuka = new Employee(101, "Dinuka", 27, 95000);
		employees.add(emp_dinuka);

		Employee emp_malalanayake = new Employee(100, "Malalanayake", 27, 85000);
		employees.add(emp_malalanayake);

		Employee emp_Thanuja = new Employee(305, "Thanuja", 23, 150000);
		employees.add(emp_Thanuja);

		Employee emp_Damith = new Employee(204, "Damith", 20, 75000);
		employees.add(emp_Damith);

		System.out.println("=======Before Sorting=====");
		for (Employee emp : employees) {
			System.out.println(emp.toString());
		}

		Collections.sort(employees);
		System.out.println("=======After Sorting=====");
		for (Employee emp : employees) {
			System.out.println(emp.toString());
		}
	}
}

You can see in the above case we can have only one sorting logic over the employee class. Lets think about sometimes if I need to sort the Employee by their salaries and sometimes I need to sort by their age. Then you can see we don’t have space to put that logic in my Employee class because already I have method implementation according to the age. In this kind of situation Comparator comes to the picture to solve the problem.

Lets look at how to use Comparator

  • So first you have to have comparator implementation class. In above case I have created SalaryComparator class which is provide the salary comparison.
  • Then you can pass the employee collection to overloaded method in collections api to sort the given collection according to the given comparator.
package comparator.sample.app;

import java.util.Comparator;

import sample.common.Employee;

/**
 * Salary Comparator
 *
 * @author malalanayake
 *
 */
public class SalaryComparator implements Comparator<Employee> {

	public int compare(Employee o1, Employee o2) {
		if (o1.getSalary() > o2.getSalary()) {
			return 1;
		}

		if (o1.getSalary() < o2.getSalary()) {
			return -1;
		}
		return 0;
	}

}
package sample.app;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import comparator.sample.app.SalaryComparator;
import sample.common.Employee;

/**
 * Application Class
 *
 * @author malalanayake
 *
 */
public class App {
	public static void main(String[] args) {
		List<Employee> employees = new ArrayList<Employee>();

		Employee emp_dinuka = new Employee(101, "Dinuka", 27, 95000);
		employees.add(emp_dinuka);

		Employee emp_malalanayake = new Employee(100, "Malalanayake", 27, 85000);
		employees.add(emp_malalanayake);

		Employee emp_Thanuja = new Employee(305, "Thanuja", 23, 150000);
		employees.add(emp_Thanuja);

		Employee emp_Damith = new Employee(204, "Damith", 20, 75000);
		employees.add(emp_Damith);

		SalaryComparator comparator = new SalaryComparator();
		Collections.sort(employees, comparator);
		System.out.println("=======After Sorting By Salary=====");
		for (Employee emp : employees) {
			System.out.println(emp.toString());
		}
	}
}

So If you have multiple sorting logics then you have to go for the Comparator because then only you can have multiple logics. Otherwise you can encapsulate the logic inside your custom implementation class by using the Comparable interface. I hope you got the idea of how to use the comparator and comparable interfaces.

 
Leave a comment

Posted by on December 5, 2014 in java, Other

 

Tags: , , , ,

 
%d bloggers like this: