mardi 25 septembre 2007

Le langage JAVA

Les interfaces


Une interface possède en Java

  • Un nom
  • Un package
  • Des méthodes

Exemple de code :



package mypackage;

interface Voiture{
accelere();
freine();
tourneLeVolant(float degres);
};


Les classes

La classe est l'un des élément de base de java. Une classe d'objets ou classe doit avoir :

  • Un nom
  • Un package
  • Des attributs
  • Des méthodes

Voici une définition de classe :

Exemple de code :


package mypackage;

class Client {
String nom;
String prenom;
};


Les relations entre les classes

On ne peut dériver un classe que d'une seule classe en Java, c'est une des limitation de Java savament désignée par : Il n'est pas possible de faire du polymorphisme en Java. (Ca fait bien ! )

Voici une définition de classe qui dérive de la classe client. La dérivation s'écrit à l'aide du mot clé extends


package mypackage;

class ClientHeureux extends Client{
void smile();
};



Le mot clé extends signifie que la classe ClientHeureux est une spécialisation du Client. Le ClientHeureux possède aussi un nom et un prénom mais il possède la spécificité de sourire.

Les portées ou scopes

Un scope est utilisé pour caractériser le champ d'application d'une fonction. Une bonne pratique consiste à utiliser les visibilité minimale, toutefois, Voici les différentes visibilités qui sont utilisées en JAVA.

  • default
  • private
  • protected
  • public

Private

Caractérise un attribut ou une méthode organique, c'est à dire qu'elle n'est accessible que depuis la classe d'objet considérée.

Protected

Caractérise un attribut ou une méthode organique. Toutefois, les descendant des cette classe d'objet auront également accès à cette méthode.

Public

Caractérise uns attribut ou une méthode qui est accessible depuis n'importe quelle classe.

Default

Caractérise un attribut ou une méthode accessible depuis tout le package. Si aucun mot clef n'est donné, c'est cette visibilité qui est utilisée.

Les packages

Les packages permettent d'organiser les classes et interfaces en sous ensembles, c'est la notion de package. Dans un package on place plusieurs classes.

Du point de vue strict de la programmation cette organisation apporte des bénéfices au niveau de la clarté.

Ce qui est important c'est de définir pour le projet une organisation homogène. Dans la pratique, on utilise les package pour faire un découpage fonctionnel et ou technique.

Par exemple, toutes les classes qui se rapportent à un client peuvent être regroupées dans un même package, on procède dans ce cas à un découpage fonctionnel. Il est également possible de regrouper les classes d'un point de vue technique, par exemple on regroupe entre elle toutes les classes se rapportant au parsing d'un fichier.



Les Exceptions


Une des caractéristiques de JAVA est de gérer depuis la runtime les exceptions. Ainsi il est impossible de créer un erreur de segmentation. Tous les programmes JAVA se termine donc de manière normale.

Pour parler des exceptions il est nécessaire de se placer dans le cadre de l'execution d'un programme. Un programme est manipulé au niveau de la runtime comme une pile d'appel. Le sommet de la pile donne l'instruction qui va être executée. Parfois certaines opérations sont impossibles et provoquent un résultat indéterminé, c'est par exemple le cas d'une division par zéro. Le processeur n'est pas en mesure de donner le résultat, il a donc le choix :

  • Soit il donne une réponse aléatoire... Mais ce n'est pas le genre de l'informatique
  • Soit il s'arrête et déclare qu'il ne sait pas faire
  • En Java, il a une troisième possibilité : Il lance une exception.

Attention à ne pas confondre une exceptions avec le traitement d'erreur, car il y aura toujours des cas qu'on avait pas prévu, on s'appuie sur la machine virtuelle pour ces cas là. Pour autant, il n'est pas bon de faire du traitement d'erreur avec des exceptions.

La lancée d'une exception c'est comme une bulle dans la stack, elle remonte jusqu'a ce qu'un bloc d'un frame supérieur soit en mesure de traiter l'exception.

La lancée d'une exception se fait par le mot clé throw. Si l'exception n'étend pas une RuntimeException, il est nécessaire que vous ajoutiez dans votre code un block de traitement d'exception par les mots clés try et catch.

La récupération d'une exception se fait par le mot clé catch.

Pour pouvoir attrapper une exception il est nécessaire de l'avoir placée dans un block try.

Il existe deux types d'exceptions :

  • Les exceptions standards qui dérivent de la classe Exception
  • Les exceptions RuntimeException qui sont une sous classe de la classe des Exception, celle ci sont déclenchées par une impossibilité du processeur de traiter l'instruction. Par exemple ArithmeticException est une RuntimeException car le processeur ne sait pas diviser par zéro.

Clément Soullard, Juin 2004

Ce document est placé sous la licence libre Free Doc. La reproduction partielle ou totale de ce document est permise et encouragée. Cependant, les images du film Matrix ne sont évidement pas libres de droits.

Si vous faites évoluer le document merci de me tenir au courant.

Barbarismes Javaien et liens

Java Term Definition More Information
Applet An applet is a Java program that runs within the web browser. Applets use a graphical user interface and may have text, images, buttons, scrollbars, and sound. AWT and SWING are frequently associated with articles and tutorials about creating applets.
  1. Building Applets
  2. Trail: Writing Applets
  3. Package java.applet
AWT The Abstract Window Toolkit (AWT) is a package of classes for creating components such as buttons, menus, and scrollbars for applets and standalone applications.
  1. AWT Fundamentals
  2. Using the AWT to Create a GUI
  3. Package java.awt Description
  4. AWT Forums
