O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Java Programming for Beginners

Book Description

Java Programming for Beginners is an introduction to Java programming, taking you through the Java syntax and the fundamentals of object-oriented programming.

About This Book

  • Learn the basics of Java programming in a step-by-step manner
  • Simple, yet thorough steps that beginners can follow
  • Teaches you transferable skills, such as flow control and object-oriented programming

Who This Book Is For

This book is for anyone wanting to start learning the Java language, whether you’re a student, casual learner, or existing programmer looking to add a new language to your skillset. No previous experience of Java or programming in general is required.

What You Will Learn

  • Learn the core Java language for both Java 8 and Java 9
  • Set up your Java programming environment in the most efficient way
  • Get to know the basic syntax of Java
  • Understand object-oriented programming and the benefits that it can bring
  • Familiarize yourself with the workings of some of Java’s core classes
  • Design and develop a basic GUI
  • Use industry-standard XML for passing data between applications

In Detail

Java is an object-oriented programming language, and is one of the most widely accepted languages because of its design and programming features, particularly in its promise that you can write a program once and run it anywhere.

Java Programming for Beginners is an excellent introduction to the world of Java programming, taking you through the basics of Java syntax and the complexities of object-oriented programming. You'll gain a full understanding of Java SE programming and will be able to write Java programs with graphical user interfaces that run on PC, Mac, or Linux machines. This book is full of informative and entertaining content, challenging exercises, and dozens of code examples you can run and learn from.

By reading this book, you’ll move from understanding the data types in Java, through loops and conditionals, and on to functions, classes, and file handling. The book finishes with a look at GUI development and training on how to work with XML. The book takes an efficient route through the Java landscape, covering all of the core topics that a Java developer needs. Whether you’re an absolute beginner to programming, or a seasoned programmer approaching an object-oriented language for the first time, Java Programming for Beginners delivers the focused training you need to become a Java developer.

Style and approach

This book takes a very hands-on approach, carefully building on lessons learned with snippets and tutorials to build real projects.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book 
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Getting Started with Java
    1. What is Java?
      1. Features of Java
      2. Java applications
    2. Setting up your development environment
      1. Installing JDK
      2. Installing the NetBeans IDE
      3. Writing our first Java program
        1. Creating a new project
        2. Creating a Java class
        3. Writing the code
          1. Java comments
          2. The main() function
          3. Printing a string
          4. Java syntax
          5. Executing our program
    3. How to interpret errors detected by NetBeans?
      1. The code completion feature
    4. Summary
  3. Understanding Typed Variables
    1. Integer variables
      1. The solution of variables
      2. Memory allocation for integer variables
    2. Floating point variables
      1. Behavior difference between integer and float data types
      2. Type casting
      3. The double data type
    3. The Math class library
      1. Using the pow() function
      2. Importing class libraries
    4. Char variables
    5. Strings
      1. The concatenation operator
      2. The toUpperCase() function
      3. The replace() function
      4. Escape sequences
    6. Summary
  4. Branching
    1. Understanding if statements
      1. Conditional statements
        1. Using if statements
        2. Using else statements
    2. Complex conditionals
      1. The contains function
      2. Complex conditional statements
      3. The boolean variable
    3. Switch, case, and break
      1. Program using switch, case, and break
    4. While and do...while loops
      1. While loops
      2. The do...while loops
    5. For loops
    6. Summary
  5. Data Structures
    1. Working with arrays
      1. Declaring and initializing an array
      2. Assigning values to an array
      3. Array example in NetBeans
        1. Creating an array
        2. Creating a for loop
        3. Printing the alphabet
      4. Default initialization of arrays in Java
    2. Multidimensional arrays
      1. A multidimensional array example in NetBeans
        1. Creating a multidimensional array
        2. Using nested loops for multidimensional arrays
        3. Assigning different colors to our chessboard
        4. Printing the chessboard
    3. ArrayLists
      1. An ArrayList example in NetBeans
        1. Creating an ArrayList
        2. Getting user input
        3. Printing the ArrayList of user input
        4. Giving control to the user
        5. Analyzing ArrayIndexOutOfBoundsException
    4. Maps
      1. Creating a Map
      2. Assigning values to our Map
      3. Fetching information from our Map
    5. Summary
  6. Functions
    1. The basics of Java functions
    2. Methods
      1. Temperature conversion program
        1. Setting up the control flow
        2. Exploring separate cases - C, K, and F
          1. Celsius type
          2. Kelvin type
          3. Fahrenheit type
      2. Executing the first part of the program
      3. Executing the second part of the program
      4. Final touch to the program
    3. Advanced Java functions
    4. Operating java variables
      1. Using variables in the program
        1. Understanding passing parameters
    5. Summary
  7. Modeling with Object-Oriented Java
    1. Creating classes and objects
      1. Creating custom classes
        1. Creating member variables
        2. Creating member functions
    2. Creating constructors
      1. Types of constructors
    3. Summary
  8. More Object-Oriented Java
    1. Inheritance
      1. Creating a superclass
      2. The is-a relationship
        1. Overriding
        2. Data structure
        3. The abstract method
        4. The protected method
    2. Abstract
    3. Summary
  9. Useful Java Classes
    1. The Calendar class
      1. Subclasses of Calendar
      2. Fetching the current day, month, and year
      3. How Calendar works
    2. String functionality
      1. String literals versus String objects
      2. String functions
        1. The replace() function
        2. The format() function
    3. Exceptions
      1. Analyzing the console exception messages
      2. Handling exceptions
        1. The try and catch blocks
        2. The finally block
    4. The Object class
      1. Overriding the equals() method
      2. Other Object methods
    5. Primitive classes
    6. Summary
  10. File Input and Output
    1. Writing data to files
      1. The FileWriter class
        1. Relieving resources using the close() method
      2. The BufferedWriter class
    2. Reading data from files
      1. The FileReader and BufferedReader class
    3. The Serializable class
      1. Serializing an object
      2. Deserializing an object
    4. Summary
  11. Basic GUI Development
    1. Swing GUIs
      1. The JFrame class
        1. Setting the size of our window
        2. Adding a label
        3. Closing our application
      2. The SwingUtilities class
    2. A visual GUI editor tool – palette
      1. How palette works
    3. Event handling
      1. Adding a button
      2. Adding functionality to our button
    4. Summary
  12. XML
    1. Reading XML data
    2. Parsing XML data
      1. Accessing Jane's XML element
      2. Printing Jane's car details
    3. Writing XML data
      1. Java code to write XML data
      2. Modifying XML data
    4. Summary