Welcome to Java
This chapter is a gentle introduction to the world of Java. In the next few pages, you find out what Java is, where it came from, and where it’s going. You also discover some of the unique strengths of Java, as well as some of its weaknesses. Also, you see how Java compares with other popular programming languages such as C, C++, and C#
By the way, I assume in this chapter that you have at least enough background to know what computer programming is all about. That doesn’t mean that I assume you’re an expert or professional programmer. It just means that I don’t take the time to explain such basics as what a computer program is, what a programming language is, and so on. If you have absolutely no programming experience, I sug gest that you pick up a copy of Java For Dummies, 7th Edition, or Beginning Pro gramming with Java For Dummies, 5th Edition, both by Barry Burd (Wiley).
Throughout this chapter, you find little snippets of Java program code, plus a few snippets of code written in other languages, including C, C++, and Basic. If you don’t have a clue what this code means or does, don’t panic. I just want to give you a feel for what Java programming looks like and how it compares with program ming in other languages
All the code listings used in this book are available for download at www.dummies. com/go/javaaiofd6e.
What Is Java, and Why Is It So Great?
Java is a programming language in the tradition of C and C++. As a result, if you have any experience with C or C++, you’ll often find yourself in familiar territory as you discover the various features of Java. (For more information about the sim ilarities and differences between Java and C or C++, see the section “Java versus Other Languages,” later in this chapter.)
Java differs from other programming languages in a couple of significant ways, however. I point out the most important differences in the following sections.
Platform independence
One of the main reasons Java is so popular is its platform independence, which sim ply means that Java programs can be run on many types of computers.
Before Java, other programming languages promised platform independence by providing compatible compilers for different platforms. (A compiler is the program that translates programs written in a programming language into a form that can actually run on a computer.) The idea was that you could compile different versions of the programs for each platform. Unfortunately, this idea never really worked. The compilers were never identical on each platform; each had its own little nuances. As a result, you had to maintain a different version of your program for each platform you wanted to support
Java’s platform independence isn’t based on providing compatible compilers for different platforms. Instead, Java is based on the concept of a virtual machine called the Java Virtual Machine (JVM). Think of the JVM as a hypothetical computer platform — a design for a computer that doesn’t exist as actual hardware. Instead, the JVM simulates the operation of a hypothetical computer that is designed to run Java programs.
The Java compiler doesn’t translate Java into the machine language of the com puter that the program is running on. Instead, the compiler translates Java into the machine language of the JVM, which is called bytecode. Then the JVM runs the bytecode in the JVM.
When you compile a Java program, the runtime environment that simulates the JVM for the targeted computer type (Windows, Linux, macOS, and so on) is included with your compiled Java programs
That’s how Java provides platform independence — and believe it or not, it works pretty well. The programs you write run just as well on a PC running any version
of Windows, a Macintosh, a Unix or Linux machine, or any other computer that has a compatible JVM — including smartphones or tablet computers
While you lie awake tonight pondering the significance of Java’s platform inde pendence, here are a few additional thoughts to ponder:
Platform independence goes only so far. If you have some obscure type of computer system — such as an antique Olivetti Programma 101 — and a JVM runtime environment isn’t available for it, you can’t run Java programs on it.
I didn’t make up the Olivetti Programma 101. It was a desktop computer made in the early 1960s, and it happened to be my introduction to computer programming. (My junior high school math teacher had one in the back of his classroom, and he let me play with it during lunch.) Do a Google search for “Olivetti Programma 101” and you can find several interesting websites about it
Java’s platform independence isn’t perfect. Although the bytecode runs identically on every computer that has a JVM, some parts of Java use services provided by the underlying operating system. As a result, minor variations sometimes crop up, especially in applications that use graphical interfaces.
Because a runtime system that emulates a JVM executes Java bytecode, some people mistakenly compare Java with interpreted languages such as Basic or Perl. Those languages aren’t compiled at all, however. Instead, the interpreter reads and interprets each statement as it is executed. Java is a true compiled language; it’s just compiled to the machine language of JVM rather than to the machine language of an actual computer platform.
» If you’re interested, the JVM is completely stack-oriented; it has no registers for storing local data. (I’m not going to explain what that term means, so if it doesn’t make sense to you, skip it. It’s not important. It’s just interesting to nerds who know about stacks, registers, and things of that ilk.)
Object orientation
Java is inherently object-oriented, which means that Java programs are made up from programming elements called objects. Simply put (don’t you love it when you read that in a computer book?), an object is a programming entity that repre sents either some real-world object or an abstract concept
All objects have two basic characteristics
Objects also have behavior, which means that they can perform certain tasks. In Java, these tasks are called methods. An object that represents a car might have methods such as start, stop, drive, and crash. Some methods simply allow you to access the object’s data. A book object might have a getTitle method that tells you the book’s title
Classes are closely related to objects. A class is the program code you write to cre ate objects. The class describes the data and methods that define the object’s state and behavior. When the program executes, classes are used to create objects.
Suppose you’re writing a payroll program. This program probably needs objects to represent the company’s employees. So the program includes a class (probably named Employee) that defines the data and methods for each Employee object. When your program runs, it uses this class to create an object for each of your company’s employees.
The Java API
The Java language itself is very simple, but Java comes with a library of classes that provide commonly used utility functions that most Java programs can’t do without. This class library, called the Java API (short for application programming interface), is as much a part of Java as the language itself. In fact, the real chal lenge of finding out how to use Java isn’t mastering the language; it’s mastering the API. The Java language has only about 50 keywords, but the Java API has sev eral thousand classes, with tens of thousands of methods that you can use in your programs
The Java API has classes that let you do trigonometry, write data to files, cre ate windows onscreen, and retrieve information from a database, among other things. Many of the classes in the API are general purpose and commonly used. A whole series of classes stores collections of data, for example. But many are obscure, used only in special situations.
Fortunately, you don’t have to learn anywhere near all of the Java API. Most pro grammers are fluent with only a small portion of it: the portion that applies most directly to the types of programs they write. If you find a need to use some class from the API that you aren’t yet familiar with, you can look up what the class does in the Java API documentation at http://docs.oracle.com/en/java/javase/14.
The Internet
Java is often associated with the Internet, and rightfully so, because Al Gore invented Java just a few days after he invented the Internet. Okay, Java wasn’t really invented by Al Gore. It was developed right at the time the World Wide Web was becoming a phenomenon, and Java was specifically designed to take advan tage of the web. In particular, the whole concept behind the JVM is to enable any computer connected to the Internet to run Java programs, regardless of the type of computer or the operating system it runs.
Most Java programming on the Internet uses servlets, which are web-based Java programs that run on an Internet server computer rather than in an Internet user’s web browser.
A servlet generates a page of HTML and JavaScript that is sent to a user’s com puter to be displayed in the user’s web browser. If you request information about a product from an online store, the store’s web server runs a servlet to generate the HTML page containing the product information you requested
Java versus Other Languages
Superficially, Java looks a lot like many of the programming languages that pre ceded it, most notably C and C++. For example, here’s the classic Hello, World! program, written in the C programming language
main() { printf("Hello, World!"); }
This program simply displays the text "Hello, World!" on the computer’s con sole. Here’s the classic Hello, World! program written in Java:
Although the Java version is a bit more verbose, the two have several similarities
Objects have data, also known as state. An object that represents a book, for example, has data such as the book’s title, author, and publisher
This program simply displays the text "Hello, World!" on the computer’s con sole. Here’s the classic Hello, World! program written in Java
Although the Java version is a bit more verbose, the two have several similarities
Both require each executable statement to end with a semicolon (;)
Both use braces ({}) to mark blocks of code.
Both use a routine called main as the main entry point for the program. 1
Many other similarities aren’t evident in these simple examples, but the examples bring the major difference between C and Java front and center: Object-oriented programming rears its ugly head even in simple examples. Consider the following points:
In Java, even the simplest program is a class, so you have to provide a line that declares the name of the class. In this example, the class is named HelloApp. HelloApp has a method named main, which the JVM automatically calls when a program is run.
In the C example, printf is a library function you call to print information to the console. In Java, you use the PrintStream class to write information to the console.
PrintStream? There’s no PrintStream in this program! Wait a minute — yes, there is. Every Java program has available to it a PrintStream object that writes information to the console. You can get this PrintStream object by calling the out method of another class, named System. Thus, System.out gets the PrintStream object that writes to the console. The PrintStream class in turn has a method named println that writes a line to the console. So System.out.println really does two things, in the following order:
1. It uses the out field of the System class to get a PrintStream object.
2. It calls the println method of that object to write a line to the console. Confusing? You bet. Everything will make sense, however, when you readabout object-oriented programming in Book 3, Chapter 1.
void looks familiar. Although it isn’t shown in the C example, you could have coded void on the main function declaration to indicate that the main function doesn’t return a value. void has the same meaning in Java. But static? What does that mean? That, too, is evidence of Java’s object orienta tion. It’s a bit early to explain what it means in this chapter, but you can find out in Book 2, Chapter 7.
Important Features of the Java Language
If you believe the marketing hype put out by Oracle and others, you think that Java is the best thing to happen to computers since the invention of memory. Java may not be that revolutionary, but it does have many built-in features that set it apart from other languages. The following sections describe just three of the many fea tures that make Java so popular
Type checking
All programming languages must deal in one way or the other with type checking — the way that a language handles variables that store different types of data. Num bers, strings, and dates, for example, are commonly used data types available in most programming languages. Most programming languages also have several types of numbers, such as integers and real numbers.
All languages must check data types, so make sure that you don’t try to do things that don’t make sense (such as multiplying the gross national product by your last name). The question is, does the language require you to declare every variable’s type so you can do type checking when it compiles your programs, or does the language do type checking only after it runs your program?
Some languages, such as Perl, are not as rigid about type checking as Java. For example, Perl does not require that you indicate whether a variable will contain an integer, a floating point number, or a string. Thus, all the following statements are allowed for a single variable named $a:
Here three different types of data — integer, string, and double — have been assigned to the same variable
Java, on the other hand, does complete type checking when the program is com piled. As a result, you must declare all variables as a particular type so that the compiler can make sure you use the variables correctly. The following bit of Java code, for example, won’t compile:
If you try to compile these lines, you get an error message saying that Java can’t multiply an integer and a string
In Java, every class you define creates a new type of data for the language to work with. Thus, the data types you have available to you in Java aren’t just simple predefined types, such as numbers and strings. You can create your own types. If you’re writing a payroll system, you might create an Employee type. Then you can declare variables of type Employee that can hold only Employee objects. This capa bility prevents a lot of programming errors. Consider this code snippet
This code creates a variable (newHire) that can hold only Employee objects. Then it tries to assign the number 21 to it. The Java compiler won’t let you run this program because 21 is a number, not an employee
An important object-oriented programming feature of Java called inheritance adds an interesting — and incredibly useful — twist to type checking. Inheritance is way too complicated to dive into just yet, so I’ll be brief here: In Java, you can create your own data types that are derived from other data types. Employees are people, for example, and customers are people too, so you might create a Person class and then create Employee and Customer classes that both inherit the Person class. Then you can write code like this:
Confused yet? If so, that’s my fault. Inheritance is a pretty heady topic for Chapter 1 of a Java book. Don’t panic if it makes no sense just yet. It will all be clear by the time you finish reading Book 3, Chapter 4, which covers all the subtle nuances of using inheritance
No comments:
Post a Comment