By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,742 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,742 IT Pros & Developers. It's quick & easy.

jython and toString

P: n/a
Hello,

For one java class (Hello) i use another (HelloPrinter) to build the
string representation of the first one. When i've tried to use this
from within jython, HelloPrinter.toString(hello) call gives results
like Object.toString() of hello has being called. The example below
shows this behaviour.
Could somebody explain this?
// Hello.java
package jythontest;
public class Hello {
private String name;
public Hello(String name)
{
this.name = name;
}
public String sayHello()
{
return "Hello, "+name;
}
}

// HelloPrinter.java
package jythontest;
public class HelloPrinter {
public static String toString(Hello h)
{
return h.sayHello();
}

public static String toMyString(Hello h)
{
return h.sayHello();
}
}

# calljava.py
from jythontest import *
h = Hello("theName")
print h
print HelloPrinter.toString(h)
print HelloPrinter.toMyString(h)

OUTPUT:
jythontest.Hello@523ca2 // GOOD
jythontest.Hello@523ca2 // WRONG
Hello, theName // GOOD
Jython 2.1 on java (JIT: null)

java version "1.5.0_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_03-b07)
Java HotSpot(TM) Server VM (build 1.5.0_03-b07, mixed mode)

Oct 16 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a

ivansh wrote:
Hello,

For one java class (Hello) i use another (HelloPrinter) to build the
string representation of the first one. When i've tried to use this
from within jython, HelloPrinter.toString(hello) call gives results
like Object.toString() of hello has being called. The example below
shows this behaviour.
Could somebody explain this?
// Hello.java
package jythontest;
public class Hello {
private String name;
public Hello(String name)
{
this.name = name;
}
public String sayHello()
{
return "Hello, "+name;
}
}

// HelloPrinter.java
package jythontest;
public class HelloPrinter {
public static String toString(Hello h)
{
return h.sayHello();
}

public static String toMyString(Hello h)
{
return h.sayHello();
}
}

# calljava.py
from jythontest import *
h = Hello("theName")
print h
print HelloPrinter.toString(h)
print HelloPrinter.toMyString(h)

OUTPUT:
jythontest.Hello@523ca2 // GOOD
jythontest.Hello@523ca2 // WRONG
Hello, theName // GOOD
Jython 2.1 on java (JIT: null)

java version "1.5.0_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_03-b07)
Java HotSpot(TM) Server VM (build 1.5.0_03-b07, mixed mode)

I'm guessing your toString(Hello h) is never being called because
there's another toString(something) behind the scenes that's being
preferred. I could be well wrong, but I'm guessing toString isn't meant
to be static, and when you create an object in Java they inherit from
object which defines a default toString. It might be a temporary object
of type HelloPrinter is created in the call to "print
HelloPrinter.toString(h)", and the method you end up calling is the
toString for that temporary (not your static one). This would
especially make sense if there's a version of toString which takes an
object, and returns its toString result...

I'm basing this purely on the fact PrintHello.toMyString() works... so
take with a pinch of salt.

Jon.

Oct 16 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.