473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

jython and toString

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
1 4639

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Chermside | last post by:
Ype writes: > For the namespaces in Jython this 'Python internal thread safety' > is handled by the Java class: > > http://www.jython.org/docs/javadoc/org/python/core/PyStringMap.html > > which...
6
by: Dave Benjamin | last post by:
Hey good people, I've been doing a lot of simultaneous Jython and CPython programming lately, and just wanted to say, with no intended ill will toward any of the individuals who have been...
4
by: angel | last post by:
A java runtime environment includes jvm and java class (for example classes.zip in sun jre). Of course jython need jvm,but does it need java class. Thanx
7
by: Jan Gregor | last post by:
Hello I found that jython catches exact java exceptions, not their subclasses. Is there some way to get around this limitation (or error) ? My program has class representing database source...
1
by: scott | last post by:
I installed darwinports and did a "sudo port install jython" ------------------------- scott$ which jython /opt/local/bin/jython ------------------------- Jython works in interactive...
12
by: Mark Fink | last post by:
I wrote a Jython class that inherits from a Java class and (thats the plan) overrides one method. Everything should stay the same. If I run this nothing happens whereas if I run the Java class it...
3
by: Sloan.Kohler | last post by:
Is Jython development dead or has it just seemed that way for over a year?. The jython.org website has a recent new appearance (but no new content) and there is some message traffic on the...
4
by: Neil Wallace | last post by:
Hi all, I am a novice Python/Jython programmer, and Ubuntu user. Ubuntu still only supports only version 2.1 of Jython. I have used the GUI installer of Jython 2.2, and installed it to the...
5
by: sarup26 | last post by:
Hello .. I would like to know more about Python and Jython? What is the difference between both of them? What is the future for Jython and which are the areas where it is used? Swot
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.