Wednesday, June 11, 2008

Hello World


The first ever program written by a newbie is HelloWorld, be it C, C++, Java or any other programming language. Gives a sense of satisfaction to see the output on the screen. But few get there in the first shot. As for me, it took me god damm five freaking hours to understand what was going wrong.

Most of the times, the first ever HelloWorld program is copied by us till the last 'white character'. Seldom any chance of making any mistakes there. The first problem that hits us is:


C:\>javac HelloWorld.java
'javac' is not recognized as an internal or external command,operable program or batch file.C:\>

javac.exe is a file that gets installed on our machine as a part of jdk installation. This executable is required for generating the byte code i.e. essentially the .class file from our .java files. javac.exe is present in the /bin folder. This bin folder should be available in the 'Path' variable for the command prompt to locate javac.exe.

Copy the path till /bin and set it into the 'Path' variable withing the command prompt and then try the 'javac' command. Don't forget to append %PATH% at the end otherwise you will end up messing up the existing path variables in the current window. Sample from my environment:


C:\>set PATH=D:\apps\JDK_1.5.0.11\bin;%PATH%
C:\>javac
Usage: javac where possible options include:

-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath Specify where to find user class files
-cp Specify where to find user class files
-sourcepath Specify where to find input source files
-bootclasspath Override location of bootstrap class files
-extdirs Override location of installed extensions
-endorseddirs Override location of endorsed standards path
-d Specify where to place generated class files
-encoding Specify character encoding used by source files
-source Provide source compatibility with specified release
-target Generate class files for specific VM version
-version Version information
-help Print a synopsis of standard options
-X Print a synopsis of nonstandard options
-J Pass directly to the runtime system
C:\>

