0

Would it be faster to point to a function in java that has .'s in it, so java wouldn't have to look up the function, like how it works in Python?

Something similar to this,

print = System.out.println;
print("");
Dubstep
  • 137
  • 1
  • 10
  • 2
    Neither Python nor Java have pointers, to be pedantic. – juanpa.arrivillaga Sep 14 '20 at 16:50
  • 1
    And no, I don't think it would be faster. The JVM optimizes all code before running it, and long names are removed and replaced by direct function calls, so no there's no speed up. – markspace Sep 14 '20 at 16:52
  • 1
    @juanpa.arrivillaga don't know about pyhton, but every reference to a non-primitive variable in java ***is*** inherently a pointer. – Gyro Gearloose Sep 14 '20 at 16:52
  • 1
    In any case, in Python, this isn't a "function with dots". Basically, since Python is a very dynamic language, the compiler cannot optimize multiple expression like `foo.bar.baz`, like say, in a loop. And the full attribute resolution has to occur every time. Java, on the other hand, is much more free to do all sorts of nifty optimizations. – juanpa.arrivillaga Sep 14 '20 at 16:53
  • @GyroGearloose no not really. Prove me wrong: perform pointer arithmetic on such a pointer. Show me the syntax for dereferencing a pointer. But even more than that, because the JVM is free to move objects around memory, the way references work in Java is not as simple. See this Q/A: https://stackoverflow.com/questions/2629357/does-java-have-pointers – juanpa.arrivillaga Sep 14 '20 at 16:59
  • Short answer: use Scala instead – OneCricketeer Sep 14 '20 at 17:05
  • 1
    @juanpa.arrivillaga from the "address in memory" view, *it is* a pointer. If your definition has more to it, we are separated by use of words, not by real facts. – Gyro Gearloose Sep 14 '20 at 17:05
  • @GyroGearloose again, read that Q/A. "address in memory" is not so simple. Depending on the exact implementation, it can be more complicated than that, because again, the JVM is free to re-arrange objects in memory during, say, garbage collection. If you treat references naively as pointers and use unsafe APIs for various implementations to treat them as such, it can easily blow up in your face. – juanpa.arrivillaga Sep 14 '20 at 17:07
  • The Java way of doing that: either as method references, as part of the whole lambda support. Alternativly, you could use reflection, to be precise: the MethodHandle construct. – GhostCat Sep 14 '20 at 17:10
  • @GhostCat but that would almost certainly not improve performance, would it? I would assume the compiler handles all the method resolution at compile time, no? – juanpa.arrivillaga Sep 14 '20 at 17:14
  • 1
    @juanpa.arrivillaga "it can be more complicated than that, because again, the JVM is free to re-arrange objects in memory during, say, garbage collection." Yes, right. But then it will redirect those addresses. Might not fit the absolute definition of a memory address, but for any real live application, it will be good enough. – Gyro Gearloose Sep 14 '20 at 17:14
  • OP, you seem to be assuming an execution model (direct interpretation) that only kinda applies to Python and _certainly_ doesn't apply to Java. Your question doesn't make sense in terms of the language definitions. – chrylis -cautiouslyoptimistic- Sep 14 '20 at 17:28
  • As others have said: you are getting java performance wrong. What makes you think that the standard way of just system.out.println isn't good enough for you? Have you measured anything and found that the few nanoseconds of INVOKING a method do matter in your setup? – GhostCat Sep 14 '20 at 17:29

0 Answers0