
Beginning Java Programming
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
More details
Other editions
Additional editions


Persons
Content
Introduction xxii
Chapter 1: A General Introduction To Programming 1
The Programming Process 2
Object-Oriented Programming: A Sneak Preview 5
Programming Errors 6
Syntax/Compilation Errors 6
Runtime Errors 6
Logic/Semantic Errors 7
Principles of Software Testing 7
Software Maintenance 8
Adaptive Maintenance 8
Perfective Maintenance 8
Corrective Maintenance 8
Preventive Maintenance 9
Principles of Structured Programming 9
Chapter 2: Getting To Know Java 11
A Short Java History 12
Features of Java 13
Looking Under the Hood 13
Bytecode 14
Java Runtime Environment (JRE) 15
Java Application Programming Interface (API) 16
Class Loader 17
Bytecode Verifier 18
Java Virtual Machine (JVM) 18
Java Platforms 19
Java Applications 19
Standalone Applications 19
Java Applets 20
Java Servlets 20
Java Beans 21
Java Language Structure 21
Classes 22
Identifiers 22
Java Keywords 22
Variables 23
Methods 23
Comments 24
Naming Conventions 26
Java Data Types 27
Primitive Data Types 27
Literals 28
Operators 29
Arithmetic Operators 29
Assignment Operators 30
Bitwise Operators 31
Logical Operators 32
Relational Operators 34
Arrays 34
Type Casting 37
Summary 40
Chapter 3: Setting Up Your Development Environment 41
Integrated Development Environments 42
Coding in Text Editors 42
Choosing an IDE 46
Eclipse 47
NetBeans 47
IntelliJ IDEA 47
Continuing with One IDE 47
Installing Eclipse on Your Computer 48
Downloading and Installing Eclipse 48
Using Eclipse 50
Chapter 4: Moving Toward Object-Oriented Programming 61
Basic Concepts of Object-Oriented Programming 62
Classes and Objects in Java 63
Defining Classes in Java 63
Creating Objects 71
Storing Data: Variables 76
Instance Variables 76
Class Variables 80
Final Variables 82
Variable Scope 87
Defining Behavior: Methods 91
Instance Methods 91
Class Methods 94
Constructors 95
The Main Method 100
Method Argument Passing 109
Java SE Built-in Classes 115
Classes in the java.lang Package 115
Classes in the java.io and java.nio Packages 117
Classes in the java.math Package 118
Classes in the java.net, java.rmi, javax.rmi, and org.omg.CORBA Packages 118
Classes in the java.awt and javax.swing Packages 118
Classes in the java.util Package 118
Collections 119
Other Utility Classes 126
Other Classes and Custom Libraries 127
Chapter 5: Controlling the Flow of Your Program 129
Comparisons Using Operators and Methods 130
Comparing Primitive Data Types with Comparison Operators 130
Comparing Composite Data Types with Comparison Methods 132
Understanding Language Control 135
Creating if-then Statements 135
Nesting if-then Statements 137
Creating for Loops 138
What is an Enhanced for Loop? 143
Nesting for Loops 146
Creating while Loops 148
What is a do while Loop? 152
Comparing for and while Loops 156
Creating Switches 156
Comparing Switches and if-then Statements 161
Reviewing Keywords for Control 162
Controlling with the return Keyword 162
Controlling with the break Keyword 163
Controlling with the continue Keyword 164
Specifying a Label for break or continue Control 164
Reviewing Control Structures 168
Chapter 6: Handling Exceptions and Debugging 171
Recognizing Error Types 172
Identifying Syntax Errors 172
Identifying Runtime Errors 175
Identifying Logical Errors 176
Exceptions 180
Common Exceptions 181
Catching Exceptions 187
Debugging Your Applications 195
Using a Debugger Tool 195
Using a Logging API 200
Testing Your Applications 210
Summary 219
Chapter 7: Delving Further Into Object-Oriented Concepts 221
Annotations 222
Overloading Methods 222
The this KeyWord 224
Information Hiding 229
Access Modifiers 230
Getters 231
Setters 232
Class Inheritance 240
The Keyword super 241
Method Overriding 243
Polymorphism 243
Static Binding 244
Dynamic Binding 244
The Superclass Object 245
Abstract Classes and Methods 246
Packages 251
Interfaces 252
Garbage Collection 259
Chapter 8: Handling Input and Output 261
General Input and Output 262
Input and Output in Java 266
Streams 268
Byte Streams 269
Character Streams 275
Buffered Streams 276
Data and Object Streams 278
Other Streams 281
Scanners 281
Input and Output from the Command-Line 283
Input and Output from Files 290
Java NIO2 File Input and Output 291
The Path Interface 291
The Files Class 293
Checking Existence 293
Legacy File Input and Output 304
A Word on FileUtils 305
Conclusion 305
Chapter 9: Working With Databases in Java 307
Covering the Basics of Relational Databases 308
Accessing Relational Databases from Java 315
Java Database Connectivity (JDBC) 315
SQLJ 321
Ensuring Object Persistence 324
Hibernate 325
Object-Oriented Database Access from Java 341
Comparing Java Database Access Technologies 343
What's Ahead 344
Chapter 10: Accessing Web Sources 347
A Brief Introduction to Networking 348
Web Services 360
RPC and RMI 360
SOAP 364
REST 366
Accessing Web Services and Sources with Java 368
Accessing SOAP Services 368
Installing JAX-WS 368
Accessing SOAP Services with JAX-WS Without WSDL 369
Accessing SOAP Services with JAX-WS with WSDL 395
Accessing REST Services 406
Accessing REST Services Without Authentication 408
Accessing REST Services with Authentication 421
Screen Scraping 449
Screen Scraping Without Cookies 451
Screen Scraping with Cookies 453
Creating Your Own Web Services with Java 457
Setting Up an HTTP Server 457
Providing REST Services 461
Chapter 11: Designing Graphical Interfaces 463
Covering the Basics of GUIs in Java 464
Highlighting the Built-In GUI Libraries 464
Abstract Window Toolkit (AWT) 464
Swing 464
Standard Widget Toolkit (SWT) 465
JavaFX 465
Other Toolkits and Libraries 466
Choosing a GUI Library 466
Building with Containers and Components 467
Looking at the Full Picture 472
Comparing Layout Managers 473
FlowLayout 474
BorderLayout 476
GridLayout 478
GridBagLayout 482
CardLayout 486
BoxLayout 489
GroupLayout and SpringLayout 493
Absolute Positioning (No Layout Manager) 494
Understanding Events 496
Introduction to Events 496
Event Listeners 497
On Threading and Swing 514
Closing Topics 524
Best Practices: Keeping Looks and Logic Separated 524
Let's Draw: Defining Custom Draw Behavior 525
Visual GUI Designers: Making Life Easy? 540
JavaFX: The Road Ahead? 545
Chapter 12: Using Object-Oriented Patterns 557
Introduction to Patterns 558
Object-Oriented Patterns 558
Creational Patterns 559
Singleton Pattern and Static Utility Class 559
Service Provider Pattern and Null Object Pattern 565
(Abstract) Factory Pattern 566
Structural Patterns 568
Adapter Pattern 568
Bridge Pattern 570
Decorator Pattern 571
Facade Pattern 574
Composite Pattern 575
Type Pattern and Role Pattern 583
Behavioral Patterns 591
Chain-of-Responsibility Pattern 591
Observer Pattern and Model-View-Controller Pattern 592
Iterator Pattern 605
Visitor Pattern 607
Template Method Pattern 610
Strategy Pattern 612
Helpful Libraries 614
Apache Commons 614
Google Guava 615
Trove 615
Colt 615
Lombok 616
OpenCSV 616
HTML and JSON Libraries 616
Hibernate and Other JPA-Compliant Libraries 617
Joda-Time 617
Charting Libraries 617
3D Graphics Libraries 617
Financial Libraries 618
Index 619
INTRODUCTION
Congratulations! By picking up this book you have made the first step in your voyage toward learning Java. Java is a programming language with a long history, starting with its inception in 1991, when it was still named "Oak," through the first public release (Java 1.0) in 1995 and the newly released Java 8. Its "write once, run anywhere" approach, together with robust language features and numerous libraries led to a spectacular adoption rate. Java is one of the most popular languages in use today, and has been especially successful in enterprise and business environments.
Note, however, that Java is not without its criticism (no programming language is). You might have picked upon the often-repeated criticism that Java is verbose, unsecure, suffering from a slow release cycle, and that it is fading in popularity compared to the interest in new, more exciting languages (Ruby, Erlang, and Haskell, to name a few) by the computer science and programmer communities. The reality, however, tells a different story. Java remains widely taught in schools and universities and is regarded as the language of choice in many organizations. The introduction of Java 7 in 2011 made many tasks simpler, and the availability of many seasoned and stable libraries, tools, and feature-complete IDEs is unmatched by the ecosystem found around other languages. In 2014, Java 8 introduced lambda expressions to streamline code and a reengineered date and time interface that simplifies and improves the internationalization of applications. Java's strong architectural foundations make the language ideally suited for both newcomers and experienced programmers who want to expand their knowledge of Object-Oriented Programming.
Before reaching the end of this book, you will agree that being proficient in Java is a strong skill to possess indeed. We aim to get you started and up to speed as quickly as possible, without making sacrifices in terms of depth and breadth of topics. The goal is not to guide you in simply adding yet another (or first) language to your repertoire, but also to familiarize you with Java's underlying approach toward robust and structured Object-Oriented Programming. As you will see, Java's "verbosity" makes it ideally suited to teach programming best practices in an explicit manner. Providing step-by-step explanations together with many examples-inspired by real-life environments rather than toy exercises-will help you quickly appreciate Java's design and usefulness, and learn that programming in Java can even be great fun!
WHO THIS BOOK IS FOR
Java is a great language to learn for both new and experienced programmers. As such, this book is geared towards a broad audience, including practitioners, analysts, programmers, and students wanting to apply Java in a pragmatic context. It doesn't matter whether you are new to programming and have chosen Java as a place to start or whether you come in from another programming language; Java is a great choice. Many books exist on the topic of Java (just look at the shelf where you found this book!), but we feel that existing offerings reuse the same approach to demonstrate concepts. Therefore, you will not find the archetypical (and honestly, completely useless) "Hello World" example in this book. Instead, we delve into concrete, thought-out examples that illustrate how Java can be useful and used in real life. Whether you are an analyst struggling with spreadsheet formulas to perform a somewhat advanced calculation (there has to be a smarter way, right?), a student wondering how your future employer is using Java, or a hobbyist programmer trying to keep track of stock quotes or a book database, this book aims to familiarize you with all the necessary concepts.
TOPICS COVERED IN THIS BOOK
The topics discussed in this book can, broadly speaking, be outlined in the following three categories. First is a general introduction to programming and Java. The first chapters briefly discuss programming in general, before moving on to a high-level description of Java's history and language features. We also make sure to set up everything you need to get started with Java.
The second part deals with Object-Oriented Programming in Java. The goal is to help readers acquire a strong knowledge of how Object-Oriented Programming works and how Java programs are structured.
The third part is more focused and practical, and shows how you can leverage Java to talk to data sources (such as files, databases, and even web services) and how you can create a graphical user interface around your program logic.
Note that we have not structured the book around the aforementioned three parts. Instead of splitting the book into "theoretical" and "practical" parts, we chose to introduce new concepts step-by-step as they are needed so that you can quickly move on to examples and exercises. The best way to learn is by doing, and this saying particularly holds true when learning to program.
In Chapter 1, we provide a brief general introduction to programming geared toward newcomers and novice programmers. In Chapter 2, we start introducing Java by providing an overview of the language's history, the different technological components that make up the language, the general language structure, and data types. At that point, you will know enough to get started, so that in Chapter 3, we will guide you toward setting up your development environment and trying out some basic examples and exercises. In Chapter 4, we introduce Object-Oriented Programming basics; flow-control statements are covered in Chapter 5. At that point, you will be able to create simple but functional programs. Chapter 6 explains how to catch errors and debug your programs, which will come in handy as you start using resources and coding more advanced programs. At this point, you will have all the necessary components to start looking at some more advanced Object-Oriented Programming concepts in Chapter 7. After this, you'll be ready to tackle more complex interactions with files, external sources, and users. In Chapter 8 you will look at dealing with file-based input and output, including how to load files, perform basic operations, and save the results back to disk. Chapters 9 and 10 build on this by explaining how to interact with databases and web sources. At this point, you will be itching to move away from command-line based applications, and Chapter 11 explains in-depth how to build graphical user interfaces. Chapter 12 concludes the theory by providing an overview of some common architectural patterns (best practices, if you will) used by seasoned Java developers.
By the end of this book, you will have gained a strong knowledge of Java's internals, you will know what is meant by Object-Oriented Programming, know how to debug and deal with errors in your programs, know how to handle file-based input and output, talk to databases, talk to web services, make a full-fledged graphical application, and be familiar with some common and well-known programming "patterns," which are best practices to structure and organize a program's architectural setup.
TOPICS NOT COVERED IN THIS BOOK
This book is not a reference manual. The goal is to get readers acquainted with the basics of Java and Object-Oriented Programming to use within practical applications, not to provide a full overview of Java's API. As such, given the scope of this book, there are some concepts that are not discussed in detail. However, we have taken care to avoid elements you can live without at this point in your Java career.
Working with generics in Java, for example, is not discussed explicitly, but instead explained briefly where needed (when we talk about collections in Java, such as lists or sets, for example). Working with generic classes can be daunting for novice Java programmers, and the Object-Oriented Programming concepts discussed suffice to cover the multitude of use cases. That said, familiarizing yourself with generic types after going through this book should not prove difficult.
Other topics that are not discussed in-depth include networking in Java (socket programming), multi-threaded and concurrent programming, reflection, and the lambda expressions introduced with Java 8. Networking aspects, however, are dealt with from a "higher-level" view. We discuss how to interact with web services, which provides a great starting point for practitioners to load data coming from the web. Concurrent, multi-threaded programming is a beneficial practice when performance and speed becomes an issue in applications, but for most use cases in a practical context, Java performs just fine without having to deal with multi-threading. Additionally, programming in a concurrent fashion introduces some particular challenges and "gotchas" that are unfit for beginning Java programmers to deal with. Reflection is a part of the Java API that allows programmers to examine and "reflect" on Java programs while they are running and perform changes to programs while they are being executed. While very helpful in some cases, it also is out of scope for a beginner's book on Java. Finally, the recently released Java 8 introduces some new concepts, most notably lambda expressions. Java 7 also provides functionality to work with so-called "anonymous classes," which are ad hoc implementations of a base class without a specific name or definition. Other than the fact that these classes can appear somewhat verbose, they are...
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.