- import static java.lang.Math.max; lets use max(2, 7) in place of Math.max(2, 7);
- A derived class does not inherit private methods of the base class.
- An overridden method must match the signature (+ return type) of its base class counterpart, except that it can have covariant return type. In the overridden method more permissive access modifier can be used, e.g., private -> public not the other way around.
- A final method cannot be overridden and a final class cannot be subclassed.
- Most to least restriction: private, default (inside same package), protected (same package + subclass), and public.
- (a instanceof B) returns true even if a's class is a subclass of B. So, if we wish to check if a's class is exactly equal to B, we need getClass() method.
- Define catch blocks starting with most specific and ending with most general, e.g., NegativeNumberException should come before Exception, if not, NegatveNumberException never gets caught.
- try[-throws]-catch-finally: if no matching catch is there the method ends there, so codes after catch blocks do not get executed. However, finally ensures, even in this case the codes inside it get executed.
- ObjectInputStream and ObjectOutputStream let readObject and writeObject from and to file, if object is serializable. In order for a class to become serializable, all its class-type instance variables must be serializable as well.
- RandomAccessFile lets access bytes in the file at random.
- An abstract class can implement an interface, in that case not all methods mentioned in the interface have to be defined in the abstract class. An interface can extend another interface. All fields in an interface are public, static, and final; usually they are the useful constants.
Wednesday, October 3, 2012
Java
Tuesday, October 2, 2012
Monday, October 1, 2012
Javadoc
A comment must:
1. immediately precede a public item
2. start with /** and end with */
Tags are:
@param name description
@return description
@throws type explanation
@deprecated
@see package_name.class_name
@author name
@version info
usage: javadoc [options] package_name
Options are:
-link link_to_other_docs (may be url to java doc)
-d documentation_directory (where to put after generation)
-author (extracts author info from comments)
-version
-classpath list_of_directories (overrides CLASSPATH)
-private (extracts even the privates, not good)
1. immediately precede a public item
2. start with /** and end with */
Tags are:
@param name description
@return description
@throws type explanation
@deprecated
@see package_name.class_name
@author name
@version info
usage: javadoc
Options are:
-link link_to_other_docs (may be url to java doc)
-d documentation_directory (where to put after generation)
-author (extracts author info from comments)
-version
-classpath list_of_directories (overrides CLASSPATH)
-private (extracts even the privates, not good)
Tuesday, September 25, 2012
Now and Then...
- Pseudocode is a programming language that never complains about syntax errors.
- As long as you are rigorous and precise, you can be as sloppy as you want.
- Quintus: "People should know when they are conquered." Maximus: "Would you, Quintus? Would I?"
- Do not mistake the pointing finger for the moon.
- Say what you mean, mean what you say.
- Gregory (Scotland Yard detective): "Is there any other point to which you would wish to draw my attention?" Holmes: "To the curious incident of the dog in the night-time." Gregory: "The dog did nothing in the night-time." Holmes: "That was the curious incident."
- EXERCISES 4. [HM45] Prove that when n is an integer, n > 2, the equation x^n + y^n = z^n has no solution in positive integers x, y, z.
- A poet once said, "The whole universe is in a glass of wine." We will probably never know in what sense he meant that, for poets do not write to be understood. But it is true that if we look at a glass of wine closely enough we see the entire universe. There are the things of physics: the twisting liquid which evaporates depending on the wind and weather, the reflections in the glass, and our imagination adds the atoms. The glass is a distillation of the earth's rocks, and in its composition we see the secrets of the universe's age, and the evolution of stars. What strange array of chemicals are in the wine? How did they come to be? There are the ferments, the enzymes, the substrates, and the products. There in wine is found the great generalization: all life is fermentation. Nobody can discover the chemistry of wine without discovering, as did Louis Pasteur, the cause of much disease. How vivid is the claret, pressing its existence into the consciousness that watches it! If our small minds, for some convenience, divide this glass of wine, this universe, into parts—physics, biology, geology, astronomy, psychology, and so on—remember that nature does not know it! So let us put it all back together, not forgetting ultimately what it is for. Let it give us one more final pleasure: drink it and forget it all!
Friday, September 21, 2012
A Microsoft Life
A collection of annecdotes from his fifteen years with Microsoft: first in technical support, then in security response, and finally with xbox team. Humors do exist in the book. Do not expect glimpses into a coder's mind, but you will find a human being living his life.
Saturday, September 15, 2012
Why 2's Complement
Say we have 3 bits to represent integers. We have 8 possible numbers.
000
001
010
011
100
101
110
111
We wish to use one of these eight numbers to represent 0, half to represent positive and half to represent negatives. But with eight of them, we cannot do justice both to positives and to negatives! Well we wish to represent 2 and -2 in such a way that 2+(-2) becomes zero even in our new representation.
Let us use 000 to represent 0. Say now we use 001 to represent 1. Then what could be a reasonable representation of -1 so that 1+(-1) = 0. In other words,
001
+ xxx
-----
000
Obviously xxx = 110, just flip the bits in 001. That almost worked, but what then does 111 mean? It must be the flipped version of 000 meaning 111 = -0, which is not good - we don't want negative zero. However, if we add 1 to 111, it becomes 000, the extra one falls off the left end because we just have 3 bits in our system. Ok. Then say our algorithm for representing negative number is as follows:
flip the bits in the positive number and then add 1 to the result of flipping.
Let's see what we get.
000 ---flip---> 111 + 1 = 000 (great! there is no negative 0)
001 ---flip---> 110 + 1 = 111 (= -1)
010 ---flip---> 101 + 1 = 110 (= -2)
011 ---flip---> 100 + 1 = 101 (= -3)
100 ---flip---> 011 + 1 = 100 (= -4)
So, we have got what we wanted, almost! And notice that all negative numbers have their left most bit set to 1. So, it is quite easy to recognize them in this representation. Say if the computer sees 110, it immediately knows 110 is a negative number as its left most bit is 1. Now to find out the magnitude of 110, all the computer needs to do is to apply the same two steps again:
110 ---flip---> 001 + 1 = 010 = 2. That means 110 = -2!
In this way we got one number as 0, four to be negatives and three to be positives and we can do subtraction using adding unit.
This flip and then add 1 is just 2's complement.
000
001
010
011
100
101
110
111
We wish to use one of these eight numbers to represent 0, half to represent positive and half to represent negatives. But with eight of them, we cannot do justice both to positives and to negatives! Well we wish to represent 2 and -2 in such a way that 2+(-2) becomes zero even in our new representation.
Let us use 000 to represent 0. Say now we use 001 to represent 1. Then what could be a reasonable representation of -1 so that 1+(-1) = 0. In other words,
001
+ xxx
-----
000
Obviously xxx = 110, just flip the bits in 001. That almost worked, but what then does 111 mean? It must be the flipped version of 000 meaning 111 = -0, which is not good - we don't want negative zero. However, if we add 1 to 111, it becomes 000, the extra one falls off the left end because we just have 3 bits in our system. Ok. Then say our algorithm for representing negative number is as follows:
flip the bits in the positive number and then add 1 to the result of flipping.
Let's see what we get.
000 ---flip---> 111 + 1 = 000 (great! there is no negative 0)
001 ---flip---> 110 + 1 = 111 (= -1)
010 ---flip---> 101 + 1 = 110 (= -2)
011 ---flip---> 100 + 1 = 101 (= -3)
100 ---flip---> 011 + 1 = 100 (= -4)
So, we have got what we wanted, almost! And notice that all negative numbers have their left most bit set to 1. So, it is quite easy to recognize them in this representation. Say if the computer sees 110, it immediately knows 110 is a negative number as its left most bit is 1. Now to find out the magnitude of 110, all the computer needs to do is to apply the same two steps again:
110 ---flip---> 001 + 1 = 010 = 2. That means 110 = -2!
In this way we got one number as 0, four to be negatives and three to be positives and we can do subtraction using adding unit.
This flip and then add 1 is just 2's complement.
Wednesday, September 12, 2012
Multiple password prompt issue with Tortoise SVN
What we need to do is
The procedure is as follows:
- To generate a public-key/private-key pair on the SVN server.
- Add the public key to the server's list of authorized keys.
- Save the private key into a file on my computer.
- Use PuTTy-gen to make this private-key PuTTy compatible.
- Add this PuTTy-compatible private key to Pageant.
The procedure is as follows:
- Download PuTTy, PuTTy-gen, and Pageant.
- Log in to SVN server.
- Type "ssh-keygen -b 1024 -t dsa -f mykey"
- You will be asked to enter a pass-phrase for the key-pair. Do not forget it. You need it while making it PuTTy-compatible on your machine.
- If you type "ls" you will see "mykey" and "mykey.pub" inside the current directory.
- "mykey" is the private key and "mykey.pub" is the public key.
- Type "mkdir .ssh" to create a folder that will hold the list of authorized keys on the server.
- Type "cp mykey.pub .ssh/authorized_keys" to add the public key into the list of authorized keys.
- Type "cat mykey" to get the contents of the private key.
- Copy the output of the "cat mykey" command and save it in a file with .ppk extension on your machine.
- Now startup PuTTy-gen and load this .ppk file you have just saved on your machine and hit generate.
- PuTTy-gen will generate a private key file compatible with it.
- Hit "Save private key" and save this generated private key file in another .ppk file.
- Now startup Pageant.
- Hit add key, and choose this compatible .ppk file.
- As long as pageant is running, the Tortoise SVN gets the key from it instead of asking you for password again and again.
- ONE LITTLE PROBLEM: if you exit Pageant, then when you start it again, its list of keys will be empty. So, you need to add the compatible private key and to add it to Pageant it will require you to enter the pass-phrase you used while generating the private-key/public-key pair. But this is much better to enter the pass-phrase once per session of Pageant that to enter password many many times within a single session.
Subscribe to:
Posts (Atom)