A Funny Java Flavoured Look at the World

Thursday, August 31, 2006

Increasing the Memory used by Tomcat in Linux

Following on from my blog entry on installing Java 5 and Tomcat entry here on Linux and then moving the shutdown port so I could have to versions of Tomcat, blog entry here. So After doing that my next step was to increase the memory to my version of Tomcat on Linux

The basic premise of increasing the memory Tomcat uses is by increasing the memory the JVM Tomcat uses. You do this by passing in some variables to the JVM. You can see a list of the variables you pass in by doing on a dos or terminal

Java -X

You probably won’t need to use these settings very often, Tomcat on windows sets these automatically and IDE’s have usually slapped a nice GUI for you to use but in the end they are usually setting up the values passed to the JVM on initialisation. Except when you use Linux where you have to start getting your hands dirty because there aren’t many nice GUI’s to help you out.

The two main settings I am interested in for increasing the memory in Tomcat are

-Xms sets the initial Java heap size in megabytes
-Xmx sets the maximum Java heap size in megabytes

So how do we get these changes into Tomcat, well the answer is we have to change the Catalina.sh and add in the extra memory allocation into the JAVA_OPTS variable. I have read that you can also add values to the CATALINA_OPTS but when I tried I couldn’t get it to work so I decided to use the JAVA_OPTS instead. Below are the instructions how I increased the memory for my Tomcat on Linux

I edited the file

catalina.sh

which can be found here or where ever you tomcat home is

/usr/local/tomcat/bin

I edited the line

# Set juli LogManager if it is present
if [ -r "$CATALINA_HOME"/bin/tomcat-juli.jar ]; then
JAVA_OPTS="$JAVA_OPTS "-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"

"-Djava.util.logging.config.file="$CATALINA_BASE/conf/logging.properties"
fi

to

# Set juli LogManager if it is present
if [ -r "$CATALINA_HOME"/bin/tomcat-juli.jar ]; then
JAVA_OPTS="$JAVA_OPTS "-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" "-Xms256m"

"-Xmx512m"

"-Djava.util.logging.config.file="$CATALINA_BASE/conf/logging.properties"
fi

I have added the two variables which are passed to the JVM when starting Tomcat and they are memory settings

"-Xms256m"
"-Xmx512m"

-Xms sets the initial Java heap size in megabytes
-Xmx sets the maximum Java heap size in megabtyes

saved the file and then stopped and started the tomcat server

to test that the changed had worked I went to the tomcat manager

http://localhost:8181/manager/html

and then clicked on the Server Status hyperlink and half way down the page you will see some information regarding the JVM
JVM

Free memory: 1.66 MB Total memory: 8.30 MB Max memory: 508.06 MB

Wednesday, August 30, 2006

Using the Java API can save you time

I learnt a good lesson today, look in the Java API it could save you time. It could save you time writing something that has already been written.

luckily for me in this case it wasn't anything that took me very long, I saw me I had to ask someone about the logic of it but I thought I would write the a small util method in case I ever needed to do it again.

I was trying to convert a number which was a colour back into the RGB (Red, Green, Blue) value. I had the number I wanted to convert into an RGB but I wasn't sure how to do it. I was then given the logic to convert an RGB to the current colour number (I don't know what it's called)
(red * 65536) + (green * 256) + blue.

I'm afraid I am rubbish at maths so I had to ask one of my developer buddies and this is the code that we made which was put into a static helper class called MoreMaths because it was for classes which he thought should have been included in Maths. This method maybe should be included in here but we haven't got a NumberUtils class, although I will be probably cursing next time I want a similar function and go through this all again.

public class MoreMaths {

/** Creates a new instance of MoreMaths */
public MoreMaths() {
}

public static Color convertRGB (final int numberToConvert) {

//Color colorAdded = new Color(numberToConvert);
//below is the logic more for interest than use so if you were
//wondering how you convert a number into an RGB
final int red = (numberToConvert / 65536 );

final int gb = numberToConvert - (65536 * red);

final int green = ( gb / 256 );

final int blue = gb - (256 * green);

return new Color(red, green, blue);

}
}

It was only when I was thinking what should I return, do I really want to return an int array and then I thought hmmm I will return a Color object. It was at this time just as I was writing a unit test to see what the method produced that I thought hmmm I bet Color does all the converting for me and yes it does. If I had just looked at the API straight away I would have seen it and saved myself working out how to convert it and then writing a method that converted it.


so there you go, go and root around in the API, it could save you a lot of time and you will be using reliable tested code.

Finding out what ports are being used on your machine

I have been having some troubles trying to deploy two tomcats on my machine. I had one on port 8080 and the other on port 8181.

Although I changed the port used to show the web stuff I hadn't changed the port used to shutdown Tomcat and this meant I had problems because Tomcat usually listens out on port 8005 for instructions to shutdown the Tomcat server. Tomcat does also have something listening on port 8009 which is something to do with a Tomcat Adapter, which brings some horrible memories about setting up a redirect on ISS so that Tomcat could listen on port 80.

it all seemed to work fine once I had changed the shutdown port from 8005 to 8007 on one of my instances of Tomcat.

