The JDK7 milestone 5 update is available for download. Developers now have a chance to try coding with the new language semantics and see for themselves what it is like. The 4 major changes that affect the way one codes in java as of JDK 7 are
- Using underscores in numerals.
- Diamond syntax used to work with collections + generics.
- Using Strings in switch statements.
- Making use of binary literals
Here is a short code sample that you can use to check the new features out. Use a plain text editor and your old friends javac and java, to test it out. IDEs will not support the new syntax and will most likely complain.
Sample JDK 7 Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
| public class Jdk7Tests {
public static void main(String[] args) {
Jdk7Tests jdk7Tests = new Jdk7Tests();
jdk7Tests.integersWithUnderscores();
jdk7Tests.stringSwitch();
jdk7Tests.binaryLiteral();
jdk7Tests.diamond();
}
private void integersWithUnderscores()
{
int i = 1_2;
System.out.println(i);
i*=10;
System.out.println(i);
int j=2_0;
System.out.println(i-j);
}
private void stringSwitch()
{
String key = "akey";
switch (key)
{
case "":
{
System.out.println("Nothing");
break;
}
case "akey":
{
System.out.println("Matched akey");
break;
}
default:
break;
}
}
private void binaryLiteral()
{
byte aByte = (byte)0b001;
short aShort = (short)0b010;
System.out.println(aByte + " " + aShort);
}
private void diamond()
{
Set<String> set = new TreeSet<>();
set.add("c");
set.add("b");
set.add("a");
for (String val : set)
{
System.out.println(val);
}
}
} |
Underscores and numerals:
There are many apache commons projects out there that save your time either directly or indirectly. Some of the well known commons projects are
- Commons beanutils – Services wrapped around java beans.
- Commons digester – XML to java mappings
- Commons lang – Helper utilities for the java.lang API
- Commons logging – A generic logging implementation
There are other commons implementations that can save your time, but you are probably not using them. One of them is commons email. Commons email provides a facade to the underlying java mail services that come from mail.jar. Sending an email using mail.jar is not that difficult, but when you use commons email it is downright simple
Check out a sample program that sends a simple email
1
2
3
4
5
6
7
| SimpleEmail email = new SimpleEmail();
email.setHostName("mail.myserver.com");
email.addTo("jdoe@somewhere.org", "John Doe");
email.setFrom("me@apache.org", "Me");
email.setSubject("Test message");
email.setMsg("This is a simple test of commons-email");
email.send(); |
Thats it. Use the HTMLEmail class to send HTML based emails. You can read more about the commons email API from their user guide. The project is active and should simplify the parts of your application that send email.
As most of you are aware by now, developers can write java robots that can aid a conversation that happens in google wave. A conversation is a wavelet and each reply in this wavelet is called a blip. There are some ‘getting started’ tutorials available out there that are of great help. These links should help you
Official google wave guide
Google wave getting started – Sort of an abridged version of the official guide written by Vogella.
Grasping the overall picture of a java robot is a little difficult. This is because there are no flow or architecture diagrams (at least none that I know of) that show you the sequence of events. Given below is a diagram that does that. Assume that you wrote a java robot that is meant to edit blips in a wavelet. The robot should provide a profanity filter service which will delete objectionable words from the wave. This is how the series of events happen.
If you are a newbie to working with java threads, this post will help you. Certifications like the SCJP require you to understand java threads to a fair degree. Threads behavior can be difficult to understand even for experienced programmers, so I will try to present some examples which will help candidates identify how threads wait / lock and synchronize.
Lets cut the chit chat and jump into a problem. A program increments a counter in a for loop as shown below.
Synchronizing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| public class StaticSync
{
public static final Object s_lock = new Object();
public static int s_counter=0;
public static void main(String... args)
{
new StaticSync().go();
}
public void go()
{
for(int iCounter=0; iCounter<50; iCounter++)
{
Thread thread = new Thread(new Incrementer(new StaticSync()));
thread.start();
}
}
}
class Incrementer implements Runnable
{
StaticSync m_staticSync=null;
public Incrementer(StaticSync staticSync_INP)
{
m_staticSync = staticSync_INP;
}
public void run()
{
synchronized (m_staticSync.s_lock)
{
m_staticSync.s_counter++;
System.out.println(m_staticSync.s_counter);
}
}
} |
This program is pretty simple. This is the sort of code that you can expect on the SCJP exam. Advanced versions of the code above can also appear by adding notify() wait() sleep() etc into the picture. Let us concentrate on the program for now. What can be guaranteed about the output of this program ?
I have taken many java interviews over the last few years. As time passed by, I learned from mistakes I have made. One of them being to ask candidates trick questions or questions that do not necessarily have an obvious answer.
I read a blog post recently that detailed such a question. I will highlight the question here along with the answer
1
2
3
4
5
6
7
8
9
10
| public class JavaPuzzler{
public static void main(String[] args) {
HashSet<Short> s = new HashSet<Short>();//1
for(short i = 0; i<100;i++){//2
s.add(i);//3
s.remove(i-1);//4
}
System.out.println(s.size());//5
}
} |
Can you guess the answer to this question ? Simply drag and select the text near the spoiler to see the answer.
Spoiler: The answer to the question is 100. The gotcha is that the statement s.remove(i-1); at //4 will autobox to an Integer and not a Short. Equals comparison between an Integer and Short fails.