Java For ABAP Programmers 4-5
Java For ABAP Programmers 4-5
In these two lectures Im going to talk about commenting in Java and naming conventions and standards. Im not only going to talk about the Sun conventions but also the conventions I use to make a program easier to read. This is also known as good programming style.
Block Comments
Java provides a way of commenting out an entire block of code. The following example illustrates this: /* This is a block Comment in Java. You may not nest block comments in Java. You can only have one start and one end comment. */ Notice the use of /* to start the comment and */ to end the comment. You can use this form of commenting for a single line, but Java does provide for this with:
Alistair C. Rooney 2002 All rights reserved ABAP is the registered trademark of SAP AG. Java is the registered trademark of Sun Microsystems Inc.
Line Comments
Line comments allow you to comment a line, or part of a line. This is very similar to the double quote symbol used in ABAP. Lets have a quick look at an example of this: int myVariable = 0; // Initialize myVariable to zero. // Now initialize the other variable. float myFloat = 3.45f; The two different methods are shown above. The double slash // can start at the beginning of a line or half way through a line, telling the compiler that anything after it is to be ignored.
Javadoc Comments
I intend to touch on this briefly here. If you want to find out more about the Javadoc utility and how to comment for it specifically, then have a look at the following url: http://java.sun.com/products/jdk/1.1/docs/tooldocs/win32/javadoc.html To use javadoc comments you start the comments with /** and end them with */. The other benefits of using the javadoc comment method is that you can now use tags within your comment block. For example: /** Start of comment block. @author Alistair Rooney @version 1.1 */ For a list of the standard tags, have a look at the url above. In conclusion I should add that contemporary wisdom dictates that all programs should be fully documented as this is where you will find developers looking for clues about the programs functionality. Document your programs as much as possible.
Alistair C. Rooney 2002 All rights reserved ABAP is the registered trademark of SAP AG. Java is the registered trademark of Sun Microsystems Inc.
See if you can work out why the above names are Legal or Illegal. While were on the subject of variable names, lets look at the Java syntax for declaring a variable. In ABAP we would say DATA MYVARIABLE TYPE I. In Java we would declare the variable like this: int myVariable; We can also initialise the variable at the same time like so: (We would add the VALUE addition in ABAP) int myVariable = 76; To use the equivalent chain command in ABAP (DATA:) we would simply use a comma to separate the different variables, but they must be of the same type. This is illustrated thus:
Alistair C. Rooney 2002 All rights reserved ABAP is the registered trademark of SAP AG. Java is the registered trademark of Sun Microsystems Inc.
int myInt1, myInt2, myInt3; In the next section well discuss recognised conventions that we use in Java.
Java Conventions
There are many good sites on the Internet, which discuss Java style. More than one book has been written on this subject alone! (Elements of Java Style ISBN 0-52177768-2) (A good site to look at is : http://www.javaranch.com/style.jsp and Suns site is http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html ). What I intend doing here is going over the basics of naming conventions. 1. All identifiers should start with a lower case letter. 2. Class names should start with an upper case letter. 3. If there are two words in the name the second word should always start with an upper case letter. E.g. myGreatVariableName 4. Names should always be meaningful. Do not call your identifiers $a or $b! 5. Constants should be All upper case like PI or MYCONSTANT. Constants are defined using the final keyword. Heres a quick example. final double PI = 3.14159; Remember that a constant cannot be changed in any way. Attempting to do so would throw a compiler error. Constants are also usually prefixed with the static keyword, but this is something we will cover later. There are other suggestions like prefixing a working variable with an underscore, but these are largely personal preference. In the next lesson we will have a look at the operators that are provided with Java and something new to Abapers called block scope.
Alistair C. Rooney 2002 All rights reserved ABAP is the registered trademark of SAP AG. Java is the registered trademark of Sun Microsystems Inc.