如何首先预测它是否会给出运行时错误或不会编译
我正在各个网站(如 blackbeltfactory 等)查看有关 java 的测验和测试。我遇到的问题有“不编译”或“在运行时抛出异常”等选择。
有什么方法可以乍一看就会猜到会发生什么吗?或者是熟悉java的问题? 我认为这是关于java如何工作的重要一点。
提前致谢, 布格拉
I am looking at quizzes and tests at various sites (like blackbeltfactory, etc..) about java. I come across with questions which have choices like "doesn't compile" or "throws exception at runtime".
Are there any way to guess which will occur, at first look? Or is it a matter of getting familiar with java?
I think this is an important point on how java works.
Thanks in advance,
Bugra
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于人类来说,您必须“熟悉 Java”。
对于机器(或程序)来说,它必须遵循语言指定的规则。
例如给定的类:
的结果是什么
调用A) 无法编译
? B)运行时抛出异常?
要回答这个具体问题,你必须知道 Java 中的类是如何定义的(要知道我显示的类定义是否有效),你还必须知道属性是如何定义的以及默认值等,所以你有熟悉该语言。
当您了解了所有这些概念后,您可以通过快速查看轻松回答。
BTW,著名的Sun Certified Java Programmer认证,就是要了解这类东西,而不是知道如何开发应用程序。这是关于将你自己转换为“人类编译器”
For a human, you have to "get familiar with Java".
For a machine ( or a program that is ) it has to follow the rules the language specifies.
For instance the given class:
What would be the result of invoking:
A) doesn't compile?
B) throws exception at runtime?
To answer this specific question you have to know, how classes are defined in Java ( to know if the one I show is a valid class definition or not ) , also you have to know how attributes are defined and default values etc, so you have to get familiar with the language.
When you know all these concepts, you can easily answer with a quick view.
BTW, the famous Sun Certified Java Programmer certification, is all about know this kind of stuff, rather than knowing how to develop an application. It is about converting your self in a "human compiler"
显然,更好地了解 Java 会有帮助。但是,有一些一般规则。
“无法编译”意味着编译器实际上无法理解代码的语法。发生这种情况的原因可能是方括号或圆括号放错位置或丢失、参数数量错误的方法以及其他此类情况:
“在运行时引发异常”意味着编写的代码在语法上是有意义的,但在实际执行时却发生了变化。告诉机器去做一些无论出于何种原因都是不可能的事情。一些异常内置于语言中,例如如果您尝试除以零,但许多异常也是在代码中显式定义和抛出的。程序员使用此类异常来创建程序,当他们尝试做不应该做的事情时,这些程序会明显中断,而不是默默地中断,然后导致其他问题:
一般来说,编译器错误看起来更像是拼写错误(并且通常是由于拼写错误),而运行时异常将由在特定条件下失败的行引起(例如某些变量等于零)。不过,再一次强调,真正了解这门语言是无可替代的。
Obviously, knowing Java better will help. However, there are some general rules.
"Doesn't compile" means that the compiler literally could not understand the syntax of the code. This can happen as the result of misplaced or missing brackets or parentheses, methods with the wrong number of arguments, and other such things:
"Throws exception at runtime" means that the written code makes sense syntactically, but that when it is actually executed it tells the machine to do something that is, for whatever reason, impossible. some exceptions are built into the language, such as if you try to divide by zero, but many are also explicitly defined and thrown in the code. Programmers use such exceptions to create programs which visibly break when they try to do something they shouldn't, rather than breaking silently and then causing other problems down the road:
Generally speaking, compiler errors will look more like typos (and often will result from typos), whereas runtime exceptions will result from lines which fail under specific conditions (e.g. some variable is equal to zero). Once again, though, there is no real substitute for truly knowing the language.
老实说,这是一个熟悉 java 的问题,但有些编译错误乍一看很明显,而且它们与其他语言非常相似。
In my honest opinion is a matter of getting familiar with java but some compiling errors are clear at first sight and they are very similar to other languages.