When I was thinking about setting the shutdown listener to another port I wondered what ports on my machine were being used. I found that you can find what ports are being used on your machine in either a terminal window on Linux or a DOS window on windows using the netstat command. There are a number of options but I found that

netstat -a

returned the details of the ports being used and it is slightly interesting reading for about 10 seconds.

I was reading about ports today from this manual and below is the quote

Use the default ports if they are not already in use. Otherwise, use a base number between 2048 and 65535 that is not being used. Because numbers between 1024 and 2048 are commonly used for standard system ports, Red Brick Warehouse installation and configuration utilities do not accept values below 2048. Use a number above 4098 to minimize the chance of conflict.
I am now happily running two tomcats and before you ask its for testing purposes. It also has the advantage of giving me another Tomcat to clutter up. Talking of Tomcat does anyone know why they no longer include the admin user by default, I imagine it's for security reasons with probably 90 percent of people not changing the default password. It's quite annoying having to add the user in yourself though

Wednesday, August 23, 2006

Free Ajax in Action ebook and others ebooks

I found this link today which said free ebooks. I'm a big fan of the free stuff so I had a peek. I think this link to free ebooks has been sent round before but I thought I would just highlight the fact that it has a free Ajax book which looks pretty good.

I'm not sure if I am being a bit picky and you sometimes get what you pay for but a lot of the books are links to web pages and not downloadable ebooks. On the other side they are proper books in a lot of cases and they are free, so you can't really complain that much (although I will still try)

There were a couple of decent free eBooks, well ones which I thought might be interesting.

here is a link to all of them to all the programming books and from here you can navigate to Java books.

http://www.ebooklobby.com/index.php?cid=6

of particular interest to me was the Ajax in Action ebook, which can be found here

http://www.ebooklobby.com/details.php?id=459


There was also the Java Cookbook somewhere and Bitter Java

http://www.ebooklobby.com/index.php?cid=44


I have read bits and pieces and quotes from bitter java, it's an interesting book in it's idea because it's basically talking about things that don't work and antipatterns so that you the reader don't make those mistakes.

Planning cuts time bug fixing

I was reading code complete and at the start it talks about finding bugs or problems earlier means you can fix them quicker. It is a basic introductory type chapter talking about why you should plan your code before you start working (coding) on it.

The chapter is based as an argument to help persuade your boss it's a good idea. He Steve C McConnell also has some very good sayings in his book, this chapter is "Measure twice, cut once".

The point is interesting because planning and not coding straight away would seem to managers or some programmers as not the fastest way to get the work done. Personally I have found that planning and thinking about the design of the code very useful mainly just because by thinking about the problem you can discard some ideas which initially might seem quite viable but once you investigate the work flow of the problem you can see it is flawed or not the best way. By doing a bit of planning and by this I mean some rough class or sequence diagrams maybe or just some scribbles on a piece of paper you are basically working on the problem but are not bogged down in code.

I have been involved in a few examples where the customer added in what seemed a small piece of functionality but because they didn't mention (or I didn't ask) earlier it meant adding the functionality in at a later date which can often be a lot harder and trickier than if the functionality had been in the original spec.

In some ways I think it is a bit similar to writing unit tests before. Writing unit tests basically makes you decouple the functioning parts of the program and test them to make sure they work and then when you are happy the small sections work you can change the design and navigation code which brings them all together safe in the knowledge that the small parts are working correctly.

I think the point of chapter is that fixing a problem earlier means that there is less code or less of a system/protocol which you will have to change to fix it. Fixing bugs when you have finished all the coding can often mean that you have a choice of either doing a major refactor or wedging in a "dirty fix" around the existing infrastructure and code. I maybe going a bit over the top with all this dirty fix nonsense but bug fixes are often added on to the existing code and if a few fixes are added in (often not by the person who coded the original) then the code will start to become messy and smelly.

In my experience the code in which I have thought about the design in a more detail has almost always been more robust with less bugs so I would say that it is a good point.

Friday, August 18, 2006

From soup to nuts

I was reading the Sturts-action userGuide (link here) the other day and it had this quote at the end

"It is wise to avoid creating lengthy and complex Action classes. If you start to embed too much logic in the Action class itself, you will begin to find the Action class hard to understand, maintain, and impossible to reuse. Rather than creating overly complex Action classes, it is generally a good practice to move most of the persistence, and "business logic" to a separate application layer. When an Action class becomes lengthy and procedural, it may be a good time to refactor your application architecture and move some of this logic to another conceptual layer; otherwise, you may be left with an inflexible application which can only be accessed in a web-application environment. The framework should be viewed as simply the foundation for implementing MVC in your applications. Struts Action Framework provides a useful control layer, but it is not a fully featured platform for building MVC applications, soup to nuts."

I wondered what the saying from soup to nuts meant. This website gave me an explanation

Seems to have a decent sounding explanation, coming from the 17th Century "From Eggs to Apples" The website and others I have seen states that the saying means

"through or from the beginning to the end, sparing no detail"

this website has other versions of the same saying


