1 / 28

Guidelines in Writing a Java Program

Guidelines in Writing a Java Program. Adding Comments. Three permissible styles of comments line comments e.g. //comments on a line by themselves block comments e.g. /*comments on one line or can be extended across as many lines as needed */ Javadoc comments

teenie
Télécharger la présentation

Guidelines in Writing a Java Program

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Guidelines in Writing a Java Program

  2. Adding Comments • Three permissible styles of comments • line comments • e.g. //comments on a line by themselves • block comments • e.g. /*comments on one line or can be extended across as many lines as needed */ • Javadoc comments • e.g./**javadoc comments generate documentation with a program named javadoc*/

  3. The java source code has to be saved with an extension .java • The filename and the classname must be the same • Case sensitive

  4. Java Programming conventions • Requirements when defining a classname: • A classname must begin with a letter of the alphabet, an underscore or a dollar sign. • A classname can contain only letter, digits, underscore or dollar signs. • A classname cannot be a Java programming language reserved keyword

  5. Java Programming conventions • Classname should be nouns in mixed case. • Variable names declared usually begin with a lowercase first letter and should be in mixed case • Constant variables declared usually are all uppercase with the words separated by underscore. • Method name(procedures) declared should be verbs in mixed case with the first letter in lower case.

  6. Java Programming conventions • Whitespace used to organize your program code to make reading easier. • The code between a pair of curly brackets ‘{‘ and ‘}’ within any class or method is known as a block. • All statements in Java programming ends with a semicolon( ; ). • A statement is a single line of code.

  7. Identifiers • names that are given to a variable, class or a method • case sensitive Requirements in declaring an identifier: • can used letters, digits, dollar signs and underscores • must begin with a letter, an underscore or a dollar sign • no maximum length of characters • must not use of reserved words

  8. Keywords • The basic building blocks of the language

  9. Constants and Variables Two types of identifiers in JAVA Programming: • Constants- data or values stored that do not change during the execution of the program • Variables- data or values stored that can be changed during the execution of the program

  10. Syntax to declare a constant: static final type identifier=value; The type in the declaration refers to the data type(which will be seen later in this chapter). The identifier refers to the name of the constant being defined.

  11. Some examples of constants are:

  12. Variable declaration will include the following: • A data type that identifies the type of data that the variable will store • An identifier that is the variable’s name • An optional assigned value, if we want a variable to contain an initial value • A semicolon to end the declaration

  13. Syntax to declare a variable: typeidentifier; The type refers to the data type, and identifier is the name of the variable being defined. typeidentifier1, identifier2; To define more than one variable with the same data type, the names will be separated by a comma.

  14. Syntax to declare a variable: • We can assign a value to a variable at the time of declaration or at any point later on after the variable is being declared. syntax: type identifier=value; or type identifier; identifier=value;

  15. Data Types • Primitive • boolean • byte • char • double • float • int • long • short • Reference • array • class

  16. Categories(continuation) • Logical • Boolean – can hold only true or false • e.g. boolean answer=true; • Integer

  17. Categories(continuation) • Floating Point • Character • char • e.g. char num = ‘8’;

  18. Categories(continuation) • Array • A list of variables which all have the same data types and same name Syntax: type array_name[]; or type [] array_name;

  19. Categories(continuation) • Class • String • Variable name refers to the location in memory rather than to a particular value. • e.g. String myWord=“Java is my favorite subject”;

  20. Separators • Used to define the shape and function of Java code

  21. Operators • Symbols used with data or variables to create mathematical or logical expressions • Types: • Arithmetic operator • Relational or comparison operator • Logical operator • Assignment operator • Increment/decrement operator

  22. Types of Operators • Arithmetic operator

  23. Types of Operators • Relational or comparison operator • Allows us to compare two items

  24. Types of Operators • Logical operator • Used to combine compound conditions

  25. Types of Operators • Assignment operator • Represented by the equals sign (=) • Initialization- assignment made when we declare a variable • Assignment-assignment made later

  26. Types of Operators • Pre-increment e.g. ++identifier; • Post-increment e.g. identifier++; • Pre-decrement e.g. --identifier; • Post-decrement e.g. identifier--; • Increment or decrement operator

  27. Escape Sequences • Used to store any characters that includes non-printing characters

More Related