Uh-oh, it looks like your Internet Explorer is out of date.

For a better shopping experience, please upgrade now.

Java 2 For Dummies

Java 2 For Dummies

4.5 2
by Barry A. Burd, Maria P. Canton

See All Formats & Editions

  • This updated bestseller covers programming essentials for the newest version of Java, the popular platform-independent, object-oriented programming language
  • The material is fully updated and focuses on the new Java SDK 1.5, addressing the needs of new or inexperienced Java developers
  • The fun and easy writing style walks readers through


  • This updated bestseller covers programming essentials for the newest version of Java, the popular platform-independent, object-oriented programming language
  • The material is fully updated and focuses on the new Java SDK 1.5, addressing the needs of new or inexperienced Java developers
  • The fun and easy writing style walks readers through Java syntax basics and helps them write their first program
  • Shows readers how to create basic Java objects and figure out when they can reuse existing code
  • The new edition is also modified to better address the readers who may have some programming knowledge, but who are new to Java

Product Details

Publication date:
For Dummies Series
Edition description:
Older Edition
Product dimensions:
7.42(w) x 9.24(h) x 0.83(d)

Read an Excerpt

Java 2 For Dummies

By Barry Burd

John Wiley & Sons

ISBN: 0-7645-6858-2

Chapter One

All about Java

In This Chapter

* What Java is

* Where Java came from

* Why Java is so cool

* How to orient yourself to object-oriented programming

