java for abap programmers 4-5

Upload: aaron-villar

Post on 03-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Java for ABAP Programmers 4-5

    1/4

    Alistair C. Rooney 2002 All rights reserved

    JavaTM for ABAP programmers Lesson 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 alsothe conventions I use to make a program easier to read. This is also known as goodprogramming style.

    Lesson 4 Comments in Java

    Comments in Java are just about the same as ordinary old comments in any otherlanguage including ABAP. They have one important distinction, however, and that is theuse of the Javadoc utility. This is an extremely powerful tool which will scan your Javaprogram for certain comments, method names etc. and produce very nice documentation

    from it.

    Lets have a look then at the three different types of commenting in Java:

    Block comments

    Line comments

    Javadoc comments

    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 thiswith:

    Alistair C. Rooney 2002 All rights reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.

  • 7/28/2019 Java for ABAP Programmers 4-5

    2/4

    Alistair C. Rooney 2002 All rights reserved

    Line Comments

    Line comments allow you to comment a line, or part of a line. This is very similar to thedouble 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 beignored.

    Javadoc Comments

    I intend to touch on this briefly here. If you want to find out more about the Javadocutility 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 thatyou can now use tagswithin 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 shouldbe fully documented as this is where you will find developers looking for clues about theprograms functionality. Document your programs as much as possible.

    Alistair C. Rooney 2002 All rights reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.

    http://java.sun.com/products/jdk/1.1/docs/tooldocs/win32/javadoc.htmlhttp://java.sun.com/products/jdk/1.1/docs/tooldocs/win32/javadoc.html
  • 7/28/2019 Java for ABAP Programmers 4-5

    3/4

    Alistair C. Rooney 2002 All rights reserved

    Lesson 5 Naming Standards and Conventions

    There are two distinct areas in Java that we can discuss when talking about namingstandards. The first is the Legality of the name, i.e. will the compiler allow the name. Thesecond is popular convention. The latter will not give you a compiler problem, it will

    earn you a sharp smack upside the head from your team leader and/or fellow developers.

    Ill let you decide which is worse!

    Legal and Illegal names.

    Firstly we need to cover the types of things that we name. We will name variables,methods and labels. Collectively we call these things identifiers .

    You can start an identifier with any letter, an underscore or a dollar sign. The identifiercan then have any combination of letters or numbers. It can also contain certain specialcharacters, like the underscore.

    Legal Illegal

    MYVARIABLE %myVariable

    myVariable My-Variable

    my_Variable 9988variable

    MyVariable9988 5variable

    $myVariable THE-FIRST-METHOD

    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 avariable.

    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 commato separate the different variables, but they must be of the same type. This is illustratedthus:

    Alistair C. Rooney 2002 All rights reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.

  • 7/28/2019 Java for ABAP Programmers 4-5

    4/4

    Alistair C. Rooney 2002 All rights reserved

    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 onebook has been written on this subject alone! (Elements of Java Style ISBN 0-521-77768-2)

    (A good site to look at is : http://www.javaranch.com/style.jsp and Suns site ishttp://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. myGreatVariableName4. Names should always be meaningful. Do not call your identifiers $a or $b!5. Constants should beAll upper case like PI or MYCONSTANT.

    Constants are defined using thefinalkeyword. Heres a quick example.

    final double PI = 3.14159;

    Remember that a constant cannot be changed in any way. Attempting to do so wouldthrow a compiler error. Constants are also usually prefixed with thestatic keyword, butthis is something we will cover later.

    There are other suggestions like prefixing a working variable with an underscore, butthese are largely personal preference.

    In the next lesson we will have a look at the operators that are provided with Java andsomething new to Abapers called block scope.

    Java for Abapers is soon to be available in book form!

    Alistair C. Rooney 2002 All rights reservedABAP is the registered trademark of SAP AG.Java is the registered trademark of Sun Microsystems Inc.

    http://www.javaranch.com/style.jsphttp://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.htmlhttp://www.javaranch.com/style.jsphttp://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html