Karthick Ravi

Greenhorn
+ Follow
since Feb 27, 2008
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Karthick Ravi

Indra, anything preceeded by a static modifier belongs to a class. So, there has to be a top level class to which that particular component, code is assigned.

Interfaces are used to define the skeleton of a class. So, there is no point in accessing it except for proxy, be it an instance or static way of accessing.

Method local inner class, local variables are declared inside a method. Hence, their scope is just within the method and their access to the encircling class is very limited.

Instance variables are funny because if you add a 'static' modifier to it, it is no longer an instance variable..


Got it.. Damn namespaces.

Changed the code to




and the output looks beautiful.. Thanks Paul.
:) Yup.. I figured that out.. Thanks Paul.. :thumbup:

If you still can't figure it out then by all means go ahead and post the relevant code.




Anyway, my xml file is





This code




gives me an output of

template
config
localeSet




while the following code throws a NullPointerException







Hi,

I have a parent element called "xdp", and a child element called "template". When I get all the children (getChildren) using JDOM and iterate them individually using a loop and use the getName() the answer is "template". But I get a NullPointerException when I go getChildren("template") //According to the documentation the argument is the name. Any ideas why this is happening? do you guys want the code as well?


Thanks.
K.




The above statement in your code is analagous to Object o = new Integer (2) ; Even though iterator.next() returns an object with an Object reference, it is still an Integer object. Hence the Integer version of toString executes. This is due to inheritance.




This results in a compiler error because you are equating a primitive with an object. During Autoboxing the compiler is looking for the reference type rather than the actual object you are passing through. Hence the casting.

Thulasi Arasu wrote:Karthick

That is very cool analogy for thread and object lock.
It helps me.
Thank you.



No worries dude. Anytime.
Synchronzed method

If multiple threads may access a method concurrently and you want the method to be accessed one by one then you synchronize that full method.



Synchronized block

If multiple threads may access a resource (primitives or objects) or a code and you want the resource to be accessed one by one then you lock them using the synchronized block based on an object. Another thread may access that piece of code only if the current thread releases the object.

ex


String s = new String("Hello ");


//somewhere in a method
synchronized(s)
{
s = Thread.currentThread.getName(); //1
System.out.print(s + " "); //2
}


And you start 3 threads Thread1, Thread2, Thread3, with the above block you can be sure that the three threads will execute the above block one by one. The output can be "Thread1 Thread2 Thread3" or which ever thread manages to get the lock. If there is no lock, an example scenario might be Thread1 running "1" and Thread2 running "2" but the output is "Thread1" even though thread2 executes "2".



Think of this more like a crown for a king.


There is only one crown (object) but there can be many princes want to become a king (thread). But only one prince can wear that crown and become the king (object lock). And other princes can become the king only if the current king loses the crown (object release).


Hope this helps. Chilax dude.


In K&B there was quite simillar example



This was actually brought up in a study group apparently which is a modification of the above question. They also don't have a definite answer. The answer was given as 3 earlier as my birthday present to leave me confused for the rest of the day.
But it has brought some rare insights.

Guys, For the following class


class TestClass
{
StringBuilder strBuilder = new StringBuilder()
public void appendString(String str)
{
strBuilder.append(str);
}
}


Does changing StringBuilder to StringBuffer enough to make the class Thread Safe? Or do I need to make the method synchronized to make it thread safe?
Guys, got this question which I fail to understand. Hellppppppp....


Q How many String objects are created in the method?

public String returnString() {
String str = “Hello”;
str = str + “World”;
str = str.toLowerCase();
str = str.substring(2, 5);
return str.toString();
}


My understanding:
String objects are immutable. Hence, any manipulation of an existing object is done by creating a new String object.

Therefore. My answer is that the number of objects created are 6. The list is

1. "Hello"
2. "World"
3. str + “World”
4. str.toLowerCase();
5. str.substring(2, 5);
6. str.toString();



Thanks a lot guys. I understand it completely now..

What made me confused was, since every number literal is taken as an int in java, i thought there shouldn't be a difference between those two. But looks like there is a difference.

Yes Paul, but shouldn't "42" be read as an integer? How can it get converted to byte without an explicit cast?

Guys,

What is the difference between

byte testMethod(){ return 42;}

and

byte testMethod(){ int i = 42; return i;}


The first one compiles but the second one doesn't. My understanding was, java takes in any number literal as an integer, in which case both shouldn't compile.. Thanks.