Java Programming 24-Hour Trainer, Second Edition
Total Page:16
File Type:pdf, Size:1020Kb
11 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 intro‐ duced 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 James Gosling from Sun Microsystems (acquired by Oracle Corporation in 2010). The goal was to provide a simpler and platform-independent alter‐ native to C++. Java programs run inside the Java Virtual Machine (JVM), which is the same on every platform from the application programmer’s perspective. You find out what platform independence means a little later, in the section “The Life Cycle of a Java Program”. For now, let’s look at some of the reasons why Java can be your language of choice. Java is a general-purpose programming language that’s used in all industries for almost any type of application. If you master it, your chancesCOPYRIGHTED of getting employed as a software MATERIAL 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 you’ll 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 projects—if you decide to leave the project for whatever reason, it’s 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 2 | LESSON 1 INTRODUCING JAVA 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 language is object-oriented (OO), which enables you to easily relate program constructs to objects from the real world (more on this in Chapter 3-Chapter 7). On the other hand, recently added lambda expressions (see Chapter 14) 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 envi‐ ronment. 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 you’ll 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 Edition—it 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 it’s 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 you’ll 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). JDK and JRE | 3 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 computer’s 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). JDK AND JRE If you are planning to use a specific computer to develop Java programs, you need to download and install JDK. If you are planning to use this computer only to run Java programs that were compiled somewhere else, you just need the Java Runtime Environment (JRE). If you have JDK installed on your machine, it includes JRE. Java’s platform independence comes from the fact that your Java program doesn’t know under which operating system (OS) or on which hardware it’s being executed. It operates inside the pre-installed JRE. You need to get familiar with two more terms: Java SE (Standard Edition) and Java EE (Enterprise Edition). The latter includes the server-side tools and libraries that you get familiar with starting in Chapter 25. DOWNLOADING AND INSTALLING JAVA SE Start with downloading the latest version of the JDK SE Development Kit, which at the time of this writing is Java SE 8. Download and install JDK for your platform from Oracle’s Java SE Downloads site: http://goo.gl/ X68FzJ. In some literature you see references like JDK 1.8, which is the same as JDK 8. The number 8 is followed by the letter u and a number. For example, JDK 8u5 means that Oracle has released an update number 5 for JDK 8. Installing JDK 8 for MAC OS Download the dmg file marked as MAC OS X x64. Running this program on a Mac OS X computer installs Java in /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk. MAC OS X is my platform of choice, but Java works practically the same on all platforms, and all the book examples work under Windows and Linux as well. Open the Terminal window and enter java –version. You should see an output similar to this one: 4 | LESSON 1 INTRODUCING JAVA FIGURE 1-1: Checking the Java version in MAC OS Installing JDK 8 in Windows Select and download the Windows x86 executable file, which is a Java version for 32-bit computers. This version is perfectly fine to start working with Java. Run this file, click the Next button two or three times, and then click Close. In about a minute the installation is complete. By default JDK is installed in the directory named something like c:\Program Files\Java\jdk1.8.0_05. This is the place where both JDK and JRE are installed. Now you need to add the bin folder from your java installation directory to the environment variable PATH of your Windows OS. Click Start, Control Panel and search for the environment variables. Click the Edit the System Environment Variables link and press the Environment Variables button. Edit the system variable PATH (if you don’t have one, create it) to include the path to c:\Program Files\Java\jdk1.8.0_05\bin unless you have it in a different location.