Animesh Shrivastava

Ranch Hand
+ Follow
since Jul 19, 2004
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Animesh Shrivastava

As per the API for the ArrayList's clone method, it says that:


According to what i have understood from the statement is that:
The ArrayList's elements inside the List are not copied, they point to the same reference. That is, if i change an element in the original arrayList, this change can as well be viewed in the Cloned List.
Am i right?

This program explains what Cloning in ArrayList does:

The output shows:


I have gone through the ArrayList code and they have used System.arrayCopy() method to copy the elements of the original ArrayList to the Cloned ArrayList. So the behavior, but why such behavior?
Doesnt this falsify the meaning of shallow cloning?
Amit,
As you know that there is "Main" thread also running along side your "Tux" Thread.
So sometimes what happens is that before the Tux thread get started i.e. run method is called, the Main thread is already on the continuation and reaches the print statement. So you get only "Vandeleur" printed, even tough the Tux thread is still on the go.

To get the desired output what u can do is, let the Main thread sleep for a while.
In ur main method have this:


Hope its clear
Hey All,
This is the sequence how the code runs:
1) It does a String.valueOf(null)--> If the null is passed as an argument, literal string "null" is returned
2) Then new StringBuffer("null") is called
3) Finally on the above created StringBuffer object append("abc") method is run.
The final output is a SringBuffer object having contents as "nullabc".

4) At the end toString() is called to return a new String object.

So,
a += "abc" ==> a = (new StringBuffer((String.valueOf(null)).append("abc")).toString();

Hope u got this

But there are some slight changes the way the program runs depending on the variables,
---------------------------
Now if u have


The way it runs is like this
a = (new StringBuffer((String.valueOf(a)).append(String.valueOf(b))).toString();

----------------------------

Now if u have something like this


The simplified statement is:
a = (new StringBuffer("abc").append(b)).toString();

-----------------------------------------------------------
In the method append() of StringBuffer u have something like this:


Hopw it clears your doubt

Please also look here, i have copied from this post

Concatenation
Exactly Marks,
U r very much right, i think Amit has posted a wrong code, what u have done is correct. And whatever u have explained is also right.
I just explained the intent of the statement which also can be explained the way i have explained.
Look at the updated part of the code below

Now you can see that 1) shouldn't have given any compilation error as the Object which is going to access is of type SubClassB and so no probs.
But when u see 2) u can find the problem, at runtime its actually calling SuperClass method of SuperClass's object which is not permissible.

So by looking at these statements you can say that superclassMethodA() and superclassVarA are not guaranteed to be implemented by the reference accessing them, as in 1) it can be true while in case 2) its false.
This is what i think the main intent behind the statement

Hope i am clear
Let me know if i am wrong anywhere.
[ May 24, 2005: Message edited by: Animesh Shrivastava ]
Hello Jonathan,
As u know that a finally block always gets to execute either after a try block or a catch block. So only after the full completion of a try block or a catch block(If exception gets thrwon), the finally block executes.

So in ur program what i think is that return r is executed before the finally starts its execution. Return value is already set to be returned. So even though the return value gets changed in the finally block, the return is executed with the value set before in the try block.

Hope i am making some sense

Regards,
Animesh
Hey all,
Just wanted to know where can i find the specification document written for the java classes?
Like suppose if i wanna know the specification written for Vector class, where should i look for it?

Thanks
Animesh
Well how about going for an upgrade to SCJP Tiger. Now thats going to be a hot property in coming few months because of its so many upgraded features.

In the mean time the upgrade exam comes into being u can go for SCBCD exam directly.
19 years ago
Nishant,
Please look at the following post:
initialization
Also one more thing, if the shift number is a long variable like as in here shown below, the compiler masks it 0X3F.
Foo[] can contain objects of Foo or its subclasses
When u do Foo[1] , u have in it contained either a Foo object or any of its subclass.

U can also have this done:
Face fc = Foo[1];// Because Face is a super interface of the Foo object
Bar br = Foo[1]; // Bar is Foo's super class, so thats ok

So thats the reason for ur second output.

1) Now for the first question keep in mind this:
Foo[] extends (Object, Object[]) and not Foo, Bar or Face

I hope this is ok
Jas,
I am not able to understand what u r trying to say. But this is how u calculate the final result.

After chopping off the 24 bits u get the result as:
11111001
To calculate the actual value, which is -ve ofcourse what u do is

00000110 (1's complement of the original value)
+1 (add 1 to it)
--------
00000111 (this is ur result)= 7
since, thr result has to be negative, so it comes to -7.
Hope this is clear.

If ur doubt still persists do calrify it.
6)


6)
class A extends Thread {
public void run() {
try {
synchronized (this) {
wait();
}
} catch (InterruptedException ie) {
System.out.print(interrupted());
}
}
public static void main(String[] args) {
A a1 = new A();
a1.start();
a1.interrupt();
}
}



when compile and run the output is "false"

why ? when it is interupted and method interrupted() will return boolean ... as it actually been intruppted then why its false and not true ?


According to JAVA API,


If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.


It clearly says that the interrupt status gets cleared(false) when it is blocked on wait call. And also remember that the return doesnt signify whether its interrupted or not, it returns the interrupt status.

5) Since the exception is a checked one and u havent explicitly thrown it, so u get such a compiler error

4) Thats a warning, u can ignore it. U can very well have a return statement inside ur finally block. That shouldnt be a problem.
For the linked list question,
program just runs with no output because none of the checks results in Assettion failure.
--------------------------------------
The second question u have already posted again, so better look at that
----------------------------------------

3) Here u get 22 bcoz of the following steps being followed during its run:
a) add(1) in the Base class constructor is called first which finally invokes the method of Extension Class because of the object on which this method is called.
This gives u value of i as 2
b) add(2) in the Extension class is called which results in i's value
as 6
c) finally in the method bogo u have add(8) which calls the method of Extension class and the final value of i is 6 + 8*2 = 22.
d) Print is called thereby printing the i's value.

-------------------------------------------------
4) Defualt value for the String member variable is null
-------------------------------------------------
5) Output:
0 3 4
[ May 04, 2005: Message edited by: Animesh Shrivastava ]
I guess the answer should be both 1 and 2.
Whats the correct answer?