Bruce eckel philosophy java 5 edition pdf. Wikibooks Java Philosophy. About The Philosophy of Java by Bruce Eckel

Most likely I will not be mistaken in assuming that most of those who learned Java started doing this with the help of the famous book by Bruce Eckel: "Thinking in Java", known in the Russian edition as "Java Philosophy"... Unfortunately, in electronic form (in Russian), the most widespread is the 2nd edition of this book, based on the Java 1.1 version, which has long lost its relevance. The innovations that appeared in the next versions of Java (and especially in Java SE5) were quite significant, which led to a serious revision of the book in its fourth edition (the translation of which was published in Russian). However, in an easy-to-read (and most importantly, for quick search) electronic format, the Russian version of this edition did not exist. So I decided to fill this gap and produce a complete version of this popular book in a "wikibooks" format. I believe that this information will be interesting and useful not only for language learners, but also for everyone who works in Java due to the huge number of excellent examples that illustrate almost all aspects of programming in this language. Especially when it comes to rarely used Java features.

Wikibooks "Java Philosophy" posted at:

"Spring in Action"

Books from the series "..... in Action" (usually in PDF format and usually in English) are deservedly popular in certain circles :) Among them there are capacious Talmuds, such as "JSTL in Action" (easy to read and with moderate knowledge of English, but suitable for the role of a good reference on the topic), and more modest crafts, such as "Struts in Action" ("not everything is gold ..."). Book "Spring in Action" in this list, nevertheless, from the category of "heavyweights", and in every sense of the word. Reading it without knowing "fluent English" is probably not easy. And the matter is rather not in the complexity of the material presented (it is not complicated), but in the fact that it turned out - excessively "English-artistic", or something .... Full of lyrical digressions, catchphrases, wordplay and other things blah blah blah, language authors quickly makes reading this handbook (in the original language) a tedious process. But on the other hand, it lets you know that the word "draw"(usually - "draw") can be used in the meaning of "extract from" (lit. - "pull, drag"). As a result (taking into account the general style of presentation adopted in the book) to understand exact meaning phrases such as: "... Spring draw this data ..." , it happens at the same time - not easy and extremely necessary. Therefore, readers of the chapters I have not translated will have to decide on their own what the authors wanted in such cases: to express poetically about the creation (recording) of a file, or playfully tell about reading it.

This book was converted by me from PDF to wikibooks as an express reference for personal use. Therefore, the translation is not total, but only in places - for which there was enough enthusiasm. The rest of the chapters were simply put in a quick-find way. It is published, EVERYTHING in the form - "as is", and the quality of the Russian text should not be blamed ... I am not a professional translator, and I did not have a literary editor. Perhaps I will disappoint someone by the fact that I did not translate some passages and chapters of the book (and do not even plan to translate them), but I should have left it in reserve for future generations

Wikibooks "Spring in action " posted at:

Programming is one of the most demanded IT services. A lot of people began to learn about this difficult industry and work in this direction. If the reader has always been interested in programming, but he did not know where to start, then the book "The Philosophy of Java" will be an excellent starting aid in this matter. The author Bruce Eckel is a true professional in computer technology and, with the help of an understandable presentation, quickly explains the basic principles of working with program code.

The Java language is considered one of the most popular programming languages \u200b\u200baround the world. This is due to its high flexibility and the possibility of its application in almost any field related to games, software, etc. The Java Philosophy for beginners book will help you master the basic basics, as well as key concepts, which allows you to make an excellent basis for deeper and perfect study of the science of programming. Bruce Eckel has done a truly tremendous job, citing hundreds of living examples and thoroughly explaining all the key points in the course of his book. With the help of this guide, any user who wants to learn the Java language can build an excellent knowledge base and, already relying on it, start an in-depth study.

The author recommends reading his book to all beginners and people who have just started doing the simplest programming in the Java language. The Java Philosophy is a quick and quality guide to learning the basics of Java. The book pays attention to all the little things, as well as subtleties, starting with the interface and basic settings, and ending with complex combinations and techniques that are shown on a real example using commands and clear codes with the author's comments.

Bruce Eckel will help you to focus on the core purpose and philosophy of using Java for work, business and other areas. The author also shows many examples of problems, talks about the causes of their occurrence and the main methods for troubleshooting problems in the program code. Professionals working in the field of programming highly appreciated the book "Philosophy of Java", because it is an educational textbook that will help you to better learn the Java language. Although this is specialized literature, it is much easier to read than other books in a similar direction.

