Sie sind auf Seite 1von 8

Unit - 1 - Getting Started with Java

Java is a full-fledged object-oriented programming (OOP) language. It is platform independent and is normally interpreted rather than compiled like C/C++. It is syntactically and structurally modeled after C/C++. It uses an independent bytecode that is architecture neutral, i.e. it is designed to be machine independent; the bytecodes are interpreted and executed by a Java Virtual Machine (JVM). It performs various compile-time and run-time checking operations and supports many features like threading, networking, security, and Graphical User Interface (GUI) development. It performs automatic memory management that helps to greatly reduce the problem of memory leaks found in other languages and libraries that dynamically allocate memory.

Object-oriented software development OO approach models an application around a series of real world objects, in order to solve a problem it is useful to think of the real world objects that make up the problem domain. The OO approach is based on three distinct activities:

Object Oriented Analysis (OOA): is concerned with determining the functionality of the system, i.e. what should the application do, the products of the analysis step is called analysis artifacts.

Object Oriented Design (OOD): is concerned with how the architecture supports the functionality of the application, the products of the design step is called design artifacts.

Object Oriented Programming (OOP): is concerned with actual implementation of the application.

The Unified Modeling Language (UML) is a widely used OO technique used to design and document an application. A class diagram is one of the end products of the technique and is used by programmers to create the application.

The diagram shows a partial class UML diagram depicting two classes: Customer and CustomerDriver. box represents a class and is divided into three sections: 1st section at the top is the name of the class 2nd section lists the variables that make up the class Last section lists the class methods Each

The symbols preceding the variable and method names specify the visibility of these class members. The following are the class diagram symbols used:

-: Private +: Public #: Protected (used with inheritance)

Normally, a class diagram consists of many classes and is interconnected with annotated lines showing the relationship between the classes. The class diagram is intended to clearly show what objects make up the system and how they interact. Once a class diagram is complete it can be implemented using an OOP language such as Java.

The object-oriented approach is typically used for medium-scale to large-scale projects, where many developers must communicate, and work together, to create an application. For smaller projects involving only a few programmers, such as the one dealt with in most programming classes, the object-oriented approach is not normally used.

OOP principles: generally underlying three principles must be supported by an OOP language:

Data Encapsulation: It is concerned with hiding irrelevant information from the users of a class and exposing the relevant. Its primary purpose is to reduce the level of software development complexity. By hiding the details of what is needed to perform an operation, the use of that operation is simpler. It is also used to protect the internal state of an object. By hiding the variables that represent the state of an object, modifications to the object are controlled through the methods. Any changes to the state are verified by the code in the methods. Also, by hiding variables, sharing of information between classes is eliminated. This reduces the amount of coupling possible in an application.

Inheritance: It describes the relationship between two classes such that one class re-uses the capabilities of another class, enabling the re-use of software resulting in a more productive developer.

Polymorphism: Its primary concern is to make the application more maintainable and extendable, polymorphism behavior is where the behavior of one or identical methods is dependent upon the object it is executing against. For example, a person object and a square object can both have a draw method. What it draws depends on the object the method is executed against.

Types of Java applications

Console and window applications

Server-based web applications supported by Servlets, JSPs, JSF, and other JEE standards. A servlet is a server-side application which renders an HTML page sent to a client. A JavaServer Page (JSP) is actually a disguised Servlet. It provides a more convenient way of developing web pages.

Applets that execute within a browser. Applets are normally embedded within an HTML page and offer a means of achieving client-side execution of a code.

Embedded applications

Componentized building blocks using JavaBeans. JavaBeans are building blocks for shared application functionality. They are frequently designed to be used in multiple applications and follow a standard naming convention. Enterprise Java Beans (EJB) are components designed to be used in a client/server configuration from a web server.

Structure of a Java console program:

Our program defines a Customer class and then uses it in the CustomerDriver class.

When it is executed we get the following output:

Notice, that there are two classes in this application. The CustomerDriver class contains the main method and is executed first. An instance of the Customer class is created and used within the main method.

Package: The package statement specifies the class' com.company.customer package. Packages provide a means of grouping similar classes, interfaces, enumerations, and exceptions together.

Import: The import statement informs the compiler of which packages and classes are used by an application and how they can be used. This allows the compiler to determine whether the package's members are used correctly.

Packages need to be imported for all classes, except the following classes: Found in the java.lang package Located in the current package (com.company.customer, in this case) Explicitly marked such as java.text.NumberFormat as used in the Customer class' toString method

The Customer class: The first word of the class definition was the keyword, public it specifies that the class is visible outside the package. While not required, it is frequently used for most classes and brings us to the second keyword, class, which identifies a Java class.

Instance variables: Four private instance variables were declared next. The use of the private keyword hides them from users of the class. The Locale class supports applications that can work transparently internationally. BigDecimal is the best way of representing currency in Java.

Methods: By making these instance variables private, the designer restricts access to the variables. They are then only accessible through public methods. The combination of private variables and public methods is an example of data encapsulation. Methods are found within classes and classes are found within packages.

If the instance variables are made public instead, other users can directly access the variables. This would improve the efficiency of the program, but may hinder future maintenance efforts. It would be more difficult to change these variables and enforce any sort of validation checks on the changes to the variables.

A series of getter and setter methods were present to return and set the values associated with the private instance variables. This exposes them in a controlled manner. The use of getter and setter methods is a standard approach to achieve encapsulation. For example, trying to assign a null value to a name would throw a IllegalArmumentException exception. The toString method returns a string representing an instance of a customer. In this case the name, account number, and a localized version of the balance is returned.

The CustomerDriver class' main method

The CustomerDriver class is referred to as the driver or controller class. Its purpose is to have a main method that will create and use other classes. In a Java application the main method is the first method to be executed. If the application consists of multiple classes, normally only one class has a main method. A Java application typically needs only one main method. In the main method, a new customer is created, a balance is set and then the customer is displayed. A C++ style comment was added to statements to document the declaration and creation of a customer. This was the line beginning with the double forward slashes (//).

Exploring the structure of a class: Programming can be thought of as code manipulating data. In Java, code is organized around packages, classes and methods. Packages are collections of classes with similar functionality. Classes are composed of methods that support the functionality of the class. This organization provides structure to applications. Classes will always be in a package and methods will always be in a class. If the package statement is not included in a class definition, the class becomes part of a default package which consists of all of the classes in the same directory that doesn't have a package statement.

Das könnte Ihnen auch gefallen