so there you go and now you know. It's quite a funny statment to have in a user manual type thing, it's not that clear but on the flip side of the coin it's quite interesting and quirky and that's what apache is all about, it gives the document a bit of character.

Thursday, August 17, 2006

Does passing SCJP 5 exam make you a better programmer

I thought I might as well finish off the SCJP blogs with this entry whilst it's still fresh in my mind. I recently blogged about me recently passing the SCJP 5 exam and I also posted a blog entry about a couple of articles saying why you should get certified or the benefits which is here
and I thought I would end the topic discussing does passing the exam make you a better programmer.

It's a tricky question in some ways because obviously rating and judging a programmers skills is a very subjective thing and what would you use to grade some one (what is the criteria). I suppose firstly SCJP really only increases your
knowledge of Java and in my opinion increased knowledge of the language you use to program in, doesn't to much extent effect you skills as a programmer.

The technical skills of a programmer (i.e. not work rate, ability to get things done, coping with pressure etc etc) I think are more in his ability to design solutions and code that can be easily modified and added to at a later date. I suppose what I am talking about is OO design, encapsulation and cohesion. Interestingly some of these topics have very briefly been added to the SCJP exam but only in a sense of the person taking the exam should be able to recognise good programming practices and give them a name.

I can only talk about my experience and I think the exam has made me a better programmer because you do have to learn a lot of the Java API in detail to be able to pass the exam and this can't be a bad thing really. It also lead me to look at a lot of new areas of code I hadn't used before and in particular a lot of the new features in JAVA 5 like

Generics
Collections and Generics
Enums
Assert
printf
static imports
it also made me investigate things like Threads

