Jon Aryan

Greenhorn
+ Follow
since Oct 06, 2000
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Jon Aryan

Hi Prasad,
as u know System.out variable is a PrintStream object.
In the PrintStream's println(int) the following methods is called,
String.valueOf(int);
valueOf(int) method in the String class calls the Integer.toString(int), which creates a new String object.

So, passing the primitives to the System.out.println() method creates a String object.
Passing an Object to the System.out.println() method calls the objects
toString() method if the object reference is not null, if the object reference is null, a new String "null" is created and written to the stream.
---------------------
Hi Matt,
in the following code it looks like all the 11 objects will be
garbaged collected !.
When the output stream objects get a String object,
it will take the characters out from the String and outputs to the underlying sink ( it may use char[], or byte[] for buffering and flushes the buffer).
So y the PrintStream should hold the reference to the last Object in the for loop. rite ??
So i guess ur rite, 11 objects will be garbage collected

Correct me if am wrong

[This message has been edited by Jon Aryan (edited November 14, 2000).]
If the hashCode() method should always return the memory location of the object, wat about the wrapper classes which overrides the hashCode() to return the primitive values ( or some representation of the primitive values) !!! ??
Ok. let me cut n paste the general contract of the hashCode() from the API just 2 remind u ..
--------------------------------------------------------
Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.
The general contract of hashCode is:
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
--------------------------------------------------------
Static variables are garbage collected when the class is unloaded !!! unlike the instance variables, which are garbaged collected when there are no more reference to the object. rite ?
Viji,
U mean 2 say, the 2nd thread prints 0 .. 2, without pressing the ENTER key ?? !. [ I (still !) believe the "blocked" thread is not releasing the lock of the object ( thatz wat am seeing !!) ]
please modify the for loop like
for (int count=0; count<3; count++) {<br /> System.out.println("Hash code of the thread --> " + Thread.currentThread().hashCode() );
}

[This message has been edited by Jon Aryan (edited October 24, 2000).]
[This message has been edited by Jon Aryan (edited October 24, 2000).]
[This message has been edited by Jon Aryan (edited October 24, 2000).]
hello Mukesh,
if this blocking IO operation [ System.in.read() ] is releasing the
lock of the object, can u please explain y the 2nd thread not printing the
"name + count" in the for loop[ after the 1st thread prints from 1 .. 3 ], inside the synchronized block ?

import java.io.*;
class r implements Runnable {
static byte[] b = new byte[0];
String name;
r(String name) { this.name = name; }
public void run() {
//orginal code --> synchronized (b)
// modified code
synchronized (this) {
for (int count=0; count<3; count++) System.out.println(name + count);
int i = 0;
try {
//while (i!=-1) {
i = System.in.read();
System.out.println(name + i);
//}
} catch(IOException ioe) { System.out.println(name + "IOException"); }
}
}
public static void main(String[] args) {
// modified code
r myThread = new r("me");
Thread t1 = new Thread(myThread);
t1.start();
Thread t2 = new Thread(myThread);
t2.start();

}
}


