Question

Is there anything simple Java can't do that can be done in a similar language or vice versa?

Lets say you have a piece of software in language X and you rewrite it entirely to Java (or the other way around), what are the little things that would seriously hamper the translation?

At first I was thinking of comprehensions or multiple exit loops, but these are easily rewritten with a for_each loop with an if statement and a local variable respectively.

Maybe Exceptions? But which language does not have a similar construct?
Polymorphism? But I don't see how I could show that in a few lines.

I'm looking for a short and sweet example, that would give some serious headache to work around.

EDIT

There are some issues regarding the similarity requirement. I don't think I can explain it better because it is a very theoretic question. The intention was to prevent answers critics would dismiss out of hand because the languages are so different.

For example, I especially like the Lisp conditions answer, although Lisp is a very different language the construct seems similar to Java exceptions but with a twist that cannot be translated. Something like that in C/C++,Fortran,Ruby even, would be even better.

Was it helpful?

Solution

It's a trick question: if you identify such a feature, then it just means the other language wasn't "similar" after all!

But if you relax the similarity requirement, the most obvious one to me would be conditions. In Common Lisp, conditions are like a more flexible form of exceptions. You can call a function, which signals a condition (like throwing an exception), but the caller can then say "go ahead and continue anyway". In Java, once an exception is thrown, there's really no way to continue execution at the point of throwing.

(I know I could say "macros", too, but that's an area of CL that's arguably not similar to Java at all.)

OTHER TIPS

I'm looking for a short and sweet example, that would give some serious headache to work around.

You're looking for a simple example of something that would be trivial in one language and difficult to do in Java?

How about this one-liner of inlined assembly (inside a C program)?

static inline void cpuid(int code, dword *a, dword *d) {
  asm volatile("cpuid":"=a"(*a),"=d"(*d):"0"(code));
}

Good luck Mr Java ;)

Normally, you'd never attempt a 1:1 translation. Each language has different idioms, so the same algorithm or program structure might look very different when rewritten "properly" in another language.

That said, I think list comprehensions and other functional concepts are utterly lacking in Java. For example, the idiomatic Haskell solution to this question:

cart = sequence . map (enumFromTo 0 . subtract 1)

That would take many more lines in Java to implement, and that only after you've wrapped your head around how it even works.

C has no concept of exceptions, but you could use setjmp. C++ has exceptions, thankfully. I think anything Java to C is rough (the other way, not as much... though function pointers have tripped up a few people). GObject has been used in C to do OO in C, but really, if you want OO, use C++.

Really the thing that gets you when moving from Java to another language is the library support Java provides. There's a lot of stuff taken for granted.

Also, going from Java to C/C++ requires the coder to do some memory management. You could use boost shared_ptr, but it's not the same, and you still have issues with cyclical dependencies. Consider a bi directional tree where you have cyclical references to child/parent. You need to use weak_ptr in one of the directions w/ boost to make sure things get cleaned up properly.

What about this:

typedef union {
    struct rgba {
        unsigned char r, g, b, a;
    }
    uint32 packed;
} unpacker;

unpacker x;
x.packed = some_input();
return x.a;

OK, that might still be easy; but try translating java code, that uses reflection to anything else. especially if reflection is used to generate classes on the fly ...

I like the example of unions given by Bendlas for code in C/C++ that doesn't exactly translate to Java. An example the other way around:

   public class example {

      public example(int a, int b)
      {
         ...
      }

      public static void main(String args[])
      {
         try {
           Class cls = Class.forName("example");
           Class partypes[] = new Class[2];
            partypes[0] = Integer.TYPE;
            partypes[1] = Integer.TYPE;
            Constructor ct 
              = cls.getConstructor(partypes);
            Object arglist[] = new Object[2];
            arglist[0] = new Integer(37);
            arglist[1] = new Integer(47);
            Object retobj = ct.newInstance(arglist);
         }
         catch (Throwable e) {
            System.err.println(e);
         }
      }
   }

This uses reflection to construct a Class from a String, which doesn't translate to C++.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top