Java API The Java Application Programming Interface (API) is prewritten code, organized into packages of similar topics. For instance, the Applet and AWT packages include classes for creating fonts, menus, and buttons. The full Java API is included in the Java 2 Standard Edition download.
  1. API Documentation
  2. Building an Application
JavaBeans JavaBeans architecture provides a way of designing reuseable software components that can be visually manipulated in builder tools. Beans can besimple like buttons, or more complex like a tool to access databases.
  1. JavaBeans Technology
  2. JavaB eans Technology: Unlocking The BeanContext API
  3. JavaBeans Forums
Java Foundation Classes (JFC) The Java Foundation Classes (JFC) are a set of GUI components and other services simplifying the development and deployment of desktop and Internet/Intranet applications.
  1. Java Foundation Classes (JFC)
  2. JFC FAQ
  3. Project Swing Forum
  4. Accessibility Forum
Java Native Interface (JNI) JNI is the native programming interface for Java that is part of the JDK. The JNI allows Java code to operate with applications and libraries written in other languages, such as C, C++, and assembly. Recommended only for advanced programmers.
  1. Trail: Java Native Interface
  2. Chapter 5: JNI Technology
  3. Java Native Interface, 1.2
JavaServer Pages (JSP) Create dynamic web pages with JSP by embedding scriptlets (Java programming language code) with HTML. JSP pages process forms, perform calculations, or do anything else that can be written with the Java programming language. To develop and test JSP, download the JavaSever Web Development Kit, and J2SE.
  1. JavaServer Pages Tutorial
  2. JavaServ er Pages: A Developer's Perspective
  3. JSP Pro (Two chapters in PDF)
  4. Core Servlets and JavaServer Pages (Two chapters)
  5. JavaServer Pages Forums
  6. JavaSever Web Development Kit Forums
Java 2 Platform, Enterprise Edition (J2EE) The J2EE platform provides a component-based approach to the design, development, assembly, and deployment of enterprise applications. The J2EE platform gives you a multitiered distributed application model, the ability to reuse components, a unified security model, and flexible transaction control.
  1. Java 2 Platform, Enterprise Edition
  2. J2EE Tutorial
  3. Downloads
  4. Java BluePrints
  5. Enterprise JavaBeans(EJB) Technology Fundamentals
  6. Java Programming Forums
Java 2, Micro Edition (J2ME) Java 2 Micro Edition (J2ME) is targeted for the consumer and embedded market. The API specifications are based on Java 2 Standard Edition (J2SE), but modified to meet the unique requirements of each product. J2ME makes it possible to write Java applications for cell phones, smart cards, pagers, and other consumer devices.
  1. Java 2 Micro Edition (J2ME) Technology
  2. Wireless Developer
  3. How Wireless Applications Work
  4. Java Wireless Technology Discussions
Java 2, Standard Edition (J2SE) This download includes the essential compiler, tools, runtimes, and APIs for writing, deploying, and running applets and applications in the Java programming language.
  1. Java 2 Platform, Standard Edition (J2SE)
  2. Online Documentation
  3. Introducing the Java Platform
  4. Getting Started
Java Virtual Machine (JVM) The Java virtual machine executes instructions that a Java compiler generates. This run time environment, or JVM, is embedded in various products, such as web browsers, servers, and operating systems.
  1. Java Virtual Machine Forums
JDBC JDBC is a Java API for executing SQL statements. By using the JDBC API, you can access almost any data source, from relational databases to spreadsheets to flat files. J2SE includes the JDBC API.
  1. JDBC API
  2. Lesson: Learn JDBC Basics
  3. JDBC Forums
JDK JDK is the short-cut name for the set of Java development tools, consisting of the API classes, a Java compiler, and the Java Virtual Machine interpreter, regardless of which version. The JDK is used to compile Java applications and applets. The most current version is the J2SE., the preferred term these days. If you use J2SE 1.2 and later to develop applications, you are using what's known as the Java 2 Platform.
  1. Download J2SE 1.4
  2. Online Documentation
  3. Introducing the Java Platform
  4. Getting Started
JINI The Jini network technology enables any service--from enterprise systems to kitchen appliances--to network smoothly and simply. The Jini architecture lets each service (device or software) tell others how to talk to it, without any administrator settings.
  1. Jini Network Technology
  2. Distribu ted Events in Jini
  3. Technology
  4. How to Attach a User Interface to a Jini Service
  5. Core Jini
  6. General Distributed Computing Forums
Project Swing The javax.swing package of classes is used to create GUI components for applets and applications. Project Swing classes enable programmers to specify a different look and feel for each platform, or a uniform look across all platforms. Swing is the project code name for the lightweight GUI components in JFC.
  1. Java Foundation Classes
  2. Fundamentals of JFC/Swing: Part I
  3. Fundamentals of JFC/Swing: Part II
  4. Creating GUI with JFC/Swing
  5. Project Swing: Building a User Interface
  6. Project Swing Forums
RMI Remote Method Invocation (RMI) lets Java applications communicate across a network. The communicating applications can be running on different computers on opposite sides of the planet. This higher-level and method-based approach to network communications allows access to a remote object as easily as a local object.
  1. Java Rmote Invocation (RMI)
  2. Fundamentals of RMI Short Course
  3. RMI-IIOP Documentation
  4. Lesson 8: Remote Method Invocation
  5. RMI-IIOP Forums
Servlets A servlet is an extension to a server that enhances the server's functionality. Servlets are most commonly used to process forms, handle redirects or authenticate user names and passwords, and create dynamic content.
  1. Java Servlet Technology
  2. Lesson: Overview of Servlets
  3. Lesson 5: Writing Servlets
  4. What 's New in the Servlet API?
  5. Funda mentals of Java Servlets
  6. JavaServer Web Development Kit Forums

Aucun commentaire: