Java Fundamentals
eBook - ePub

Java Fundamentals

A fast-paced and pragmatic introduction to one of the world's most popular programming languages

Gazihan Alankus, Rogério Theodoro de Brito, Basheer Ahamed Fazal, Vinicius Isola, Miles Obare

Share book
  1. 408 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Java Fundamentals

A fast-paced and pragmatic introduction to one of the world's most popular programming languages

Gazihan Alankus, Rogério Theodoro de Brito, Basheer Ahamed Fazal, Vinicius Isola, Miles Obare

Book details
Book preview
Table of contents
Citations

About This Book

Enhance your career options with this well-crafted object-oriented programming language that enjoys the support of an enormous ecosystem of tools and libraries

Key Features

  • Get introduced to Java, its features, and its ecosystem
  • Understand how Java uses object-oriented programming
  • Become an expert Java exception handler

Book Description

Since its inception, Java has stormed the programming world. Its features and functionalities provide developers with the tools needed to write robust cross-platform applications. Java Fundamentals introduces you to these tools and functionalities that will enable you to create Java programs.

The book begins with an introduction to the language, its philosophy, and evolution over time, until the latest release. You'll learn how the javac/java tools work and what Java packages are - the way a Java program is usually organized. Once you are comfortable with this, you'll be introduced to advanced concepts of the language, such as control flow keywords. You'll explore object-oriented programming and the part it plays in making Java what it is. In the concluding chapters, you'll get to grips with classes, typecasting, and interfaces, and understand the use of data structures, arrays, strings, handling exceptions, and creating generics.

By the end of this book, you will have learned to write programs, automate tasks, and follow advanced courses on algorithms and data structures or explore more advanced Java courses.

What you will learn

  • Create and run Java programs
  • Use data types, data structures, and control flow in your code
  • Implement best practices while creating objects
  • Work with constructors and inheritance
  • Understand advanced data structures to organize and store data
  • Employ generics for stronger check-types during compilation
  • Learn to handle exceptions in your code

Who this book is for

Java Fundamentals is designed for tech enthusiasts who are familiar with some programming languages and want a quick introduction to the most important principles of Java.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Java Fundamentals an online PDF/ePUB?
Yes, you can access Java Fundamentals by Gazihan Alankus, Rogério Theodoro de Brito, Basheer Ahamed Fazal, Vinicius Isola, Miles Obare in PDF and/or ePUB format, as well as other popular books in Informatica & Programmazione in Java. We have over one million books available in our catalogue for you to explore.

Information

Year
2019
ISBN
9781789802955

Chapter 1

Introduction to Java

Learning Objectives

By the end of this lesson, you'll be able to:
  • Describe the working of the Java ecosystem
  • Write simple Java programs
  • Read input from the users
  • Utilize classes in the java.util package

Introduction

In this first lesson, we are embarking on our study of Java. If you are coming to Java from a background of working with another programming language, you probably know that Java is a language for programming computers. But Java goes beyond just that. It's more than a very popular and successful language that is virtually present everywhere, it is a collection of technologies. Besides the language, it encompasses a very rich ecosystem and it has a vibrant community working on many facets to make the ecosystem as dynamic as it can be.

The Java Ecosystem

The three most basic parts of the Java ecosystem are the Java Virtual Machine (JVM), the Java Runtime Environment (JRE), and the Java Development Kit (JDK), which are stock parts that are supplied by Java implementations.
Figure 1.1: A representation of the Java ecoystem
Figure 1.1: A representation of the Java ecosystem
Every Java program runs under the control of a JVM. Every time you run a Java program, an instance of JVM is created. It provides security and isolation for the Java program that is running. It prevents the running of the code from clashing with other programs within the system. It works like a non-strict sandbox, making it safe to serve resources, even in hostile environments such as the internet, but allowing interoperability with the computer on which it runs. In simpler terms, JVM acts as a computer inside a computer, which is meant specifically for running Java programs.

Note

It is common for servers to have many JVMs in execution simultaneously.
Up in the hierarchy of stock Java technologies is the JRE. The JRE is a collection of programs that contains the JVM and also many libraries/class files that are needed for the execution of programs on the JVM (via the java command). It includes all the base Java classes (the runtime) as well as the libraries for interaction with the host system (such as font management, communication with the graphical system, the ability to play sounds, and plugins for the execution of Java applets in the browser) and utilities (such as the Nashorn JavaScript interpreter and the keytool cryptographic manipulation tool). As stated before, the JRE includes the JVM.
At the top layer of stock Java technologies is the JDK. The JDK contains all the programs that are needed to develop Java programs, and it's most important part is the Java Compiler (javac). The JDK also includes many auxiliary tools such as a Java disassembler (javap), a utility to create packages of Java applications (jar), system to generate documentation from source code (javadoc), among many other utilities. The JDK is a superset of the JRE, meaning that if you have the JDK, then you also have the JRE (and the JVM).
But those three parts are not the entirety of Java. The ecosystem of Java includes a very large participation of the community, which is one of the reasons for the popularity of the platform.

Note

Research into the most popular Java libraries that are used by the top Java projects on GitHub (according to research that has been repeated in 2016 and 2017) showed that JUnit, Mockito, Google's Guava, logging libraries (log4j, sl4j), and all of Apache Commons (Commons IO, Commons Lang, Commons Math, and so on), marked their presence, together with libraries to connect to databases, libraries for data analysis and machine learning, distributed computing, and almost anything else that you can imagine. In other words, for almost any use that you want to write programs to, there are high chances of an existing library of tools to help you with your task.
Besides the numerous libraries that extend the functionality of the stock distributions of Java, there is a myriad of tools to automate builds (for example, Apache Ant, Apache Maven, and Gradle), automate tests, distribution and continuous integration/delivery programs (for example, Jenkins and Apache Continuum), and much, much more.

Our First Java Application

As we briefly hinted before, programs in Java are written in source code (which are plain text, human-readable files) that is processed by a compiler (in the case of Java, javac) to produce the Java bytecode in class files. The class files containing Java bytecode are, then, fed to a program called java, which contains the Java interpreter/JVM that executes the program that we wrote:
Figure 1.2: The process of compilation in Java
Figure 1.2: The process of compilation in Java

Syntax of a Simple Java Program

Like all programming languages, the source code in Java must follow particular syntaxes. Only then, will the program compile and provide accurate results. Since Java is an object-oriented programming language, everything in Java is enclosed within classes. A simple Java program looks similar to this:
public class Test { //line 1
public static void main(String[] args) { //line 2
System.out.println("Test"); //line 3
} //line 4
} //line 5
Every java program file should have the same name as that of the class that contains main (). It is the entry point into the Java program.
Therefore the preceding program will compile and run without any errors only when these instructions are stored in a file called Test.java.
Another key feature of Java is that it is case-sensitive. This implies that System.out.Println will throw an error as it is not capitalized correctly. The correct instruction should be System.out.println.
main() should always be declared as shown in the sample. This is because, if main() is not a public method, it will not be accessed by the compiler, and the java program will not run. The reason main() is static is because we do not call it using any object, like you would for all other regular methods in Java.

Note

We will discuss these the public and static keywords later in this book, in greater depth.
Comments are used to provide some additional information. The Java compiler ignores these comments.
Single line comments are denoted by // and multiline comments are denoted by /* */.

Exercise 1: A Simple ...

Table of contents