One advantage is you can make better decisions on how you should code certain situations in the future because you will be making the decisions (or I will) knowing more about inner classes (scary) and collections, generics and enums and I will (possibrecognisele to recongise where different parts of the Java language would be suited to certain situations where as if people have looked at the other parts of the Java code they might not think to use a inner class or a generic collection or class.

Another advantage I found that by looking at lots of different parts of Java you can see how good programmers have designed their classes and packages. Like that Calendar, DateFormat, Number format are abstract classes and to customize the serialiaztion process you overrite the writereadObject and then add a call to DefaultWriteObject and then add your code. It's interesting ideas like adding this hook in and passing in a comparator to sort collections. I might use some of these methods when I am coding in the future.

I also enjoyed looking at threads and serialization because they were things I had a hazy brief knowledge of but never really needed or had the time to look at them at work. I don't know if I will be actually using but it was interesting to look at them
Conclusion

So what conclusion have I come to, I think in my case it certainly improved me as a programmer merely because of the fact that I learnt to use a lot more of the API in Java and know what the code does and how to use them. I don't think it will make me a better programmer because learning Java in more depth won't really increase the skills that make people a good programmer. Those skills are a bit like chalk and cheese, just different. Learning Java in more detail certainly won't hurt me and I think being certified will certainly look good on my CV.

On the other side of the coin, studying for the exam was a right hassle and it certainly did take me a lot of work. The scope of the exam is massive and being able to program with items on the exam won't be enough to pass the exam because you need to have a compiler like knowledge of what won't work. I certainly have some respect for people who pass the exam because it is tough, especially considering that everything they have added in Java 5 makes tricky questions, especially Generics, printf, enums.

overall it increases your knowledge of Java but I suppose the bottom line is that alone won't help you be a good programmer because it's what you do with that knowledge that counts.

At Last I finally passed my SCJP 5 exam

Yes that's right I'm finally in the Java certified club and I am now waiting for sun to send me my special name badge and instructions about the special certified handshake and secret signals

After months of reading and staring at that the SCJP book I have finally taken and passed the exam.

I got a rather ropey 66 percent but a pass is a pass is a pass.

The last few days before the exam were a real cram fest. I decided to take advantage of suns offer of a free retake if you registered before the 15th August (which they now extended). Having a free retake in your back pocket is a useful thing (especially as the exam cost £150) to have but the biggest motivation for me was to just get it over and done with, to brush off the King Kong sizedgorillaa once and for all.

Although I initially read the offer wrong and thought you had to take the exam before the 15th to get a free resit. I did read it again later but I thought, bugger it lets set the date, 10 days time , revise and do it.

I had beenpracticingg questions before the exam and Marcus Green's questions were going very well and I was getting between 60 and 90 percent. So I got to the last four days I tookFridayy off and revised solidly for 3 days and nights. The more I revised the more you think, Jesus there is so much to bloody study. My study processed was a case of slowly reducing my notes in size, until basically I had a list of gotcha's and important facts.

I had beenpracticingg with the questions in the book and some whizlabspracticee questions, until the morning before the exam I was getting at least half wrong and with the questions in the book I was getting about 40 percent. Althoughh some of the questions I got wrong were because I hadn't read the question properly, I was still quite concerned. All the otherpeopleI had read about were getting 70 and 80 percent before the exam.

My hopes of passing were based on this. I had read the SCJP book I was using about three times. I had learning loads more information during my fours days of cramming and in the real exam I would focus (like a lazer beam) and wouldn't make any silly mistakes.

As you can imagine I was slightly nervous and rated my chances as about 50/50. The first part of the exam went really well but I suddenly began torealizee my time was running out and the last half of the exam I was feeling the pressure and last 10 questions I was just racing through.

To make matters worse when I finished and was expecting my results the computer said printing error!!!

So even thought thetechnicianss said I had passed it wasn't until today and when I got my hands on my marks that I actually believe it.

I have to admit I don't feel that different but the good thing is at least now I don't have to keep reading about Java 5 features and I can read more interesting Java stuff.

So this will also be good news for people who read my blog because they can now expect some non Java 5 blog entries.

Tuesday, August 15, 2006

Why Should I Get Certified in Java Technology

Whilst studying for SCJP 5 exam I have often wondered this question and I haven't really found any of any interest or an article's that really made some good points why people should take the exam (s).

today I found that Sun had an article on the exact subject, of course they want you to get certified because they get £150 for 3 hours 15 minutes of their time.

Obviously this could be biased account but it's good to hear some reasoning behind me doing hours of painful study

here are suns 5 reasons, if you go onto the site it will explain in more detail the reasoning behind them

1. Achieving a Sun certification improves your opportunity for professional advancement, including salary increases, job role changes and promotions.
2. Sun certifications are HOT!
3. Sun certifications are relevant, valid, accurate, and reliable measures of the certification candidate's skills and knowledge.
4. The value of Sun certifications is recognized around the world.
5. Certified IT professionals recognize that achieving a professional credential also offers non-financial benefits.

reading the list, although maybe a littleexaggeratedd alot of them are true because the exam is so hard it is worth something and making the exam difficult means that not everyone is qualified. So because less people are Certified this does make them valuable and makes the certified people stick out from the non certified. I'm not saying that the people without certification are worse programmers, the exam doesn't really comment on people's programming ability but more their knowledge of Java.

After studying for my SCJP 5 I can certainly say that it does increase your knowledge. I would say that I have learnt as much studying for six months than I have for about a year working. WhatIi liked about the exam is you get to learn different parts of the Java language which you might not use in your everyday job. I would also say that when we look at people's CV's it does make a difference if they are Certified.

The Sun article was okay, this article below is in a lot more depth and raises some very interesting points and basically says, people who are Java certified get more money (woohoo)

http://www.certmag.com/issues/jul00/feature_hecksel-green.cfm

I like the observations in this article

  • 70.8 percent of IT managers view certification as a criteria for promotion.

  • 85 percent of managers (up 10 percent in the last year according to the Gartner Group) view certified employees as more productive. Whether you believe that is true or not, if you want to manage and advance your career, paying attention to what the boss views as important will help get you there.
so if anything it changes the opinion your boss and other employees have of your Java skills!

Wednesday, August 09, 2006

Idiots guide to installing Java 5 and Tomcat 5 on Linux Red Hat

This website was very useful for easy to use instructions on installing Tomcat 5 and Java 5. It has a load of stuff before the tomcat installation instructions but I just ignored them and went straight to the main event

The bit I wanted from the guide is down at the bootom of this email and well worth checking out

I had a bit of a problem as well because I needed to have OC4J installed and using Java 1.4 and I need Java 5 and because of Java 5 I needed to install Tomcat 5. The last thing I needed to do to add a bit more interest is to put it on port 8181 just in case there were any oracle apps listening and hogging 8080.

Installing Java was a fairly straight forward, I downloaded the latest Java 5 JDK and then ran the file, firstly it unzipped it and then I doubled clicked on that and it installed Java 5 into the, right next door to Java 4 but without doing any damage to it.

/usr/java/jdk1.5.Untitled 10_07

Tomcat was a bit different (for a windows developer) the download basically just you a gz file and when you clicked on it, it just produced an unzipped directory structure of tomcat.

So on the advice of the article I copied into usr/local/tomcat

I had a problem now that OC4J wanted the JAVA_HOME variable to be pointing to JAVA 1.4 and the tomcat mentioned using JAVA_HOME all over it's start up files and in other places.

The JAVA_HOME variable is set up in the profiles file which is here

/etc/profiles/

and this is what I had in there, I copied it in the bottom

export JAVA_HOME=/usr/java/j2sdk1.4.2_04
export PATH=$JAVA_HOME/bin:$PATH

export CATALINA_HOME=/usr/local/tomcat
export JAVA5_HOME=/usr/java/jdk1.5.0_07
export PATH=$JAVA5_HOME/bin:$PATH

export JRE_HOME=/usr/java/jdk1.5.0_07
export PATH=$JRE_HOME/bin:$PATH

I changed the JRE_HOME because tomcat mentioned it and I didn't think OC4J did (well I didn't know what I was doing is the real answer)

So I set up an environment variable in windows called JAVA5_HOME and then restarted Linux. I tested the my environment variables were working by going into a terminal and then doing $JAVA5_HOME , you just put a dollar sign ahead of any of the environment variables and it echo's it out.

Changing port 8080 to 8181

This setting is found in this file

/usr/local/tomcat/conf/server.xml

if you search for 8080 and then change this to 8181 or another port you fancy.

I tried to start tomcat and wondered how I would do this and found that running the sh (shell script) startup.bat does the trick, so once again I went to the terminal