[This message has been edited by Jon Aryan (edited October 23, 2000).]
Jane ... "y" is a local variable ... not an instance variable ! ... may be a typo rite ??
Aryan.
Hi Kishan,
I thought the Java API has cheated me, when u told the above code is working !!!
-----------
public void consume(producer b) {
for(int i = 0;i<20;i++) {
synchronized(b) {
try {
while(!b.flag) {
System.out.println("waiting");
/////// Can't be !!! U did not acquire the lock for Consumer object
this.wait();
}
b.flag = false;
System.out.println("Got " + b.a);
b.notify();
Thread.sleep(1000);
} catch(InterruptedException e) {
System.out.println("Interrupted eception caught in consumer");
}
--------------
Did u noticed that the message in the while loop is not executing ???
--------------
Modify that code by removing the while loop, then execute the code !!! i mean like this
//while(!b.flag) {
this.wait();
//}

-------------
Then put the original while loop back, and comment the Thread.sleep() method from both the classes !!
-------------
I modifed ur code, try this out ..
------------
import java.util.*;
class producer implements Runnable{
public int a;
public boolean flag = false;
public synchronized void produce() {
for(int index = 0; index < 5; index++) {
try {
while(flag == true) {
System.out.println("Producer waiting ..");
wait();
}
a = (new Random()).nextInt();
flag = true;
System.out.println("produced " + a);
notifyAll();
} catch (InterruptedException e) {
System.out.println("Interrupted eception caught in producer");
}
}
}
public void run() {
produce();
}
}
class consumer implements Runnable {
producer ab;
public consumer(producer pr) {
ab = pr;
}
public void consume(producer b) {

for(int i = 0;i<5;i++) {
synchronized(b) {
try {
while(b.flag == false) {
System.out.println("Consumer waiting ...");
b.wait();
}
b.flag = false;
System.out.println("Got " + b.a);
b.notifyAll();
} catch(InterruptedException e) {
System.out.println("Interrupted eception caught in consumer");
}
}
}
}
public void run() {
consume(ab);
}
}
class prodcons {
public static void main(String args[]) {
producer p;
Thread tp = new Thread((p = new producer()));
Thread tc = new Thread(new consumer(p));
tc.setPriority(Thread.MAX_PRIORITY);
tp.setPriority(Thread.MIN_PRIORITY);
tp.start();
tc.start();
}
}
-----------

[This message has been edited by Jon Aryan (edited October 12, 2000).]
[This message has been edited by Jon Aryan (edited October 12, 2000).]
Hi Kishan,
check ur code
--------------------
class consumer implements Runnable {
producer ab;
public consumer(producer pr) {
ab = pr;
}
public void consume(producer b) {
// ACQUIRE THE LOCK ON the Producer instance
synchronized(b) {
try {
while(!b.flag) {
// !!! WAIT on the "Consumer's" wait pool
wait();
// U can't do this !! rite ??, since u did not acquire the lock
// for this Consumer instance
// Instead u can say
// " b.wait() ", ie wait on "Producer" wait pool
// or synchronize "this", ie on "Consumer" instance
}
b.flag = false;
System.out.println("Got " + b.a);
// Same here !! u did not acquire the lock for this instance !!
// so u cannot notify the threads waiting on this instance monior
// !!!
notify();
/// !!!
Thread.sleep(1000);
.........
------------
[This message has been edited by Jon Aryan (edited October 12, 2000).]
Hi Sagar,
* The class need not be Serializable or Externalizable to declare itz fields as "transient" [ though itz meaningless to do so ]
* But to write the instance of the class ( serialize) into a stream, the class need to implement Serializable or Externalizable, else "NotSerializableException" will be thrown.
This link shown some codes http://www.javaranch.com/ubb/Forum24/HTML/004691.html
Hi krishna,

class Me {
public void jump() {
// Codes and more codes
}
}
class You {
public void jump() {
// Codes and more codes
}
class They {
public void jump() {
// Codes and more codes
}
}


Ok, now I want to notify all the instances of the above classes
when some interesting events happens
in some other object (call it "X"). When "X" jumps, objects of class
"Me", "You", "They" needs to notifed, that is i want 2 call the
jump() method in those objects.
[ Later u may want to create few more classes, that needs to be notified. ]
How do i go about the design ? How do the class "X" calls the
jump() method of these objects ?. [ assuming an instance of class "X" has
reference to the above instances, in a Collection (some how i managed it !! ) ]

So i need to say

" class You extends Me " ??? // Assuming am not a human !!

and

" class They extends You " ?? // They r humans !! maybe ..

NOOOO.

I don't inherit from some meaningless class just to reuse some codes.

But the above classes have something in common, they are interested to
know when "X" jumps, thats all in common.

So wat do i do ??

I will take that "commonality" in those classes and make an "interface" !!.

interface JumpingListener {
public void jump();
}

and i modify the classes to say that they are jumping listeners!!.

class Me implements JumpingListener {
public void jump() {
// implementing the method in my own way.
}
}

class You implements JumpingListener {
public void jump() {
// implementing the method in Your own way.
}

class They implements JumpingListener {
public void jump() {
// implementing the method the way They like.
}
}
Now all these classes have something in common, they r jumping listeners.

Now some skeleton of class X
class X {
Collection l;
public void addJumpingListener(JumpingListener jl) {
// add to to collection
}

// X jumps here ( the event )

// listeners gets notifed here [ the listeners are taken from the collection
// and their jump() method is called.

}
* X need not know the exact "class" of the object, ("Me", "You".. and some classes
not yet invented)all it want to know is the type of the object, in this case
these objects will be of type "JumpingListener". So "X" deals with the
JumpingListener types.
* In future you can create many more objects of type "JumpingListener",
class "X" need not know about them.

( these r just the few things abt the interfaces, and u will really appreciate the interfaces when u c the Java APIs)
So r "interface" good enough ??? Convinced ?

OK atleast u got bored !.
---------
Jon Aryan
---------

Originally posted by psr krishna:
hi all
Still iam not convinced why because?
the real use of inheritance is when your really inheriting one or more defined properties from parent class or interface.But in case of interface the property(method) is (are) completely not implemented,So instead of inheriting that un implemnted methods from interface i can easily give complete implementation for those methods.We can take the case of EVENT LISTENER INTERFACES
For example I can simply write addActionPerformed() and etc., methods with complet implementation as per my need without inhereting from ActionListener interface then it will work.
So now my point is better clear now if iam wrong please pardon .
--thanks
sivarm



[This message has been edited by Jon Aryan (edited October 11, 2000).]
Object references are in the stack memory and objects are created in the heap memory.
The stack pointer is moved down to allocate an object reference and moved up to remove it, which is not an expensive process, unlike an object creation in the heap memory.
[This message has been edited by Jon Aryan (edited October 09, 2000).]
Hi gautam,
Hint:
* if there are some white spaces or control character in the String, then a substring operation is done on the String and a "new" String is returned after trimming the String.

* trim() returns the same String instance if nothing to trim !!!
-- try the ur code with an intern() method
like
if(" String ".trim().intern() == "String")
[ Later i will put some codes ]
Hi sola,
Look at the question once more.
"When is the Integer object, created in line 3, eligible for garbage collection? "
Itz not asking about the object which is pointed by "x"
[ "x" is infact the copy of the orginal reference ] before the method is called.
-------------------------------------
Lets modify the code to make it more clear.
class A {
public static void main(String args[]) {
X b = new X();
String n = "aryan";

// The copy of the reference "n" is passed to the method m().
// The orginal "n" points to the string "aryan" . rite ?

b.m(n);
}
}
public class X {
public void m(Object x) {

// remember "x" is the copy of "n"

// Now "x" will point to the new object, no more to the orginal String object it was pointing.

x = new Integer(99);

// now "y" also point to the same object pointed by "x"
// [ an instance of Integer ]


Integer y = (Integer)x;

// now the above object created got only one reference ie "x"

y = null;
System.out.println("x is" + x);

// "x" is going to die, now , since its life period ends after the
// method call. V r talking about the "x", which is a copy of
// "n".
// So the "x" dies, Integer object is orphaned, and Garbage collector
// gets a an orphan to sweep, and its happy. [ the String object is
// still living happly some where in the memory, coz, it still has
// a reference, "n".

// So question was abt the Integer object not abt the String object.
// now u know the answer .. rite ?
}
}

[This message has been edited by Jon Aryan (edited October 08, 2000).]
Yes,
final static transient String s = "Me"
is a valid declaration .
--------------------------------------------
import java.io.*;
class A implements Serializable {
final static transient String s = "Me"; // Serialized [ transient !!, but static ]
transient String ss = "Marlboro"; // Not serialized
static String sss = "Lights";
static B b = new B();
// By default serializable fields of a class are defined to be the
// non-transient and non-static fields. Eh!. V will catch him later !!.
// Spare him 4 now.
int i;
A() {
}
}
class Tester {
public static void main(String args[]) throws IOException, ClassNotFoundException {
A a = new A();
FileOutputStream out = new FileOutputStream("tmp.ser");
ObjectOutputStream o = new ObjectOutputStream(out);
o.writeObject(a);
FileInputStream in = new FileInputStream("tmp.ser");
ObjectInputStream o2 = new ObjectInputStream(in);
A aa = (A) o2.readObject();
System.out.println("Static Transient string !! --> " + aa.s);
System.out.println("Instance variable i --> " + aa.i);
System.out.println("Non-static Transient string --> " + aa.ss);
System.out.println("Static string --> " + aa.sss);
System.out.println("Instance variable of class B --> " + aa.b.i);
}
}
class B implements Serializable {
int i = 20;
}

-------------------
Outout is
* Static Transient string !! --> Me
* Instance variable i --> 0
* Non-static Transient string --> null
* Static string --> Lights
* Instance variable of class B --> 20
-------------------

Originally posted by Shameen Mazhar:
Plz tell me if transient variables can be static?? The book by Rasmussen says that these cannot b static, at the same time in the questions it does not mark a :
final transient static private double PI= 3.14...........
to b illegal. I tried to run it and it works, then why does the book at pg 126 says that "transient modifier cannot be specified for static variables" ??



[This message has been edited by Jon Aryan (edited October 08, 2000).]
Ur rite Ashok ............