Why is java better at handling recursion ? [closed] - java

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
I heard one of my colleagues saying that Java handles recursion more efficiently than c or C++, I was just curious what why is it able to do so? I mean what is "under the hood" process that makes this happen.
All efforts appreciated.

The usual issue around recursion (not 100% sure this is what your colleague was referring to) is whether 'it' (the compiler, the JIT, the runtime, whatever) can (and does) implement 'tail call optimization'. The goal is that instead of having the code make 'real' calls (introducing a new frame onto the call stack) that recurse (either into the same function or through the same 'cycle' of functions), you can get the same effect without doing so.
The wikipedia page is pretty decent on describing it.
http://en.wikipedia.org/wiki/Tail_call

If it's correct it's because the JIT compilation is able to optimize a recursion better than the C compiler. http://en.wikipedia.org/wiki/Just-in-time_compilation

Related

Abstraction, an OOPs vs non-OOPs concept [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I am reading OOPS concepts and got stuck on Abstraction. I am not able to fully understand the concept. As I am feeling that it doesn't belongs to OOPS only. It was also used in C. But how
java abstraction different from C language abstraction. I know it is not a good question
for this forum but i am not able to get the perfect answer.
abstraction means to hide or to separate the complex details of one part of code to other part. say, you have to use a method that does complex calculation, and gives some result. So instead of writing your method inline, its better to write it in a method that just expose the signature (params and return type). in that way your caller (of method) remains unaware of complex code behind the method.
in general, when you use library function in c/c++ or APIs in java, it is also an abstraction.
So indeed, abstraction is not only OOP, but a general concept can be applied anywhere (even beyond the programming).

Why can you throw anything in Java? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
In Java theoretically you can throw only Throwables.
This is allowed by the language and checked during class-loading. But if you disable class checking
java -Xverify:none -cp . BadClassThatCompiles
then you can run a class that throws any class (not derived from Throwable) (Example)
Why?
Why is it designed this way .. meaning a virtual machine that allows throwing objects and a verifier that has to filter out wrong code. As if some code could be wrong. It's not the code, it's the design!
Why?
Why is it designed this way .. meaning a virtual machine that allows throwing objects and a verifier that has to filter out wrong code. As if some code could be wrong. It's not the code, it's the design!
Why?
Simply because the design works from almost all perspectives.
Well what would the alternative be?
I guess you would have to have a special kind of "things" that were NOT instances of classes that were designed for the sole purpose of being thrown.
That would require:
a new syntax for defining these exception non-objects
a whole new set of typing rules to handle these non-objects (for instance they cannot be assignment compatible Object ...)
and so on.
At the end of the day, the Java language would be more complex, and harder to use for the programmer. And to what end? To slightly simplify the task of the verifier?
Sorry, but if you take it to its logical conclusion, this idea is a non-starter.
And frankly, who cares if you can break the JVM by disabling the verifier. Its like complaining that you can shoot yourself if you juggle loaded pistols.

Why do Android callback methods' name start with 'on'? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm just curious. (Maybe not only in Android)
Android's callbacks respond to events. You react to events when they occur. So when an event occurs you do something. Therefore:
onOccurrence() {
doSomething;
}
It's just a convention.
Well, Java programming is designed to be readable, and although at times it does appear bloated, it is still very readable, especially Android. I'm guessing the SDK developers wanted to give names that would be read in a very readable manor.
onPause(){
Do stuff here when app is paused.
}
How more obvious can it get?
As they correspond to "events" and in Android all "event" method start with the "on" prefix. I really didn't like this at the beginning as it's not a java convention, but, after all, why not.

Why Expression Language is preferred over Scriplets? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Folks i keep on hearing that we should use Expression language in JSP instead of scriplets. But nobody gives any
concrete reason behind it. Is readability the only reason behind it? I would like to hear some good reasons for using
EL not scriplets so that i can convince my self and others also.I personally feel using scriplets easier than scriplets probably i have
better control on java than EL.
You want to avoid scriptlets with complex logic (such as conditionals or loops), because that just leads to spaghetti code.
Expression language does not have any of this (it just evaluates expressions).

Java optimizations [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
There are a number of Java programming practices that I commonly see in use that can be optimized to provide significant speedups.
Example: For lot's of + operations on Strings, use StringBuilder instead.
What are some simple, useful optimizations one can make to potentially improve your program's performance by a significant amount?
EDIT: I'm not looking for trivial premature optimizations. This is not my intention in asking this question. Instead I would like to learn common constructs/mistakes that do constitute a significant performance hit.
Spend at least an order of magnitude more time thinking hard about the most efficient algorithms and data structures for the problem as you do thinking about how to micro-optimize specific operators, control-flow constructs, etc.
See (1)

Categories

Resources