Schweitzer Fachinformationen
Wenn es um professionelles Wissen geht, ist Schweitzer Fachinformationen wegweisend. Kunden aus Recht und Beratung sowie Unternehmen, öffentliche Verwaltungen und Bibliotheken erhalten komplette Lösungen zum Beschaffen, Verwalten und Nutzen von digitalen und gedruckten Medien.
Barry Burd, PhD holds an MS in Computer Science from Rutgers University and a PhD in Mathematics from the University of Illinois. Barry is the author of numerous For Dummies books, including Java For Dummies and Beginning Programming with Java For Dummies.
Introduction 1
Part 1: Getting Started with Java Programming 5
Chapter 1: The Big Picture 7
Chapter 2: Setting Up Your Computer 21
Chapter 3: Running Programs 47
Part 2: Writing Your Own Java Programs 71
Chapter 4: Exploring the Parts of a Program 73
Chapter 5: Composing a Program 95
Chapter 6: Using the Building Blocks: Variables, Values, and Types 127
Chapter 7: Numbers and Types 149
Chapter 8: Numbers? Who Needs Numbers? 173
Part 3: Controlling the Flow 197
Chapter 9: Forks in the Road 199
Chapter 10: Which Way Did He Go? 219
Chapter 11: Around and Around It Goes 251
Chapter 12: Circling Back to Java Loops 275
Part 4: The Inside ScOOP 293
Chapter 13: Programming with Objects and Classes 295
Chapter 14: Using Methods and Fields from a Java Class 315
Chapter 15: Creating New Java Methods 347
Part 5: Smart Java Techniques 375
Chapter 16: Piles of Files: Dealing with Information Overload 377
Chapter 17: How to Flick a Virtual Switch 401
Chapter 18: Creating Loops within Loops 423
Chapter 19: Out of Many, One 443
Chapter 20: Oooey-GUI Was a Worm 477
Part 6: The Part of Tens 503
Chapter 21: Ten Useful Classes in the Java API 505
Chapter 22: Ten Bits of Advice for New Software Developers 511
Index 517
Chapter 1
IN THIS CHAPTER
Recognizing what computer programming is all about
Understanding the software that enables you to write programs
Revving up to use an integrated development environment
Computer programming? What's that? Is it technical? Does it hurt? Is it politically correct? Does Google control it? Why would anyone want to do it? And what about me? Can I learn to do it?
You've probably used a computer to do word processing: Type a letter, print it, and then send the printout to someone you love. If you have easy access to a computer, you've probably surfed the web: Visit a page, click a link, and see another page. It's easy, right?
Well, it's easy only because someone told the computer exactly what to do. If you take a computer fresh from the factory and give no instructions to it, it can't do word processing, it can't surf the web, and it can't do anything. All a computer can do is follow the instructions that people give to it.
Now imagine that you're using Microsoft Word to write the great American novel and you come to the end of a line. (You're not at the end of a sentence; just the end of a line.) As you type the next word, the computer's cursor jumps automatically to the next line of type. What's going on here?
Well, someone wrote a computer program - a set of instructions telling the computer what to do. Another name for a program (or part of a program) is code. Listing 1-1 shows you what some of Microsoft Word's code may look like.
LISTING 1-1 A Few Lines in a Computer Program
if (columnNumber> 60) { wrapToNextLine(); } else { continueSameLine(); }
If you translate Listing 1-1 into plain English, you get something like this:
If the column number is greater than 60, then go to the next line. Otherwise (if the column number isn't greater than 60), then stay on the same line.
Somebody has to write code of the kind shown in Listing 1-1. This code, along with millions of other lines of code, makes up the program called Microsoft Word.
And what about web surfing? You click a link that's supposed to take you directly to Facebook. Behind the scenes, someone has written code of the following kind:
Go to <a href="http://www.facebook.com">Facebook</a>.
One way or another, someone has to write a program. That someone is called a programmer.
Everything you do with a computer involves gobs and gobs of code. For example, every computer game is really a big (make that "very big"!) bunch of computer code. At some point, someone had to write the game program:
if (person.touches(goldenRing)) { person.getPoints(10); }
Without a doubt, the people who write programs have valuable skills. These people have two important qualities:
A language for writing steps is called a programming language, and Java is just one of several thousand useful programming languages. The stuff in Listing 1-1 is written in the Java programming language.
James Gosling and others at Sun Microsystems created Java in the early to mid-1990s. In 2010, Java became part of Oracle Corporation as part of Oracle's acquiring Sun Microsystems.
This book isn't about the differences among programming languages, but you should see code in some other languages so that you understand the bigger picture. For example, there's another language, Visual Basic, whose code looks a bit different from code written in Java. An excerpt from a Visual Basic program may look like this:
If columnNumber > 60 Then Call wrapToNextLine Else Call continueSameLine End If
The Visual Basic code looks more like ordinary English than the Java code in Listing 1-1. But, if you think that Visual Basic is like English, then just look at some code written in COBOL:
IF COLUMN-NUMBER IS GREATER THAN 60 THEN PERFORM WRAP-TO-NEXT-LINE ELSE PERFORM CONTINUE-SAME-LINE END-IF.
At the other end of the spectrum, you find languages like Forth. Here's a snippet of code written in Forth:
: WRAP? 60 > IF WRAP_TO_NEXT_LINE? ELSE CONTINUE_SAME_LINE? THEN ;
Computer languages can be very different from one another, but in some ways, they're all the same. When you get used to writing IF COLUMN-NUMBER IS GREATER THAN 60, you can also become comfortable writing if (columnNumber> 60). It's just a mental substitution of one set of symbols for another. Eventually, writing things like if (columnNumber> 60) becomes second nature.
if (columnNumber> 60)
When you create a new computer program, you complete a multistep process. The process involves three important tools:
The next three sections describe each of the three tools.
You may have heard that computers deal with zeros and ones. That's certainly true, but what does it mean? Well, for starters, computer circuits don't deal directly with letters of the alphabet. When you see the word Start on your computer screen, the computer stores the word internally as 01010011 01110100 01100001 01110010 01110100. That feeling you get of seeing a friendly-looking, five-letter word is your interpretation of the computer screen's pixels and nothing more. Computers break everything down into very low-level, unfriendly sequences of zeros and ones and then put things back together so that humans can deal with the results.
So, what happens when you write a computer program? Well, the program has to get translated into zeros and ones. The official name for the translation process is compilation. Without compilation, the computer can't run your program.
I compiled the code in Listing 1-1. Then I did some harmless hacking to help me see the resulting zeros and ones. What I saw was the mishmash in Figure 1-1.
FIGURE 1-1: My computer understands these zeros and ones, but I don't.
The compiled mumbo jumbo in Figure 1-1 goes by many different names:
.class
SomethingOrOther
To visualize the relationship between source code and object code, see Figure 1-2. You can write source code and then get the computer to create object code from your source code. To create object code, the computer uses a special software tool called a compiler.
FIGURE 1-2: The computer compiles source code to create object code.
Look at Listing 1-1 and at the listing's translation into bytecode in Figure 1-1. You may be tempted to think that a bytecode file is just a cryptogram - substituting zeros and ones for the letters in words like if and else. But it doesn't work that way at all. In fact, the most important part of a bytecode file is the encoding of a program's logic.
if
else
The zeros and ones in Figure 1-1 describe the flow of data from one part of your computer to another. I illustrate this flow in the following figure. But remember: This figure is just an illustration. Your computer doesn't look at this particular figure, or at anything like it. Instead, your computer reads a bunch of zeros and ones to decide what to do next.
Don't bother to absorb the details in my attempt at graphical representation in the figure. It's not worth your time. The thing you should glean from my mix of text, boxes, and arrows is that bytecode (the stuff in a .class file) contains a complete description of the operations that the computer is to perform. When you write a computer program, your source code describes an overall strategy - a big picture. The compiled bytecode turns...
Dateiformat: ePUBKopierschutz: Adobe-DRM (Digital Rights Management)
Systemvoraussetzungen:
Das Dateiformat ePUB ist sehr gut für Romane und Sachbücher geeignet – also für „fließenden” Text ohne komplexes Layout. Bei E-Readern oder Smartphones passt sich der Zeilen- und Seitenumbruch automatisch den kleinen Displays an. Mit Adobe-DRM wird hier ein „harter” Kopierschutz verwendet. Wenn die notwendigen Voraussetzungen nicht vorliegen, können Sie das E-Book leider nicht öffnen. Daher müssen Sie bereits vor dem Download Ihre Lese-Hardware vorbereiten.Bitte beachten Sie: Wir empfehlen Ihnen unbedingt nach Installation der Lese-Software diese mit Ihrer persönlichen Adobe-ID zu autorisieren!
Weitere Informationen finden Sie in unserer E-Book Hilfe.