MokaByte Numero 30  - Maggio 1999
La grammatica di Java
di
Donato Cappetta
In questo articolo presentiamo  in modo informale, la grammatica del linguaggio, presentazione che può essere utilizzata come riferimento rapido da chi conosce già le nozioni di base


 

Indice grammatica

 
 
Commenti Array
Letterali Cicli e condizioni
Dichiarazioni di variabili Definizioni di Classi
Assegnamento Metodi e costruttori
Operatori Package, Interfacce e import
Oggetti Eccezioni e controlli

 
Commenti
/* this is a multiline comment */

// this is a single-line comment

/** Javadoc comment */
 


 
Letterali  
number Type int
number[l | L]  Type long
0xhex Hex integer
0Xhex Hex integer
0octal Octal integer
[ number ].number  Type double
number[ f | f]  Type float
number[ d | D]  Type double
[ + | - ] number Signed
numberenumber Exponent
numberEnumber Exponent
‘character’ Single character
“characters” String
“” Empty string
\b Backspace
\t Tab
\n Line feed
\f Form feed
\r Carriage return
\” Double quote
\’ Single quote
\\ Backslash
\uNNNN Unicode escape (NNNN is hex)
true Boolean
false Boolean

 
Dichiarazione di Variabili  
[ byte | short | int | long ] varname Integers 
[ float | double ] varname Floats
char varname Characters
boolean varname Boolean
classname varname Class types
interfacename varname Interface types
type varname1, varname2, varname3 Multiple variables
Le seguenti opzioni sono disponibili solo per
variabili istanza e di classe
 
[ static ] variableDeclaration Class variable
[ final ] variableDeclaration Constants
[ public | private | protected ] variableDeclaration Access control
[volatile] varname Modified asynchro-nously
[transient] varname Not persistent

 
Assegnamento  
variable = value  Assignment
variable++  Postfix Increment
++variable  Prefix Increment
variable– –  Postfix Decrement
– –variable  Prefix Decrement
variable += value  Add and assign
variable –= value  Subtract and assign
variable *= value  Multiply and assign
variable /= value  Divide and assign
variable %= value  Modulus and assign
variable &= value  AND and assign
variable |= value  OR and assign
variable ^= value  XOR and assign
variable <<= value  Left-shift and assign
variable >>= value  Right-shift and assign
variable >>>= value  Zero-fill right-shift and assign

 
Operatori  
arg + arg  Addition
arg – arg  Subtraction
arg * arg  Multiplication
arg / arg  Division
arg % arg  Modulus
arg < arg  Less than
arg > arg Greater than
arg <= arg  Less than or equal to
arg >= arg  Greater than or equal to
arg = = arg  Equal
arg != arg  Not equal
arg && arg  Logical AND
arg || arg  Logical OR
! arg  Logical NOT
arg & arg  AND
arg | arg  OR
arg ^ arg  XOR
arg << arg  Left-shift
arg >> arg  Right-shift
arg >>> arg  Zero-fill right-shift
~ arg  Complement
(type)thing  Casting
arg instanceof class  Instance of
test ? trueOp : falseOp  Tenary (if) operator

 
Oggetti  
new class()  Create new instance
new class(arg1, arg2, arg3, ...) New instance with parameters
object.variable Instance variable
object.classvar Class variable
object.method()  Instance method (no args)
object.method(arg1, arg2, arg3, ...) Instance method
object.classmethod() Class method (no args)
object.classmethod(arg1 ,arg2 ,arg3, ...) Class method
Class.classmethod() Class method (no args)
Class.classmethod(arg1, arg2, arg3, ...) Class method

 
Array  
type varname[] Array variable
type[] varname Array variable
new type[numElements] New array object
array[index] Element access
array.length Length of array

 
Cicli e istruz. condizionali  
if ( test) block Conditional
if ( test ) block
else block
 Conditional with else
switch (test) { 
case value : statements
[break]
case value : statements
[break]
...
default : statement
}
 switch (only with integer or char types)
for (initializer; test; change ) block for loop
while ( test ) block while loop
do block 
while (test)
do loop
break [ label ]  break from loop or switch
continue [ label ] continue loops
label:  Labeled loops

 
Definizione di Classi  
class classname block  Simple Class definition
[ final ] class classname block Class cannot be subclassed
[ abstract ] class classname block  Class cannot be instantiated
[ public ] class classname block Class accessible outside package
class classname [ extends Superclass ] block Extends superclass
class classname [ implements interfaces ] block  Implement one or more
interfaces

 
Metodi e Costruttori  
returnType methodName() block Basic method
returnType methodName(parameter, parameter, ...) block Method with parametersMethod with parameters
[ abstract ] returnType methodName() block Abstract method
[ static ] returnType methodName() block Class method
[ native ] returnType methodName() block Native method
[ final ] returnType methodName() block Ifinal method
[ synchronized ] returnType methodName() block Thread lock before executing
[ public | private | protected ] returnType methodName() block Access control
super.methodName() Call a superclass’s method
classname() block basic constructor
classname(parameter, parameter, parameter...) block constructor with parameters
[ public | private | protected] classname() block  Access control
this(...) Calls class’s constructor
super(...) Calls superclass’s constructor

 
Package e Interfacce  
import package.className  Imports specific class name
import package.* Imports all public classes in package
package packagename Classes in this file belong
to this package
[ public ] interface interfaceName block Simple Interface definition
interface interfaceName [ extends anotherInterface ] block Extends interface

 
Eccezioni e Controlli  
synchronized ( object ) block  Waits for lock on object
try block
catch ( exception ) block 
[ finally block ]
Guarded statements 
Executed if exception is thrown
Cleanup code
try block
[ catch ( exception ) block ] 
finally block
Same as previous example (can use optional catch or finally)




 
Riferimenti Bibliografici
  1. "The Java ™ Language Specification" disponibile sul sito FTP di javasoft: ftp://ftp.javasoft.com/docs/specs
  2. "Teach yourself Java in 21 days"  L. Lemay C.L. Perkins - SAMS.NET Publishing (Edito in Italia da Apogeo)

  
 

MokaByte rivista web su Java

MokaByte ricerca nuovi collaboratori
Chi volesse mettersi in contatto con noi può farlo scrivendo a mokainfo@mokabyte.it