cd /usr/lib/apache-tomcat/bin

sh startup.sh

and to shutdown it's

cd /usr/lib/apache-tomcat/bin

sh shutdown.sh

This of course didn't work because they were using JAVA_HOME Reference. You need to go edit these two sh files and change JAVA_HOME to JAVA5_HOME. Actually you probably won't find a reference to JAVA_HOME in the startup and shutdown shell scripts but you will find one in the Catalina.sh shell script. The Catalina shell script is the one that sets up all the variables and memory settings and then calls the startup and shutdown shell scripts.

Then I started it and Tomcat worked but it was having problems, for some reason it hadn't put a log folder in. So in these directory I created a folder called logs

/usr/local/tomcat

I then tried to log in to the admin console and the admin user didn't exist!!!! I edited

/usr/local/tomcat/conf/tomcat-users.xml

and added





restart linux, have a cup of tea. Come back and start up Tomcat using some sh startup.sh power and hallaluh Tomcat is up and running and I can snuggle up in the safe bosum of the Tomcat manager.

Below is a section from http://www.howtoforge.com/apache2_tomcat5_mod_jk_p2 and from the first page where it tells you how to install Java and Tomcat and start tomcat.

Installing JDK (Java Development Kit)

In order to run Tomcat, you will need to install JDK and set the JAVA_HOME environment variable to identify the location of the JDK environment on your system. I have chosen to use JDK 5.0.

1. You can download JDK 5.0 at http://java.sun.com/j2se/1.5.0/download.jsp.

2. Click on Download JDK 5.0 Update 6 to go to the download page.

3. Click Accept to accept the license agreement.

4. Next choose the Linux self-extracting file. This is the download for the self-extracting binary file rather than the rpm.

5. Download to your preferred download directory. Change to that directory and make it executable by executing the following command:

chmod +x jdk-1_5_0_06-linux-i586.bin

6. Now execute the file:

./jdk-1_5_0_06-linux-i586.bin

7. You should now have a new directory called j2sdk1.5-sun. Now move this directory to the location where it should be run. I chose /usr/lib/.

mv j2sdk1.5-sun /usr/lib

8. Now create a symbolic link called jdk to JAVA_HOME by the following command. This allows you to easily switch back and forth between different jvms should you ever need to

cd /usr/lib

ln -s j2sdk1.5-sun jdk

8. Now we need to set the JAVA_HOME environment variable. Add the following at the end of /etc/profile just after export PATH.

JAVA_HOME="/usr/lib/jdk"

export JAVA_HOME

/etc/profile is executed at startup and when a user logs into the system. In order to update the environment you will need to log out and log back in to the system.

10. Check to make sure JAVA_HOME is defined correctly by executing the command below. This should report the location of the Java SDK which should be /usr/lib/jdk.

echo $JAVA_HOME

11. Now test Java with the following command. You should be returned with /usr/bin/java. If so, you have successfully completed this section.

which java

Installing Tomcat

In this section you will download and install Apache Tomcat 5.5.15. For this particular setup, there is no need to build the package from source, we will download the binary version.

1. Download the binary version to your preferred download directory from here: http://tomcat.apache.org/download-55.cgi. Choose the tar.gz from the core section for 5.5.15.

2. Now change to that directory and extract the files using the following command:

cd /mydownloads (be sure to change to your download directory)

tar xvzf apache-tomcat-5.5.15.tar.gz

3. You should now have a new directory called apache-tomcat-5.5.15. Now move this directory to the location where it should be installed. Again, I chose /usr/lib/. Note that this location will be referred to as CATALINA_HOME in the Tomcat documentation.

mv apache-tomcat-5.5.15 /usr/lib

4. Next change to the /usr/lib/ directory.

cd /usr/lib

5. Now create a symbolic link called apache-tomcat to CATALINA_HOME by the following command.

ln -s apache-tomcat-5.5.15 apache-tomcat

This will save you from having to make changes to startup and shutdown scripts each time you upgrade Tomcat and if you so desire, it also allows you to keep several versions of Tomcat on your system and easily switch amongst them.

6. You should now be able to start and stop Tomcat from the CATALINA_HOME/bin directory. If you are using another shell other than the bash shell you will nee to add sh to the beginning of the command. You should now be able to test that Tomcat is installed by starting it and opening your browser and entering http://localhost:8080 into your browser. Port 8080 is the default port for Tomcat and can be easily changed in the /usr/lib/apache-tomcat/conf/server.xml file. (We will work with this file later on.) If you plan to access this page remotely, be sure to forward the respective port to your server’s IP address within your router. You should now see the Tomcat welcome page that contains links to Tomcat documentation as well as sample JSP/Servlet scripts. Verify that Tomcat is running by executing some of the examples found on the welcome page.

cd /usr/lib/apache-tomcat/bin

sh startup.sh

To shutdown the server, you will need to execute the following command. Feel free to try it, but for now we will leave Tomcat running.

sh shutdown.sh

Tuesday, August 08, 2006

SCJP 5 Questions Questions Questions

This is my (current) tactic into getting myself match fit for the SCJP exam.

