Introduction to Programming

Book description

Get a solid understanding of Java fundamentals to master programming through a series of practical steps

About This Book
  • Enjoy your first step into the world of programming
  • Understand what a language is and use its features to build applications
  • Learn about a wide variety of programming applications
Who This Book Is For

Introduction to Programming is for anybody who wants to learn programming. All you'll need is a computer, internet connection, and a cup of coffee.

What You Will Learn
  • Understand what Java is
  • Install Java and learn how to run it
  • Write and execute a Java program
  • Write and execute the test for your program
  • Install components and confgure your development environment
  • Learn and use Java language fundamentals
  • Learn object-oriented design principles
  • Master the frequently used Java constructs
In Detail

Have you ever thought about making your computer do what you want it to do? Do you want to learn to program, but just don't know where to start? Instead of guiding you in the right direction, have other learning resources got you confused with over-explanations?

Don't worry. Look no further. Introduction to Programming is here to help.

Written by an industry expert who understands the challenges faced by those from a non-programming background, this book takes a gentle, hand-holding approach to introducing you to the world of programming. Beginning with an introduction to what programming is, you'll go on to learn about languages, their syntax, and development environments. With plenty of examples for you to code alongside reading, the book's practical approach will help you to grasp everything it has to offer. More importantly, you'll understand several aspects of application development. As a result, you'll have your very own application running by the end of the book. To help you comprehensively understand Java programming, there are exercises at the end of each chapter to keep things interesting and encourage you to add your own personal touch to the code and, ultimately, your application.

Style and approach

