Passing a String by Refference in Java


In some case we need to return more than one value in one call of method. For example  on SMS Receiver process, I want to process received SMS, than return message reply and receiver number based on received SMS. In C# there is syntax out, that possible to return more than one value. But how about Java? Yes we can use call by refference, that using method parameter as input and output line. View my code to known this condition please.

public class SMSReceiver{
	private static final String BOS1_Number = "0989889876";
	private static final String BOS2_Number = "0989889890";

	public static void main(String [] args){
		String message = args[0];
		String sender = args[1];
		String receiver = "";
		smsProcessing(message, sender, receiver);

		System.out.println("To: "+receiver);
		System.out.println("Message: "+message);
	}

	private static void smsProcessing(String message, String sender, String receiver){
		String [] vars = message.split("#");

		if(vars[0].equals("BOS1")){
			message = "Dear bos1, there is message for you, "+vars[1];
			receiver = BOS1_Number;
		}else if(vars[0].equals("BOS2")){
			message = "Dear bos2, there is message for you, "+vars[1];
			receiver = BOS2_Number;
		}else{
			message = "Invalid SMS format.";
			receiver = sender;
		}
	}
}
Console Process:
javac SMSReceiver.java
java SMSReceiver BOS1#Halooooooo 09890909080
Result:
To:
Message: BOS1#Halooooooo

In fact String can not passing by Refference. But we can custom our code to do this:

  1. Using StringBuilder
  2. String included in a Class
  3. Array of String

In this section I will implement the smsProcessing method using 3 ways above:

String Builder

smsProcessing method:

private static void smsProcessing(StringBuilder message, StringBuilder sender, StringBuilder receiver){
		String[] vars = message.toString().split("#");
		if(vars[0].equals("BOS1")){
			String msg = "Dear bos1, there is message for you, "+vars[1];
			message.delete(0, message.length()).append(msg);
			receiver.delete(0, receiver.length()).append(BOS1_Number);
		}else if(vars[0].equals("BOS2")){
			String msg = "Dear bos2, there is message for you, "+vars[1];
			message.delete(0, message.length()).append(msg);
			receiver.delete(0, receiver.length()).append(BOS2_Number);
		}else{
			message.delete(0, message.length()).append("Invalid SMS format.");
			receiver.delete(0, receiver.length()).append(sender.toString());
		}
	}

Main method:

	public static void main(String [] args){
		String message = args[0];
		String sender = args[1];
		String receiver = "";

		StringBuilder m = new StringBuilder();
		StringBuilder s = new StringBuilder();
		StringBuilder r = new StringBuilder();

		m.append(message);
		m.append(sender);
		m.append(receiver);

		//smsProcessing(message, sender, receiver);
		smsProcessing(m, s, r);

		System.out.println("To: "+r.toString());
		System.out.println("Message: "+m.toString());
	}
Console Process:
javac SMSReceiver.java
java SMSReceiver BOS1#Haloooooo 09890909080
Result:
To: 0989889876
Message: Dear bos1, there is message for you, Haloooooo

Full Source code

public class SMSReceiver{
	private static final String BOS1_Number = "0989889876";
	private static final String BOS2_Number = "0989889890";

	public static void main(String [] args){
		String message = args[0];
		String sender = args[1];
		String receiver = "";

		StringBuilder m = new StringBuilder();
		StringBuilder s = new StringBuilder();
		StringBuilder r = new StringBuilder();

		m.append(message);
		m.append(sender);
		m.append(receiver);

		//smsProcessing(message, sender, receiver);
		smsProcessing(m, s, r);

		System.out.println("To: "+r.toString());
		System.out.println("Message: "+m.toString());
	}

	private static void smsProcessing(String message, String sender, String receiver){
		String [] vars = message.split("#");

		if(vars[0].equals("BOS1")){
			message = "Dear bos1, there is message for you, "+vars[1];
			receiver = BOS1_Number;
		}else if(vars[0].equals("BOS2")){
			message = "Dear bos2, there is message for you, "+vars[1];
			receiver = BOS2_Number;
		}else{
			message = "Invalid SMS format.";
			receiver = sender;
		}
	}

	private static void smsProcessing(StringBuilder message, StringBuilder sender, StringBuilder receiver){
		String[] vars = message.toString().split("#");
		if(vars[0].equals("BOS1")){
			String msg = "Dear bos1, there is message for you, "+vars[1];
			message.delete(0, message.length()).append(msg);
			receiver.delete(0, receiver.length()).append(BOS1_Number);
		}else if(vars[0].equals("BOS2")){
			String msg = "Dear bos2, there is message for you, "+vars[1];
			message.delete(0, message.length()).append(msg);
			receiver.delete(0, receiver.length()).append(BOS2_Number);
		}else{
			message.delete(0, message.length()).append("Invalid SMS format.");
			receiver.delete(0, receiver.length()).append(sender.toString());
		}
	}
}

For 2nd and 3rd method, insya Alloh i will explain in other time

About windupurnomo

I'm interested in programming. I am active with several programming such as Java, C #, C, JavaScript, HTML. I'm also develop desktop application (Java Swing), Mobile Application (Android), and Web programming (ASP MVC).
This entry was posted in Java and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s