Proprietary Example Program Hello World public class Hello
Proprietary Example Program - Hello. World public class Hello. World { public static void main(String[ ] args) { System. out. println(“Hello. World!”); } } • Save source in file named Hello. World. java University of Houston-Clear Lake
Proprietary Compile to Byte-Code • To compile Hello. World. java , use the command: javac Hello. World. java • To execute the program with the JVM, use: java Hello. World University of Houston-Clear Lake
Proprietary Java Programs • Standalone applications – Run directly using the Java interpreter • Applets – Do not require a main() method since they run within another application – Usually run from within an Internet browser or the appletviewer program – Requires HTML with the <APPLET> tag, for example: <APPLET code=“Scribble. class” width=500 height=300> </APPLET> University of Houston-Clear Lake
Proprietary A Program in Java • Made up of one or more class definitions • Each class is compiled into its own. class file • One class must include a main() method public static void main(String args[]) • Interpreter runs until main() returns or it reaches the end of main(). University of Houston-Clear Lake
Proprietary Features of Java • No global variables • Fields and methods are called members of a class • Every field or method may be referenced by its fully qualified name, for example: package name class method name java. lang. String. substring() University of Houston-Clear Lake
Proprietary Class Path • Tells interpreter where to search for classes • On UNIX systems use the environment variable CLASSPATH setenv CLASSPATH. : /users/mine/classes use colons • On Windows systems use: setenv CLASSPATH. ; C: usersmineclasses use semicolons University of Houston-Clear Lake
Proprietary Packages and Classes • If part of a package first executable statement in the source file must be the package statement • Otherwise, it’s part of an unnamed default package class method field nested class members java 1. 1 University of Houston-Clear Lake
Proprietary The import Statement • Allows abbreviated names for classes import package. class; import package. *; • For example: import java. net. URL; • Then in your program, you may use: URL. get. Host() instead of java. net. URL. get. Host() University of Houston-Clear Lake
Accessibility • Classes are accessible if the appropriate directories and files are accessible and you have the appropriate permissions • Classes and interfaces are accessible to others within the same package (unless restricted) • All members of a class are accessible within class • Public classes are accessible outside the package, other (non-public) classes are not • Private members are accessible only from within their own class Proprietary University of Houston-Clear Lake
Proprietary Accessibility • member. A is accessible from method. B if class. A is public and member. A is public • OR member. A is accessible from method. B if class. A is public and member. A is protected and class. B is a subclass of class. A package class. A class. B member. A accessible method. B University of Houston-Clear Lake
Proprietary Other Features • Local variables (like in C) • Comments – like in C /* this is a comment */ – like in C++ // this is another comment best to use – javadoc comments /** This is a javadoc comment */ • Unlike C, there is no preprocessor, no macros • Constants -> use for example: public static final double PI 2 9. 8696 p 2 University of Houston-Clear Lake
Proprietary No Include Files • Unlike C, Java has no include files • Java maps class names to a directory java. lang. Math java/lang/Math. class • No distinction between declaring and defining a function in Java, so no need for placing prototypes in header files as common in C • Java supports the referencing of members of classes in other packages through the import statement University of Houston-Clear Lake
Proprietary Unicode and Character Escapes • 16 -bit character code (instead of 8 -bit) • Easier to internationalize your program • Character representation is transparent • Unicode escape sequence can appear anywhere in your program, e. g. u 9 A 1 C Hexadecimal value • Support for standard C escape sequences such as n t • Concatenation of strings with “+” operator University of Houston-Clear Lake
Proprietary Overview of Primitive Types See pages 23 -24 in text University of Houston-Clear Lake
Proprietary The boolean Type • Booleans are not integers and may not be cast to integers • You may wish to use: a_bool = ( my_int != 0 ); // integer-to-boolean my_int = a_bool ? 1 : 0; // boolean-to-integer University of Houston-Clear Lake
Proprietary The char Type • Literals in single quotes char c = 'A'; • Values of type char do not have a sign • No sizeof operator • No pointer arithmetic University of Houston-Clear Lake
Proprietary Integer Types • byte - 8 bits signed • short - 16 bits signed • char - 16 bits unsigned • int - 32 bits signed • long - 64 bits signed University of Houston-Clear Lake
Proprietary Floating Point Types • float and double • May append f or F and d or D • Special values of POSITIVE_INFINITY, NEGATIVE_INFINITY, and Na. N may be assumed • Floating-point arithmetic never causes exceptions, even in the case of division by zero. University of Houston-Clear Lake
Proprietary String Literals • Strings in Java are not a primitive type, but are instances of the String class • May use double quotes “foo bar baz” • When the compiler encounters such a string literal, it automatically creates the necessary String object. University of Houston-Clear Lake
Proprietary Non-Primitive Types • Non-primitive data types are objects and arrays • They are also called reference types since they are handled by reference rather than by value • In C, you might use * , -> , and & operators to handle such objects • In Java, such operators do not exist • Primitive types are always passed by value • Arrays and objects are always passed by reference University of Houston-Clear Lake
Assignment with Primitive Types int k = 6; // k contains the value 6 int j = k; // j contains a copy of the value in k k= 2; Proprietary // Changing k doesn't change j // Now, k == 2 and j == 6 University of Houston-Clear Lake
Assignment of Reference Types Button p, q; p = new Button(); q = p; // p refers to a Button object. // q refers to the same Button. p. set. Label("Hi"); String s = q. get. Label(); Proprietary Creates a new Button object // A change to the object through p //. . . is also visible through q. // s now contains "Hi" Methods for Button objects p q Hi University of Houston-Clear Lake
Cloning Objects To copy the data of one object into another object, use the clone() method: Vector b = new Vector; c = b. clone(); Proprietary b c University of Houston-Clear Lake
Proprietary Checking for Equality of Objects • The == operator tests whether two variables refer to the same object, not whether two objects contain the same values • To test whether two separate objects are the same, you must use a specially written method for that object type as you might use strcmp() to compare C strings for equality • In Java, a number of classes define an equals() method that you can use to perform this test University of Houston-Clear Lake
Proprietary No Pointers in Java • You cannot cast object or array references into integers or vice-versa • You cannot do pointer arithmetic • Reasons: – Pointers are a notorious source of bugs. Eliminating them simplifies the language and eliminates many potential bugs – Pointers and pointer arithmetic could be used to sidestep Java's run-time checks and security mechanisms University of Houston-Clear Lake
Proprietary Default Values • The value null is a special value that means "no object" or indicates an absence of reference • Each primitive type also has a default value, usually zero (or false) University of Houston-Clear Lake
- Slides: 26