Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Basic Java Syntax and Coding Conventions Copyright © 2004, 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-2 Copyright © 2004, Oracle. All rights reserved. Examining Toolkit Components The J2SE/J2EE from Sun provides: • Compiler • Bytecode interpreter • Documentation generator J2SE 3-4 Copyright © 2004, 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 3-5 Copyright © 2004, Oracle. All rights reserved. Documenting Using the J2SE The J2SE/J2EE from Sun provides documentation support for: • Comments – Implementation – Documentation • Documentation generator J2SE 3-6 Copyright © 2004, 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 • 3-7 File name must have the same name as the public class or public interface. Copyright © 2004, 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 3-8 Copyright © 2004, Oracle. All rights reserved. More About Naming Conventions • Variables: – customerName, customerCreditLimit • Constants: – MIN_WIDTH, MAX_NUMBER_OF_ITEMS • • 3-10 Uppercase and lowercase characters Numerics and special characters Copyright © 2004, Oracle. All rights reserved. Defining a Class Class definitions typically include: • Access modifier • Class keyword • Instance fields • Constructors • Instance methods • Class fields • Class methods 3-12 Copyright © 2004, Oracle. All rights reserved. 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) { … } … } 3-13 Copyright © 2004, Oracle. All rights reserved. Declaration Instance variable Instance method 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"); } } 3-15 Copyright © 2004, 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 3-16 Copyright © 2004, 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; } 3-17 Copyright © 2004, Oracle. All rights reserved. Declaration Method variables Method statements Return Declaring Variables • • • • • • 3-18 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. Copyright © 2004, 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++; … } return due; // end code block } 3-19 Copyright © 2004, Oracle. All rights reserved. Method variables Temporary variables Rules for Creating Statements • • • 3-20 Use a semicolon to terminate statements. Define multiple statements within braces. Use braces for control statements. Copyright © 2004, 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 3-21 Copyright © 2004, 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 3-22 Copyright © 2004, Oracle. All rights reserved. Exposing Properties and Methods Getter methods (public) T 3-23 private T var; T[] arr; getVar() Setter methods (public void) setVar(T val) T[] getArr() setArr(T[] val) boolean isVar() setVar(boolean val) Copyright © 2004, 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 3-24 Copyright © 2004, Oracle. All rights reserved. Compiling and Running a Java Application • To compile a .java file: prompt> javac SayHello.java … compiler output … • To execute a .class file: prompt> java SayHello Hello world prompt> • 3-25 Remember that case matters. Copyright © 2004, 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 • • 3-26 Copyright © 2004, Oracle. All rights reserved. CLASSPATH: Example Location of .class files in the oe package Setting CLASSPATH C:\>set CLASSPATH=D:labs\les03\classes\oe 3-27 Copyright © 2004, 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. 3-28 Copyright © 2004, 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 • 3-29 Compiling and running an application Copyright © 2004, Oracle. All rights reserved.