1 / 25

Basic Java Syntax and Coding Conventions

Basic Java Syntax and Coding Conventions. Objectives. After completing this lesson, you should be able to do the following: Identify the key components of the Java language Identify the three top-level constructs in a Java program Identify and describe Java packages

geisler
Download Presentation

Basic Java Syntax and Coding Conventions

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. Basic Java Syntax and Coding Conventions

  2. Objectives • After completing this lesson, you should be able to do the following: • Identify the key components of the Java language • Identify the three top-level constructs in a Java program • Identify and describe Java packages • Describe basic language syntax and identify Java keywords • Identify the basic constructs of a Java program • Compile and run a Java application • Examine the JavaBean architecture as an example of standard coding practices • Use the CLASSPATH variable and understand its importance during compile and run time

  3. Examining Toolkit Components • The J2SE/J2EE from Sun provides: • Compiler • Bytecode interpreter • Documentation generator J2SE

  4. Exploring Packages in J2SE/J2EE • The J2SE/J2EE from Sun provides standard packages for: • Language • Windowing • Input/output • Network communication J2SE

  5. Documenting Using the J2SE • The J2SE/J2EE from Sun provides documentation support for: • Comments • Implementation • Documentation • Documentation generator J2SE

  6. Contents of a Java Source • A Java source file can contain three top-level constructs: • Only onepackagekeyword followed by the package name, per file • Zero or moreimportstatements followed by fully qualified class names or “*” qualified by a package name • One or moreclassorinterfacedefinitions followed by a name and block • File name must have the same name as the public class or public interface.

  7. Establishing Naming Conventions • Naming conventions include: • Class names • Customer, RentalItem, InventoryItem • File names • Customer.java, RentalItem.java • Method names • getCustomerName(), setRentalItemPrice() • Package names • oracle.xml.xsql, java.awt, java.io

  8. More About Naming Conventions • Variables: • customerName, customerCreditLimit • Constants: • MIN_WIDTH, MAX_NUMBER_OF_ITEMS • Uppercase and lowercase characters • Numerics and special characters

  9. Defining a Class • Class definitions typically include: • Access modifier • Class keyword • Instance fields • Constructors • Instance methods • Class fields • Class methods

  10. Rental Class: Example Access modifier public class Rental { //Class variable static int lateFee; // Instance variables int rentalId; String rentalDate; float rentalAmountDue; … // Instance methods float getAmountDue (int rentId) { … } … } Declaration Instance variable Instance method

  11. Creating Code Blocks • Enclose all class declarations. • Enclose all method declarations. • Group other related code segments. public class SayHello { public static void main(String[] args) { System.out.println("Hello world"); } }

  12. Defining Java Methods • Always define within a class. • Specify: • Access modifier • Static keyword • Arguments • Return type [access-modifiers] [static] "return-type" "method-name" ([arguments]) { "java code block“ … } return

  13. Examples of a Method public float getAmountDue (String cust){ // method variables int numberOfDays; float due; float lateFee = 1.50F; String customerName; // method body numberOfDays = getOverDueDays(); due = numberOfDays * lateFee; customerName = getCustomerName(cust); return due; } Declaration Methodvariables Methodstatements Return

  14. Declaring Variables • You can declare variables anywhere in a class block, and outside any method. • You must declare variables before they are used inside a method. • It is typical to declare variables at the beginning of a class block. • The scope or visibility of variables is determined in the code block. • You must initialize method variables before using them. • Class and instance variables are automatically initialized.

  15. Examples of Variablesin the Context of a Method public float getAmountDue (String cust) { float due = 0; int numberOfDays = 0; float lateFee = 1.50F; {int tempCount = 1; // new code block due = numberOfDays * lateFee; tempCount++; … } // end code block return due; } Method variables Temporary variables

  16. Rules for Creating Statements • Use a semicolon to terminate statements. • Define multiple statements within braces. • Use braces for control statements.

  17. What Are JavaBeans? • A JavaBean is a platform-neutral reusable software component that: • Can be manipulated visually in a builder tool • Communicates with other JavaBeans via events • Comprises visible components that must inherit from other visible components • Provides an architecture for constructing the building blocks of an application

  18. Managing Bean Properties • Properties are the bean class member variables. (Variables can be primitive types or objects.) • A property can be: • Unbound, which is a simple property • Bound, which triggers an event when the field is altered • Constrained, in which changes are accepted or vetoed by interested listener objects

  19. Exposing Properties and Methods Getter methods (public) Setter methods (public void) private T var; T[] arr; T getVar() T[] getArr() boolean isVar() setVar(T val) setArr(T[] val) setVar(boolean val)

  20. JavaBean Standards at Design Time • The benefits at design time include: • A facilitated interaction between designer, tool, and bean • Instantiated and functioning beans in a visual tool • Highly iterative development environment • Building applications in small bits that plug in and out • Storage and recovery of instantiatedobjects

  21. Compiling and Runninga Java Application • To compile a .java file: • To execute a .class file: • Remember that case matters. prompt> javac SayHello.java … compiler output … prompt> java SayHello Hello world prompt>

  22. The CLASSPATH Variable • Is defined in the operating system • Directs the JVM and Java applications where to find .class files • References built-in libraries or user-defined libraries • Enables interpreter to search paths, and loads built-in classes before user-defined classes • Can be used with “javac” and “java” commands

  23. CLASSPATH: Example Location of .class files in the oe package Setting CLASSPATH C:\>set CLASSPATH=D:labs\les03\classes\oe

  24. Summary • In this lesson, you should have learned the following: • J2SE provides basic Java tools. • J2SE provides a rich set of predefined classes and methods. • Java programs are made up of classes, objects, and methods. • Adhering to programming standards makes code easier to read and reuse.

  25. Practice 3: Overview • This practice covers: • Examining the Java environment • Writing and running a simple Java application • Examining the course solution application • Inspecting classes, methods, and variables • Creating class files and an application class with a main( ) method • Compiling and running an application

More Related