Explicit vs implicit call of toString

I used to use the implicit call of toString when wanting some debug info about an object, because in case of the object is null it does not throw an Exception.

For instance:

System.out.println("obj: "+obj);

instead of:

System.out.println("obj: "+obj.toString());

Is there any difference apart from the null case?
Can the latter case work, when the former does not?

What exactly is done, in case of the implicit call?


There’s little difference. Use the one that’s shorter and works more often.

If you actually want to get the string value of an object for other reasons, and want it to be null friendly, do this:

String s = String.valueOf(obj);

Edit: The question was extended, so I’ll extend my answer.

In both cases, they compile to something like the following:

System.out.println(new StringBuilder().append("obj: ").append(obj).toString());

When your toString() is implicit, you’ll see that in the second append.

If you look at the source code to java, you’ll see that StringBuilder.append(Object) looks like this:

public StringBuilder append(Object obj) {
    return append(String.valueOf(obj));

where String.valueOf looks like this:

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();

Now, if you toString() yourself, you bypass a null check and a stack frame and go straight to this in StringBuilder:

public StringBuilder append(String str) {
    return this;

So…very similar things happens in both cases. One just does a little more work.


Wildcards in Java PreparedStatements

Here’s my current SQL statement:

SEARCH_ALBUMS_SQL = "SELECT * FROM albums WHERE title LIKE ? OR artist LIKE ?;";

It’s returning exact matches to the album or artist names, but not anything else. I can’t use a ‘%’ in the statement or I get errors.

How do I add wildcards to a prepared statement?

(I’m using Java5 and MySQL)



You put the % in the bound variable. So you do

   stmt.setString(1, "%" + likeSanitize(title) + "%");
   stmt.setString(2, "%" + likeSanitize(artist) + "%");

You should add ESCAPE ‘!’ to allow you to escape special characters that matter to LIKE in you inputs.

Before using title or artist you should sanitize them (as shown above) by escaping special characters (!, %, _, and [) with a method like this:

public static String likeSanitize(String input) {
    return input
       .replace("!", "!!")
       .replace("%", "!%")
       .replace("_", "!_")
       .replace("[", "![");

Is there a library similar to pyparsing in Java? [closed]

I need to quickly build a parser for a very simplified version of a html-like markup language in Java. In python, I would use pyparsing library to do this. Is there something similar for Java? Please, don’t suggest libraries already out there for html parsing, my application is a school assignment which will demonstrate walking a tree of objects and serializing to text using visitor pattern, so I’m not thinking in real world terms here. Basically all I need here is tags, attributes and text nodes.


Another good parser generator is ANTLR, that might be what you’re looking for.


What’s the best way to calculate date difference in Javascript

I doing a function in Javascript like the VisualBasic DateDiff.

You give two dates and the returning time interval (Seconds, Minutes, Days, etc…)

DateDiff(ByVal Interval As Microsoft.VisualBasic.DateInterval, _
  ByVal Date1 As Date, ByVal Date2 As Date) as Long

So what’s the best way to calculate the difference of Javascript Dates?


Use the Date object like so:

function DateDiff(var /*Date*/ date1, var /*Date*/ date2) {
    return date1.getTime() - date2.getTime();

This will return the number of milliseconds difference between the two dates. Converting it to seconds, minutes, hours etc. shouldn’t be too difficult.


Where does Eclipse find javac to compile a project?

Here is what I have:

JAVA_HOME=C:SoftwareJavajdk1.5.0_12 (points to JDK 5.0)

In Eclipse “Installed Runtimes” I have:
jre 1.5.0_12 (points to JRE 5.0)
jre 1.6.0_3 (points to JRE 6.0) (this one is default)

I do not have “javac” on my PATH (i.e. I cannot run javac -version from command line if I am not in JDK/bin).

My project is set to use jre 1.6.0_3 and compiler’s Compliance Level is set to 6.0.

Question is: how Eclipse is able to compile my project? Where it finds a compiler for 6.0? Moreover, is there a way to tell precisely which compiler (i.e. path to javac) is used by Eclipse?

P.S. OS XP SP2 if it is relevant
P.P.S. I do not have any -vm set in eclipse.ini. In eclipse I see eclipse.vm=C:Program FilesJavajre6binclientjvm.dll and it is still a JRE.


Eclipse has the JDT which includes the incremental compiler so it does not need an external one unless that is your wish 🙂

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..