On our literary site site you can download the book "The Philosophy of Java" by Bruce Eckel for free in formats suitable for different devices - epub, fb2, txt, rtf. Do you love to read books and always keep an eye on new releases? We have a large selection of books of various genres: classics, modern science fiction, literature on psychology and children's publications. In addition, we offer interesting and informative articles for novice writers and all those who want to learn how to write beautifully. Each of our visitors will be able to find something useful and exciting for themselves.

Chapter 11. Object Collections

Questions
1. What is the Collection interface?

Answer

The Collection interface is the root interface that describes the common functionality of all sequential containers (p. 301).


2. Can containers be used to store primitives?

Answer

Primitives cannot act as container elements. Only references to objects can be placed in a container. However, the Autoboxing mechanism automatically converts the primitive to an object type. Therefore, the programmer can "forget" about the above limitation.
(shared with MrD)


3. Which method of adding items to the ArrayList container is preferred:
Arrays.asList or Collections.addAll and why?

Answer

Collections.addAll is preferred due to its superior performance (p. 282).


4. List several methods available when working with ArrayList.

Answer

"Contains (Object o)" - check for the presence of an element in the array.
"IndexOf (Object o)" - get the index of the element.
"SubList (index1, index2)" - copy some of the elements into a new container.
"ContainsAll (Object o)" - check for the presence of elements in the container.


5. What does the remove () method called for a container of type Queue return?

Answer

The Queue.remove () method not only removes the head element of the queue, but also returns its string value.


6. What operations are faster when working with LinkedList compared to ArrayList?

Answer

Inserting and removing items from the middle of a list is faster with a LinkedList (p. 291).


7. What types of containers can you implement with LinkedList?

Answer

Stack, queue, deque.


8. How is the Set container family different from the List container family? (thanks to MrD for the edit)

Answer

In the Set container family, data can only be stored in one instance (p. 294)


9. Will an exception be thrown when trying to add an element to the Set that is already present in it?

Answer

No exception will happen.


10. How does the PriorityQueue container differ from the Queue?

Answer

Items in a PriorityQueue can be sorted according to specific rules.


11. What is the Comparator used for when applied to a PriorityQueue?

Answer

The Comparator allows you to specify the sorting rules for PriorityQueue items.


12. What do the prefixes "Tree" and "Hash", "LinkedHash" mean, for example, for the container type Set (TreeSet, HashSet, LinkedHashSet)

Answer

The "Tree" prefix means that the elements of the container are stored in sorted order
(for example, alphabetically or ascending) The "Hash" prefix indicates that the container implements hashing to speed up sampling. The "LinkedHash" prefix means that the container stores the items in the order of insertion and provides fast access (p. 309)


13. Does the "Map" container support the Iterable interface?

14. Does the foreach syntax work for a "Map" container?

Answer

For a container of type "Map", the foreach syntax does not work because it does not support the iterable interface


15. What is the main advantage of using an iterator to access the elements of a container?

Answer

An iterator can be used for various types of containers (p. 290).


16. What new features does the ListIterator provide over the regular iterator?

Answer

ListIterator provides sequential access to elements not only from the beginning to the end of the container, but vice versa (p. 290).

Exercises

11. 1. Create a class Fruit. The class must contain an int weight field, a constructor initializing this field, and a printWeight () method that prints the weight value.
In the main method, add some Fruit objects to the List container. Call printWeight () on each of the objects in the following ways:
A) using the get () method;
B) using an iterator.

Answer

// Fruit.java public class Fruit (private int weight \u003d 0; public Fruit (int weight) (this.weight \u003d weight;) public void printWeight () (System.out.println ("Weight is:" + weight); )) // Apply.java import java.util. *; public class Apply (public static void main (String args) (List fruits \u003d Arrays.asList (new Fruit (10), new Fruit (20)); // a System.out.println ("Task a:"); for (Fruit f: fruits) (f.printWeight ();) // b System.out.println ("Task b:"); Iterator it \u003d fruits.iterator (); while (it.hasNext ()) (it.next (). printWeight ();)))

11.2. Place the Fruit class from 11.1 in a map container. Let the owner's name be specified as the key, and an object of the Fruit type as the value. Implement a key iteration over each Fruit object and call the printWeight () method on the found object: using foreach; using an iterator.

Answer

Thanks to quarantino for the compact iterator solution.
class Fruit - see task 11.2

