sasi dhulipala

Ranch Hand
+ Follow
since Dec 28, 2000
Merit badge: grant badges
For More
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 sasi dhulipala


Actually what is that you want from the code you have given ?
Sasidhar
Michael,
It is like this
When we say new Extension()
First it calls its own Constructor which in turn calls
its base class contructor, when it finds add fn in the base class
constructor the add fn inside Extension class is called.
remember the method calling is dependent on the object of
reference at run time. Here the derived class reference is passed hence add fn inside Extension isd called. From then onwards it is simple program flow which goes on like:
i = 0; default
i += (v=1) *2 // add fn inside Extension
>> i =2;
again add(v) inside Extension Constructor.
i (2) += v(2)*2;
>>i =6;
Now inside bogo another add(8) is there this results in
i += v(8)*2
= 6 +16
= 22
inside bogo once we call print it prints 22 as out put.
HTH
sasi


[This message has been edited by sasi dhulipala (edited January 11, 2001).]
A is correct.
This involves little bit of bit wise operations.
MY_FLAG contains a 1 in the 5 th bit starting from left.
x val we donot know .
~MY_FLAG makes all bit positions except the 5 th bit from left to
be 1 and the 5 th bit contains a 0.
No when you do x & ~MY_FLAG which is equal to x & ( ~MY_FLAG )
All the bit positions in x where ever there is a 1 is stored as 1 in the resultant x and wherever there is 0 is storead and 0 except in the 5 th position where a 0 is stored.
In the end the operation results in erasing what ever there is in the 5 th bit position and putting a o there.
more clearly let x = 24 say
x = 0001 1000 MY_FLAG =16=0001 0000
~MY_FLAG= 1110 1111
----------------------
x& ~MY_FLAG=0000 1000
there fore x = 0000 1000
HTH
sasi

Originally posted by nikhil nandu:
static final int MY_FLAG = 16;
Method removeFlag takes int x and returns int value which do not
contains flag MY_FLAG. Which of the following implementations are
correct:
A. return (x & ~MY_FLAG);
B. return ~(x | MY_FLAG);
C. return (x & MY_FLAG > 0 ? x-MY_FLAG : x);
D. return ~(~x | MY_FLAG);
This is very confusing stuff.I didn't have ans for this qns.Please help.
Thanks in advance.


Further I am able to pass anonymous arrays of both primitive as well as object type to methods..
Sasidhar
since it is retruning it self ( i.e tostring ) above will print equal.
reproduced from API
toString
public String toString()
This object (which is already a string!) is itself returned.
Overrides:
toString in class Object
Returns:
the string itself.
Sasi
I am unable to create like what you have done However I was able to create one and return it from a function
I am giving an example below which creates anonymous arrays of primitive data types and anonymous arrays of objects and returns from methods;
//file: test.java
public class test
{
int num[];
numbers getnumber[];
public static void main(String []aa)
{
test testing = new test();
numbers someNumber = new numbers();
testing.num = testing.getint();
testing.getnumber = someNumber.getnumbers();
for ( int i = 0; i < testing.num.length; i++)
System.out.println(testing.num[i]);
}
public int[] getint()
{
return new int[] { 1,2,3,4};
}
}

class numbers
{
int val;
static int inst;
numbers()
{
val = inst++;
System.out.println( "Value : " + val);
}
public numbers[] getnumbers()
{
return new numbers[] { new numbers(), new numbers(), null, new numbers() };
}
}
HTH
Sasidhar

Originally posted by sunilkumar ssuparasmul:
i wanted to know in what order does a class initialize meaning if u have a'
1)set of class level variables
2) static block initializer
3)instancs block initializer
static blocks and static variables, if any ,would get initialised first. These are initialised when the class gets loaded. as these are per class variables
The next comes initalisation of other class variables and instance blocks. And these occur when ever an object of the class
created and these get initialised in the order they are declared in the Class definition.
HTH
Sasi
which would initialize first?

thanks in advance
sunil.s


Originally posted by amit sanghai:
To Sasi,
I didnot understand your answer to the first question!!![/B]


Hi
I have explained this in one my earlier postings...I am giving it again down below:
I would like to explain this with an example
/file: example.java
class example extends Thread
{
public increment localIncr;
example( increment var)
{
localIncr = var;
}
public static void main(String a[])
{
increment obj = new increment();
example one = new example(obj);
example two = new example(obj);
one.start();
two.start();
}
public void run()
{
localIncr.addone();
}
}

class increment
{
int incrVar;
public void addone()
{
incrVar++;
System.out.println("var Val : "+ incrVar);
}
}

