29,99 €
As part of the best-selling Pocket Primer series, this book offers a thorough introduction to Java development for beginners. It provides a fast-paced overview of core Java concepts and APIs, using Java 8 code samples. Companion files with source code are included.
The journey begins with an introduction to Java, covering Boolean logic, Unicode, and user input. It progresses through methods, constructors, loops, arrays, and recursion. The course then delves into object-oriented programming (OOP) concepts and data structures. Advanced topics include streams, file handling, and integrating Java with SQL. The book also lists new features from Java 9 to Java 13.
Understanding these concepts is crucial for developing efficient Java applications. This book transitions readers from basic to advanced Java programming, blending theoretical knowledge with practical skills. Companion files enhance the learning experience, making this book an essential resource for mastering Java development.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 293
Veröffentlichungsjahr: 2024
LICENSE, DISCLAIMER OF LIABILITY, AND LIMITED WARRANTY
By purchasing or using this book and companion files (the “Work”), you agree that this license grants permission to use the contents contained herein, including the disc, but does not give you the right of ownership to any of the textual content in the book / disc or ownership to any of the information or products contained in it. This license does not permit uploading of the Work onto the Internet or on a network (of any kind) without the written consent of the Publisher. Duplication or dissemination of any text, code, simulations, images, etc. contained herein is limited to and subject to licensing terms for the respective products, and permission must be obtained from the Publisher or the owner of the content, etc., in order to reproduce or network any portion of the textual material (in any media) that is contained in the Work.
MERCURY LEARNING AND INFORMATION (“MLI” or “the Publisher”) and anyone involved in the creation, writing, or production of the companion disc, accompanying algorithms, code, or computer programs (“the software”), and any accompanying Web site or software of the Work, cannot and do not warrant the performance or results that might be obtained by using the contents of the Work. The author, developers, and the Publisher have used their best efforts to ensure the accuracy and functionality of the textual material and/or programs contained in this package; we, however, make no warranty of any kind, express or implied, regarding the performance of these contents or programs. The Work is sold “as is” without warranty (except for defective materials used in manufacturing the book or due to faulty workmanship).
The author, developers, and the publisher of any accompanying content, and anyone involved in the composition, production, and manufacturing of this work will not be liable for damages of any kind arising out of the use of (or the inability to use) the algorithms, source code, computer programs, or textual material contained in this publication. This includes, but is not limited to, loss of revenue or profit, or other incidental, physical, or consequential damages arising out of the use of this Work.
The sole remedy in the event of a claim of any kind is expressly limited to replacement of the book and/or disc, and only at the discretion of the Publisher. The use of “implied warranty” and certain “exclusions” vary from state to state, and might not apply to the purchaser of this product.
Companion files for this title are available by writing to the publisher at [email protected].
Pocket Primer
Oswald Campesato
MERCURY LEARNING AND INFORMATION
Dulles, Virginia
Boston, Massachusetts
New Delhi
Copyright ©2022 by MERCURY LEARNING AND INFORMATION LLC. All rights reserved.
This publication, portions of it, or any accompanying software may not be reproduced in any way, stored in a retrieval system of any type, or transmitted by any means, media, electronic display or mechanical display, including, but not limited to, photocopy, recording, Internet postings, or scanning, without prior permission in writing from the publisher.
Publisher: David Pallai
MERCURY LEARNING AND INFORMATION
22841 Quicksilver Drive
Dulles, VA 20166
www.merclearning.com
800-232-0223
O. Campesato. JAVA for Developers Pocket Primer.
ISBN: 978-1-68392-549-1
The publisher recognizes and respects all marks used by companies, manufacturers, and developers as a means to distinguish their products. All brand names and product names mentioned in this book are trademarks or service marks of their respective companies. Any omission or misuse (of any kind) of service marks or trademarks, etc. is not an attempt to infringe on the property of others.
Library of Congress Control Number: 2022943450
222324321 This book is printed on acid-free paper in the United States of America.
Our titles are available for adoption, license, or bulk purchase by institutions, corporations, etc. For additional information, please contact the Customer Service Dept. at 800-232-0223(toll free).
All of our titles are available in digital format at academiccourseware.com and other digital vendors. Companion files (figures and code listings) for this title are available by contacting [email protected]. The sole obligation of MERCURY LEARNING AND INFORMATION to the purchaser is to replace the disc, based on defective materials or faulty workmanship, but not based on the operation or functionality of the product.
I’d like to dedicate this book to my parents– may this bring joy and happiness into their lives.
Preface
Chapter 1: Introduction to Java
A Very Brief Introduction to Java
Java Release Dates
Downloading a Java Release (Short Version)
Selecting a Version of Java (detailed version)
Java 8 and Java 11
Java Version Numbers
JRE Versus a JDK
Java Distributions
Java IDEs
Java Data Types, Operators, and their Precedence
Java Comments
Java Operators
Precedence of Java Operators
Creating and Compiling Java Classes
A “Hello World” Example
Working with Numbers
Working with Other Bases
Working with Random Numbers
Working with Built-in Math Functions
Working with Built-in Trigonometric Functions
Working with Bitwise Operators
The Java String Class
Characters, Arrays, and Strings in Java
Java Strings with Metacharacters
The Java new Operator
Equality of Strings
Comparing Strings
Searching for a Substring in Java
Useful String Methods in Java
Parsing Strings in Java
Overriding the Java toString() Method (Optional)
Autoboxing and Unboxing (Optional)
Summary
Chapter 2: Boolean Logic, Unicode, User Input
Conditional Logic in Java
Working with Boolean Expressions
Working with Dates
The Current Date and Time
Date Comparison
Date Formatting with SimpleDateFormat
Parsing Strings as Dates
Working with Unicode, i18n, and i10n
Working with Base64
Working with Exceptions in Java
Checked Exceptions
Unchecked Exceptions
The throws Keyword
The Throwable Class
Getting User Input and Handling Exceptions
Parsing User Input and Handling Exceptions
Nested Exceptions
A try/catch/finally Example
Summary
Chapter 3: Methods and Constructors
What is a Java Class?
Public, Private, or Protected?
The Scope of Java Variables
Static Methods
Other Static Types
What are Accessors and Mutators?
A Java Class Without Accessors
Refactoring Code with Accessor Methods
Defining a Constructor and an Accessor
Accessors, Mutators, and the this Keyword
Arrays of Classes
Static Methods
Static Code Blocks
A Second “Hello World” Example
A Third “Hello World” Example
A Fourth “Hello World” Example
A Fifth “Hello World” Example
Java Anonymous Inner Classes
Java Named Inner Classes
Anonymous Classes Versus Named Inner Classes
Limitations of Anonymous Classes
Private Constructors (Why?)
The Java import Statement
The Java package Statement
An Example of the package Statement (1)
An Example of the package Statement (2)
Creating Java JAR Files for Java Class Files
Summary
Chapter 4: Loops, Arrays, and Recursion
Working with for Loops
Determining Leap Years
Java for Loops with Integers
Checking for Palindromes
Nested Loops
Goldbach’s Conjecture
Variations of a Basic for Loop
Working with while Loops
Finding the Divisors of a Number
Working with do-while Loops
Working with a Java switch Statement
Java Arrays
Working with Arrays of Numbers
The Bubble Sort Algorithm
Java Multi-Dimensional Arrays
Java Multi-Dimensional Arrays (2)
Working with Characters and Strings
Working with Arrays of Strings
Displaying Command Line Arguments
Randomly Accessing Arrays of Strings
The StringBuilder and StringBuffer Classes
Java Example with StringBuilder and StringBuffer Classes
What is Recursion?
Adding Integers
Powers of Two
Factorial Values
Fibonacci Numbers
Calculating Factorial Values
Calculating the GCD of Two Numbers
Counting Digits in Integers
Adding Digits in Integers
Reverse a String via Recursion
Summary
Chapter 5: Introduction to OOP
Java Constructors
Overloading Java Methods
Public, Protected, and Private Keywords
Identity and Equality are Different Concepts
A Quick Introduction to Inheritance
The final Modifier with Java Methods and Classes
Working with Value Objects in Java
An Example of a Value Object in Java
What is a Java Interface?
Extending Interfaces
Java Interfaces and Abstract Classes
A Java Class That Implements an Interface
Java Abstract Methods and Classes
An Abstract Class in Java
Are Subclasses Always Concrete Classes in Java?
Method Arguments and Interfaces
What is OOP?
Inheritance
The Java Object Class
Inheritance and Overriding Methods
Encapsulation
Polymorphism
Summary
Chapter 6: Data Structures
Java Legacy Data Structures
Iterators
What is an Iterator?
Active versus Passive
Enumeration: Oldest Style Iteration
Iterator: Second Style Iteration
Generics: Third Style Iteration
forEach: Fourth Style Iteration
Streams: Fifth Style Iteration
The Collection Interfaces
The Collection Classes
Overview of Java Collections
Iterating over Wrapper-Synchronized Collections
Convenience Implementations
List View of an Array
Immutable Multiple-Copy List
The ArrayList Class
The HashMap Class
The HashSet Class
The LinkedList Class
The Queue Class
The TreeSet Class
The Collections Class
Linked Lists of Objects
Generic Methods
Generic Classes
Counting Distinct Characters
Java 8 Enhancements for Collections
Summary
Chapter 7: Streams and Files
Working with Streams in Java
The Abstract Class InputStream
The FileInputStream Class
The FilterInputStream Class
The BufferedInputStream Class
The DataInputStream Class
Reading User Input from the Console
Reading Line-Oriented User Input
Reading Character-Oriented User Input
Reading a Text File
Reading a CSV File
Writing Data to a Text File
The FileWriter Class
The PrintWriter Class
The PrintStream Class
Counting Words and Characters in a File
Search for a String in a File
The BufferedWriter Class
The BufferedWriter Class with try-with-resources (Java 7+)
Working with Directories
Serialization
Deserialization
Reflection
Annotations
Summary
Chapter 8: Java and SQL
What is SQL?
What is DCL?
What is DDL?
Delete vs. Drop vs. Truncate
What is DQL?
What is DML?
What is TCL?
Working with MySQL
Logging into MySQL
Creating a MySQL Database
Creating and Dropping Tables
Manually Creating MySQL Tables
Working with Simple SELECT Statements
The EXISTS Keyword
The LIMIT Keyword
DELETE, TRUNCATE, and DROP in SQL
More Options for the DELETE Statement in SQL
What is a SQL Index?
Types of Indexes
Creating an Index
Overhead of Indexes
Considerations for Defining Indexes
Connecting to MySQL in Java
Drop and Recreate a MySQL Table
Insert Data into a MySQL Table
Select Data from a MySQL Table
Next Steps
Java Threads and Sockets
Java Networking
Reflection, GC, and Classloaders
Summary
Index
The goal of this book is to provide a reasonably thorough introduction to Java development to people who are relatively new to the Java programming language. The biggest challenge for a book of this length is to provide adequate coverage of some fundamental aspects of Java that are illustrated via code samples that are primarily for Java 8. As this book goes to print, Java 17 is the production release. If you are planning to write a Java application, you need to determine the versions of Java that contain the functionality that you need in your application, after which you can determine the version(s) that you need to install.
Great question! If you work with modern browsers, you might know about the “browser wars” and also the incompatibility issues that arose in different versions of the same browser (and the relief when the problematic versions were discontinued).
Fortunately, the situation regarding different versions of Java is much more benign (the main source of discontent was the proprietary nature of Java, which eventually led to OpenJDK).
Java 8 was a significant milestone for Java in terms of significant new features, which introduced not only Collections, but also lambda expressions, functional programming, and Streams, all of which are also discussed in this book (along with code samples).
Knowledge of the core features of Java8 will serve you well for subsequent versions of Java. If you are interested, a comprehensive look at Java8 is here:
https://dzone.com/articles/j%CE%BBv%CE%BB-8-a-comprehensive-look
Another useful article that contains many of the new features in Java11 (and details regarding the Long-Term Support for Java8 and Java11) is here:
https://dzone.com/articles/when-will-java-11-replace-java-8-as-the-default-jaTBD
https://www.javacodegeeks.com/2019/11/supporting-java-8.htmlTBD
Clearly this book is ambitious, and therefore it’s vitally important to articulate a realistic set of expectations. First, you need some object-oriented programming experience, or at least be able to learn OO concepts on your own time (fortunately there are many free online tutorials). Second, consider the fact that there are Java books containing more than 600 pages that do not cover everything about Java.
Hence, this book can only cover a portion of Java. Some Java topics are covered lightly (but often with links where you can find additional information) in order to focus on the key idea. Some Java topics are omitted entirely because they do not belong in a short book about Java.
The value of this book is the convenience of having a set of illustrative examples in one location, along with all the code samples in the companion files. While free code samples are useful, don’t forget that you need to spend time searching for relevant code samples and then spend time reading them. You also need to determine which code samples are up-to-date and actually work correctly (which also takes time). This book saves you this potentially time-consuming process. The question that you need to answer is simple: does the value of your time exceed the cost of this book?
The code samples are derived in various ways, such as custom written code (by the author), variations of code from the Java documentation, and sections of code from discussions in online forums.
The code samples attempt to adhere to the “Four Cs”: they must be Clear, Concise, Complete, and Correct to the extent that it’s possible to do so, given the limitations of the size of this book and the importance of each code sample in relation to the other code samples in this book.
Code samples are available for downloading by writing to the publisher at [email protected].
You need an understanding of OOP (Object Oriented Programming), Java, and XML. Knowledge of another OO programming language instead of Java might be sufficient, but to be on the safe side, glance through the code samples to gauge whether or not you can manage the material.
This book is intended to reach an international audience of developers with highly diverse backgrounds in various age groups. While many readers know how to read English, their native spoken language is not English (which could be their second, third, or even fourth language). Consequently, this book uses standard English and avoids colloquial expressions that might be confusing to those readers. As you know, many people learn by different types of mimicry, which includes reading, writing, or hearing new material. This book takes these points into consideration in order to provide a comfortable and meaningful learning experience for the intended readers.
Some developers learn well from prose, others learn well from sample code (and lots of it), which means that there’s no single style that works perfectly for everyone.
Moreover, some programmers want to run the code first, see what it does, and then return to the code to understand the details (and others use the opposite approach).
Consequently, there is a variety of code samples in this book. Some are short, some are long, and other code samples “build” from earlier code samples.
You will acquire an understanding of various “core” concepts in Java, along with an understanding of how to use many Java APIs. What you decide to learn about Java after you finish reading this Java Primer depends on your goals and career path. For example, if you are a developer, you will undoubtedly continue learning more about Java. On the other hand, if you are in management, you will understand enough about Java to interact on a technical level with Java developers.
The code samples in this book were developed using an early release of Java 13 on a MacBook Pro with OS X 10.10 (Mavericks).
This book is for developers who are new to Java development. The code samples are intended to help you gain an understanding of various Java APIs, and that’s why every code sample is at most two pages in length (and often less than one page). The focus is to provide code for Java features that you are likely to need in your Java applications. Shorter code samples means that more Java APIs can be included in this book.
The decision process was as objective as possible, and it involved several criteria. The first criterion was to include “must have” concepts that are common in Java applications. The second criterion was to include “nice to have” functionality that would appeal to Java novices. The third criterion involved Java APIs that could be relevant useful for people who want to write “serious” Java applications.
Examples of such APIs include social connectivity (Facebook and Twitter). The fourth criterion involved Java APIs that are needed for Java applications in the book.
The Java features that do not meet any of the criteria listed in the previous section are not included in this book. Consequently, there is no coverage of Java Native Interface (JNI), which allows Java code to invoke C/C++ functions. In addition, AWT, Java Swing, JavaFX, Java Servlets, JSPs, JAXB, JAXP, JAXR, JEE, and Java Security are not covered in this book. However, you can find a plethora of online blog posts and articles that discuss these topics.
The answer to this question varies widely, mainly because the answer depends heavily on your objectives. Specifically, you need to decide how much effort you are prepared to invest in furthering your knowledge. The amount of time that you need to make significant advances in your technical expertise also depends on your current level of technical knowledge and experience. For instance, the needs of a manager, a student, and a professional Java developer (among others) are obviously different.
With the preceding points in mind, one recommendation involves 1) asking friends and coworkers for advice and suggestions, 2) determining the major features of Java-based projects (either personal or work-related), and 3) performing an Internet search and spend some time evaluating other resources that are available.