Import java.util. *; import java.util.Map.Entry; public class Apply (public static void main (String args) (// Use HashMap, since no sorting is needed Map fruit \u003d new HashMap (); fruit.put ("Bob", new Fruit (10)); fruit.put ("Mary", new Fruit (20)); // iteration with foreach System.out.println ("With foreach"); for (String key: fruit.keySet ()) (fruit.get (key) .printWeight ();) // iterate over using an iterator System.out.println ("With iterator"); Iterator \u003e it \u003d fruit.entrySet (). iterator (); while (it.hasNext ()) (it.next (). getValue (). printWeight ();)))

11.3. Using the Comparator for the PriorityQueue, ensure that the string variables are sorted by size.

Answer

Note: the example is taken from the Internet.
// StringComparator.java import java.util.Comparator; public class StringComparator implements Comparator (@Override public int compare (String s1, String s2) (if (s1.length ()< s2.length()) { return -1; } if(s1.length() > s2.length ()) (return 1;) return 0; )) // PriorityQueue.java import java.util.Comparator; import java.util.PriorityQueue; public class PriorityQueueUse (public static void main (String args) (Comparator comparator \u003d new StringComparator (); PriorityQueue queue \u003d new PriorityQueue (10, comparator); queue.add ("abcde"); queue.add ("abc"); queue.add ("abcdefghi"); queue.add ("a"); // Items in the queue are NOT arranged in ascending order of length String System.out.println ("Before removing:"); System.out.println (queue); // But items are removed from the queue In ascending order of length String System.out.println (); System.out.println ("Look at removing order:"); while (queue.size ()! \u003d 0) (System.out.println (queue.remove ());)))

Translation of exercises from the original book:
11.4. Create a new Gerbil class with an int gerbilNumber field. Let the field be initialized with a constructor. Also create a hop () method that prints "Gerbil's number which is hopping is:" and the value of the gerbilNumber variable. Place the Gerbil objects in the ArrayList container. Implement a get () method to iterate through the list, calling the hop () method on each of these objects.

11.5. Modify SimpleCollection.java to use the Set method for the "c" variable. Modify innerclasses / Sequence.java so that any number of elements can be added there.

11.6. Write a Generator class that lists the names of your favorite movie characters (String objects). Let each object of this list be returned via the next () method. When you reach the end of the created list, you need to go back to the beginning of the list.
Use the Generator class to populate containers like ArrayList, LinkedList, HashSet, LinkedHashSet, TreeSet. Write a generic method that outputs the contents of each of the arrays.

11.7. Modify ListFeatures.java to use Integers instead of Pets. Explain the differences in performance resulting from this modification.

11.8. Repeat Exercise 11.7 for ListFeatures.java, but replace Pets with Strings.

11.9. Create a class that generates an initialized array of objects of another class. Use this array to fill the List container. Create another container using subList () for List. Then remove the items that you fetched with subList () from the container you fetched them from.

11.10. A playful exercise in using different containers and their methods (invented by myself)
Suppose we have a list of expenses, initially consisting of 3 elements: "food", "medicine", "entertainment". You need to write these elements to the List container, and their order should not change.

1) We suddenly realized that we had forgotten to add loan repayment expenses to the list. You need to place the “taxes” record between “food” and “medicine” without using the indexes of the records directly.
2) The wife brought in an additional separate list of expenses with the entries: "coat", "restaurant". There is nothing to do: add expenses to the end of the list (so as not to flatter yourself).
3) However, the mother-in-law brought us a couple more items: “theater”, “home decoration” and “medicine”. Maybe you already had some of these points. Just in case, copy the contents of the first container into a new container of the Set type and then, without hesitation, add these items to the end of the list.
4) Without hesitation, we created an associative dynamic list of expenses map , where Integer is the planned costs for each of the cost items (choose the amounts yourself).
5) We got rich dramatically decided to increase the sum of all expenses by 30%. Write a method to do this for you.
6) Stop! Enough with us "home decoration!" And, perhaps, too much is spent on coat. Add these items to a new List and pass it to the method, which, having read this sheet, will find the corresponding expense items in the Map container and reduce them by ... 80%!
7) Let's dump some of the expenses for the mother-in-law. Moreover, let's make her a surprise! Let's generate two random numbers that will determine the starting and ending indices, all expenses between which will be recorded in a separate List (which will later be slipped by the mother-in-law). Let's not forget to update the Map by removing the corresponding expense items from it.
8) Let's write a method that determines what the smallest amount of expenses is spent on, print the name of these expenses and proudly remove this item from the list.
9) However, additional consumables were piled up to us: "food" and "restaurant", decorated in the form of another array! Let's check if these items are on our list? And if there is at least one expense, then in no case will we add them! If there is none of this, then (there is nothing to be done), we add both of these items at once (how hungry I am!).
10) We're crazy! Let's read all the Integer sums of our remaining expenses from the Map list, write them into a "some" container that will sort them for us in ascending order, then mix them in a rage and delete them with a pass in the reverse order, furiously using a special iterator for this.

