Hot Posts:

Feb 27

Overriding toString() method


The toString() method is inherited by every class from the java.lang.Object class. Whenever an object reference is passed to the System.out.println() method, the object’s toString() method is called.
For example-

public class test {
int x;
public test(int x){
this.x=x;
}
public static void main(String args[]) {
test obj = new test(2);
System.out.println(obj);
}
}

Output-
test@33
The preceding is what you get when toString() method is not overridden. The output is the class name followed by the @ symbol, followed by the unsigned hexadecimal representation of the object’s hashcode. So the above output is not much of a help for a human. So to get a more meaningful information about the objects of a class, you need to override the toString() method.
Modified version of the previous class-

public class test {
int x;
public test(int x){
this.x=x;
}
public  String toString(){
return "Hi this is test class and my x value is "+this.x;
}
public static void main(String args[]) {
test obj = new test(2);
System.out.println(obj);
}
}

Output-
Hi this is test class and my x value is 2

Feb 27

Why to override hashCode() method?


The hashCode() method is inherited by every class from the java.lang.Object class. Before we actually talk about why we need to override the hashCode() method, let us first talk about what is a hashcode? and where it is used?
You can think of hashcode as kind of an object ID number, it isn’t necessarily unique. If you actually try to print an object of a class using System.out.print(), we get the name of the class followed by a @ symbol, followed by the unsigned hexadecimal representation of the object’s hashcode(assuming that the class didn’t override the toString() method).
for example-

public class test
{
public static void main(String args[]) {
test obj = new test();
System.out.println(obj);
}
}

output-
test@1a46e30
Hashcodes are typically used to increase the performance of collections of data. Collections such as HashMap and Hashtable use the hashcode value of the object to determine how the object should be stored in the collection and use the same hashcode value to locate the object in the collection. To understand how the hashcodes work lets look at the following scenario which talks about how some of the collections might use hashcodes.

Let us imagine there are a set of buckets named in the following way “1-10″,”11-20” and so on. Suppose given the word “Test”, we try to find out the hashcode value in the following way. Replace the letters with their corresponding integer value with A being 1, B being 2 and so on. Then we add all the integer values to get the hashcode of that particular word. In this case the value of “Test” will be calculated in the following way

Test = T(20)+E(5)+S(19)+T(20)[Hashcode Algorithm] = 64 [Hashcode] so Test will be put into the bucket marked “60-70”. The flaw of this algorithm is that two words can be put into the same bucket. So while retrieving the word we use the same hashcode algorithm to find out the bucket in which the word is stored and then use the equals() method to compare and get back the correct value. For retrieving the values it is necessary to override both the hashCode() and equals() methods in situations where the objects of a particular class are used as keys in the Collection classes.
Implementing the hashCode():

public class test {
int x;
public test(int x){
this.x=x;
}
public boolean equals(Object o){
test obj=(test)o;
if(obj.x==this.x){
return true;
}
else{
return false;
}
}
public int hashCode(){
return (x*19)+13;
}
public static void main(String args[]) {
test obj = new test();
System.out.println(obj);
}
}

So the hashCode() method will tell the collection on how to store the object and equals() method tells you that if two object’s x values are equal then they are logically equal. For more information on equals() method check out Why to override equals() method?

The general contract of hashCode is:
* Whenever it is invoked on the same object more than once during an execution of a
Java application, the hashCode method must consistently return the same integer,
provided no information used in equals comparisons on the object is modified. This
integer need not remain consistent from one execution of an application to another
execution of the same application.
* If two objects are equal according to the equals(Object) method, then calling the
hashCode method on each of the two objects must produce the same integer result.
* It is not required that if two objects are unequal according to the
equals(java.lang.Object) method, then calling the hashCode method on each of the two
objects must produce distinct integer results. However, the programmer should be aware
that producing distinct integer results for unequal objects may improve the
performance of hashtables. 
Feb 26

OperaTor – Don’t Compromise Your Privacy – Surf Web Anonymously from USB


OperaTor is a software bundle that can be easily installed on a portable memory (pendrive, usb stick, hard drive) to allow anonymous surfing while at an internet cafe, library etc.
It combines the power of the Opera Browser, The Onion Router and Privoxy.
With OperaTor no data will be stored at the computer you plugged your portable memory into.

Feb 26

Watch Free TV on your PC


Watch streaming Internet TV from all over the world with this freebie. Using anyTV means you can watch numerous of international TVs without a television, TV Tuner Card or a satellite receiver. Watch anywhere you have your internet Online. No additional equipments required. Interestingly anyTV is a completly FREE Software. It contains absolutely NO ADWARE, NO SPYWARE, NO REGISTRATION, NO POPUPS, NO MALWARE or other unwanted software.
Give a try.
Download Link: http://www.anytvplayer.com/

Feb 24

P2P on Linux – Configuring Sopcast on Linux


