Java Array toString

Doing a toString on a array don’t really tell that much about the array. Here is one way to get the contents of the array as a string.

int[] myArray = new int[2];
myArray[0] = 1; myArray[1] = 2;
System.out.println("array contains " + java.util.Arrays.toString(myArray));
 
//will output array contains [1, 2]

Java Converting a int to a string

The Integer Class have a static method toString that takes a int as input and gives us back a string.

int myInt = 1;
System.out.println(Integer.toString(myInt);

This is a log more efficient and nice in the long run than just concating the int with a empty string ( myInt + “” ) as that actually creates a bit of overhead. Not much, but still.

Java Converting between Boolean & String

To convert from a Boolean to a string

new Boolean(new String("hej".equalsIgnoreCase("HEJ")).toString();

and then from a string to a boolean

boolean theValue = Boolean.parseBoolean(strBoolean);

Java safe conversion of long to int

In most cases this is really not needed, if possible please keep the long as a long and work with it as such, but if needed the following works

public static int longToInt(long value) {
    if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
        throw new IllegalArgumentException
            (value + " cannot be cast to int without changing its value.");
    }
    return (int) value;
}

The reason that theLong.intValue() is not suggested here is that intValue rounds the value to fit a int – it don’t say it can’t be done.

using RedirectMatch to make Apache2 listen to several dns names and still not get content duplication

The trick we do here is to make one virtualhost for the “main” domain you wish indexed (and shown in browsers etc) and another one for all the other domains that will simply redirect to the main domain.

<VirtualHost *:80>
    ServerName example.com
    ServerAlias example.net
    ServerAlias example.biz
    RedirectMatch permanent /(.*) http://www.example.com/$1
</VirtualHost>
 
<VirtualHost *:80>
    ServerName www.example.com
    ... other stuffs ...
</VirtualHost>

ASP.net Sending mail using gmail

In order to send mail using a gmail address this can be used:

In Web.config

	<system.net>
		<mailSettings>
			<smtp from="myemail@gmail.com" deliveryMethod="Network">
				<network
					defaultCredentials="true"
					host="smtp.gmail.com"
					port="587"
					enableSsl="true" />
			</smtp>
		</mailSettings>
	</system.net>

And then in the code (in this case C# is used)

SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = true; //reads the settings from web.config
smtpClient.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword");
//and then send the message
smtpClient.Send(mailMessage);

Or if you don’t wish to save any settings in web.config

string host = "smtp.gmail.com";
int port = 587;
SmtpClient smtpClient = new SmtpClient(host, port);
smtpClient.UseDefaultCredentials = false;
smtpClient.EnableSsl = true;
smtpClient.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword");
//and then send the message contained in mailMessage
smtpClient.Send(mailMessage);

SmtClient at msdn
It might also be interesting to read up on mailmessage