This step-by-step guide will familiarize you with programming using some practical examples.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Introduction to Programming
  3. Dedication
  4. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  5. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. Java Virtual Machine (JVM) on Your Computer
    1. What is Java?
      1. Basic terms
      2. History and popularity
      3. Principles
    2. Java platforms, editions, versions, and technologies
      1. Platforms and editions
      2. Versions
      3. Technologies
    3. Java SE Development Kit (JDK) installation and configuration
      1. Where to start
      2. The page with Java installers
      3. How to install
    4. Main Java commands
      1. The JVM execution command
      2. The compilation command
      3. Command jcmd and other commands
    5. Exercise – JDK tools and utilities
      1. Answer
    6. Summary
  8. Java Language Basics
    1. The basic terms of Java programming
      1. Byte code
      2. Defects (bugs) and their severity and priority
      3. Java program dependencies
      4. Statements
      5. Methods
      6. Classes
      7. The Main class and the main method
    2. Classes and objects (instances)
      1. Java classes
      2. Java object (class instance)
    3. Class (static) and object (instance) members
      1. Private and public
      2. Static members
      3. Object (instance) members
      4. Method overloading
    4. Interface, implementation, and inheritance
      1. Interface
      2. Implementation
      3. Inheritance
      4. The java.lang.Object class
        1. The equals() method
        2. The hashCode() method
        3. The getClass() method
        4. The toString() method
        5. The clone() method
        6. The wait() and notify() methods
    5. OOP concepts
      1. Object/class
      2. Encapsulation
      3. Inheritance
      4. Interface (abstraction)
      5. Polymorphism
    6. Exercise – Interface versus abstract class
      1. Answer
    7. Summary
  9. Your Development Environment Setup
    1. What is the development environment?
      1. Java editor is your main tool
      2. Source code compilation
      3. Code sharing
      4. Code and test execution
    2. Setting the classpath
      1. Manual setting
      2. Searching on the classpath
      3. IDE sets the classpath automatically
    3. There are many IDEs out there
      1. NetBeans
      2. Eclipse
      3. IntelliJ IDEA
    4. Installing and configuring IntelliJ IDEA
      1. Downloading and installing
      2. Configuring IntelliJ IDEA
    5. Exercise – Installing NetBeans IDE
      1. Answer
    6. Summary
  10. Your First Java Project
    1. What is a project?
      1. Definition and origin of project
      2. Project-related terms
      3. A project's life cycle
    2. Creating a project
      1. Creating a project using a project wizard
      2. Maven project configuration
      3. Changing IDE settings at any time
    3. Writing an application code
      1. Java package declaration
      2. Creating a package
      3. Creating the MyApplication class
      4. Building the application
      5. Hiding some files and directories
      6. Creating the SimpleMath class
      7. Creating methods
    4. Executing and unit testing an application
      1. Executing the application using the IDE
      2. Creating a unit test
      3. Executing the unit test
      4. How many unit tests are enough?
    5. Exercise – JUnit @Before and @After annotations
      1. Answer
    6. Summary
  11. Java Language Elements and Types
    1. What are the Java language elements?
      1. Input elements
      2. Types
    2. Comments
    3. Identifiers and variables
      1. Identifier
      2. Variable
      3. Variable declaration, definition, and initialization
      4. Final variable (constant)
    4. Reserved and restricted keywords
      1. Reserved keywords
      2. Restricted keywords
    5. Separators
      1. Semicolon ";"
      2. Braces "{}"
      3. Parentheses "()"
      4. Brackets "[]"
      5. Comma ","
      6. Period "."
      7. Ellipsis "..."
      8. Colons "::"
      9. At sign "@"
    6. Primitive types and literals
      1. The Boolean type
      2. Integral types
      3. Floating-point types
      4. Default values of primitive types
      5. Primitive type literals
    7. Reference types and String
      1. Class types
      2. Interface types
      3. Arrays
      4. Default value of a reference type
      5. Reference type literals
      6. String immutability
      7. Enum types
      8. Passing reference type values as method parameters
    8. Exercise – Variable declarations and initializations
      1. Answer
    9. Summary
  12. Interfaces, Classes, and Object Construction
    1. What is an API?
      1. Java APIs
      2. Command line APIs
      3. HTTP-based APIs
      4. Software component API
    2. Interface and object factory as API
      1. Interface
      2. Object factory
      3. Reading configuration file
        1. Using the json-simple library
        2. Using the json-api library
        3. Unit test
      4. Calculator API 
        1. Adding static methods to API
        2. The API is complete
    3. Overloading, overriding, and hiding
      1. Interface method overloading
      2. Interface method overriding
      3. Interface static member hiding
      4. Class member hiding
      5. Instance method overriding
      6. Instance method overloading 
    4. This, super, and constructors
      1. Keyword this and its usage
      2. Keyword super and its usage
      3. Constructors
    5. Final variable, final method, or final class
      1. Final variable
      2. Final method
      3. Final class
    6. Exercise – Restricting a class instantiation to a single shared instance
      1. Answer
    7. Summary
  13. Packages and Accessibility (Visibility)
    1. What is importing?
      1. Structure of the .java file and packages
      2. Single class import
      3. Multiple classes import
      4. Static import
    2. Access modifiers
      1. The accessibility of a top-level class or interface
      2. Access to class or interface members
      3. The constructor's accessibility is the same as any class member
    3. Encapsulation
      1. Data hiding and decoupling
      2. Flexibility, maintainability, and refactoring
      3. Reusability
      4. Testability
    4. Exercise – Shadowing
      1. Answer
    5. Summary
  14. Object-Oriented Design (OOD) Principles
    1. What is the purpose of design?
      1. The project's feasibility
      2. Requirement gathering and prototyping
      3. High-level design
      4. Detailed design
      5. Coding
      6. Testing
    2. A roadmap to a good design
      1. Encapsulating and coding to an interface
      2. Taking advantage of polymorphism
      3. Decoupling as much as you can
      4. Preferring aggregation over inheritance
      5. So many OOD principles and so little time
    3. Exercise – Design patterns
      1. Answer
    4. Summary
  15. Operators, Expressions, and Statements
    1. What are the core elements of Java programming?
      1. Operators
        1. Arithmetic unary (+  -) and binary operators:  +   -   *   /   %
        2. Incrementing and decrementing unary operators:  ++   -- 
        3. Equality operators:  ==   !=
        4. Relational operators:  <  >  <=  >=   
        5. Logical operators:  !  &  |  
        6. Conditional operators:  &&   ||    ? : (ternary)
        7. Assignment operators (most popular):  =   +=   -=   *=   /=   %=
        8. Instance creation operator: new
        9. Type comparison operator:  instanceof
        10. Preferring polymorphism over the instanceof operator
        11. Field access or method invocation operator:  .
        12. Cast operator:  (target type)
      2. Expressions
      3. Statements
    2. Operator precedence and evaluation order of operands
      1. Operator precedence
      2. Evaluation order of operands
    3. Widening and narrowing reference types
      1. Widening
      2. Narrowing
    4. Widening and narrowing conversion of primitive types
      1. Widening
      2. Narrowing
      3. Methods of primitive type conversion 
    5. Boxing and unboxing between primitive and reference types
      1. Boxing
      2. Unboxing
    6. Method equals() of reference types
      1. Using the implementation of the base class Object
      2. Overriding the equals() method
      3. Using the identification implemented in the parent class
      4. The equals() method of the String class
      5.  The equals() method in wrapper classes of primitive types
    7. Exercise – Naming the statements
      1. Answer
    8. Summary
  16. Control Flow Statements
    1. What is a control flow?
      1. Selection statements
      2. Iteration statements
      3. Branching statements
      4. Exception handling statements
    2. Selection statements
      1. if 
      2. if...else 
      3. if...else if-...-else 
      4. switch...case 
    3. Iteration statements
      1. while
      2. do...while
      3. for 
      4. for enhanced
      5. for with multiple initializers and expressions
    4. Branching statements
      1. Break and labeled break
      2. Continue and labeled continue
      3. return
    5. Exception handling statements
      1. throw
      2. try...catch 
      3. Checked and unchecked (runtime) exceptions
      4. throws
      5. Custom exceptions
      6. What is exception handling?
      7. Some best practices of exception handling
      8. finally
      9. Assert requires JVM option -ea
    6. Exercise – Infinite loop
      1. Answer
    7. Summary
  17. JVM Processes and Garbage Collection
    1. What are JVM processes?
      1. Loading
      2. Linking
      3. Initialization
      4. Instantiation
      5. Execution
      6. Garbage collection
      7. Application termination
    2. JVM architecture
      1. Runtime data areas
      2. Classloader
      3. Execution engine
    3. Threads
      1. Extending the Thread class
      2. What is daemon?
      3. Running threads extending Thread 
      4. Implementing Runnable
      5. Runing threads implementing Runnable
      6. Extending Thread vs implementing Runnable
    4. How to execute the main(String[]) method
      1. Using IDE 
      2. Command line with classes on classpath
      3. Command line with a .jar file on classpath
      4. Command line with an executable .jar file
    5. Garbage collection
      1. Responsiveness, throughput, and stop-the-world
      2. Object age and generations
      3. When stop-the-world is unavoidable
    6. Exercise – Monitoring JVM while running an application
      1. Answer
    7. Summary
  18. Java Standard and External Libraries
    1. What are standard and external libraries?
    2. Java standard libraries
      1. java.lang
      2. java.util
      3. java.time
      4. java.io and java.nio
      5. java.sql and javax.sql
      6. java.net
      7. java.math
      8. java.awt,  javax.swing, and  javafx
    3. Java external libraries
      1. org.junit
      2. org.mockito
      3. org.apache.log4j and org.slf4j
      4. org.apache.commons
        1. org.apache.commons.io
        2. org.apache.commons.lang and lang3
        3. org.apache.commons.codec.binary
    4. Exercise – Comparing String.indexOf() and StringUtils.indexOf()
      1. Answer
    5. Summary
  19. Java Collections
    1. What are collections?
      1. The java.util package
      2. Apache Commons collections
      3. Collections vs arrays
      4. Here is what we are going to discuss
    2. List - ArrayList preserves order
      1. Prefer variable type List
      2. Why is it called ArrayList?
      3. Adding elements
      4. size(), isEmpty(), clear()
      5. Iterate and stream
      6. Add using generics
      7. Add collection
      8. Implementing equals() and hashCode()
      9. Locating element
      10. Retrieving elements
      11. Removing elements
      12. Replacing elements
      13. Sorting String and numeral types
      14. Sorting custom objects
      15. Comparing with another collection
      16. Converting to array
      17. List implementations
    3. Set - HashSet does not allow duplicates
      1. Preferring variable type Set
      2. Why is it called HashSet?
      3. Adding elements
      4. size(), isEmpty(), and clear()
      5. Iterate and stream
      6. Adding using generics
      7. Adding collection
      8. Implementing equals() and hashCode()
      9. Locating element
      10. Retrieving elements
      11. Removing elements
      12. Replacing elements
      13. Sorting
      14. Comparing with another collection
      15. Converting to array
      16. Set implementations
    4. Map – HashMap stores/retrieves objects by key
      1. Preferring variable type Map
      2. Why is it called HashMap?
      3. Adding and maybe replace
      4. size(), isEmpty(), and clear()
      5. Iterate and stream
      6. Adding using generics
      7. Adding another Map
      8. Implementing equals() and hashCode()
      9. Locating element
      10. Retrieving elements
      11. Removing elements
      12. Replacing elements
      13. Sorting
      14. Comparing with another collection
      15. Map implementations
    5. Exercise – EnumSet methods
      1. Answer
    6. Summary
  20. Managing Collections and Arrays
    1. Managing collections
      1. Initializing collections
        1. Collection constructor
        2. Instance initializer (double brace)
        3. Static initialization block
        4. Factory methods of()
        5. Using other objects and streams
      2. Immutable collections
        1. Immutable versus unmodifiable
        2. Immutable without methods of()
        3. Methods add() and put() confusion
      3. java.util.Collections class  
        1. Copy 
        2. Sort and equals()
        3. Reverse and rotate
        4. Search and equals() 
        5. Comparing two collections
        6. Min and max elements
        7. Add and replace elements
        8. Shuffle and swap elements
        9. Converting to a checked collection
        10. Convert to a thread-safe collection
        11. Convert to another collection type
        12. Create enumeration and iterator
      4. Class collections4.CollectionUtils
    2. Manage arrays
      1. Initialize arrays
        1. Creation expression
        2. Array initializer
        3. Static initialization block
        4. From collection
        5. Other possible methods
      2. Class java.util.Arrays
      3. Class lang3.ArrayUtils
    3. Exercise – Sort list of objects
      1. Answer
    4. Summary
  21. Managing Objects, Strings, Time, and Random Numbers
    1. Managing objects
      1. Class java.util.Objects
        1. equals() and deepEquals() 
        2. hash() and hashCode()
        3. isNull() and nonNull()
        4. requireNonNull() 
        5. checkIndex()
        6. compare()
        7. toString()
      2. Class lang3.ObjectUtils
    2. Managing strings
      1. StringBuilder and StringBuffer
      2. Class java.lang.String
        1. Constructors
        2. format()
        3. replace()
        4. compareTo()
        5. valueOf(Objectj)
        6. valueOf(primitive or char[])
        7. copyValueOf(char[])
        8. indexOf() and substring()
        9. contains() and matches()
        10. split(), concat(), and join()
        11. startsWith() and endsWith()
        12. equals() and equalsIgnoreCase()
        13. contentEquals() and copyValueOf()
        14. length(), isEmpty(), and hashCode()
        15. trim(), toLowerCase(), and toUpperCase()
        16. getBytes(), getChars(), and toCharArray()
        17. Get code point by index or stream
      3. Class lang3.StringUtils
    3. Managing time
      1. java.time.LocalDate
      2. java.time.LocalTime
      3. java.time.LocalDateTime
      4. Period and Duration
    4. Managing random numbers
      1. Method java.lang.Math.random()
      2. Class java.util.Random
    5. Exercise – Objects.equals() result
      1. Answer
    6. Summary
  22. Database Programming
    1. What is Java Database Connectivity (JDBC)?
      1. Connecting to the database
      2. Closing the database connection
      3. Structured Query Language (SQL)
    2. Creating a database and its structure 
      1. CREATE and DROP the database and its user
      2. CREATE, ALTER, and DROP table
    3. Create, read, update, and delete (CRUD) data
      1. INSERT statement
      2. SELECT-statement
      3. UPDATE-statement
      4. DELETE-statement
      5. Using the PreparedStatement class
    4. Exercise – Selecting unique first names
      1. Answer
    5. Summary
  23. Lambda Expressions and Functional Programming
    1. Functional programming
      1. What is a functional interface?
      2. Ready-to-use standard functional interfaces
        1. Function<T, R>
        2. Consumer<T>
        3. Supplier<T>
        4. Predicate<T>
        5. Other standard functional interfaces
      3. Chaining standard functions
        1. Chain two Function<T,R>
        2. Chain two Consumer<T>
        3. Chain two Predicate<T>
        4. identity() and other default methods
    2. Lambda expressions
      1. What is a lambda expression?
      2. Re-implementing functions
      3. Lambda limitations
        1. Effectively final local variable
        2. The this keyword interpretation
      4. Method references
    3. Exercise – Using the method reference to create a new object
      1. Answer
    4. Summary
  24. Streams and Pipelines
    1. What is a stream?
      1. Stream operations
    2. Creating a stream
      1. Stream interface
        1. empty(), of(T t), ofNullable(T t)
        2. iterate(Object, UnaryOperator)
        3. concat(Stream a, Stream b)
        4. generate(Supplier)
        5. of(T... values) 
      2. The Stream.Builder interface
      3. Other classes and interfaces
    3. Intermediate operations
      1. Filtering
      2. Mapping
      3. Sorting
      4. Peeking
    4. Terminal operations
      1. Processing each element
      2. Counting all elements
      3. Matching all, any, or none
      4. Finding any or first
        1. Class Optional
      5. Min and max
      6. The toArray() operation
      7. The reduce operation
      8. The collect operation
        1. Class collectors
    5. Numeric stream interfaces
      1. Creating a stream
        1. range(), rangeClosed()
      2. Intermediate operations
        1. boxed() and mapToObj()
        2. mapToInt(), mapToLong(), and mapToDouble()
        3. flatMapToInt(), flatMapToLong(), and flatMapToDouble()
      3. Terminal operations
        1. sum() and average()
    6. Parallel processing
      1. Stateless and stateful operations
      2. Sequential or parallel processing?
    7. Exercise – Multiplying all the stream elements
      1. Answer
    8. Summary
  25. Reactive Systems
    1. How to process a lot of data quickly
      1. Asynchronous 
        1. Sequential versus parallel streams
        2. Using the CompletableFuture class
      2. Non-blocking 
        1. The java.io versus java.nio package
        2. Event loop, or run loop
      3. Distributed
      4. Scalable
      5. Reactive 
        1. Responsive
        2. Resilient
        3. Elastic
        4. Message-driven
    2. Microservices
      1. Vert.x basics
      2. The HTTP server as a microservice
      3. Periodic service as a microservice
      4. The HTTP client as a microservice
      5. Other microservices
    3. Reactive systems
      1. Message-driven system
        1. Message consumer
        2. Message sender
        3. Message publisher
      2. Reality check
    4. Exercise – Creating io.reactivex.Observable
      1. Answer
    5. Summary
  26. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Introduction to Programming
  • Author(s): Nick Samoylov
  • Release date: June 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788839129