Similar problems are observed with the 'jar' command. jar.exe is an executable which helps us in creating java archive (jar), web archive (war) and enterprise archive (ear) files. In case jar -uvf xyz.jar com/* command gives you an error, the solution is same as above.
Many a times I have resources asking me that even if /bin is in the classpath, we get 'javac not recognised command ....' error. JRE is abbreviation for Java Runtime Environment. The bin folder under jre holds set of executables and binaries required by JVM (Java Virtual Machine). Thus development time executables, like javac, jar, javadoc, rmic to name a few, will not be present in this bin folder. Runtime executables like java, rmiregistry etc will be available in jre/bin.
Hope this blog serves as a good appetizer.
Cheers, Amey

Tuesday, June 10, 2008

Java Programmers aren't BORN !!!


In most of the interviews, which I have conducted till date, the candidates frequently ask me: “How can I become a good Java programmer?” or “How can I improve my Java skills” or “How did you learn Java”. I thought of sharing some of my methodologies, which might prove useful to some of you.

There is really no short cut to mastering Java, so in this column I will try to talk about the pedagogy. Let us picture for a moment, that a fan is asking Sachin Tendulkar a similar question about cricket:
Fan: "Good morning Sachin, my son would like to start a career in cricket. What advice can you give him?"
Sachin: "Is he playing in Ranjhi?"
Fan: "Oh no, he does not play cricket, but he follows it religiously on television. He has all the statistics on his finger tips"
Sachin: "Oooooh. What other sports does he play?"
Fan: "He plays soccer …… on his computer, you heard about Fifa98/99 …2004. Other than that, we once played table tennis together."

The point I am trying to make here is, you cannot learn Java just by reading fat books by international authors. You need to dirty your hands by writing programs, go through the pain of writing and compiling programs without IDEs and code assist.

Where and how does it all start?
Becoming a programmer starts early in life. You would have been at a great advantage if you were good in mathematics and physics. Here I am not looking for the tools that you learn doing maths, but rather the interest in "thinking" subjects. There are two types of java programmers; ones who know the legal constructs by heart and others who can think in Java.

Then the next step is to want to program for relaxation. Yes, you heard me right. It is not always necessary that the code you write will be used in the project that one is working on. You can code utility methods for some one working on a different project or just practice what you learnt, but all of this apart from the regular deliverables, chat, orkut and mailers. When I embarked on my programming career, all I did during free time was to download code samples from the Internet and study the behavior. It really did not make any sense at first, but as I read through tutorials and code samples, things started correlating.

I believe that being a programmer is not a job; it is a life. In order to learn it, you need to eat, breathe and sleep Java. There should be very few waking moments where you are not thinking about Java.

Let's say, for example, that your official work hours are 9:00am to 6:00pm. It is what happens after those hours that will determine your future as a programmer, i.e. what are you doing between 6:00pm and 9:00am. Before you set off for work, you could be programming in Java. And what about the time from 6:00pm (when you get home) until 23:59:59? If we take into account a few responsibilities, you have an additional 1.5 hours in the morning, and 3 hours in the evening.I am not making this up. If you want to be a good Java programmer, you need to have the dedication to further your own knowledge "after hours". Instead of orkut, surf for Java concepts, Object orientation, Design Patterns, etc.The key is the drive behind what you do. If your motivation to be a Java programmer is just to do a job, and to thereby earn a salary, you will never be good. Becoming an excellent Java programmer is neither difficult nor too easy.

Follow these steps:
  1. Read Java Complete Reference thrice. The first time for legal constructs and sample codes, the second time for understanding the concepts and the third for understanding how to use the concepts programmatically.
  2. Program for pleasure, not for money. Spend at least few hours of your own time per day on learning more about programming in Java.
  3. Never stop learning. The half-life of our IT knowledge is 18 months. We cannot afford to stand still; otherwise we will be obsolete in a very short period of time.
  4. Don't read books - STUDY them. When I read a new Java book, I open it next to my workstation and then I type in the code as I progress. This is a bit slower, but you learn faster.

There are very few people who have the potential to become good programmers. It is a tiny percentage of the programmers on the apex of the pyramid that can do it. Our goal is to reach the apex. These are some thoughts on how to become an excellent Java programmer. They are tough jobs to do, and by no means complete, but are meant to get you thinking.

Sunday, June 8, 2008

My First Address To Freshers


Hello Everyone,

I would have personally liked to address you before the training kicks off, but as I had to fly to Japan, I have requested Mr. K to do the honors.

I underwent a corporate training for Java and J2EE back in 2004 when I was associated with a different employer. That’s when I met Mr. J, who was the trainer. For four days we talked at length from OOP concepts to Reflections and that was indeed an enlightening experience.I was on the interview panel and I found some common misunderstanding of concepts amongst the candidates that I interviewed. That is the reason I why I to take this opportunity to share some of my experiences specifically with Mr. J and generally with “How to make the most from the training session”. These are some of my cheat sheets and it is not an obligation for you all to follow these steps in case our views differ.

Concepts: The most important and fundamental key to be good developer is to have your concepts clear. This training course is not similar to the commercial ones available in the markets where they teach you the syntactically legal constructs of writing Java/C++ programs. We have tailored the course contents to put more emphasis on topics that we feel should be discussed at length. Ask as many questions that arise in your mind and follow a simple principal “No question is stupid, no question is pathetic”. Once the concepts are clear, writing code will be very simple.I would like to emphasize that you all put enough efforts to get the OOP concepts crystal clear by the time you all finish with the training. This was one thing in common that I found most of you lacked.

Implementation: The exercises given during the training need to be essentially carried out individually rather than in groups. Try to relate what you are typing. Don’t feel shy to ask Mr. J when you get confused over what really has to be done. I bet Mr. J will never spoon feed any of you; so try to give your best shot. Remember, once you get on with the projects, you will be all alone. I feel this is the best time when you all can start cranking up your debugging skills, imagination as to how the code might be working internally and so on. Try to squeeze as much information from Mr. J as possible. This includes best implementation practices, code reuse, how to decide the best fit java classes for a particular functionality when we need to make a choice from couple of types available at hand etc.

Post training: It will be good if you all can read some more literature after every session. Google.com is one unlimited source of information and “Complete Reference by Herbert Schields” is another ready reference. Read this book in parallel to the training and get your doubts cleared if you find a discrepancy between what is written and what was taught. It often happens that the wording in the book misleads us to believe in something that is actually not true. Be careful.

Mr. J: He is a well read and a no nonsense guy. I bet you won’t feel sleepy or bored during any of his lectures as he makes the discourses humorous enough. Try to understand the similarities and differences when he compares two technologies or the implementations in different technologies. Make ample notes from his presentations, specifically the definitions or different terms, java tenets, java constraints and so forth. They have proved very helpful to me in the long run. Take his assignments seriously as the feedback he provides on the implementation is genuine enough, it will train your mind while coding and also improve writing good performance code in the first shot. Trust me, you will be able to make a lot of use of these assignments when you come aboard on live projects. If time permits try to go the extra mile to implement additional features that might be discussed in the class.

Last but not the least. We are all here to help you out in case you are stuck with some implementation and Pankaj is not around. But don’t just get someone to fix the code and leave, try and understand the logic behind fix. The bottom line to successful coding is having the right logic in place as after that its only the semantics that have to be followed.