The Java Philosophy Bruce Eckel

(No ratings yet)

Title: Java Philosophy

About The Philosophy of Java by Bruce Eckel

Bruce Eckel is an outstanding programmer, author of several books on the programming language. His work quickly became popular as he expounded complex concepts in simple language. They have helped many learn programming from scratch and succeed in this area. The author has received awards for his work more than once. His books have always been warmly received by critics, and those who wanted to learn more deeply different programming languages \u200b\u200bwere looking forward to his new work. The most famous book by the author is The Philosophy of Java. It sold out in huge numbers and was reprinted several times. Even now, over a decade later, this work is a must-read for anyone who wants to become a professional programmer.

In his book "Java Philosophy" the author reveals the secrets of this programming language. He advises to look at it as something alive. He skillfully gives examples that prove that the programming language is evolving, various additions are made to it. It can be used to describe any process.

Most novice programmers cannot understand Java logic. The author tries to intelligibly explain to readers what it is. Once they understand this, it will be much easier to start writing programs that won't crash and confuse specialists. Thanks to the author's experience, it will be easier to independently identify and fix errors that are often made by programmers. A deeper study and understanding of the Java programming language will open up perspectives that previously seemed impossible.

Bruce Eckel, in The Philosophy of Java, tried to explain difficult things in simple terms. To prevent readers from getting confused in terms, he connects them with nature, everyday situations. Thus, useful information is memorized faster and stays with a person forever. When difficult situations arise, he recalls the story from the book and uses it.

Bruce Eckel in his book "Java Philosophy" tried to convey all his experience, so he put everything on the shelves. Readers can only absorb it and use it in their daily work. Thanks to the book, you will be able to avoid most of the common mistakes and become an excellent programmer. The author's work has helped many people understand how Java works, write great programs, and improve software.

On our website about books, you can download the site for free without registration or read the online book "The Philosophy of Java" by Bruce Eckel in epub, fb2, txt, rtf, pdf formats for iPad, iPhone, Android and Kindle. The book will give you a lot of pleasant moments and real pleasure from reading. You can buy the full version from our partner. Also, here you will find the latest news from the literary world, find out the biography of your favorite authors. For novice writers, there is a separate section with useful tips and tricks, interesting articles, thanks to which you yourself can try your hand at literary skills.


To change the default document, edit the "blank.fb2" file manually.

Foreword 13

Java SE5 and SE6 14

Acknowledgments 14

Chapter 1. Introduction to Objects 17

Development of abstraction 18

Object has interface 20

Object provides services 22

Hidden realization 23

Reuse implementation 24

Inheritance 25

Interchangeable Objects and Polymorphism 29

Single root hierarchy 33

Containers 33

Parameterized Types 35

Creation, use of objects and their lifetime 36

Exception Handling: Dealing with Errors 38

Parallel execution 38

Java and the Internet 39

Chapter 2. Everything is an Object 48

All objects must be created explicitly 49

Objects never have to be deleted 53

Creating New Data Types 54

Methods, Arguments, and Return Values \u200b\u200b56

Writing a Java Program 58

Static 60 keyword

Our First Java Program 61

Comments and Inline Documentation 64

Style of programming 70

Chapter 3. Operators 71

Simple Print Commands 71

Java Operators 72

Literals 82

Java is missing sizeof () 92

Summary 100

Chapter 4. Control Structures 101

Foreach syntax 105

break and continue 108

Bad Team goto 109

Summary 115

Chapter 5. Initialization and Completion 116

Constructor guarantees initialization 116

Method Overloading 118

Cleanup: Finalization and Garbage Collection 130

Initialization of Class Members 137

Constructor Initialization 140

Initializing Arrays 146

Summary 151

Chapter 6. Access Control 152

Package as a Library Unit 153

Java Access Specifiers 159

