Why can you throw anything in Java? [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 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.

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).

Meta-test to test that all JUnit tests are subclasses of a given test class [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.
We have a policy that all test classes extend a given test class. The base class sets the JUnit runner to use as well as checking other things about the test class, so not subclassing from it means that other things you're doing wrong aren't going to be checked. (The most serious of these is when there is a category indicating that a test is expensive to run, but because the runner isn't being used, the test will run even for the run-fast-tests build target.)
I figure this is going to involve parsing the Java code, but I can only seem to find parsers for class files. Is there a good parser out there somewhere for parsing Java source?
(This could tie into another question I was going to ask about enforcing use of certain Javadoc tags, since such a parser would surely also parse Javadoc.)
Alternatively is there a DRY way to set a JUnit runner without subclassing? The whole subclassing business is really, really inconvenient.

Java: If I got stuck while programming, how do I know where to look in the documentation? [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.
Here's a real life example I had experienced before, and this is something that makes me ponder and question this mystery.
Before: I have no idea how to instantiate a class dynamically just by using the name of the class.
After: I looked around in someone else's Java source code, being determined to look for something unrelated to the above, like how to do collision detection, and just accidentally came across a piece of code that answers correctly my question of how to instantiate a class dynamically.
The piece of code in question is this:
A a = (A) Class.forName("A").newInstance();
Where A is the name of a Java source file, A.java.
From here, I was not only intrigued about it, but also starting to wonder, how a Java programmer learn all of this if they were given only the Java language documentation.
How do they know where to look for in the documentation, if they are practicing Java language?
I know that novice Java programmers get some experiences from expert Java programmers, but where do the expert Java programmers learn all of this, if they at first don't know where to look for in the documentation?
Or do they just read from page 1 to the end of the documentation, and follow along closely as much as possible, and start from there? That would take a long time to finish, and it would be an impressive feat to do.
Usually you search google for the Java docs... that send you to the Oracle website that contains it. http://docs.oracle.com/javase/7/docs/api/
On some cases only the API is necessary, but when you need a full formed example you could search for code samples or snippets, or the problem you want to solve itself (i.e. "java instantiate class dinamically").
On the issue of the dynamic instantiation there are a lot of ClassLoader things that can be done but the most basic exemple is similar to the one you found... but there is a catch.
When you reference the A class on your code the jvm automatically loads it for you.
When you do
A a = (A)Class.forName("A").newInstance();
You will reference the A class you already have a grip of.
What you could do is create an interface that the classes you want to instantiate have to implement, like this:
String classPath = ...
AInterface a = (AInterface)Class.forName(classPath).newInstance();
And you use it from there.
But you have to remember to add the try-catch block, because the newInstance method will only know if the referenced class has a default constructor when it tries ti instantiate it at runtime, and if it doesn't have there will be an exception.
The same goes for a exception thrown by the contructor itself, it will be encapsulated and thrown back at you.

Runtime performance of Public vs Private 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 10 years ago.
Is there a runtime performance differance between public and private variables/methods?
I know that it is considered good practice to keep things private if possible, but is there any optimisation related reason.
Like most of these questions I would say; write clear, simple code and it will perform well also.
If some one tells you something is a good idea for performance reasons, make sure this is backed up with real numbers, is still the case for the version of Java you are using (much of this advise is out of date), and it is appropriate for your application.
Often "performance reasons", is an excuse to write obscure code, when actually it may be no faster or can even be much slower (as it confuses the JVM optimiser, just as it will confuse you)
Some people are so sceptical of performance optimisation that you have the quote "premature optimisation is the root of all evil" This is an exaggeration, but it is a good warning, not to worry about performance concerns unless you really know you need to improve performance, and your changes really make a difference.
To this specific question, you can't call a private method from another class. So basically, you can't from another outer class, and from another class in the same outer class, and accessor is created which would normally be inlined if called enough.
I don't think there is directly. Access modifiers are more of a compile-time thing in my view anyway.
Even if there was, don't go that way, there is a very good reason (several of them probably) that you shouldn't make class fields public.
There is an incredibly small performance impact because you have to call the getter and setter methods for a field, but unless you do complex operations there, it definitely won't be noticable. It's a matter of miliseconds at most.
There is no difference runtime performance between private and public variables/methods.It only depend on your program requirements. for example, you have a method that is required for the entire program then you should use public method.Its reduce code duplication. But you have a method that is required only one class then you should use private method.

Why is java better at handling recursion ? [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.
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

Categories

Resources