没有合适的资源?快使用搜索试试~ 我知道了~
首页Core Java SE 6教程:实战指南与经典示例
"《Core Java, Volume I--Fundamentals》第八版是针对经验丰富的程序员量身打造的经典之作,专注于Java SE 6平台下的核心技术与库功能讲解。这本书以其实用且经过严格测试的实例来引导读者,旨在帮助他们将Java应用于实际项目中。作者通过精心设计的示例程序,确保了内容易于理解,同时具有很高的实用性,为开发者提供了一个优秀的代码起点。 该书覆盖了Java语言的基础知识,包括语法、类和对象、异常处理、集合框架、输入/输出流、多线程编程等核心概念。每个主题都围绕着真实世界的场景展开,使读者在学习过程中能够迅速掌握Java的核心技术,并能将其应用到日常开发工作中。 对于那些希望深入理解Java并提升技能的专业人士来说,这本书不仅是学习指南,也是可靠的参考手册。书中还强调了版权和商标的重要性,提到了诸如Sun Microsystems、Java、J2ME、Solaris、NetBeans等知名品牌的商标权益,以及对UNIX等其他注册商标的尊重。 《Core Java, Volume I--Fundamentals》第八版的全球发行版涵盖了各大主要城市,如Upper Saddle River、Boston、Indianapolis等,以及国际上的多个地区,体现了其广泛影响力和专业认可度。无论是初学者还是经验丰富的开发者,阅读这本书都能从中受益匪浅,提升Java编程技能,更好地应对现代软件开发挑战。"
资源详情
资源推荐
Chapter 1
■
An Introduction to Java
2
T
he first release of Java in 1996 generated an incredible amount of excitement, not
just in the computer press, but in mainstream media such as The New York Times, The
Washington Post, and Business Week. Java has the distinction of being the first and only
programming language that had a ten-minute story on National Public Radio. A
$100,000,000 venture capital fund was set up solely for products produced by use of a
specific computer language. It is rather amusing to revisit those heady times, and we
give you a brief history of Java in this chapter.
Java As a Programming Platform
In the first edition of this book, we had this to write about Java:
“As a computer language, Java’s hype is overdone: Java is certainly a good program-
ming language. There is no doubt that it is one of the better languages available to
serious programmers. We think it could potentially have been a great programming
language, but it is probably too late for that. Once a language is out in the field, the ugly
reality of compatibility with existing code sets in.”
Our editor got a lot of flack for this paragraph from someone very high up at Sun Micro-
systems who shall remain unnamed. But, in hindsight, our prognosis seems accurate.
Java has a lot of nice language features—we examine them in detail later in this chapter.
It has its share of warts, and newer additions to the language are not as elegant as the
original ones because of the ugly reality of compatibility.
But, as we already said in the first edition, Java was never just a language. There are lots
of programming languages out there, and few of them make much of a splash. Java is a
whole platform, with a huge library, containing lots of reusable code, and an execution
environment that provides services such as security, portability across operating sys-
tems, and automatic garbage collection.
As a programmer, you will want a language with a pleasant syntax and comprehensible
semantics (i.e., not C++). Java fits the bill, as do dozens of other fine languages. Some
languages give you portability, garbage collection, and the like, but they don’t have
much of a library, forcing you to roll your own if you want fancy graphics or network-
ing or database access. Well, Java has everything—a good language, a high-quality exe-
cution environment, and a vast library. That combination is what makes Java an
irresistible proposition to so many programmers.
The Java “White Paper” Buzzwords
The authors of Java have written an influential White Paper that explains their design
goals and accomplishments. They also published a shorter summary that is organized
along the following 11 buzzwords:
Simple Portable
Object Oriented Interpreted
Network-Savvy High Performance
Robust Multithreaded
Secure Dynamic
Architecture Neutral
Chapter 1. An Introduction to Java
The Java “White Paper” Buzzwords
3
In this section, we will
• Summarize, with excerpts from the White Paper, what the Java designers say about
each buzzword; and
• Tell you what we think of each buzzword, based on our experiences with the cur-
rent version of Java.
NOTE: As we write this, the White Paper can be found at http://java.sun.com/docs/white/
langenv/. The summary with the 11 buzzwords is at http://java.sun.com/docs/overviews/java/
java-overview-1.html.
Simple
We wanted to build a system that could be programmed easily without a lot of eso-
teric training and which leveraged today’s standard practice. So even though we
found that C++ was unsuitable, we designed Java as closely to C++ as possible in
order to make the system more comprehensible. Java omits many rarely used, poorly
understood, confusing features of C++ that, in our experience, bring more grief
than benefit.
The syntax for Java is, indeed, a cleaned-up version of the syntax for C++. There is no
need for header files, pointer arithmetic (or even a pointer syntax), structures, unions,
operator overloading, virtual base classes, and so on. (See the C++ notes interspersed
throughout the text for more on the differences between Java and C++.) The designers
did not, however, attempt to fix all of the clumsy features of C++. For example, the syn-
tax of the
switch
statement is unchanged in Java. If you know C++, you will find the tran-
sition to the Java syntax easy.
If you are used to a visual programming environment (such as Visual Basic), you will
not find Java simple. There is much strange syntax (though it does not take long to get
the hang of it). More important, you must do a lot more programming in Java. The
beauty of Visual Basic is that its visual design environment almost automatically pro-
vides a lot of the infrastructure for an application. The equivalent functionality must be
programmed manually, usually with a fair bit of code, in Java. There are, however,
third-party development environments that provide “drag-and-drop”-style program
development.
Another aspect of being simple is being small. One of the goals of Java is to enable
the construction of software that can run stand-alone in small machines. The size of
the basic interpreter and class support is about 40K bytes; adding the basic stan-
dard libraries and thread support (essentially a self-contained microkernel) adds an
additional 175K.
This was a great achievement at the time. Of course, the library has since grown to huge
proportions. There is now a separate Java Micro Edition with a smaller library, suitable
for embedded devices.
Object Oriented
Simply stated, object-oriented design is a technique for programming that focuses
on the data (= objects) and on the interfaces to that object. To make an analogy with
carpentry, an “object-oriented” carpenter would be mostly concerned with the chair
Chapter 1. An Introduction to Java
Chapter 1
■
An Introduction to Java
4
he was building, and secondarily with the tools used to make it; a “non-object-
oriented” carpenter would think primarily of his tools. The object-oriented facilities
of Java are essentially those of C++.
Object orientation has proven its worth in the last 30 years, and it is inconceivable that a
modern programming language would not use it. Indeed, the object-oriented features of
Java are comparable to those of C++. The major difference between Java and C++ lies in
multiple inheritance, which Java has replaced with the simpler concept of interfaces,
and in the Java metaclass model (which we discuss in Chapter 5).
NOTE: If you have no experience with object-oriented programming languages, you will
want to carefully read Chapters 4 through 6. These chapters explain what object-oriented
programming is and why it is more useful for programming sophisticated projects than are
traditional, procedure-oriented languages like C or Basic.
Network-Savvy
Java has an extensive library of routines for coping with TCP/IP protocols like
HTTP and FTP. Java applications can open and access objects across the Net via
URLs with the same ease as when accessing a local file system.
We have found the networking capabilities of Java to be both strong and easy to use.
Anyone who has tried to do Internet programming using another language will revel in
how simple Java makes onerous tasks like opening a socket connection. (We cover net-
working in Volume II of this book.) The remote method invocation mechanism enables
communication between distributed objects (also covered in Volume II).
Robust
Java is intended for writing programs that must be reliable in a variety of ways.
Java puts a lot of emphasis on early checking for possible problems, later dynamic
(runtime) checking, and eliminating situations that are error-prone. . . . The single
biggest difference between Java and C/C++ is that Java has a pointer model that elim-
inates the possibility of overwriting memory and corrupting data.
This feature is also very useful. The Java compiler detects many problems that, in other
languages, would show up only at runtime. As for the second point, anyone who has
spent hours chasing memory corruption caused by a pointer bug will be very happy
with this feature of Java.
If you are coming from a language like Visual Basic that doesn’t explicitly use pointers,
you are probably wondering why this is so important. C programmers are not so lucky.
They need pointers to access strings, arrays, objects, and even files. In Visual Basic, you
do not use pointers for any of these entities, nor do you need to worry about memory
allocation for them. On the other hand, many data structures are difficult to implement
in a pointerless language. Java gives you the best of both worlds. You do not need point-
ers for everyday constructs like strings and arrays. You have the power of pointers if
you need it, for example, for linked lists. And you always have complete safety, because
you can never access a bad pointer, make memory allocation errors, or have to protect
against memory leaking away.
Chapter 1. An Introduction to Java
The Java “White Paper” Buzzwords
5
Secure
Java is intended to be used in networked/distributed environments. Toward that
end, a lot of emphasis has been placed on security. Java enables the construction of
virus-free, tamper-free systems.
In the first edition of Core Java we said: “Well, one should ‘never say never again,’” and
we turned out to be right. Not long after the first version of the Java Development Kit
was shipped, a group of security experts at Princeton University found subtle bugs in
the security features of Java 1.0. Sun Microsystems has encouraged research into Java
security, making publicly available the specification and implementation of the virtual
machine and the security libraries. They have fixed all known security bugs quickly. In
any case, Java makes it extremely difficult to outwit its security mechanisms. The bugs
found so far have been very technical and few in number.
From the beginning, Java was designed to make certain kinds of attacks impossible,
among them:
• Overrunning the runtime stack—a common attack of worms and viruses
• Corrupting memory outside its own process space
• Reading or writing files without permission
A number of security features have been added to Java over time. Since version 1.1, Java
has the notion of digitally signed classes (see Volume II). With a signed class, you can be
sure who wrote it. Any time you trust the author of the class, the class can be allowed
more privileges on your machine.
NOTE: A competing code delivery mechanism from Microsoft based on its ActiveX technol-
ogy relies on digital signatures alone for security. Clearly this is not sufficient—as any user
of Microsoft’s own products can confirm, programs from well-known vendors do crash and
create damage. Java has a far stronger security model than that of ActiveX because it con-
trols the application as it runs and stops it from wreaking havoc.
Architecture Neutral
The compiler generates an architecture-neutral object file format—the compiled
code is executable on many processors, given the presence of the Java runtime sys-
tem. The Java compiler does this by generating bytecode instructions which have
nothing to do with a particular computer architecture. Rather, they are designed to
be both easy to interpret on any machine and easily translated into native machine
code on the fly.
This is not a new idea. More than 30 years ago, both Niklaus Wirth’s original implemen-
tation of Pascal and the UCSD Pascal system used the same technique.
Of course, interpreting bytecodes is necessarily slower than running machine instruc-
tions at full speed, so it isn’t clear that this is even a good idea. However, virtual
machines have the option of translating the most frequently executed bytecode
sequences into machine code, a process called just-in-time compilation. This strategy
has proven so effective that even Microsoft’s .NET platform relies on a virtual machine.
Chapter 1. An Introduction to Java
Chapter 1
■
An Introduction to Java
6
The virtual machine has other advantages. It increases security because the virtual
machine can check the behavior of instruction sequences. Some programs even produce
bytecodes on the fly, dynamically enhancing the capabilities of a running program.
Portable
Unlike C and C++, there are no “implementation-dependent” aspects of the specifi-
cation. The sizes of the primitive data types are specified, as is the behavior of arith-
metic on them.
For example, an
int
in Java is always a 32-bit integer. In C/C++,
int
can mean a 16-bit
integer, a 32-bit integer, or any other size that the compiler vendor likes. The only
restriction is that the
int
type must have at least as many bytes as a
short int
and cannot
have more bytes than a
long int
. Having a fixed size for number types eliminates a major
porting headache. Binary data is stored and transmitted in a fixed format, eliminating
confusion about byte ordering. Strings are saved in a standard Unicode format.
The libraries that are a part of the system define portable interfaces. For example,
there is an abstract Window class and implementations of it for UNIX, Windows,
and the Macintosh.
As anyone who has ever tried knows, it is an effort of heroic proportions to write a pro-
gram that looks good on Windows, the Macintosh, and ten flavors of UNIX. Java 1.0
made the heroic effort, delivering a simple toolkit that mapped common user interface
elements to a number of platforms. Unfortunately, the result was a library that, with a
lot of work, could give barely acceptable results on different systems. (And there were
often different bugs on the different platform graphics implementations.) But it was a
start. There are many applications in which portability is more important than user
interface slickness, and these applications did benefit from early versions of Java. By
now, the user interface toolkit has been completely rewritten so that it no longer relies
on the host user interface. The result is far more consistent and, we think, more attrac-
tive than in earlier versions of Java.
Interpreted
The Java interpreter can execute Java bytecodes directly on any machine to which
the interpreter has been ported. Since linking is a more incremental and lightweight
process, the development process can be much more rapid and exploratory.
Incremental linking has advantages, but its benefit for the development process is
clearly overstated. Early Java development tools were, in fact, quite slow. Today, the
bytecodes are translated into machine code by the just-in-time compiler.
High Performance
While the performance of interpreted bytecodes is usually more than adequate,
there are situations where higher performance is required. The bytecodes can be
translated on the fly (at runtime) into machine code for the particular CPU the
application is running on.
In the early years of Java, many users disagreed with the statement that the perfor-
mance was “more than adequate.” Today, however, the just-in-time compilers have
become so good that they are competitive with traditional compilers and, in some cases,
even outperform them because they have more information available. For example, a
just-in-time compiler can monitor which code is executed frequently and optimize just
Chapter 1. An Introduction to Java
剩余824页未读,继续阅读
IUCSDN
- 粉丝: 1
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Hadoop生态系统与MapReduce详解
- MDS系列三相整流桥模块技术规格与特性
- MFC编程:指针与句柄获取全面解析
- LM06:多模4G高速数据模块,支持GSM至TD-LTE
- 使用Gradle与Nexus构建私有仓库
- JAVA编程规范指南:命名规则与文件样式
- EMC VNX5500 存储系统日常维护指南
- 大数据驱动的互联网用户体验深度管理策略
- 改进型Booth算法:32位浮点阵列乘法器的高速设计与算法比较
- H3CNE网络认证重点知识整理
- Linux环境下MongoDB的详细安装教程
- 压缩文法的等价变换与多余规则删除
- BRMS入门指南:JBOSS安装与基础操作详解
- Win7环境下Android开发环境配置全攻略
- SHT10 C语言程序与LCD1602显示实例及精度校准
- 反垃圾邮件技术:现状与前景
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功