You are on page 1of 6

Introduction to Java

Java is an object-oriented, platform-neutral, secure language designed to be easier to learn than C++ and harder to misuse than C and C++. Object-oriented programming (OOP) is a software development methodology in which a program is conceptualized as a group of objects that work together. bjects are created from templates called classes, and they contain data and the statements re!uired to use that data. Java is primarily object-oriented, as you see later today when you create your first class and use it to create objects. Platform neutrality is a program"s ability to run without modification in different computing environments. Java programs are transformed into a format called bytecode that can be run by any computer or device e!uipped with a Java virtual machine. #ou can create a Java program on a $indows %ista machine that runs on a &inu' web server, an (pple )ac using * +, and a *amsung (ndroid phone. (s long as a platform has a Java virtual machine, it can run the bytecode. (lthough the relative ease of learning one language over another is always a point of contention among programmers, Java was designed to be easier than C++ primarily in the following ways: Java automatically takes care of memory allocation and deallocation, freeing programmers from this error-prone and comple' task. Java doesn"t include pointers, a powerful feature for e'perienced programmers that can be easily misused and introduce major security vulnerabilities. Java includes only single inheritance in object-oriented programming. ,he lack of pointers and the presence of automatic memory management are two key elements of Java security.

Selecting a Development Tool


-ow that you"ve been introduced to Java as a spectator, it"s time to put some of these concepts into play and create your first Java program. .f you work your way through the /0 days of this book, you"ll become well versed in Java"s capabilities, including graphics, file input and output, +)& processing, and (ndroid app development. #ou will write programs that run on web pages and others that run on your computer, web servers, or other computing environments. 1efore you get started, you must have software on your computer that can be used to edit, prepare, and run Java programs that use the most up-to-date version of the language2 Java 3. *everal popular integrated development environments 4.56s7 for Java support version 3, including .ntelliJ .56( and the open source software 6clipse.

.f you are learning to use these tools at the same time as you learn Java, it can be a daunting task. )ost .56s are aimed primarily at e'perienced programmers who want to be more productive, not new people who are taking their first forays into a new language. ,he simplest tool for Java development is the Java 5evelopment 8it, which is free and can be downloaded fromwww.oracle.com9technetwork9java9javase9downloads. $henever racle releases a new version of Java, it also makes a free development kit available over the $eb to support that version. ,he current release is Java *6 5evelopment 8it 3. :or the sake of a few trees, in this book the language is usually referred to as simply Java and the kit as the J58. ,he drawback of developing Java programs with the J58 is that it is a set of command-line tools. ,herefore, it has no graphical user interface for editing programs, turning them into Java classes, and testing them. 4( command line is simply a prompt for typing te't commands. ,he $indows command line is accessible on the *tart menu under (ccessories, Command ;rompt or (ccessories, *ystem ,ools, Command ;rompt.7 racle offers a free .56 for Java programmers called -et1eans from the website www.netbeans.org. 1ecause -et1eans is easier to use for most people than the J58, it"s employed throughout this book. .f you don"t have a Java development tool on your computer yet and you want to try -et1eans, you can find out how to get started with the software in(ppendi' (, <=sing the -et1eans .ntegrated 5evelopment 6nvironment.> ,he appendi' covers how to download and install the kit and use it to create a sample Java program to make sure it works. (s soon as you have a Java development tool on your computer that supports Java 3, you"re ready to dive into the language. Tip :or more information on the other .56s for Java, visit the .56( site at www.jetbrains.com9idea and the 6clipse site at www.eclipse.org. 6clipse also is used for (ndroid programming in 5ay /0, <$riting (ndroid (pps for Java.>

Ob ect!Oriented "rogramming
,he biggest challenge for a new Java programmer is learning object-oriented programming while learning the Java language. (lthough this might sound daunting if you are unfamiliar with this style of programming, think of it as a two-for-one discount for your brain. #ou will learn object-oriented programming by learning Java. ,here"s no other way to make use of the language.

bject-oriented programming is an approach to building computer programs that mimics how objects are assembled in the physical world. 1y using this style of development, you can create programs that are more reusable, reliable, and understandable. ,o get to that point, you first must e'plore how Java embodies the principles of object-oriented programming. .f you already are familiar with object-oriented programming, much of today"s material will be a review for you. 6ven if you skim over the introductory material, you should create the sample program to get some e'perience in developing, compiling, and running Java programs. ,here are many different ways to conceptualize a computer program. ne way is to think of a program as a series of instructions carried out in se!uence, which commonly is called procedural programming. *ome programmers start by learning a procedural language such as a version of 1(*.C. "rocedural languages mirror how a computer carries out instructions, so the programs you write are tailored to the computer"s manner of doing things. ne of the first things a procedural programmer must learn is how to break a problem into a series of simple steps followed in order. Ob ect!oriented programming looks at a computer program from a different angle, focusing on the task the program was created to perform, not on how a computer handles tasks. In ob ect!oriented programming# a computer program is conceptuali$ed as a set of ob ects that wor% together to accomplish a tas% . 6ach object is a separate part of the program, interacting with the other parts in highly controlled ways. &or a real!life e'ample of ob ect!oriented design# consider a stereo system. )ost systems are built by hooking together a bunch of different objects, which are more commonly called components, such as the following2 ? *peaker components play midrange and high-fre!uency sounds. ? ( subwoofer component plays low bass fre!uency sounds. ? ( tuner component receives radio broadcast signals. ? ( C5 player component reads audio data from C5s. ? ( turntable component reads audio data from vinyl records. ,hese components are designed to interact with each other using standard input and output connectors. 6ven if you bought the speakers, subwoofer, tuner, C5 player, and turntable from different companies, you could combine them to form a stereo system@as long as each component has standard connectors.

bject-oriented programming works under the same principle2 #ou put together a program by creating new objects and connecting them to each other and to e'isting objects provided by racle or another developer. ,hese objects are each a component in the larger program and are combined in a standard way. 6ach object plays a specific role in the larger program. (n object is a self-contained element of a computer program that represents a related group of features and that is designed to accomplish specific tasks.

Ob ects and Classes


bject-oriented programming is modeled on the observation that in the physical world, objects are made up of many kinds of smaller objects. ,he ability to combine objects is only one aspect of object-oriented programming. (nother important feature is the use of classes. ( class is a template used to create an object. 6very object created from the same class has similar features. Classes embody all features of a particular set of objects. $hen you write a program in an object-oriented language, you don"t define individual objects. .nstead, you define classes used to create those objects. .f you were writing a networking program in Java, you could create a )odem class that describes the features of all computer modems. )ost modems have the following common features2 ? ,hey connect to a computer"s serial port. ? ,hey send and receive information. ? ,hey dial phone numbers. ,he )odem class serves as an abstract model for the concept of a modem. ,o actually have something concrete you can manipulate in a program, you need an object. #ou must use the )odem class to create a )odem object. ,he process of creating an object from a class is called instantiation, which is why objects also are called instances. ( )odem class can be used to create different )odem objects in a program, each with different features such as the following2 ? *ome are internal modems, and others are e'ternal modems. ? *ome use the C )0 port, and others use the C )/ port. ? *ome have error control, and others don"t.

6ven with these differences, two )odem objects still have enough in common to be recognizable as related objects. :igure 0.0 shows a )odem class and several objects created from that template.

Figure 1.1. The

(odem class and several (odem objects.

Aere"s another e'ample2 =sing Java, you could create a class to represent all command buttons@the clickable rectangles that appear on windows, dialog bo'es, and other parts of a program"s graphical user interface. $hen the Command1utton class is developed, it could define these features2 ? ,he te't displayed on the button ? ,he size of the button ? (spects of its appearance, such as whether it has a B5 shadow ,he Command1utton class also could define how a button behaves, deciding the following things2 ? $hether the button re!uires a single click or a double-click ? $hether it should ignore mouse clicks ? $hat it does when clicked (fter you define the Command1utton class, you can create instances of that button @in other words, Command1utton objects. ,he objects all take on the basic features of a button as defined by the class. 1ut each one could have a different appearance and slightly different behavior, depending on what you need that object to do. 1y creating a Command1utton class, you don"t have to keep rewriting the code for each button you want to use in your programs. .n addition, you can reuse theCommand1utton class to create different kinds of buttons as you need them, both in this program and in others. $hen you write a Java program, you design and construct a set of classes. $hen your program runs, objects are created from those classes and used as needed. #our task as a Java programmer is to create the right set of classes to accomplish what your program needs to accomplish. :ortunately, you don"t have to start from scratch. ,he Java language includes the Java Class &ibrary, more than B,CDD classes that implement most of the functionality you will need. ,hese classes are installed along with a development tool such as the J58.

$hen you"re talking about programming in the Java language, you"re actually talking about using this class library and some standard keywords and operators defined in Java. ,he class library handles numerous tasks, such as mathematical functions, te't, graphics, user interaction, and networking. $orking with these classes is no different from working with the Java classes you create. :or complicated Java programs, you might create a whole set of new classes that form their own class library for use in other programs. Eeuse is one of the fundamental benefits of object-oriented programming . -ote .n the Java Class &ibrary, one of Java"s standard classes, J1utton in the java'.swing package, encompasses all the functionality of this hypothetical Command1utton e'ample, along with a lot more. #ou get a chance to create objects from this class during 5ay C, <$orking with *wing.>

You might also like