Interface and Implementation 163

Access to classes 164

Summary 167

Chapter 7. Reusing Classes 169

Composition syntax 170

Inheritance Syntax 172

Delegation 176

Combining composition and inheritance 178

Composition Versus Inheritance 184

Upward Type Conversion 186

Final 188 keyword

Summary 197

Chapter 8. Polymorphism 198

Back to top conversion. ... ... \u003e 199

Features 201

Constructors and Polymorphism 208

Return Covariance 216

Developing with Inheritance 217

Summary 220

Chapter 9. Interfaces 221

Abstract Classes and Methods 221

Interfaces 224

Decoupling Interface from Implementation 227

Extending an Interface Through Inheritance 233

Interfaces as a means of adaptation 236

Nested interfaces 239

Interfaces and Factories 242

Summary 244

Chapter 10. Inner Classes 245

Creating Inner Classes 245

Communication with external class 246

Constructions.this and.new 248

Inner Classes and Upstream Conversion 249

Unnamed Inner Classes 253

Inner classes: why? 261

Inheriting From Inner Classes 272

Is it possible to override the inner class? 272

Local Inner Classes 274

Summary 276

Chapter 11. Object Collections 277

Parameterized and Typed Containers 277

Basic concepts 280

Adding Groups of Elements 281

Iterators 288

Set 294

Queue 298

PriorityQueue 299

Collection and Iterator 301

Method Adapter Idiom 306

Summary 309

Chapter 12. Error Handling and Exceptions 310

Major exceptions 310

Catching Exceptions 312

Creating Your Own Exceptions 314

Exception Specifications 319

Catching Arbitrary Exceptions 320

Java Standard Exceptions 328

Completion with finally 330

Using finally with return 334

Limitations on Using Exceptions 336

Constructors 339

Identifying Exceptions 343

Alternative solutions 344

Summary 351

Chapter 13. Type Information 352

The Need for Dynamic Type Inference (RTTI) 352

Registration of factories 372

Reflection: Dynamic Class Information 376

Dynamic intermediaries 380

Undefined Objects 384

Interfaces and Type Information 390

CV 394

Chapter 14. Parametrization 397

Easy parameterization 398

Parameterized 404 interfaces

Parameterized Methods 407

Building Complex Models 419

Restrictions 437

Metacharacters 440

CV 452

Chapter 15. Arrays 454

Features of 454 Arrays

Array as Object 456

Returning an array 458

Multidimensional Arrays 460

Arrays and Parameterisation 463

Creating Test Data 465

Creating Arrays Using Generators 470

Arrays 474 Helper Toolkit

Resume 482

Chapter 16. Java 483 I / O System

File 484 class

Input and output 489

Adding Attributes and Interfaces 491

The Reader and Writer Classes 494

RandomAccessFile: by itself 497

Typical Usage of I / O Streams 498

File Readers and Writers 505

Standard I / O 507

New I / O (nio) 510

Data Compression 531

Serializing Objects 536

Preferences 553

CV 555

Chapter 17. Parallel Execution 557

Thread class 559

Artists 561

Resource Sharing 578

Interaction between threads 598

Interlock 602

New library components 607

CountDownLatch 607

CyclicBarrier 609

PriorityBlockingQueue 614

Semaphores 619

Modeling 624

CV 629

Index 631

Introduction to objects

We dissect nature, transform it into concepts and ascribe meaning to them the way we do in many ways, because we are all parties to an agreement that is valid in a society bound by speech, and which is fixed in the structure of language ... We cannot communicate at all, except by agreeing with the organization and data classification established by this agreement.

Benjamin Lee Worf (1897-1941)

We owe the computer revolution to the machine. Therefore, our programming languages \u200b\u200btry to be closer to this machine.

But at the same time, computers are not so much mechanisms as a means of amplifying thoughts ("bicycles for the mind", as Steve Jobe likes to say), and another means of self-expression. As a result, programming tools tend to lean less towards machines and more towards our minds, as well as other forms of expression of human aspirations, such as literature, painting, sculpture, animation and cinema. Object Oriented Programming (OOP) is part of making the computer a vehicle for self-expression.

This chapter will introduce you to the basics of OOP, including an overview of the basic methods of program development. It, and the book in general, implies that you have experience in programming in a procedural language, not necessarily C. If it seems to you that before reading this book you do not have enough knowledge of programming and syntax of C, use the Thinking in C multimedia seminar. which can be downloaded from the site