Presentation is loading. Please wait.

Presentation is loading. Please wait.

3 Copyright © 2005, Oracle. All rights reserved. Basic Java Syntax and Coding Conventions.

Similar presentations


Presentation on theme: "3 Copyright © 2005, Oracle. All rights reserved. Basic Java Syntax and Coding Conventions."— Presentation transcript:

1 3 Copyright © 2005, Oracle. All rights reserved. Basic Java Syntax and Coding Conventions

2 3-2 Copyright © 2005, Oracle. All rights reserved. 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 3-3 Copyright © 2005, Oracle. All rights reserved. Notes Page

4 3-4 Copyright © 2005, Oracle. All rights reserved. Examining Toolkit Components The J2SE/J2EE from Sun provides: Compiler Bytecode interpreter Documentation generator J2SE

5 3-5 Copyright © 2005, Oracle. All rights reserved. Exploring Packages in J2SE/J2EE The J2SE/J2EE from Sun provides standard packages for: Language Windowing Input/output Network communication J2SE

6 3-6 Copyright © 2005, Oracle. All rights reserved. Documenting Using the J2SE The J2SE/J2EE from Sun provides documentation support for: Comments –Implementation –Documentation Documentation generator J2SE

7 3-7 Copyright © 2005, Oracle. All rights reserved. Contents of a Java Source A Java source file can contain three top-level constructs: –Only one package keyword followed by the package name, per file –Zero or more import statements followed by fully qualified class names or * qualified by a package name –One or more class or interface definitions followed by a name and block File name must have the same name as the public class or public interface.

8 3-8 Copyright © 2005, Oracle. All rights reserved. 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

9 3-9 Copyright © 2005, Oracle. All rights reserved. Notes Page

10 3-10 Copyright © 2005, Oracle. All rights reserved. More About Naming Conventions Variables: – customerName, customerCreditLimit Constants: – MIN_WIDTH, MAX_NUMBER_OF_ITEMS Uppercase and lowercase characters Numerics and special characters

11 3-11 Copyright © 2005, Oracle. All rights reserved. Notes Page

12 3-12 Copyright © 2005, Oracle. All rights reserved. Defining a Class Class definitions typically include: Access modifier Class keyword Instance fields Constructors Instance methods Class fields Class methods

13 3-13 Copyright © 2005, Oracle. All rights reserved. Rental Class: Example 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 Access modifier

14 3-14 Copyright © 2005, Oracle. All rights reserved. Notes Page

15 3-15 Copyright © 2005, Oracle. All rights reserved. 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"); }

16 3-16 Copyright © 2005, Oracle. All rights reserved. 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

17 3-17 Copyright © 2005, Oracle. All rights reserved. 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 Method variables Method statements Return

18 3-18 Copyright © 2005, Oracle. All rights reserved. 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.

19 3-19 Copyright © 2005, Oracle. All rights reserved. Examples of Variables in 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

20 3-20 Copyright © 2005, Oracle. All rights reserved. Rules for Creating Statements Use a semicolon to terminate statements. Define multiple statements within braces. Use braces for control statements.

21 3-21 Copyright © 2005, Oracle. All rights reserved. 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

22 3-22 Copyright © 2005, Oracle. All rights reserved. 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

23 3-23 Copyright © 2005, Oracle. All rights reserved. Exposing Properties and Methods Getter methods (public) Setter methods (public void) T getVar() T[] getArr() boolean isVar() setVar(T val) setArr(T[] val) setVar(boolean val) private T var; T[] arr;

24 3-24 Copyright © 2005, Oracle. All rights reserved. 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 instantiated objects

25 3-25 Copyright © 2005, Oracle. All rights reserved. Compiling and Running a 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>

26 3-26 Copyright © 2005, Oracle. All rights reserved. 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

27 3-27 Copyright © 2005, Oracle. All rights reserved. CLASSPATH : Example C:\>set CLASSPATH=D:labs\les03\classes\oe Setting CLASSPATH Location of.class files in the oe package

28 3-28 Copyright © 2005, Oracle. All rights reserved. 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.

29 3-29 Copyright © 2005, Oracle. All rights reserved. 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

30 3-30 Copyright © 2005, Oracle. All rights reserved. Notes Page for Practice 3

31 3-31 Copyright © 2005, Oracle. All rights reserved. Practice 3: Notes

32 3-32 Copyright © 2005, Oracle. All rights reserved. Practice 3: Notes

33 3-33 Copyright © 2005, Oracle. All rights reserved. Practice 3: Notes

34 3-34 Copyright © 2005, Oracle. All rights reserved.


Download ppt "3 Copyright © 2005, Oracle. All rights reserved. Basic Java Syntax and Coding Conventions."

Similar presentations


Ads by Google