Although reading articles, books and online stuff helps to learn the basic knowledge of Java, I have decided that to really get my knowledge up I am going to have to keep practicing questions.

I think the problem with reading books/articles is that it's quite a passive way of learning and sometimes it's difficult to know if you are actually taking in the information.

I have been doing my daily question as usual at at Marcus Green's excellent SCJP Mock exam question a day site

http://www.examulator.com/phezam/question.php

it also has a link to a database of 60 Java 5 SCJP questions. I try to do the question of the day and then 10 more before starting work. If I get a question wrong then I note it down. I have found that doing questions helps you test your knowledge and reinforce it if you get it right and then if you get it wrong, you investigate why and are less likely to caught out again.

Doing questions also helps you get up to speed with the typical exam tricks that you are going to be faced with.

So recently I have been looking for some more questions to do and I found this site

http://www.javareference.com/testcenter/

It doesn't have that many questions but it's good practice until you have done them all.

I have been working through the tricky tricky questions in my SCJP book, the questions are right swines and I am only getting about half right at the moment (ahhh 9 percent short of a pass!!!) but even though I have got only half of them right I think it is worth while doing the questions because it has highlighted some areas that I am not sure about. It has also hopefully picked up some areas where the exam is going to try and catch me out.

If I take them again in a few days I will hopefully get more right because I will be watching out for certain tricks, although this isn't that a good test because I will have answered the questions before. I think the point I am trying to make is that by doing questions you are finding areas where you knowledge is lacking but how would you know this by just reading a chapter of a book or article etc.

I have a link to 11 questions by Katherine Sierra and Bert Bates which I plan to tackle in the next day or so.

http://hoskinator.blogspot.com/2006/05/11-scjp-java-questions-come-on-test.html

If anyone has any links to any automated SCJP 5 question websites then please leave them in the comments, it's much easier when you just have to tick a box etc rather than writing the answers on a piece of paper.

Also Java Black belt is quite good for testing yourself, although some of the questions waiting to be verified can be of poor quality, overall it isn't bad

http://www.javablackbelt.com/

They have also recently added a Java 5 exam, which is pretty good.

Monday, August 07, 2006

Tomcat 5 on Linux Step-By-Step

I have recently been given the task of installing Tomcat 5 and Java 5 (not in the order of course) onto a Linux machine. Where my Linux skills should be is a big black hole, so I did what any developer would do and went on google and had a look for some documentation to help me do it.

The more I see of Linux the more I think it's not that, especially as there is a decent GUI interface so I can see the folders and navigate my way around. I don't really have a choice where I work and have worked, all the operating systems were already installed so all I have used is Windows. The wierd thing with using Linux is that everything is named slightly differently and you get to complete standstills on things like running a bat file (or sh in Linux)

Before I actually install Java and Tomcat we have been mucking around with OC4J and have installed java 1.4

Anyway I found some decent links to help me install Tomcat and thought I would blog about them because then I will be able to find them easily tomorrow and they might be of interest to other people

This is a comprehensive guide to installing Tomcat 5 on Linux
and this is instructions on how to install Java

Sunday, August 06, 2006

Excellent SCJP Study Resources and links

I found this link today and it is a really good SCJP Java 5 Study resource. It just has links to most of the areas that need to be studied for the exam. Here is the a link to the site. I would recommend visiting it because it also has other SCJP stuff like study notes and mock exams and some articles of it's own

http://www.xyzws.com/scjp.do?cat=scjp&smenu=SGS12

Here are the links, it includes Generics, Varargs, enums. I think I have linked to most of these linked on my blog but not in one easy list. It's quite amusing I have read a lot of this links apart from the Wikipedia ones and the ones about garbarge collection infact.

The Members' Collection of SCJP/SCJP(5.0) Resources
SCJP Tutorial by IBM Developer Works
Taming Tiger, Part 1 - An introduction to Java 1.5
Taming Tiger, Part 2 - Understanding generics
Chapter 3: Enumerated Types (PDF Format) in Java 5.0 Tiger: A Developer's Notebook
Chapter 5: Creating Varargs in Java 5.0 Tiger: A Developer's Notebook
Chapter 8: Generics in Learning Java
Chapter 6: Objects and Classes in The Complete Java 2 Certification Study Guide
Cohesion in WikiPedia
Coupling in WikiPedia
Information hiding in WikiPedia
Lesson: Object-Oriented Programming Concepts in The Java Tutorial
Abstraction, Encapsulation, and Information Hiding by Edward V. Berard
Encapsulation is not information hiding in JavaWorld
Programming With Assertions from Sun.
Using Foreach Loops in J2SE 1.5
Reference Objects and Garbage Collection
The Truth About Garbage Collection
Garbage Collection (Chapter 9 of Inside the Java Virtual Machine)
Garbage Collection and setting to null
Object finalization and cleanup
Using Finally Versus finalize To Guarantee Quick Resource Cleanup
Achieve strong performance with threads
Programming Java threads in the real world


that should help you guys out there

SCJP 5 - Switch Statements

