Discover millions of audiobooks, ebooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

Practical Java Programming for IoT, AI, and Blockchain
Practical Java Programming for IoT, AI, and Blockchain
Practical Java Programming for IoT, AI, and Blockchain
Ebook762 pages6 hours

Practical Java Programming for IoT, AI, and Blockchain

Rating: 0 out of 5 stars

()

Read preview

About this ebook

Learn practical uses for some of the hottest tech applications trending among technology professionals 

We are living in an era of digital revolution. On the horizon, many emerging digital technologies are being developed at a breathtaking speed. Whether we like it or not, whether we are ready or not, digital technologies are going to penetrate more and more, deeper and deeper, into every aspect of our lives. This is going to fundamentally change how we live, how we work, and how we socialize.

Java, as a modern high-level programming language, is an excellent tool for helping us to learn these digital technologies, as well as to develop digital applications, such as IoT, AI, Cybersecurity, Blockchain and more. Practical Java Programming uses Java as a tool to help you learn these new digital technologies and to be better prepared for the future changes.

  • Gives you a brief overview for getting started with Java Programming
  • Dives into how you can apply your new knowledge to some of the biggest trending applications today
  • Helps you understand how to program Java to interact with operating systems, networking, and mobile applications
  • Shows you how Java can be used in trending tech applications such as IoT (Internet of Things), AI (Artificial Intelligence), Cybersecurity, and Blockchain

Get ready to find out firsthand how Java can be used for connected home devices, healthcare, the cloud, and all the hottest tech applications. 

LanguageEnglish
PublisherWiley
Release dateJul 2, 2019
ISBN9781119560036
Practical Java Programming for IoT, AI, and Blockchain

Related to Practical Java Programming for IoT, AI, and Blockchain

Related ebooks

Programming For You

View More