Here in above the example class extends Thread class and creates two variables one and two of class exapmle in the main method. To the constructor of the example class I am passing a reference to
an increment object. you can see that while creating two thread one and two I am passing the same reference obj. Basically this increment object is common to both the thread objects one and two This is what is data sharing..
Multiple threads always share and operate in the same data segment. In other words they share the same process space.
Now the run method of example class tries to call addone method of increment object passed to it.. depending at what time there is a context switch between the threads ( I mean depending on when the OS decide to switch the threads) one and two the final value printed by addone method would vary.
I hope I am clear.
Sasi.
Jane,
I fully agree with you. Your above explanation is correct for the case I am giving below:
class Mystery {
public static void main(String args[])
{
Mystery mys = new Mystery();
Changer ch = new Changer();
System.out.println("value of i before method call "+ ch.i); // prints 10
mys.method(ch);
System.out.println("value of i after method call"+ ch.i);
}
public void method( Changer obj)
{
obj.i = 25; // this is visible
obj = null; // this must have no affect
}
}
class Changer
{
int i = 10;
}

Now let us go to the method given by Vivek,
8. void method(String s[])
{
9. String temp = s[0];
10. s[0] = s[1];
11. s[1] = temp;
12. }
Assuming that here also the references are passed by value
the affect of s[0] = s[1] and s[1] = temp; should actually have no affect on the original args[].
No my expalanation for the above real Mystery is as below:
String are objects
Arrays are also passed as references.
when we pass an array if string objects we are passing something like a double pointer ( pardon for my C inclination ) : a kinda
reference to a reference.
Which means that we are passing an array of references and since as array allows us to change the value of its each individual element ( which is a reference in this case ) we are able to
change the ref values of s[0] and s[1] inside method and still see the change out side the method call.
...Pl correct me if I am wrong/
Sasi


[This message has been edited by sasi dhulipala (edited January 10, 2001).]

What about the 5th question?[/B]
5 th question is very ambiguous.
Default constructor is provided for all classes
this is true if an only if you donot define any costructors for
a class other wise it is false
Default constructor is provided for the class which does define any constructor.
If a class is defining a constructor a default constructor is NOT
provided, we have to explicitly define a default constructor depending on whether we are going to use it or not. like in Q.6 where we are required to define a default constructorin parent class.
HTH
sasi
true is being printed on my sys. because the if statement
if (flag = true) is assigning true to flag and the enitre value of the expression becomes true making if to pass through and print
"true".

Sasidhar

[This message has been edited by sasi dhulipala (edited January 09, 2001).]

In this question, the ans is 10 is one of the output.
This is correct.
But my question is what is the" this "in line one means?
this means the current object referring to the Test class
it means it is the reference returned by the call to new Test();
I complile the program and it outputs some numbers seem no meanings.
All the out out has a meaning attached to it
The statement System.out.println("In test"); gives rise to
out put >> In test;
the statement at line one orints the currenct reference to the object of Test on my system it give Test@1dae076f but it may differ on yr system Nothing wrong in it.
line 2 gives an out put of 10;
And for line 2, I put total instead of this.total still got the same result. In this case, Is there any different that I use this or no this ??
since total is declared as static it is a class varaible and can be approached directly from class name or through an object of it.
so all calls like
int temp = total; // being class variable to avaiable
//directly to this method
int temp = this.total; // approach from an object of class
int temp = Test.total; // approach from class name directly.
HTH
sasi
[/b]

[This message has been edited by sasi dhulipala (edited January 09, 2001).]

Originally posted by amit sanghai:

Hi sasi,

Q1) Why does the println() function does not work inside finalize method() ?
There seems to be some twist here. See the API doc
"protected void finalize()
throws Throwable
Called by the garbage collector on an object when garbage collection determines that there are
no more references to the object. A subclass overrides the finalize method to dispose of
system resources or to perform other cleanup. "
What above means is that finalize is called when the object has already gone out of scope( and at this point of time it is at the mercy of the gc to be started by OS) that is why println method is not working. if we call explicitly the finalize method it is working. No more references means it is out of scope of all methods .

Q2) I meant object is out of scope? I think that you cannot find out whether an object is available for garbage collection. Is that true?


some times you can make finalize method to resurect the ( about to be garbage collected object )object.
to sum it all (as you said ): once an oject is out of scope( of all the reference to it ) we can say it has become ready for garbage collection. but when it is gc'd ( i.e exited ) exactly cannot be known.
sasi
anybody can tell me what im missing..
Actually nothing is missing and both the answers are correct.
starting a thread by st.start() does NOT guarantee that run() method will be invoked immediately.. It is up to the operating system when to make a thread running.. the start method only brings thread st into RUNNABLE state. but OS only decides when it runs mean while other methods can start executing.. like
myThread above..
HTH
Sasidhar

[This message has been edited by sasi dhulipala (edited January 09, 2001).]

Originally posted by amit sanghai:

Q1) What are the thread-to-thread communication?
Q2)what is a deadlock? How can you avoid a deadlock?


Thread to thread communications can be achieved by local variables I mean the common data segment they both share.
a deadlock occurs when one thread is waiting on the resource
claimed by another thread and the ( another)second thread is waiting for the resource claimed by the first thread.
The best way to avoid is to use yield method so that for some time the resources claimed by the thread are released and other threads can run.
Sasidhar