How to return multiple parameter values in Java

Here’s a java tip I discovered.
Java doesn’t allow functions to return multiple parameters. You can’t pass by reference like in C/C++.
Say you have this function in C or C++:

int get(int a, int* b, int* c)
{
*b = 2 * a;
*c = 3 * a;

return 4 * a;
}

Now you want to write that function in Java. One option is to create an object which stores 3 integer values and return that.
An option involving less work, is the following:
Java lets you pass in an object, to which you can modify the content of the object, so:
First create a java class which stores an integer:

public class _Integer
{
public int _integer;

public _Integer
{
}
}

Now pass this object to your java function whenever you need to return more than one value:

int a = 1;
_Integer bInteger = new _Integer();
_Integer cInteger = new _Integer();

int d = get(a, bInteger, cInteger);

int get(int a, _Integer b, _Integer c)
{
b._integer = 2 * a;
c._integer = 3 * a;

return 4 * a;
}

If you need to pass doubles, create a _Double class, etc.

I thought that was a pretty cool workaround I came up with and just wanted to share.:)

More Java Info

How to get user defined environment variable in Java

I’m porting a C++ library over to Java 1.4.2 and I need to get the setting for a user defined environment variable. There used to be a very easy way, but it turns out some brilliant people decided to deprecate – get rid of – System.getenv for java 1.4. There must have been complaints because for Java 1.5 System.getenv is undeprecated. Unfortunately, I can’t guarantee the users of my java software will be using java 1.5. Fortunately, there are people much smarter than I. I did some searching and found a website with some code which determines what system you are on and then calls the corresponding function to get all environment variables. You can then make a call to get the value of the environment variable you need. Here’s the link to the site and the code itself:

import java.io.*;
import java.util.*;

public class ReadEnv {
public static Properties getEnvVars() throws Throwable {
Process p = null;
Properties envVars = new Properties();
Runtime r = Runtime.getRuntime();
String OS = System.getProperty(“os.name”).toLowerCase();
// System.out.println(OS);
if (OS.indexOf(“windows 9”) > -1) {
p = r.exec( “command.com /c set” );
}
else if ( (OS.indexOf(“nt”) > -1)
|| (OS.indexOf(“windows 2000”) > -1 )
|| (OS.indexOf(“windows xp”) > -1) ) {
// thanks to JuanFran for the xp fix!
p = r.exec( “cmd.exe /c set” );
}
else {
// our last hope, we assume Unix (thanks to H. Ware for the fix)
p = r.exec( “env” );
}
BufferedReader br = new BufferedReader
( new InputStreamReader( p.getInputStream() ) );
String line;
while( (line = br.readLine()) != null ) {
int idx = line.indexOf( ‘=’ );
String key = line.substring( 0, idx );
String value = line.substring( idx+1 );
envVars.setProperty( key, value );
// System.out.println( key + ” = ” + value );
}
return envVars;
}

public static void main(String args[]) {
try {
Properties p = ReadEnv.getEnvVars();
System.out.println(“the current value of TEMP is : ” +
p.getProperty(“TEMP”));
}
catch (Throwable e) {
e.printStackTrace();
}
}
}

Java Resources

Copper.net

I think I’ve mentioned before that all I can get in my area for an internet connection is dial up. Really sucks, but maybe someday there will be a faster option. Anyways, I’ve been using NetZero’s 3g high speed for the past 2 years without much of a problem. Lately though, the high speed part has been very flaky. It keeps disconnecting and speeds have been slow. The other day I got fed up and came across a couple of different providers with good reviews. One is Toast.net. I haven’t tried it yet though. The one I’m trying now is Copper.net High Speed. They have a really good deal going on right now, which is why I tried it first. It’s $1 for 3 months, after which you can cancel if not satisfied. So far it’s not bad. Netzero would always take a long time to load certain sites, such as blogs, including this one. Copper.net doesn’t seem to have that problem. My blog actually loads pretty quick. The only drawback so far is Copper.net’s high speed also seems to have a problem staying connected initially. Once it gets going though it is fine. So far connection speeds seem a bit faster than Netzero.
Regular price for the high speed version is $14.95 per month. Without high speed, it’s $9.95 per month. The customer service number is also free. Unlike Netzero, which charges $2 per minute. Most numbers are V.92. You get a free 25mb email address. There seem to be a lot of access numbers, although just 2 for my area, but it’s rural. Copper.net connects faster than Netzero, since it uses the built in dial-up networking in Windows. Netzero has it’s own java application, which is much slower. Copper.net also lets you stay connected for a maximum of 4 hours, whereas Netzero disconnects you after 3 hours. If you’re looking for a new dial up isp or a backup internet connection, you might want to check out Copper.net.
BTW, Copper.net’s high speed is also compatible with the wiflyer.