> Tech > Figure 2 : Exemple de code Java

Figure 2 : Exemple de code Java

Tech - Par Renaud ROSSET - Publié le 24 juin 2010

Defining and initializing variables

int counter;
String customerLastName;
int loopLimit = 5;
MyClass myObjectTwo = new MyClass( );
float myVariable = 7.2;
CustomerName myNameVariable = new CustomerName("Sharon Hoffman");

Manipulating variables

counter = 0;
customerLastName = new String("Last");
customerLastName = new String(lastName);
displayName = (displayName + " " + customerMiddleInitial +

« . « );

Arithmetic operations

counter = (counter +1);
nextCustomerNumber = (nextCustomerNumber +1);
outputPremium = (inputPremium * 2);

Logical comparisons

(counter <= loopLimit)
(counter == 0)
(!customerMiddleInitial.equals(" "))

Looping (do...while)

do
{
/*
* do while loop processing goes here
*/
}
while(i < 8);
Looping (for)
for(int x = 0; x<5; x= (x+1))
{
/*
* for loop processing goes here
*/
}

Branching

if (!customerMiddleInitial.equals(" "))
{
displayName.append(" ").append(customerMiddleInitial).append(". ");
}
else
{
displayName.append(" ");
}

Input

InputStreamReader myInput = new InputStreamReader(System.in);
BufferedReader myBuffer = new BufferedReader(myInput);
String myInputString = new String();
try
{
myInputString = (myBuffer.readLine());
}
catch(IOException exc)
{
/*
* error handling goes here
*/
}

Output

System.out.println("Hello World");

Téléchargez cette ressource

Mac en entreprise : le levier d’un poste de travail moderne

Mac en entreprise : le levier d’un poste de travail moderne

Ce livre blanc répond aux 9 questions clés des entreprises sur l’intégration du Mac : sécurité, compatibilité, gestion, productivité, coûts, attractivité talents, RSE et IA, et l’accompagnement sur mesure proposé par inmac wstore.

Les plus consultés sur iTPro.fr

A lire aussi sur le site

À la une de la chaîne Tech