I was curious about the effect of autoboxing on the switch statements and it has provided a potential exam gotcha. I was reading my SJCP book about control flow, if statements, switch statements and I was thinking to myself do I really need to read this stuff, I know it.

But there are a few cheeky things in there. Like you are now allowed to use Enums as well as anything Int and below.

Also if you use a byte you can’t use a value higher than 127 otherwise it throws an error.

I am a bit confused about the fact you need a constant. Initially I thought this meant the value you were testing in the select had to be a constant but now after a bit of investigation I understand (what a fool I have been) that the it'’s the case statements that need the constants.

I have also found that if you are testing a btye value in the select then you have to have byte constants in the case. Usually you don'’t notice because you use ints.

My investigation into the realms of the Wrapper classes and shown me that you can indeed use a wrapper class value in the select statement. Autoboxing unboxes the value and changes it to the primative.

Finally a few Switch statement gotcha's

1. You can't have to case constants with the same value, it will throw a compiler error
2. Case Constants are evaluated from the top to the bottom.
3. The default constant can go anywhere and remember it is an entry into the code, so without any break statements it will run all the code below in the other case statements.

Friday, August 04, 2006

Generic Epiphany - an Angled bracket vision

The past few days I have been looking at a lot of Generic code, reading articles about generics, looking at more generics code, snacking on generic code and washing it down with a nice generic drink and finally going to bed and dreaming about ...yes of course Generics.

I have been reading all sorts of stupid examples about putting apples and pears into a fruitbasket and how the Doctors class is using generics to check the different animals but of course you can't just put a dog or elephant into an animal class oh no.

I have blogged about Generics a fair bit, here is the last effort and it goes back through the others if you want a generic frenzy of information

http://hoskinator.blogspot.com/2006/08/type-safe-collections-generics-arent.html

I have kept plugging away with Generics and I have a decent grasp of it but one thing that I always had to double check was the placment of the angled brackets. Even copying out examples to play around with the code, I would often put the angled brackets in the wrong place. Anyway I was typing in an example and then all of a sudden, the angled brackets suddenly made sense, just like that, boom. It was a like a magic eye picture had suddenly come into view or when you look at the picture of the young woman looking one way and then suddenly realise that it can also be an old woman (if you have seen the picture you know what I mean, if not you probably think I'm mad)

I understand the bit inside the angled brackets is the "type" but I often got slightly confused with the talk of the base type and then then the type but it's actually not as difficult as people talking about it seem to make out.

I have found it's often easy to look at Generic code and reverse it (mentally) to non generic code and then see what you have got.

so you have this

List<String> myList = new ArrayList<String>();

becomes

List myList = new ArrayList();

look at it this way helped me understand how Generics worked more clearly and simplified the angled bracket placing conundrum I had been experiencing. It also help me understand the base class they were talking about e.g. List and ArrayList (List is the Base class and ArrayList is the subclass).

Looking at it now it seems fairly obvious and probably is to a lot of people out there but I now understand the angled brackets need to go anywhere where you are mentioning a class/object you are going to use e.g. List, ArrayList etc. so now I see the above

so this

List<String> myList = new ArrayList<String>();

I now see

base class type Variable name = new subclass class type ()

but I find it helpful to take this code back to the non generic style to understand what is going on
List myList = new ArrayList();

Obviously this is an easy example and it can get very complex but here is another example

A method example taking in an ArrayList

public List<String> changeStrings(ArrayList<String> s) { }

to this:

public List changeStrings(ArrayList s) { }

Once again it seems easier to imagine the pre generic code show first and then adding it in to see what is actually happening in the code.

I just thought I would share my Generic breakthrough on the blog in case anyone else was having a few problems with Generics, it's definitly one of the tricky sections on the SCJP exam I think.

Graduate IT Salaries Average at £22000

I got this email the other day from CW Jobs, every now and again (once every two weeks) they send me some speculative job email, which I usually delete without reading. Unfortunately I am just too lazy to go onto the website and take my details off but the other day I read this bit at the start of the email. I suppose I should link to CW jobs site as I am quoting a bit of their email so if you wanna check them out go here http://www.cwjobs.co.uk/

Anyway the totally unrelevant piece of information at the top of this email was about graduates and their starting salaries, roll VT

The future looks good for IT graduates. A survey of more than 2,000 advertised
graduate jobs by CV Screen found that salaries averaged at almost £22,000 for
those with little or no work experience. The high starting salaries reflect the
increased demand for qualified IT professionals.
Matthew Iveson, managing
director at CV Screen says that employers are on the look out for graduates who
have some commercial experience and it would be wise for those currently
undertaking their degrees to seek some kind of work experience in order to make
them even more marketable.
I have to admit I am always amazed and annoyed about the starting salaries of graduates, how come they are always so bloody high, they are students with no experience for gods sake, perhaps it's like paying for a talented young footballer and hoping he will become a big star. Maybe it's me and I'm working in the fag end of the market but these starting salaries always seem a bit on the high side, I wonder to myself how does this work, is there some where in the UK where loads of students are getting paids millions so it brings up the average for all the under paid graduates.

Another reason why I have decided to blog about this because my Mother who is always on the look out for computer programming related facts in the newspaper that she can quote to me. The last week it was about the test that with 3 lines of code can tell who will be good programmers and who will be crap, although no doubt it's most of the crap one's who go on to be programmers. If you want to read about that, here's a link

http://hoskinator.blogspot.com/2006/07/can-you-spot-good-programmer-from-50.html

My Mother rang me up, to ask me if I had seen the article. What article I asked her, the article which says not so many graduates are taking up IT courses (because of the development being pushed off to India she informed me). Well I like this kind of news, hoping that one day in the future it will be a year 2000 golden time again where the lesser spotted programmers are getting paid a squillion pounds an hour because we are in such short supply (Hosk wakes from the nice dream to the harsh food to mouth existence). Anyway I wondered if this had any bearing on the wages for graduates, less graduates equaling more money for ones who are left.

To be honest though the average starting wage has slowly crept up, I think it was about £18,000 or £19,000 when I graduated in 2000. Still there is nothing to be bitter about really, the poor buggers will only have to use the money to pay off the 10 grand student loans they had to take out to pay for the can't be botherlecturersr's to bore them their course.

SCJP Revision Tips

I have been looking about on the web for any SCJP resources I haven't bumped into and I found this site which although based on an older SCJP exam it still had a couple of interesting pages.

it had this page called SCJP Revision Tips

http://www.tipsmart.com/studytools/revtips.htm
Set is unique and do not allow duplicate objects to be stored. Elementsstored in
the Set are unordered and unsorted. HashSet class directlyimplements Set
interface

try block without atleast one catch block or finally block after it givescompile
error.
Note that you can't create an array object with following declaration int
intArray[30];

They are things you should know but it's a good page to help you revise and go over things which might catch you out.


The other page I found useful was called Study Tips

http://www.tipsmart.com/studytools/studytips.htm

it is just a list of some useful study tips and they are good common sense and practical tips.

Actually there is another decent page the links page, this particular links page is full of links to mock exams, although as with many links on the net it is to an older exam, still practise is practise.

http://www.tipsmart.com/studytools/certlinks.htm

I hope people out there who are studying for the exam find these useful and if you have any good SCJP link please leave them in the comments and I will check them out.

Wednesday, August 02, 2006

Type Safe Collections (Generics) aren't so bad

I was reading some stuff about Generics and Collections and doing some boring study for SCJP 5 Exam and passing this exam really is the carrot at the end of the tunnel. I have read a number of blog entries about Generics and complaining that it's the opposite of Generic and that it's infact prohibative. Although I agree with this in some way but it doesn't really bother me and in fact I think that Generics works fine and if you create an List then it should only be able to accept JavaClass unless you wanna put a wild card ? and allow subclasses etc.

Although it is adding extra complexity and catches you out the first few times in the long run I think it makes it easier to understand because you can tell what is going to be in the Collection.


which I think is brilliant and have reviewed it here

Anyway I read this linke

"Arrays in Java have always been type safe—an array declared as type String (String[]) can't accept Integers (or ints), Dogs, or anything other than Strings. But remember that before Java 5 there was no syntax for declaring a type safe collection"

It's funny but I had never thought about arrays being type safe but they are. I think that any move to catch more bugs in compile time and making Collections type safe is just to catch any code which tries to put the wrong type of object into a Collection is a good thing.

I think Generics will be a sort of problem area for me in the exam because we don't use Java 5 yet at work so the only coding I have done with it is just mucking about. I have been writing small examples but it would be good if we could use Generics and Java 5 at work, fistly because it would give us some type safe collections and secondly because then I could practise writing code using Generics.

I have blogged about Generics before


and if you want some resources to learn about Generics and Collections, then look no further I have a blog entry with some links for you


and if that lots doesn't send you to sleep I don't know what will.

Don't go over board with your overloading

I was writing a class today and I was creating a method which returned an xml Element and then I thought perhaps I should write another method which returns a String version in case someone wants that.

At this point I caught myself and thought "why are you doing that"

I was about to spend extra time (not that much in this case) writing another method to return a String instead of an xml Element but why. I am never going to use that method and I doubt anyone else is ever going to use it either. All it would do would slow down any auto complete function in my IDE as I have to choose between two similar looking methods. Plus it would probably be some more code to maintain.

This extra overloading is one of the things I am trying to cut out, it's a complete waste of time for no benefit. I'm trying to get out of this habit because if someone else wants it returned in another way then they can bloody well write it, wrap my method if was an external user.

I just thought I would blog about this because it seems like such an stupid thing to do, writing methods which might be used. This is one of the benefits of writing unit tests firsts, it helps you do just what you need to pass the test and what this really means is you write code to your requirements and not any extra (and not needed) code.

Simplicity and less code is the way forward my friends.

I can't remember where I read/heard it but I think it was A Josh Bloch interview and they asked him if he had any advice to give to new programmers, his advice was "if in doubt, leave it out". I didn't really understand it to much at the time but I think I am beginning to. They also asked how he had been going on a project and he said "pretty good, I took out 40 lines of code today. I don't judge my work on how much code I write but how much I take away these days".

So take the code out before you put it in, a pre-emptive strike.