- 浏览: 746455 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (419)
- 杂软粉墨 (2)
- 创意灵感 (3)
- 经验记录 (137)
- 开源轨迹 (2)
- sip-communicator (2)
- 闲侃杂谈 (8)
- 问题交流 (24)
- 概念模式 (32)
- 难点备案 (5)
- JwChat (1)
- 中国象棋 (1)
- 教育探索 (6)
- 英语研究 (58)
- 星际争霸 (1)
- 电信知识 (1)
- 软件架构 (3)
- 哲学探索 (26)
- 算法灵魂 (8)
- 近视探索 (6)
- 数学数学 (3)
- 牛角钻尖 (23)
- 至强文言 (3)
- 数据结构 (1)
- 宇宙物理 (2)
- 网络架构 (3)
- 游戏领域 (4)
- 图形处理 (2)
- 修炼之路 (8)
- 读书天地 (20)
- 编解乱码 (2)
- 概念探索 (8)
- 格物致知 (1)
- 其它语言 (1)
- 测试领域 (3)
- 文化风流 (1)
- JQuery (1)
- 網頁領域 (1)
- Unix/Linux (1)
- Inside JVM (1)
- 异常分析 (1)
最新评论
-
suyujie:
引用
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
iamzhoug37:
您能说一下"局部变量不受文本顺序限制" 是 ...
声明前为什么能赋值却不能输出,都是使用
2011.03.24
now i've been through 81 puzzles. every puzzle, i've read it as carefully and detailedly as possible, the next is the 82nd, Beer Blast.
2011.03.27
now i've arrived at the puzzle 90.
The snippet given is the following:
public class Outer { class Inner1 extends Outer {} class Inner2 extends Inner1 {} } // DOES NOT COMPILE!!
The problem is that due to how default constructor is defined, we really have the following:
// Same as above but with default constructor included explicitly public class Outer { class Inner1 extends Outer { Inner1() { super(); } } class Inner2 extends Inner1 { Inner2() { super(); } } } // STILL DOES NOT COMPILE!!
The problem is that Inner2
's superclass is itself an inner class Inner1
, thus making Inner2
's default constructor illegal since it requires an enclosing instance to be supplied to the constructor.
The "brute-force" way to fix the problem is to provide this explicitly with a qualified-this
expression:
// "brute-force" fix public class Outer { class Inner1 extends Outer { Inner1() { super(); } } class Inner2 extends Inner1 { Inner2() { Outer.this.super(); } } } // NOW COMPILES!
However, the puzzle prescribes that such complicated situation is best avoided in the first place. Here are some quotes:
This compiles, but it is mind-numbingly complex. There is a better
solution: Whenever you write a member class, ask yourself, Does this
class really need an enclosing instance? If the answer is no, make it static
.
Inner classes are sometimes useful, but they can easily introduce
complications that make a program difficult to understand. They have
complex interactions with generics (Puzzle 89), reflection (Puzzle 80),
and inheritance (this puzzle). If you declare Inner1
to be static
, the problem goes away. If you also declare Inner2
to be static
, you can actually understand what the program does: a nice bonus indeed.
In summary, it is rarely appropriate for one class to be both an
inner class and a subclass of another. More generally, it is rarely
appropriate to extend an inner class; if you must, think long and hard
about the enclosing instance. Also, prefer static
nested classes to non-static
. Most member classes can and should be declared static
.
2011.03.28
today, i've arrived at puzzle 94: Lost in the shuffle, this one really made me suffer a lot from deep thoughts, cuz in the first place, i didn't understand the following paragraph:
Why does the graph have this shape? We don't know all the details but we can offer some intuition. Let's restrict our attention to the array's first element. After the first iteration of the loop, it has the correct expected value of (n - 1) / 2. In the second iteration, however, there is 1 chance in n that the random number generator will return 0 and the value in the first element will be replaced by 1 or 0. In other words, the second iteration systematically reduces the expected value of the first element. In the third iteration, there's a further 1 chance in n that the first element is replaced by 2, 1, or 0, and so on. For the first n / 2 iterations of the loop, the expected value of the first element decreases. For the second n / 2 iterations, it increases but never catches up to its fair value. Note that the last element in the array is guaranteed to have the correct expected value, as the last step in the execution of the method is to select it at random from all the elements in the array.
why after second iteration, the first element will be replaced by 1 or 0, where does the 0 come from? yes, it comes from the first iteration, if the randomly generated number of the first iteration is 1, a[0] and a[1] are exchagned, thus a[1] has been replaced by 0. great!
2011.03.29
i've finished it.
发表评论
-
effective java 2nd Item 18
2012-08-15 15:45 1218Prefer interfaces to abstract ... -
Head First HTML with CSS and XHTML
2012-03-27 14:50 974i finished it by fast reading a ... -
head first jquery
2012-03-22 10:21 1136Thursday, March 22, 2012 fi ... -
Software Architecture Design Patterns in Java
2011-05-13 22:53 813to be continued... -
Effective Java 2nd edition
2011-04-25 16:19 1016Item 3: Enforce the singleton p ... -
Clean Code: chapter 13~15
2011-04-20 17:20 1024What follows is a series of p ... -
Clean Code: chapter 11~12
2011-04-15 09:35 1053The startup process is a concer ... -
Clean Code: chapter 9~10
2011-04-12 11:49 997Yes, we’ve come a long way; but ... -
Clean Code: chapter 6~8
2011-04-06 17:05 8542011.04.07 Objects and D ... -
Open Stanford Course: programming methodology 04
2011-04-05 11:52 921because this is sort of pre thi ... -
Open Stanford Course: programming methodology 03
2011-04-04 13:01 951any questions to start off with ... -
Clean Code: chapter 1~5
2011-04-02 11:26 929you should try several diffe ... -
Open Stanford Course: programming methodology 02
2011-03-31 00:30 974If you are stuck in the back, j ... -
Open Yale Course: Frontiers of Biomedical Engineering 01
2011-03-30 11:21 905http://www.verycd.com/topics/28 ... -
Open Stanford Course: programming methodology 01
2011-03-25 11:00 926you can pick them up on the ... -
How Computers Work
2011-03-04 09:58 1016Yes, im at last reading this fa ... -
The Java Language Specification Third Edition
2011-01-25 10:36 845starting day: 2011.01.25 -
Tricks of the Java Programming Gurus
2011-01-24 11:22 921In the world of programming, t ... -
Thinking in Java Fourth Edition
2011-01-17 15:59 987以前这些经典书籍都零零散散地阅览过,现在准备重新精读一遍,随时 ...
相关推荐
《Java Puzzler》是Java开发者的一本独特指南,它以一种有趣且富有挑战性的方式揭示了语言中的陷阱和易犯错误。这本书的核心理念是通过一系列精心设计的谜题,帮助读者深入理解Java语言的微妙之处,从而提高编程技能...
《Javapuzzler+JAVA解惑 中英双语 完整源代码》是一份集学习与实践于一体的Java编程资源,包含《Java Puzzlers》和《JAVA解惑》两部分,以及对应的完整源代码。这份资料对于深入理解Java语言的特性和陷阱,提升编程...
不错的好东西 一个个小题目 非常关注细节
Java的优秀图书,欢迎下载。
《Java Puzzler》是一本深受Java开发者喜爱的书籍,它揭示了Java语言中的一些微妙陷阱和不明显的特性,帮助程序员避免在实际编程过程中遇到的困惑。这些“Puzzlers”通常是由看似简单但实则暗藏玄机的代码片段构成,...
Java Puzzlers是Java编程中的一系列巧妙问题,旨在揭示语言中的陷阱、误导性和意外行为。这些谜题通常由简单的代码片段构成,看似无害,但执行结果却往往出乎意料。通过解决这些Puzzlers,开发者可以增强对Java语言...
《Java解惑》是一本专为Java程序员设计的书籍,旨在揭示编程中常见的陷阱、误解和易犯的错误。这本书的中文版使得更多的中国开发者能够深入理解这些“谜题”,提高编程技能。Java Puzzlers是由Java之父James Gosling...
例如,一个经典的Java Puzzler是关于变量初始化的。在Java中,局部变量必须先初始化后才能使用,但有时候编译器可能会做出一些你意想不到的优化。如果在函数内部声明并初始化一个变量,然后在后续的代码块中再次初始...
### Java Puzzler: The Joy of Sets #### Introduction 在探讨Java编程中可能出现的各种奇怪行为时,"Java Puzzlers"系列提供了一系列短小精悍的示例代码,旨在揭示那些初看之下难以理解的行为背后的原因。这些...
This lively book reveals oddities of the Java programming language through entertaining and thought-provoking programming puzzles." --Guy Steele, Sun Fellow and coauthor of The Java(t) Language ...
Java编程语言中有许多微妙而有趣的细节,这些细节可能会在开发过程中造成困扰,这就是"Java解惑"系列试图解决的问题。本文将深入探讨PPT8中提及的三个Java谜题:Puzzle 76 乒乓、Puzzle 77 搞乱锁的妖怪和Puzzle 78 ...
每个Puzzler都是一次深入学习的机会,通过解决这些谜题,开发者不仅可以提升对Java语言的理解,还能增强代码调试和问题解决的能力。书中附带的源代码可以方便读者直接运行和测试,加深对知识点的理解。无论你是Java...
例如,puzzler之一可能涉及自动装箱和拆箱,当Integer对象与int基本类型进行操作时,如果不理解它们之间的关系,可能会出现意料之外的结果。源码中可能包含这样的例子,展示了如何正确处理这种情况,避免隐式转换...
本書包含了Java程式語言和核心函式庫中的各種謎題,任何具備使用Java經驗的讀者都可以看得懂,但是書裡有不少謎題的難度頗高,即便是對經驗豐富的Java程式設計師而言,都是一項挑戰,所以如果你解不出來,別覺得難過...
每个puzzler都会引导读者思考为什么这段代码会以某种意想不到的方式运行,然后提供详细的解答,解释背后的语言原理。这些谜题涵盖了从基本语法到高级特性的各种主题,包括但不限于类型转换、对象引用、内存管理、多...
Java编程中的迷惑问题,或者称为Puzzlers,是学习Java时常常遇到的陷阱和微妙之处。这些Puzzlers有助于深入理解Java的语法规则和运行机制。以下将详细解析给出的Puzzles。 首先,我们来看Puzzle 66:一件私事。这个...
《Java解惑PPT5》深入探讨了Java编程中的一些常见困惑和陷阱,特别是关于类、构造器重载以及静态域的使用。以下是对其中两个关键Puzzle的详细解析: **Puzzle 46:令人混淆的构造器** 在这个谜题中,作者展示了...
【Java解惑PPT1】深入探讨Java编程中的常见迷惑 在Java编程中,了解语言的细微之处至关重要,因为这可能会导致意想不到的行为。本PPT主要涵盖了五个有趣的Java谜题,帮助开发者理解Java的一些核心概念。 **谜题1:...
Java解惑是本很不错的书,讲解了Java中一些很有趣的东西
在Java编程中,有一些微妙而重要的知识点,这些知识点在实际编程中可能会引起混淆或错误。在"Java解惑PPT6"中,我们探讨了几个关键的Java特性,特别是关于不变性、equals()和hashCode()方法的约定以及它们在HashSet...