Monday, February 22, 2010

Installing Ubuntu inside Windows XP using VirtualBox



The screenshots in this tutorial use Ubuntu 7.10 (which is no longer supported), but the same principles apply also to Ubuntu 8.04, 8.10, 9.04, and 9.10. Actually, you can install pretty much any Linux distribution this way.
Introduction
VirtualBox allows you to run an entire operating system inside another operating system. Please be aware that you should have a minimum of 512 MB of RAM. 1 GB of RAM or more is recommended.
Comparison to Dual-Boot
Many websites (including the one you're reading) have tutorials on setting up dual-boots between Windows and Ubuntu. A dual-boot allows you, at boot time, to decide which operating system you want to use. Installing Ubuntu on a virtual machine inside of Windows has a lot advantages over a dual-boot (but also a few disadvantages).
Advantages of virtual installation
  • The size of the installation doesn't have to be predetermined. It can be a dynamically resized virtual hard drive.
  • You do not need to reboot in order to switch between Ubuntu and Windows.
  • The virtual machine will use your Windows internet connection, so you don't have to worry about Ubuntu not detecting your wireless card, if you have one.
  • The virtual machine will set up its own video configuration, so you don't have to worry about installing proprietary graphics drivers to get a reasonable screen resolution.
  • You always have Windows to fall back on in case there are any problems. All you have to do is press the right Control key instead of rebooting your entire computer.
  • For troubleshooting purposes, you can easily take screenshots of any part of Ubuntu (including the boot menu or the login screen).
  • It's low commitment. If you later decide you don't like Ubuntu, all you have to do is delete the virtual hard drive and uninstall VirtualBox.
  • You don't have to burn a CD to install Ubuntu easily.
Disadvantages of virtual installation
  • In order to get any kind of decent performance, you need at least 512 MB of RAM, because you are running an entire operating system (Ubuntu) inside another entire operating system (Windows). The more memory, the better. I would recommend at least 1 GB of RAM.
  • Even though the low commitment factor can seem like an advantage at first, if you later decide you want to switch to Ubuntu and ditch Windows completely, you cannot simply delete your Windows partition (as you would be able to in a dual-boot situation). You would have to find some way to migrate out your settings from the virtual machine and then install Ubuntu over Windows outside the virtual machine.
  • Every time you want to use Ubuntu, you have to wait for two boot times (the time it takes to boot Windows, and then the time it takes to boot Ubuntu within Windows).
Installation Process
The first thing you have to do is obtain VirtualBox. Visit the VirtualBox website's download page.  
Select the appropriate Windows download. In most cases, you should select x86. Use AMD64 only if you know you have a 64-bit processor.
Follow these instructions to get a Ubuntu disk image (.iso file). Your download should take quite a while, at least an hour on a broadband connection.
     
While waiting for Ubuntu to download, you can install VirtualBox. The setup is just like with most Windows software. Double-click the installation file you downloaded earlier. Then keep clicking through the installation wizard. The default answers should work fine.

Next, start up VirtualBox from the Start menu. If, for some reason, it doesn't show up in the menu, you can also find it in C:\Program Files\innotek VirtualBox\VirtualBox.exe

Click New to set up a new virtual machine profile.

Click Next

Title your virtual machine. Here I called it Ubuntu. The type of OS is probably Linux 2.6, but if you don't know the OS type, there is also an option for unknown.

VirtualBox will try to guess how much RAM to allocate for the virtual machine. Since my computer has 512 MB of RAM, it decided 256 MB would be good (I agree). If you have 1 GB of RAM, 512 MB might be a good allocation.

You probably don't have a virtual hard drive to install Ubuntu to, so go ahead and create a new one.
    
It doesn't hurt to go with the defaults for the virtual hard drive creation process.

The next thing we want to do is click on the CD-ROM settings.
   
Add a CD to mount and select the .iso file you downloaded from the Ubuntu website.

Now you're ready to get started! Select the newly created virtual machine profile and click Start.

Select Start or Install Ubuntu
 
After it boots up, click the Install icon on the desktop.
     
Answer all the questions. If you don't know the answer, just go with the defaults.

Wait for Ubuntu to install. This can take anywhere between fifteen minutes and an hour, depending on your computer's specifications.

Instead of rebooting right away, choose to continue to use the "live" session.
 
Then, go to System > Quit > Shut down to shut down your virtual machine.
 
After it has shut down, restart VirtualBox and change the CD-ROM settings so that you are no longer using the .iso you downloaded (you won't need it any more, now that you've installed Ubuntu).
 
This time, when Ubuntu boots up, you'll get a log in screen and can actually start using your installation!


From http://www.psychocats.net/ubuntu/virtualbox

In case anyone is interested in virtualization take a look at http://www.virtualbox.org/



Please check with Manuals. Author is NOT responsible for any misinformation / incorrect information or typographical errors.

NOTE: If you would like to share any useful info, please mail to the authors.

Thursday, February 18, 2010

Java Best Practices - Must for Java Developers

Quote 1: Avoid creating unnecessary objects and always prefer to do Lazy Initialization

Object creation in Java is one of the most expensive operation in terms of memory utilization and performance impact. It is thus advisable to create or initialize an object only when it is required in the code.
public class Countries {

 private List countries;

 public List getCountries() {

  //initialize only when required
  if(null == countries) {
   countries = new ArrayList();
  }
  return countries;
 }
}

Quote 2: Never make an instance fields of class public

Making a class field public can cause lot of issues in a program. For instance you may have a class called MyCalender. This class contains an array of String weekdays. You may have assume that this array will always contain 7 names of weekdays. But as this array is public, it may be accessed by anyone. Someone by mistake also may change the value and insert a bug!
public class MyCalender {

 public String[] weekdays =
  {"Sun", "Mon", "Tue", "Thu", "Fri", "Sat", "Sun"};

 //some code 

}
Best approach as many of you already know is to always make the field private and add a getter method to access the elements.
private String[] weekdays =
  {"Sun", "Mon", "Tue", "Thu", "Fri", "Sat", "Sun"};

 public String[] getWeekdays() {
  return weekdays;
 }
But writing getter method does not exactly solve our problem. The array is still accessible. Best way to make it unmodifiable is to return a clone of array instead of array itself. Thus the getter method will be changed to.
public String[] getWeekdays() {
  return weekdays.clone();
 }

Quote 3: Always try to minimize Mutability of a class

Making a class immutable is to make it unmodifiable. The information the class preserve will stay as it is through out the lifetime of the class. Immutable classes are simple, they are easy to manage. They are thread safe. They makes great building blocks for other objects.
However creating immutable objects can hit performance of an app. So always choose wisely if you want your class to be immutable or not. Always try to make a small class with less fields immutable.
To make a class immutable you can define its all constructors private and then create a public static method
to initialize and object and return it.
public class Employee {

 private String firstName;
 private String lastName;

 //private default constructor
 private Employee(String firstName, String lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
 }

 public static Employee valueOf (String firstName, String lastName) {
  return new Employee(firstName, lastName);
 }
}

Quote 4: Try to prefer Interfaces instead of Abstract classes

First you can not inherit multiple classes in Java but you can definitely implements multiple interfaces. Its very easy to change the implementation of an existing class and add implementation of one more interface rather then changing full hierarchy of class.
Again if you are 100% sure what methods an interface will have, then only start coding that interface. As it is very difficult to add a new method in an existing interface without breaking the code that has already implemented it. On contrary a new method can be easily added in Abstract class without breaking existing functionality.

Quote 5: Always try to limit the scope of Local variable

Local variables are great. But sometimes we may insert some bugs due to copy paste of old code. Minimizing the scope of a local variable makes code more readable, less error prone and also improves the maintainability of the code.
Thus, declare a variable only when needed just before its use.
Always initialize a local variable upon its declaration. If not possible at least make the local instance assignednull value.

Quote 6: Try to use standard library instead of writing your own from scratch

Writing code is fun. But “do not reinvent the wheel”. It is very advisable to use an existing standard library which is already tested, debugged and used by others. This not only improves the efficiency of programmer but also reduces chances of adding new bugs in your code. Also using a standard library makes code readable and maintainable.
For instance Google has just released a new library Google Collections that can be used if you want to add advance collection functionality in your code.

Quote 7: Wherever possible try to use Primitive types instead of Wrapper classes

Wrapper classes are great. But at same time they are slow. Primitive types are just values, whereas Wrapper classes are stores information about complete class.
Sometimes a programmer may add bug in the code by using wrapper due to oversight. For example, in below example:
int x = 10;
int y = 10;

Integer x1 = new Integer(10);
Integer y1 = new Integer(10);

System.out.println(x == y);
System.out.println(x1 == y1);
The first sop will print true whereas the second one will print false. The problem is when comparing two wrapper class objects we cant use == operator. It will compare the reference of object and not its actual value.
Also if you are using a wrapper class object then never forget to initialize it to a default value. As by default all wrapper class objects are initialized to null.
Boolean flag;

if(flag == true) {
 System.out.println("Flag is set");
} else {
 System.out.println("Flag is not set");
}
The above code will give a NullPointerException as it tries to box the values before comparing with true and as its null.

Quote 8: Use Strings with utmost care.

Always carefully use Strings in your code. A simple concatenation of strings can reduce performance of program. For example if we concatenate strings using + operator in a for loop then everytime + is used, it creates a new String object. This will affect both memory usage and performance time.
Also whenever you want to instantiate a String object, never use its constructor but always instantiate it directly. For example:
//slow instantiation
String slow = new String("Yet another string object");

//fast instantiation
String fast = "Yet another string object";

Quote 9: Always return empty Collections and Arrays instead of null

Whenever your method is returning a collection element or an array, always make sure you return emptyarray/collection and not null. This will save a lot of if else testing for null elements. For instance in below example we have a getter method that returns employee name. If the name is null it simply return blank string “”.
public String getEmployeeName() {
 return (null==employeeName ? "": employeeName);
}

Quote 10: Defensive copies are savior

Defensive copies are the clone objects created to avoid mutation of an object. For example in below code we have defined a Student class which has a private field birth date that is initialized when the object is constructed.
public class Student {
 private Date birthDate;

 public Student(birthDate) {
  this.birthDate = birthDate;
 }

 public Date getBirthDate() {
  return this.birthDate;
 }
}
Now we may have some other code that uses the Student object.
public static void main(String []arg) {

 Date birthDate = new Date();
 Student student = new Student(birthDate);

 birthDate.setYear(2019);

 System.out.println(student.getBirthDate());
}
In above code we just created a Student object with some default birthdate. But then we changed the value of year of the birthdate. Thus when we print the birth date, its year was changed to 2019!
To avoid such cases, we can use Defensive copies mechanism. Change the constructor of Student class to following.
public Student(birthDate) {
 this.birthDate = new Date(birthDate);
}
This ensure we have another copy of birthdate that we use in Student class.

Quote 11: Never let exception come out of finally block

Finally blocks should never have code that throws exception. Always make sure finally clause does not throw exception. If you have some code in finally block that does throw exception, then log the exception properly and never let it come out :)

Quote 12: Never throw “Exception”

Never throw java.lang.Exception directly. It defeats the purpose of using checked Exceptions. Also there is no useful information getting conveyed in caller method.

Quote #13: Avoid floating point numbers

It is a bad idea to use floating point to try to represent exact quantities like monetary amounts. Using floating point for dollars-and-cents calculations is a recipe for disaster. Floating point numbers are best reserved for values such as measurements, whose values are fundamentally inexact to begin with. For calculations of monetary amounts it is better to use BigDecimal.

From Viral

Ganex Improves .. Headline Animator