DuraSoft TechLet 03, 2003
|
Welcome
to the March 2003 TechLet. If you have received this through some one you know and
would like to subscribe directly for our future issues, please send an email to
techlet@durasoftcorp.com with a subject subscribe. This subscription currently
is absolutely free.
In this issue, we have a detailed article on Strings in Java and .NET. |
|
If you like any question to be addressed or topic to be discussed, please send
us an email to techlet@durasoftcorp.com. At the end of the year, one person
will be randomly selected from the list of respondents for a special prize. We
would like to hear from you.
Please send suggestions, corrections and comments to techlet@durasoftcorp.com. |
| Strings in Java and .NET Strings are one of the most basic data types in a language, yet they have been treated very differently in almost all the languages. Even Java developers with reasonable experience make some typical mistakes in this area. This article talks about working with Strings in Java and .NET languages. We present here things that we need to be aware of while working with Strings in Java. We also discuss how some of these hold true and some don't when it comes to Strings in .NET. The complete article can be found at http://www.durasoftcorp.com/download |
Make minimal change to the following C# code so that it prints the following output:
$22.25 & 89.00 %
using System;
namespace Sample
{
class Test
{
[STAThread]
static void Main(string[] args)
{
double val1 = 22.25;
double val2 = 0.89;
Console.WriteLine(val1.ToString() + " & " + val2.ToString());
}
}
}
Send your response to techlet@durasoftcorp.com, and you may become the winner of this year's TechLet special prize. Quiz from the past issue The following code does not compile. Make minimal change (one word) to fix it. Explain the reason why Java compiler insists on this change?
public class Test
{
public static void call(Test obj)
{
new Thread(new Runnable()
{
public void run()
{
obj.doSomething(); // Perform in a new thread.
}
}
).start();
}
public void doSomething()
{ // does some thing...
}
}
Answer! Thanks to those who wrote to use with a response to this quiz. Your names have been entered for the year end drawing! Of course, the change that needs to be made to the code is to add the word "final" to the parameter of the call method as in: public static void call(final Test obj). There is more than one answer to this question. One of the correct answers came from Ken. He writes, "This is because the method call can could change the object that obj points at (change the reference). This would mean that the obj.doSomething() would be calling on the wrong object... Setting it final prevents this change. It does not prevent the programmer from altering the contents of obj, which would need to be made thread safe. This also lets the JRE optimize the thread environment to depend on the object references not changing between threads.." Ken is right on the money. Another way to look at this would be to see that the reference "obj" is a local variable within the call method and is on the stack for the method invocation. It goes out of scope as soon as the method ends and the stack is unwound. It gets sticky if some other thread needs this reference on the call's stack! This problem is avoided by each inner class object making a copy of the reference. Essentially, the object of the anonymous inner class (that implements Runnable) will hold a copy of the obj reference. Of course, if copies of reference is made by inner class, it would not make sense (should I say mathematically?) to change these references in the call method. Declaring the reference final imposes this semantic constraint. |
| It is not worth an intelligent man's time to be in the majority. By definition, there are already enough people to do that. - G. H. Hardy |