Reviews for Practical Java Programming for IoT, AI, and Blockchain

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Practical Java Programming for IoT, AI, and Blockchain - Perry Xiao

    Preface

    I had my first programming experience when I was studying at Jilin University of Technology, China, in the 1990s. At that time, we were using mainframe computers, as personal computers (PCs) were not available in China. A mainframe computer is just one computer sitting somewhere in a room, and we connected to it through text-mode dumb terminals. As we connected to the mainframe computer using a shared account, a lot of strange things could happen. The program you created early on could be deleted or modified by someone else later. Printing was also a nightmare. For example, when you were printing your Fortran code, halfway through another student might send a printing request. The printer would immediately stop printing your code and start to print the other student's code. When finished, the printer would come back to resume printing the rest of your code. What kind of logic is that? So every time we wanted to print, we would shout I am printing! and hope others would not start to print their code at the same time. The programming language we used was Fortran, short for formula translation. Fortran is primarily designed for scientific calculations, and it is a very powerful language, but the Go To statement drove me crazy. It makes the code very difficult to read.

    Later, when IBM PCs started to become available, our university also bought some IBM PCs and built a dedicated two floor building to host these computers. Yes, you heard me, a whole building was dedicated to these IBM PCs. The building was designed like a clean room, with red carpeting, and reception. You needed to take off your shoes, change into slippers, and wear a white lab coat to go in. The time for each student to use these computers was strictly limited.

    Have you ever wondered why computer hard drives always start with Drive C, and not Drive A or Drive B? The reason is that in the beginning, IBM PCs did not have hard drives; instead, they had two 5¼ inch floppy drives. To use the computer, you needed two floppy disks—one floppy disk for the MS-DOS operating system, used to boot up the computer, and one floppy disk for saving your data. Each floppy disk could hold 512 KB of data. It could not fit even as much as a smart phone photo today. But that time, it was plenty. We started to learn the BASIC (Beginner's All-purpose Symbolic Instruction Code) programming language. BASIC is very simple to use, and we had endless fun with BASIC programming for both scientific calculations and drawing text based pictures.

    I started to learn Java in 1997, just two years after it was first released by what was then Sun Microsystems, now part of Oracle Corporation. Since web pages at that time were pretty much just static text and static pictures, I was fascinated with Java's ability to create animations (Java Applets) inside a web page. But the time when I really fell in love with Java and fully appreciated its beauty was a few years later. I was preparing for an MSc module on Java network programming, and I was astonished by how simple it was to learn and use Java, especially for network programming. With just a few lines of Java code, you have a server!

    I was also impressed with Java's memory garbage collection ability and the fact that Java has no pointers. C and C++ programmers, you'll be relieved to learn there are no pointers in Java! I had a tough time before Java, when I was using the C programming language for finite difference analysis and finite elements analysis. Pointers are variables to point to particular positions in memory, and they are essential for working with matrices. Ninety-nine percent of the time my programs crashed because of the mishandling of pointers. So I was over the moon when I heard there were no pointers in Java. Hallelujah! There is also no Go To statement in Java, which makes the Java code much easier to understand.

    Java's exception handling is also worth mentioning. Java can throw exceptions. So when you run a program, if an exception/error occurs, for example, division by zero, reading a file that does not exist, or connecting to a remote computer that is not responding, instead of hanging the program or crashing the entire computer, Java will simply terminate the program and gracefully display an error message.

    I have since developed many Java programs for teaching and researching. I have thoroughly enjoyed working with Java programming language, and I hope you will enjoy it too.

    Dr. Perry Xiao

    November 2018, London

    Introduction

    We are now living in an era of digital revolution. On the horizon, many emerging digital technologies, such as IoT (Internet of Things), AI (Artificial Intelligence), Cyber Security, Blockchain, and more are being developed at breathtaking speed. Whether we like it or not, whether we are ready or not, these digital technologies are going to penetrate deeper and deeper into every aspect of our lives. This is going to fundamentally change how we live, how we work, and how we socialize. Java, as a modern high-level programming language, is an excellent tool for helping us to learn these digital technologies, as well as to develop digital applications.

    The aim of this book is to use Java as a tool to help readers to learn these new digital technologies, to demystify these digital technologies, and to be better prepared for the future.

    How This Book Is Organized

    This book is divided into three parts. Part I provides a basic introduction of Java programming language and gets readers started with Java programming. The chapters in Part II provide Java examples of conventional programming topics like console applications, Windows applications, network applications, and mobile applications. All of that is preparation for Part III. These chapters are the core of the book, providing an easy to read guide for the latest digital technologies (IoT, AI, Cyber Security, Blockchain, and Big Data), illustrated with Java programming examples.

    Part I

    Chapter 1: Introduction to Java

    Chapter 2: Getting Started with Java Programming

    Part II

    Chapter 3: Basic Java Programming

    Chapter 4: Java Programming for Windows Applications

    Chapter 5: Java Programming for Networking Applications

    Chapter 6: Java Programming for Mobile Applications

    Part III

    Chapter 7: Java Programming for IoT Applications

    Chapter 8: Java Programming for AI Applications

    Chapter 9: Java Programming for Cybersecurity Applications

    Chapter 10: Java Programming for Blockchain Applications

    Chapter 11: Java Programming for Big Data

    Appendices

    Appendix A: Java Documentation and Archiving Tools and Online Resources

    Appendix B: Apache Maven Tutorial

    Appendix C: Git and GitHub Tutorial

    Downloading the Code Examples

    All the example source code is available on the website that accompanies this book.

    Who This Book Is For

    This book is intended for university/college students, as well as software and electronic hobbyists, researchers, developers, and R&D engineers. It assumes that readers have a basic understanding of computers and a computer's main components such as CPU, RAM, hard drive, network interfaces, and so on. Readers should be able to use a computer competently, performing basic tasks such as switching on and off the computer, logging in and out, running some programs, and copying/moving/deleting files. It also assumes that readers have some basic programming experience, ideally in Java, but optionally also in other languages such as C/C++, Fortran, MatLAB, C#, BASIC, or Python, and that they know the basic syntax, the different types of variables, standard inputs and outputs, the conditional selections, and structures like loops and subroutines. Finally, it assumes that readers have a basic concept of computer networks and the Internet, and can use some of the most commonly used Internet services such as the World Wide Web, email, file download/upload, and online banking/shopping. This book can be used as a core textbook as well as a supplemental textbook.

    What This Book Is Not For

    This book is not for readers just want to learn Java programming language; there are already a lot of good Java programming books on the market. However, in order to make the information here accessible to as many programmers as possible, the first three chapters provide a basic introduction to Java and show how to get started with Java programming; so even if you have never programmed Java before, you can still use the book. If you want to learn all technical details of Java, please refer to the following suggested prerequisite reading list and to Appendix A for more books and resources.

    Suggested Prerequisite Reading

    The following sources will be helpful if you need more background to get up to speed on any of the topics covered in this book.

    Computer Basics

    Absolute Beginner's Guide to Computer Basics, 5th Edition, Michael Miller, QUE, 2009.

    Computers for Beginners (Wikibooks)

    https://en.wikibooks.org/wiki/Computers_for_Beginners

    Java Programming

    Head First Java, 2nd Edition, Kathy Sierra, Bert Bates, O'Reilly Media; 2005.

    Effective Java, 3rd Edition, Joshua Bloch, Addison-Wesley Professional, 2017

    Java: A Beginner's Guide, 6th Edition, Herbert Schildt, McGraw-Hill Education; 2014

    Java: The Complete Reference, 9th Edition, Herbert Schildt, McGraw-Hill Education; 2014.

    Java Programming (Wikibooks)

    https://en.wikibooks.org/wiki/Java_Programming

    Networking and the Internet

    Computing Fundamentals: Digital Literacy Edition, Faithe Wempen with Rosemary Hattersley, Richard Millett, Kate Shoup, ISBN: 978-1-118-97474-2, 2014.

    Understanding Data Communications: From Fundamentals to Networking, 3rd Edition, Gilbert Held, ISBN: 978-0-471-62745-6, 2000.

    Communication Networks (Wikibooks)

    https://en.wikibooks.org/wiki/Communication_Networks

    The Raspberry Pi

    Raspberry Pi For Dummies (For Dummies (Computers)), 3rd Edition, Sean McManus and Mike Cook, John Wiley & Sons, 2017.

    What You Need

    To work through the examples in this book, you will need the following:

    A standard personal computer with minimum 124MB hard drive, 128MB RAM, Pentium 2 266 MHz processor, running Windows operating systems (Vista/7/8/10 and Internet Explorer 9 and above) or Linux operating systems (Ubuntu Linux 12.04 and later, Oracle Linux 5.5 and later, Red Hat Linux 5.5 and later, etc.). You can also use a Mac computer (with Mac OS X 10.8.3 and later, administrator privileges for installation, 64-bit browser).

    Java JDK software (http://www.oracle.com/technetwork/java/javase/downloads/index.html)

    Text editors and Java IDEs (see Chapter 2)

    Raspberry Pi (https://www.raspberrypi.org/) (optional)

    Part I

    In This Part:

    Chapter 1: Introduction to Java

    Chapter 2: Getting Started with Java Programming

    CHAPTER 1

    Introduction to Java

    Experience is simply the name we give our mistakes.

    —Oscar Wilde

    1.1 What Is Java?

    1.2 Versions of Java

    1.3 Java Architecture

    1.4 Editions of Java

    1.5 The Java Spring Framework

    1.6 Advantages and Disadvantages of Java

    1.7 Java Certificates

    1.8 Summary

    1.9 Chapter Review Questions

    1.1 What Is Java?

    Java is a high-level, object-oriented, general-purpose programming language that was originally developed by James Gosling, a Canadian computer scientist, at what was then Sun Microsystems, in the U.S. state of California in 1991. Sun Microsystems was later acquired by Oracle Corporation, also in California, in 2010. Java was a byproduct of Sun's Green project, and it was originally designed as a platform-independent language for programming household electronic appliances. However, Java was too advanced for such applications. Gosling designed Java syntax based on the C and C++ languages, but with fewer low-level facilities. Java was named after the popular Indonesian Java coffee. Java first appeared in 1995, through the HotJava and Netscape web browsers, as a plug-in called Java Applets, which could add dynamic content and interactions to static, pale web pages. Java soon became popular with all the major web browsers incorporating the ability to run Java applets. You've probably seen the famous Java logo, a cup of hot coffee, along with the Java mascot, Duke. Today, after decades of effect, Java has been developed into a fully functional, multipurpose, and powerful language suitable for both individual and enterprise users. Java is different from JavaScript, which is a script language that runs only within a web browser.

    The Java language has five main principles; it was designed to be all of the following:

    Simple, object-oriented, and familiar

    Robust and secure

    Architecture-neutral and portable

    High-performance

    Interpreted, threaded, and dynamic

    The main advantage of Java is its platform independence; that is, programs written in the language can be write once, run anywhere (WORA). This independence is achieved through the concept of the Java Virtual Machine (JVM), illustrated in Figure 1.1. With conventional programming languages like C/C++, to run on different operating systems such as Windows, Mac, and Linux, the C/C++ source file needs to be compiled separately on each operating system. Because each executable file runs in its native operating system, the executable files compiled in one operating system cannot run in another operating system. Java works differently. The Java source code (a .java file) is compiled into Java bytecode (a .class file). The bytecode files are not executable files and cannot run directly in the operating system. Instead, they run in the JVM, which handles the differences between operating systems and presents an identical environment for Java programs to run in. JVM is a novel idea that makes Java platform-independent. The drawback of the JVM is that Java programs run much more slowly than the corresponding C programs; but for most applications, this difference is not noticeable.

    Process diagrams with arrows connecting platforms for conventional compilation process of the C/C++ programming language (top) and Java compilation process on different platforms (bottom).

    Figure 1.1: The conventional compilation process of the C/C++ programming language on different platforms (top) and the Java compilation process on different platforms (bottom)

    Java is one of the most popular programming languages, especially for networking applications. According to Oracle, worldwide there are an estimated 9 million Java developers and about 3 billion devices that run Java.

    1.2 Versions of Java

    Java has had many versions; at the writing of this book, the current version is Java 11; by the time you read this, it will be Java 12. Alpha and Beta were the initial releases of the Java Development Kit (JDK) in 1995. JDK 1.0 was the first official version, released in 1996. Java JDK version 1.2 and newer are generally called Java 2. The collection of Java 2 languages, libraries, and tools is referred to as the Java 2 platform, or Java 2 Standard Edition (J2SE). Similarly, there are Java 5, Java 6, Java 7, and Java 8. The latest Java releases are Java 9 (July 2017), Java 10 (March 2018), Java 11 (September 2018), and Java 12 (March 2019). See Table 1.1 for details.

    Table 1.1: Java Version History

    For Java releases after Java SE 8, Oracle has designated a long-term-support (LTS) release every three years, and in between are non-LTS releases, also called feature releases, every six months. Java SE 9, Java SE 10, and Java 12 are all non-LTS releases, and Java SE 8 and Java SE 11 are LTS releases. Java end of life (EOL) occurs when the Java release is no longer publicly supported by Oracle. For the non-LTS releases, the EOL is the date of the next new release, and all the public support will be superseded. But for the LTS releases, the EOL is much longer, and customers will continue to get public support even after the new releases. That is why the widely used Java SE 8 has a much longer EOL than other releases. The next planned LTS release will be Java SE 17. This book is focused on the application of Java; the Java example codes used in this book will not be affected by the future Java releases.

    For more information about Java releases and support road map, please visit the following:

    https://www.oracle.com/technetwork/java/java-se-support-roadmap.html

    https://en.wikipedia.org/wiki/Java_version_history

    Each Java release is distributed as two different packages.

    The Java Runtime Environment (JRE) is for running Java programs and is intended for end users. The JRE consists of the JVM and runtime libraries. You can use the JRE when you don't need to compile the Java program.

    The Java Development Kit (JDK) is for software developers to compile, debug, and document Java programs. You will need to use the JDK in this book, as you will need to compile your Java programs.

    1.3 Java Architecture

    Figure 1.2 shows the relationship between the JDK, JRE, and JVM in the Java architecture. The JDK includes the JRE and Java development tools, and the JRE includes the JVM and library classes, as well as other files. Inside the JVM, there is a just-in-time (JIT) compiler, which compiles Java bytecode to native machine code during the execution of a Java program, that is, at run time. JIT improves the performance of Java applications.

    Schematic diagram depicting relationship between the JDK, JRE, and JVM in Java architecture with JDK (superset); JVM, JIT, Library Classes, and Other Files in JRE and Development Tools.

    Figure 1.2: The relationship between the JDK, JRE, and JVM in Java architecture

    Figure 1.3 shows a more detailed version of Java architecture; this was re-created from the original Oracle Java architecture diagram found here:

    Image described by caption and surrounding text.

    Figure 1.3: A conceptual diagram of the Java architecture

    https://www.oracle.com/technetwork/java/javase/tech/index.html

    1.4 Editions of Java

    There are four Java platform editions.

    Java Card for smartcards

    Java ME (Micro Edition) for mobile devices

    Java SE (Standard Edition) for standard personal computers

    Java EE (Enterprise Edition) for large distributed enterprise or Internet environments

    Java SE is what most people use for Java programming. This edition comes with the complete Java Class Library, which includes the basic types and objects, networking, security, databases, and the classic Swing graphical user interface (GUI) toolkit. Most versions also include the modern JavaFX toolkit, which is intended to replace the Swing GUI toolkit; however, starting with Java SE 11, the JavaFX toolkit is no longer included in the Java SDK and is redesigned as a separate, stand-alone library. This book will be mainly focused on Java SE.

    1.5 The Java Spring Framework

    Java Spring is the most popular development framework for creating Java enterprise applications. Java Spring is an open source framework. Initially written by Rod Johnson, it was released under the Apache 2.0 license in June 2003. One of the main advantages of the Spring framework is its layered architecture, which allows developers to select which of its components to use. Figure 1.4 shows the home page of the Java Spring Framework (https://spring.io/). Figure 1.5 shows the Guides page for the Framework (https://spring.io/guides).

    Image described by caption and surrounding text.

    Figure 1.4: The home page of the Java Spring Framework

    Image described by caption and surrounding text.

    Figure 1.5: The Guides page in the Java Spring Framework

    There are also several good Java Spring framework tutorials online.

    https://www.tutorialspoint.com/spring/spring_overview.htm

    https://howtodoinjava.com/spring-5-tutorial/

    https://java2blog.com/introduction-to-spring-framework/

    1.6 Advantages and Disadvantages of Java

    I've already noted some of Java's advantages, but it also has a few disadvantages that may affect your choice of a development language. This section provides a quick summary of both. Many items are the topics of chapters or sections later in this book.

    1.6.1 Advantages

    These are the advantages:

    Free Cost Java is free to use, even for commercial applications, although you do need to pay for security and certain updates.

    Simplicity Java is much easier to learn and to use than other programming languages. Java also uses automatic memory allocation and garbage collection.

    Platform Independence Once compiled, Java programs can run on any operating system, thanks to the JVM.

    Object Orientation Java is a fully object-oriented programming language that allows you to create reusable Java modules (classes). Chapter 3 introduces Java's object orientation.

    Security Java is designed to be secure and safe. See Chapter 9 for information about security.

    Multithreading With Java, you can easily develop multithreaded programs that run several tasks simultaneously. Chapter 3 also introduces multithreaded programming.

    Networking Java provides a range of functions to make it easier to develop networking applications. Chapter 5 covers developing networking apps.

    Mobile Development With Java, you can develop mobile applications, called apps, on Android systems. Chapter 6 covers developing apps for mobile devices.

    Enterprise Development With Java, you can develop many enterprise applications, such as web servers and other application servers.

    1.6.2 Disadvantages

    These are the disadvantages:

    Performance Java is much slower than other natively compiled languages, such as C or C++, because of the use of the JVM. Java also takes more memory space and has limited options for latency critical tuning.

    GUI Development Generally speaking, it is not easy to develop GUI programs with Java, and the look and feel of the Java Swing toolkit is very different from native Windows, Mac, and Linux applications, although there are significant improvements in the JavaFX GUI toolkit. Chapter 4 shows how to overcome the difficulties and develop GUI apps using Java Swing and JavaFX.

    1.7 Java Certification

    Oracle offers a range of Java certificates, which can be generally divided into two levels, Associate and Professional, as shown in Figure 1.6 (https://education.oracle.com/pls/web_prod-plq-dad/ou_product_category.getPageCert?p_cat_id=267). You can start by applying for Java Foundations Certified Junior Associate, then move on to Oracle Certified Associate, and finally become an Oracle Certified Professional. Different Java versions require their own certificates. For example, there are separate certifications for Java SE 7 Programmer and Java SE 8 Programmer. Certificates for newer Java versions will continue to be introduced.

    Screen capture depicting Associate and Professional in the Java SE page with View button under each certification.

    Figure 1.6: The Oracle Java Certification path

    1.8 Summary

    This chapter introduced the Java programming language, including its history, versions, and the four Java platform editions. It also introduced the popular Java Spring Framework for enterprise Java application development, summarized Java's advantages and disadvantages, and finally provided information about Java certification.

    1.9 Chapter Review Questions

    CHAPTER 2

    Getting Started with Java Programming

    If a craftsman wants to do good work, he must first sharpen his tools.

    —Ancient Chinese proverb

    2.1 Downloading and Installing Java

    2.2 Java IDEs

    2.3 Java Hello World Program

    2.4 Java Online Compilers

    2.5 Java Online Code Converter

    2.6 Java Free Online Courses and Tutorials

    2.7 Java Version Control

    2.8 Summary

    2.9 Chapter Review Questions

    2.1 Downloading and Installing Java

    Let's get started with Java programming. First, you will need to download the latest Java software from the Oracle web site, as shown in Figure 2.1 (www.oracle.com/technetwork/java/javase/downloads/index.html). There are three download choices: JDK, Server JRE, and JRE. The server JRE and JRE are for running the Java programs only; since you will need to compile Java programs in this book, you need to download the JDK software package. Just follow the instructions on the web site to download and install the Java SE JDK on your computer.

    Image described by caption and surrounding text.

    Figure 2.1: The Java SE download web site

    After installation, you will need to set up a few environmental variables, such as PATH and JAVA_HOME. Environment variables help programs know which folder to install files in, where to store temporary files, where to find user profile settings, and so on. To set up the environmental variables in Windows, go to Control Panel ➪ Advanced System Settings ➪ Environment Variables. In the section System Variables, find the PATH environment variable and double-click it to edit it. For example, if your Java JDK installation directory is C:\Program Files\Java\jdk-10.0.2\bin, then you just append a semicolon to the end, as in C:\Program Files\Java\jdk-10.0.2\bin;. The semicolon here is used to separate different PATH variables. Also in the section System Variables, find the JAVA_HOME environment variable and double-click it to edit it. Change its value to C:\Program Files\Java\jdk-10.0.2, as shown in Figure 2.2.

    Image described by caption and surrounding text.

    Figure 2.2: The Advanced tab of the System Properties window (top left); the Environmental Variables window (top right); and setting up the PATH (bottom, left) and JAVA_HOME (bottom right) variables

    For more details of setting up Java environment variables, please visit https://www.java.com/en/download/help/path.xml.

    To set up Java environmental variables in Linux/Unix and macOS, you will need to edit your shell startup scripts. For example, if your Java JDK is installed in the /usr/local/jdk-10.0.2/ directory, in the Bash shell, you can edit the startup file (~/.bashrc) like this:

    JAVA_HOME = /usr/local/jdk-10.0.2/ export JAVA_HOME PATH=/usr/local/jdk-10.0.2/bin:$PATH export PATH

    Save and close the file, and type in the following command to load the startup file:

    ./.profile

    If you find Java installation difficult, Figure 2.3 shows an intuitive and simple Java installation guide from Nanyang Technological University in Singapore (www.ntu.edu.sg/home/ehchua/programming/howto/JDK_HowTo.html).

    Image described by caption and surrounding text.

    Figure 2.3: An intuitive and simple Java SE installation guide from Nanyang Technological University in Singapore

    Once the Java JDK has been installed properly, you can check the Java version from the Windows command prompt by typing java –version, as shown in Figure 2.4 (top). You can invoke the command prompt in Windows by choosing Start ➪ Run and typing cmd. You can also check the Java compiler by typing javac, as shown in Figure 2.4 (bottom).

    Image described by caption and surrounding text.

    Figure 2.4: The commands to check the Java version (top) and to test the Java compiler (bottom)

    2.2 Java IDEs

    As you do with any programming language, to write a Java program you need a good integrated development environment (IDE), which can make programming much easier. There are many good options, and here I will introduce a few popular ones.

    The first is Notepad++, shown in Figure 2.5. Notepad++ is an open source text editor that supports many programming languages, including Java. It supports opening multiple Java files, color-coding the Java keywords, and displaying line numbers. It also supports code folding. Text editors are lightweight IDEs and do not have all the functions of full IDEs. But they are much simpler to use.

    Image described by caption and surrounding text.

    Figure 2.5: The Notepad++ text editor

    The following is the download link for Notepad++:

    https://notepad-plus-plus.org/download/v7.5.8.html

    Other popular text editors include the following:

    Textpad (https://www.textpad.com/), Windows operating systems

    Sublime Text (https://www.sublimetext.com/), Windows and Linux operating systems

    Another option is IntelliJ IDEA, which is a fully functional, powerful, dedicated Java IDE. IntelliJ IDEA is also relatively easy to use; see Figure 2.6. Compared to text editors, IntelliJ IDEA comes with many powerful features, such as smart completion, data flow analysis, language injection, inspections, and quick fixes. It also has many built-in developer tools, such as version control, a decompiler, an application server, and build tools such as Maven, Gradle, Ant, and others.

    Image described by caption and surrounding text.

    Figure 2.6: The IntelliJ IDEA Java IDE

    The following is the download link for IntelliJ IDEA:

    https://www.jetbrains.com/idea/

    Another option is Eclipse, which is a powerful IDE that supports Java and many other programming languages such as C and C++. But for beginners, Eclipse might look daunting, with its complex user interface and sophisticated configurations, as shown in Figure 2.7.

    Image described by caption and surrounding text.

    Figure 2.7: The Eclipse Java IDE

    The following is the download link for Eclipse:

    www.eclipse.org/downloads/packages/release/mars/r/eclipse-ide-java-developers

    Last but not least, another option is NetBeans, which can be downloaded with the JDK as a bundle. So, you will not need to download the JDK software separately. NetBeans also supports drag-and-drop graphical user interface (GUI) components, which make it easier to develop Windows applications; see Figure 2.8.

    Image described by caption and surrounding text.

    Figure 2.8: The NetBeans Java IDE

    The following is the download link for NetBeans:

    https://netbeans.apache.org/download/index.html

    For beginners, I recommend starting with a text editor, such as Notepad++, as you gain more and more experience and then moving on to a Java IDE.

    2.3 Java Hello World Program

    A computer program is essentially a collection of statements (instructions) that can be executed by a computer automatically. To create a Java program, first you need to create the Java source code, which is a text file with .java as the extension name. The source code contains statements, which are lines of code that perform some actions. Second, you need to compile the source code into bytecode, a machine-understandable binary file with .class as the extension name, which can then be executed within Java runtime environments (JREs). Java's platform independence is achieved by installing the JRE on every operating system. The JRE handles the differences of various operating systems and provides a common platform for the bytecode to run. Therefore, the same bytecode can run on all JREs and give you the same result, irrespective of what the underlying operating system is.

    Example 2.1 shows a simple Java program named HelloWorld.java, which just prints Hello World! to your screen.

    EXAMPLE 2.1 HELLOWORLD.JAVA

    ======================================================================== //Example 2.1 public class HelloWorld {     public static void main(String[] args) {         System.out.println(Hello World!);     } } ========================================================================

    In Java, every program consists of at least one class, and the source code file name (including the case) should be exactly the same as the class name, or it should be the name of the main class when there is more than one class.

    Like C/C++, Java uses braces, { and }, to group blocks of codes, and it uses // to mark single-line comments and /* */ for multiple-line comments. Here's an example of a single-line comment:

    //This is a single line comment

    Here's an example of a multiple-line comment:

    /*   This is a multiple line comment   Where you can write multiple lines */

    Every Java application program (except for Java applets) has one, and only one, public static void main(String [] args) method. For the main() method, the keyword public means it is accessible to other Java classes, static means it can be invoked without using any object, void means it does not return any value, and args is a String type array variable, which saves all the parameters typed at the command line. The main() method should always be static and public. In Java, each line that ends with a semicolon (;) is called a statement; that is, it's an action that

    Enjoying the preview?
    Page 1 of 1