Say what you want about computers. As far as I'm concerned, computers are good for just two simple reasons:

  •   When computers do work, they feel no resistance, no stress, no boredom, and no fatigue. Computers are our electronic slaves. I have my computer working 24/7 doing calculations for SETI@home - the search for extraterrestrial intelligence. Do I feel sorry for my computer because it's working so hard? Does the computer complain? Will the computer report me to the National Labor Relations Board? No.

    I can make demands, give the computer its orders, and crack the whip. Do I (or should I) feel the least bit guilty? Not at all.

  •   Computers move ideas, not paper. Not long ago, when you wanted to send a message to someone, you hired a messenger. The messenger got on his or her horse and delivered your message personally. The message was on paper, parchment, a clay tablet, or whatever physical medium was available at the time.

    This whole process seems wasteful now, but that's only because you and I are sitting comfortably at the dawn of the electronic age. The thing is that messages are ideas. Physical things like ink, paper, and horses have little or nothing to do with real ideas. These physical things are just temporary carriers for ideas (temporary because people used them to carry ideas for several centuries). But, in truth, the ideas themselves are paperless, horseless, and messengerless.

    So the neat thing about computers is that they carry ideas efficiently. They carry nothing but the ideas, a couple of photons, and a little electrical power. They do this with no muss, no fuss, and no extra physical baggage.

    When you start dealing efficiently with ideas, something very nice happens. Suddenly, all the overhead is gone. Instead of pushing paper and trees, you're pushing numbers and concepts. Without the overhead, you can do things much faster and do things that are far more complex than ever before.

    What You Can Do with Java

    It would be so nice if all this complexity was free, but unfortunately, it isn't. Someone has to think hard and decide exactly what the computer will be asked to do. After that thinking is done, someone has to write a set of instructions for the computer to follow.

    Given the current state of affairs, you can't write these instructions in English or any other language that people speak. Science fiction is filled with stories about people who say simple things to robots and get back disastrous, unexpected results. English and other such languages are unsuitable for communication with computers for several reasons:

  •   An English sentence can be misinterpreted. "Chew one tablet three times a day until finished."

  •   It's difficult to weave a very complicated command in English. "Join flange A to protuberance B, making sure to connect only the outermost lip of flange A to the larger end of the protuberance B, while joining the middle and inner lips of flange A to grommet C."

  •   An English sentence has lots of extra baggage. "Sentence has unneeded words."

  •   English is difficult to interpret. "As part of this Publishing Agreement between John Wiley & Sons, Inc. ('Wiley') and the Author ('Barry Burd'), Wiley shall pay the sum of one-thousand-two-hundred-fifty-seven dollars and sixty-three cents ($1,257.63) to the Author for partial submittal of Java 2 For Dummies, 2nd Edition ('the Work')."

    To tell a computer what to do, you have to speak a special language and write terse, unambiguous instructions in that language. A special language of this kind is called a computer programming language. A set of instructions, written in such a language, is called a program. When they're looked at as a big blob, these instructions are called software or code. Here's what code looks like when it's written in Java:

    import static java.lang.System.out;

    class PayBarry { public static void main(String args) { double checkAmount = 1257.63; out.print("Pay to the order of"); out.print("Dr. Barry Burd"); out.print("$"); out.println(checkAmount); } }

    Why You Should Use Java

    It's time to celebrate! You've just picked up a copy of Java 2 For Dummies, 2nd Edition, and you're reading Chapter 1. At this rate, you'll be an expert Java programmer in no time at all, so rejoice in your eventual success by throwing a big party.

    To prepare for the party, I'll bake a cake. I'm lazy, so I'll use a ready-to-bake cake mix. Let me see ... add water to the mix, and then add butter and eggs ... Hey, wait! I just looked at the list of ingredients. What's MSG? And what about propylene glycol? That's used in antifreeze, isn't it?

    I'll change plans and make the cake from scratch. Sure, it's a little harder. But that way, I get exactly what I want.

    Computer programs work the same way. You can use somebody else's program or write your own. If you use somebody else's program, you use whatever you get. When you write your own program, you can tailor the program especially for your needs.

    Writing computer code is a big, worldwide industry. Companies do it, freelance professionals do it, hobbyists do it, all kinds of people do it. A typical big company has teams, departments, and divisions that write programs for the company. But you can write programs for yourself or someone else, for a living or for fun. In a recent estimate, the number of lines of code written each day by programmers in the United States alone exceeds the number of methane molecules on the planet Jupiter. Take almost anything that can be done with a computer. With the right amount of time, you can write your own program to do it. (Of course, the "right amount of time" may be very long, but that's not the point. Many interesting and useful programs can be written in hours or even minutes.)

    Getting Perspective: Where Java Fits In

    Here's a brief history of modern computer programming:

  •   1954-1957: FORTRAN is developed.

    FORTRAN was the first modern computer programming language. For scientific programming, FORTRAN is a real racehorse. Year after year, FORTRAN is a leading language among computer programmers throughout the world. A well-known computer scientist, Tony Hoare, once said, "I don't know what the language of the year 2000 will look like, but I know it will be called FORTRAN."

  •   1959: COBOL is created.

    The letter B in COBOL stands for Business, and business is just what COBOL is all about. The language's primary feature is the processing of one record after another, one customer after another, or one employee after another.

    Within a few years after its initial development, COBOL became the most widely used language for business data processing. Even today, COBOL represents a large part of the computer programming industry.

  •   1972: Dennis Ritchie at AT&T Bell Labs develops the C programming language.

    The look and feel that you see in this book's examples come from the C programming language. Code written in C uses curly braces, if statements, for statements, and so on.

    In terms of power, you can use C to solve the same problems that you can solve by using FORTRAN, Java, or any other modern programming language. (You can write a scientific calculator program in COBOL, but doing that sort of thing would feel really strange.) The difference between one programming language and another isn't power. The difference is ease and appropriateness of use. That's where the Java language excels.

  •   1986: Bjarne Stroustrup (again at AT&T Bell Labs) develops C++.

    Unlike its C language ancestor, the language C++ supports objectoriented programming. This represents a huge step forward.

  •   May 23, 1995: Sun Microsystems releases its first official version of the Java programming language.

    Java improves upon the concepts in C++. Unlike C++, Java is streamlined for use on the World Wide Web. Java's "Write Once, Run Anywhere" philosophy makes the language ideal for distributing code across the Internet.

    In addition, Java is a great general-purpose programming language. With Java, you can write windowed applications, build and explore databases, control handheld devices, and more. Within five short years, the Java programming language had 2.5 million developers worldwide. (I know. I have a commemorative T-shirt to prove it.)

  •   November 2000: The College Board announces that, starting in the year 2003, the Computer Science Advanced Placement exams will be based on Java.

    Wanna know what that snot-nosed kid living down the street is going to be learning in high school next year? You guessed it - Java.

  •   March 2003: SkillMarket (mshiltonj.com/sm) reports that the demand for Java programmers tops the demand for C++ programmers by 42 percent.

    And there's more! The demand for Java programmers beats the combined demand for C++ and C# programmers by 10 percent. Java programmers are more employable than VB (Visual Basic) programmers by a whopping 111 percent.

    Object-Oriented Programming (OOP)

    It's three in the morning. I'm dreaming about the history course that I failed in high school. The teacher is yelling at me, "You have two days to study for the final exam, but you won't remember to study. You'll forget and feel guilty, guilty, guilty."

    Suddenly, the phone rings. I'm awakened abruptly from my deep sleep. (Sure, I disliked dreaming about the history course, but I like being awakened even less.) At first, I drop the telephone on the floor. After fumbling to pick it up, I issue a grumpy, "Hello, who's this?" A voice answers, "I'm a reporter from The New York Times. I'm writing an article about Java and I need to know all about the programming language in five words or less. Can you explain it?"

    My mind is too hazy. I can't think. So I say anything that comes to my mind, and then go back to sleep.

    Come morning, I hardly remember the conversation with the reporter. In fact, I don't remember how I answered the question. Did I tell the reporter where he could put his article about Java?

    I put on my robe and rush to the front of my house's driveway. As I pick up the morning paper, I glance at the front page and see the two-inch headline:

    Burd Calls Java "A Great Object-Oriented Language"

    Object-oriented languages

    Java is object-oriented. What does that mean? Unlike languages such as FORTRAN, which focus on giving the computer imperative "Do this/Do that" commands, object-oriented languages focus on data. Of course, object-oriented programs still tell the computer what to do. You start, however, by organizing the data, and the commands come later.

    Object-oriented languages are better than "Do this/Do that" languages because they organize data in a way that lets people do all kinds of things with it. To modify the data, you can build on what you already have, rather than scrap everything you've done and start over each time you need to do something new. Although computer programmers are generally smart people, they took awhile to figure this out. For the full history lesson, see the sidebar "The winding road from FORTRAN to Java" (but I won't make you feel guilty if you don't read it).

    Objects and their classes

    In an object-oriented language, you use objects and classes to organize your data.

    Imagine that you're writing a computer program to keep track of the houses in a new real-estate development. The development (still under construction) is a condominium. The houses differ only slightly from one another. Each house has a distinctive siding color, an indoor paint color, a kitchen cabinet style, and so on. In your object-oriented computer program, each house is an object.

    But objects aren't the whole story. Although the houses differ slightly from one another, all the houses share the same list of characteristics. For instance, each house has a characteristic known as siding color. Each house has another characteristic known as kitchen cabinet style. In your object-oriented program, you need a master list containing all the characteristics that a house object can possess. This master list of characteristics is called a class.

    So there you have it. Object-oriented programming is misnamed. It should really be called "programming with classes and objects."

    Now notice that I put the word classes first. How dare I do this! Well, maybe I'm not so crazy. Think again about a housing development that's under construction. Somewhere on the lot, in a rickety trailer parked on bare dirt, is a master list of characteristics known as a blueprint. An architect's blueprint is like an object-oriented programmer's class. A blueprint is a list of characteristics that each house will have. The blueprint says, "siding." The actual house object has gray siding. The blueprint says, "kitchen cabinet." The actual house object has Louis XIV kitchen cabinets.

    The analogy doesn't end with lists of characteristics. Another important parallel exists between blueprints and classes. A year after you create the blueprint, you use it to build ten houses. It's the same with classes and objects. First, the programmer writes code to describe a class. Then when the program runs, the computer creates objects from the (blueprint) class.

    So that's the real relationship between classes and objects. The programmer defines a class, and from the class definition, the computer makes individual objects.

    What's so good about an object-oriented language?

    Based on the previous section's story about home building, imagine that you have already written a computer program to keep track of the building instructions for houses in a new development. Then, the big boss decides on a modified plan - a plan in which half the houses have three bedrooms, and the other half have four.


    Excerpted from Java 2 For Dummies by Barry Burd Excerpted by permission.
    All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
    Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.

  • Meet the Author

    Barry Burd is the author of Java 2 For Dummies, Beginning Programming with Java For Dummies, and Java & XML For Dummies, and a computer science professor at Drew University.

    Customer Reviews

    Average Review:

    Post to your social network


    Most Helpful Customer Reviews

    See all customer reviews

    Java 2 For Dummies 4.5 out of 5 based on 0 ratings. 2 reviews.
    Guest More than 1 year ago
    Most of the JAVA books confuse reference book and teaching text and do neither very well. This book is an excellent teaching text that is very clear. The accompanying CD has really good stuff. A winner!
    Guest More than 1 year ago
    I really thought this book was just great! I gave it 4 stars though because it was confusing at some points. It's also very Funny! It takes you step throught staep, you understand like probably 80% of it and don't get the other 20%!!! But it's still the best Java book ever! I really recomend this book for ANYONE who wants to learn Java, or Java 2! It's great!