First of all I want to thank my family for understanding that stealing (once again) time from family to write a computer book is OK.
Id also like to thank the technical editors, Rajesuwer P. Singaravelu and Martijn Verberg, for their valuable input.
My special thanks to Chad Darby for producing the awesome videos for this book.
I give particular thanks to my business partners and colleagues at Farata Systems. They didnt contribute to this book directly, but working in the same team with these top notch professionals makes me a better programmer day in and day out.
Big thanks to the Wiley editors for doing a great job of editing and for not cursing me for not meeting deadlines.
Lesson 1
Introducing Java
During the last two decades Java has maintained its status as one of the most popular programming languages for everything from programming games to creating mission-critical applications, such as those for trading on Wall Street or controlling Mars rovers. For the current popularity chart see the Tiobe Index at http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
. In this lesson you are introduced to some of the very basic Java terms. You also download and install the Java Development Kit (JDK) and compile your first program.
Why Learn Java?
The Java programming language was originally created in 1995 by . For now, lets look at some of the reasons why Java can be your language of choice.
Java is a general-purpose programming language thats used in all industries for almost any type of application. If you master it, your chances of getting employed as a software developer will be higher than if you specialize in some domain-specific programming languages.
There are more than nine million professional Java developers in the world, and the majority of them are ready to share their knowledge by posting blogs and articles or simply answering technical questions online. If you get stuck solving some problem in Java, the chances are very high that youll find the solution on the Internet.
Because the pool of Java developers is huge, project managers of large and small corporations like to use Java for the development of new projectsif you decide to leave the project for whatever reason, its not too difficult to find another Java programmer to replace you. This would not be the case if the project were being developed in a powerful, but less popular language, such as Scala. At this point you may ask, Does that also mean that my Java skills will be easily replaceable? It depends on you. To improve your value and employability, you need to master not only the syntax of the language but also the right set of Java-related technologies that are in demand (you learn them in this book in the Java EE section).
Not only is Java open-source, but there are thousands and thousands of open-source projects being developed in Java. Joining one of these projects is the best way to get familiar with the process of project development and secure your very first job without having any prior real-world experience as a programmer.
The Java ) allow you to program in Java in a functional style.
The IT world is changing and people often use more than one language in the same project. Java is not the only language that runs in JVM. Such languages as Scala, Groovy, Clojure, JavaScript and others also run on JVM. So being familiar with the JVM opens the doors to being a polyglot programmer within the same operating environment.
The server-side applications that are deployed in the JVM scale well. The processing of thousands of users requests can be arranged in parallel by splitting the job between rather inexpensive servers in a cluster.
Java as a development platform has many advantages over other environments, which makes it the right choice for many projects, and youll have a chance to see this for yourself while reading this book, watching the screencasts from the accompanying DVD, and deploying all code samples from the book on your computer.
Setting the Goals
The goal of this rather slim tutorial is to give you just enough information about most of the Java language elements, techniques, and technologies that are currently being used in the real world. The first 25 lessons of the book are about the Java Standard Edition, whereas the remaining part is about Java Enterprise Editionit covers server-side Java technologies, and this is where Java shines in the enterprise world.
The brevity of some of the lessons may make you wonder if its even possible to explain a subject in just 10 pages when there are whole books devoted for the same topic. My approach is to cover just enough for you to understand the concept, important terms, and best practices. Prerecorded screencasts on the DVD help you to repeat the techniques explained in the lesson on your own.
There are plenty of additional materials online that help you to study any specific topic more deeply. But youll get a working and practical knowledge about Java just by using the materials included with this book.
The goal of this book is not just to get you familiar with the syntax of the Java language, but to give you practical Java skills that will enable you to develop business applications either on your own or by working as a team member in a larger-scale project.
The Life Cycle of a Java Program
There are different types of programming languages. In some of them you write the text of the program (aka the source code) and can execute this program right away. These are interpreted languages (for example, JavaScript).
But Java requires the source code of your program to be compiled first. It gets converted to a bytecode that is run by Java Virtual Machine, which may turn some of it into a platform-specific machine code using the so-called Just-In-Time (JIT) compiler.
Not only will the program be checked for syntax errors by a Java compiler, but other libraries of Java code can be added (linked) to your program after the compilation is complete (deployment stage). There are plenty of readily available libraries of reusable components, and a vast majority of them are free of charge.
In this lesson you start with writing a very basic Java program that outputs the words Hello World on your computers monitor.
Technically you can write the source code of your Java program in any plain text editor that you prefer (Notepad, TextEdit, Sublime Text, vi, and so on), but to compile your program you need additional tools and code libraries that are included in the Java Development Kit (JDK).