Do you want to enjoy all the freely available P2P stuff on Linux?
SOPCAST is a simple, free way to broadcast video and audio or watch the video and listen to radio on the Internet.
Configuring SopCast on Linux: Basic Download Site

  1. Download SopCast Linux Version 1.1.1: http://download.sopcast.cn/download/sp-auth.tgz
  2. If you don’t have stdc++ 5 in your system, please download the libstdcpp5.tgz http://www.sopcast.com/download/libstdcpp5.tgz
  3. Install GUI Version if you want: http://wiki.magiclinux.org/ftp/haulm/qsopcast/qsopcast-0.3.5-2mgc.i686.rpm and http://wiki.magiclinux.org/ftp/haulm/qsopcast/sp-sc-1.0.2-1mgc.i686.rpm
  4. The SopCast package has installer for VLC Player. Install it.
  5. http://myp2p.eu/ my favorite website which carries lot of P2p Links. Get the Channel_Number for your favorite program.

In console execute:
#./sp-sc sop://broker.sopcast.com:3912/CHANNAL_NUMBER 3908 8908 > /dev/null &
# vlc http://localhost:8908/tv.asf 

Feb 21

Oops – Memory Leaks in Java


Some interesting stuff:
Most people think Java cannot leak memory, but actually that’s not true. Java, unlike languages such as C or Javascript, can have two kinds of memory leaks:

  • True leaks – unreferenced, unrecoverable segments of memory
  • Soft leaks – memory that could be garbage collected, but is “accidentally” referenced

The first kind of memory leak, the true leak, is common to C. It’s trivially easy to write a C program which leaks memory like a sieve by simple putting a call to malloc() inside a tight loop. This creates unbounded amounts of heap memory and eventually runs out of space. Additionally, the memory is lost if you don’t save a pointer to it. However, the same program in Java will not run out of memory.
Unfortunately, though, there is a way to create true memory leaks in java, and that is with a poor implementation of the finalize method,
Read the rest of this entry »

Feb 20

What are RAID Levels? – Redundant Array of Independent Disks


RAID – or Redundant Array of Independent Disks – comes in different flavours from RAID 0 and RAID 1 to combination of those two, and going up to RAID 5 and RAID 10.
You can use either IDE, Serial ATA or SCSI disks in RAID configurations, but not a mixture of any of them.
Some basics for RAID levels:
RAID 0
RAID 0 is simply the combination of two or more hard disks to appear to the computer as one large hard disk. It’s also called striping. You could take two 250 GB hard disks, set them up as RAID 0 and the PC will see them as a single 500 GB hard disk. Once that is configured and setup for all practical purposes the PC will behave like there is only one 500 GB hard disk in the machine.
The main advantage of striping is the extra speed that you will get. Writing to hard disks is the bottleneck in most modern PCs. However with RAID 0 the writing is split across two drives, and the PC is writing simultaneously to both; that does speed things up. You won’t get 100% speed increases but 20-30% is quite possible.
The main disadvantage is that there is double the risk of a hard disk failure. You need only one of your two drives to go faulty and you’ve lost all your data.
RAID 1
RAID 1, also called mirroring, is setting up the two disks such that the second one mirrors the first providing you an up to the minute backup if something ever goes wrong with the first disk. Should the first hard disk fail you simply remove it, put the second disk in it’s place and carry on where you left off.
[Source]
Read the rest of this entry »

Feb 17

Audacity – Best Free Audio Editor, Recorder


Audacity is free, open source cross-platform software for recording and editing sounds.
Some of the important features:

  • Record from microphone, line input, or other sources.
  • Dub over existing tracks to create multi-track recordings.
  • Import and export WAV, AIFF, AU, and Ogg Vorbis files
  • Import and export all file formats supported by libsndfile
  • Edit and mix an unlimited number of tracks.
  • Alter frequencies with Equalization, FFT Filter, and Bass Boost effects
  • Record and edit 16-bit, 24-bit, and 32-bit (floating point) samples
  • Runs on Mac OS X, Windows, and GNU/Linux

Read the rest of this entry »

Feb 16

Why to override equals() method?


To compare two object references either “==” or “equals” method, inherited from class Object, are used. The == operator evaluates to true only when both the references refer to the same object. This is because == only looks at the bits in the variables. So to find out if two references are meaningfully equal then equals() method has to be used. Since String class and the wrapper classes have overridden the equals() method, we could compare the two different objects to see if their contents are meaningfully equivalent.
Read the rest of this entry »

Feb 13

Hadoop – Best Open Source Distributed System


Hadoop is a Free Java software framework that supports distributed applications running on large clusters of commodity computers that process huge amounts of data. It is a top level Apache project.
Hadoop implements a computational paradigm named Map/Reduce, where the application is divided into many small fragments of work, each of which may be executed or reexecuted on any node in the cluster. In addition, it provides a distributed file system (HDFS) that stores data on the compute nodes, providing very high aggregate bandwidth across the cluster. Both Map/Reduce and the distributed file system are designed so that node failures are automatically handled by the framework.
[Source: Wiki]
Read the rest of this entry »