810 likes | 908 Views
Unit 2: Java Introduction to Programming. Kirk Scott. 2.1 Initial Example 2.2 Information on Program Appearance and Printing 2.3 The Java API Documentation 2.4 Creating and Using Objects 2.5 Errors. 2.1 Initial Example.
E N D
Unit 2: Java Introduction to Programming Kirk Scott
2.1 Initial Example 2.2 Information on Program Appearance and Printing 2.3 The Java API Documentation 2.4 Creating and Using Objects 2.5 Errors
The goal of this section is to give an initial explanation of the first example program, given at the end of the previous set of notes. Here is the program: • public class FirstProg • { • public static void main(String[] args) • { • System.out.println("Hello World"); • } • }
What follows is a step by step description of the syntax of the program along with some explanation of how and why the code is written in this way. • At this early stage it is most important to become familiar with the syntax, so that you can make use of it when beginning to write your own programs.
The explanations are given because most people don’t want to try and learn something in a vacuum. • However, if the explanations seem cryptic or incomplete, don’t worry. • More detail will be given in future units.
1. The first line of code shows the syntax for the declaration of a class: • public class FirstProg • All user written programs are classes. • The class is public, which means it is freely available for general use. • The name of the class is “FirstProg”.
2. The second line of code is quite easy to explain, and it goes with the last line of code. • { • All class definitions are enclosed in a matched set of braces, {}.
3. The third line of code introduces a method. • public static void main(String[] args) • The name of this method is “main()”. • A stand-alone program has to have a main() method. • When you ask the Java system to run the program, the system looks inside the program class and causes this method to run.
The keyword “public” signifies that the method is freely available for the system to call. • The keyword “static” signifies that main() is a general purpose method, not one that is used on an object. • This will be explained in greater detail later. • The keyword “void” indicates that running this method does not cause any value to be returned.
Every method name is followed by a matched pair of parentheses. • When methods are referred to in these notes, they will always be referred to by name plus parentheses, such as “main()”. • This makes it clear that the name “main” is the name of a method rather than the name of something else.
The main() method has something in the parentheses. • These are parameters for command line arguments. • This program does not make use of them, but it is still necessary to include the declaration in the code. • The actual form of the declaration will be explained in a future set of notes.
4. The fourth and sixth lines of code are quite easy to explain. • All method definitions are enclosed in a matched set of braces, {}.
5. The fifth line of code does the real work of the program. • System.out.println("Hello World"); • Here the method println() is called. • This causes output to be sent to the Console. • In general a method is called “on” something, and that is symbolized by the words System and out and the use of dots. • The method is called with the parameter “Hello World”. • Calling methods is important and will be a major topic in the note files.
If you were able to compile and run the program when it was given in Unit 1, you know what it does: • It prints the message “Hello World” in the Console. • The ability to accomplish this is the starting point for learning how to do other things in Java.
Comments are explanatory notes that a programmer can include in a program. • They are not code and they do not affect how the program runs. • On any line where “//” appears, whatever follows this pair of symbols is treated as a comment. • Likewise, anything that appears between “/*” and “*/” will be treated as a comment.
Here is a commented version of the first program: • /* This is my first program. */ • public class FirstProg • { • public static void main(String[] args) • { • // A method is called here. • System.out.println("Hello World"); • } • }
When writing a program it is helpful to indent every set of matched braces so that it is clear where a block of code begins and ends, and whether or not there are nested sets of blocks. • Eclipse will do this for you automatically, and all examples given will be shown in this form.
Here is a brief summary of some of the aspects of printing in Java when using println() and related methods. • 1. The method println() prints a line of output followed by a new line or carriage return. • You can also make use of the method print(), which prints a line without a carriage return. • The complete call would be of the form: • System.out.print("Some Parameter");
2. You can print arithmetic constants as well as strings of characters. • A call such as this would print out the numeric value 3: • System.out.println(3);
Note that what would appear on the screen for the following call would be exactly the same, but that there is an important distinction in the meaning of the program code. • The example above prints a numeric value, while the example below prints a string containing the symbol “3”: • System.out.println("3");
3. You can print out more than one parameter at a time. • The symbol that accomplishes this is the “+” sign. • When printing strings, if you want blank spaces between things, you need to supply them explicitly. If you did this: • System.out.println("Hello" + "World"); • You would see this: • HelloWorld
However, you could also do this: • System.out.println("Hello" + " World"); • Or this: • System.out.println("Hello" + " " + "World"); • And you would see: • Hello World
4. Using the “+” sign when printing numeric values has a different effect. • It does the arithmetic. • If you did this: • System.out.println(3 + 4); • You would see this: • 7
5. You can also combine a parameter in quotes with a numerical value without quotes using a “+” sign. • In this case the system automatically converts the numerical value to a string and does concatenation. • If you did this: • System.out.println("Hello" + 7); • You would see this: • Hello7
The full explanation of the different ways in which the “+” sign works will be given later. • For the time being you should simply be able to use it correctly with strings and numbers.
6. Although the dot has a special syntactic meaning in various places in Java code, in places where simple parameters in the form of numbers are allowed, such as when printing, it has its customary meaning—that of a decimal point. • Thus, if you did this: • System.out.println(3.4); • You would see this value: • 3.4
7. In Java printing, the backslash “\” is used as the escape sequence. • This means that any symbol immediately following the backslash is simply treated as a printable character. • That character is not treated as having any syntactic meaning. • This allows you to print characters that would otherwise be ambiguous or syntactically incorrect in a printing parameter.
If you wanted to print a double quote, you could do this: • System.out.println("\""); • If you wanted to print the backslash itself, you could do this: • System.out.println("\\");
The backslash can also be used to insert special printing instructions into a printed string. \n represents a new line. • Thus, if you did this: • System.out.println("Hello\nWorld"); • You would see this: • Hello • World
What follows are the contents of the Java Application Programming Interface (API) Documentation for the system supplied class Point. • The complete online Java API documentation can easily be found through a Web search. • This is a comprehensive reference for questions about programming in Java. • An explanation of the excerpt from the documentation is given following it.
java.awtClass Point • java.lang.Object | +--java.awt.geom.Point2D | +--java.awt.PointAll Implemented Interfaces: • Cloneable, Serializable
public class Point • extends Point2D • implements Serializable • A point representing a location in (x, y) coordinate space, specified in integer precision. • Since: • JDK1.0 • See Also: • Serialized Form
The information given at the top of the documentation concerns the naming of the class and where it is located. • System supplied classes are arranged in packages. • If you scan through the information at the top you will eventually find this: • java.awt.Point. • “awt” stands for “abstract windowing toolkit”. • This is the name of a package in Java which includes classes related to doing graphical things.
Point is one of those classes. If a program uses a system supplied class, a line like this is put at the top of the code: • import java.awt.Point; • The idea is that this will make the class available for use in the program. • This will be done in the example programs. • It is possible to import all classes in a package at once. • If you chose to do this, you would use the * as a wildcard: • import java.awt.*;
The next segment of interest in the documentation is entitled “Field Summary”. • In the documentation, what are referred to in these notes as instance variables are referred to as fields. • In other words, you discover from this documentation that an object created from the Point class will have two instance variables, an x coordinate and a y coordinate. • These instance variables are given the type “int”, which signifies that these coordinates can take on integer values.
The next segment in the documentation is entitled “Constructor Summary”. • Constructors are special pieces of code used for creating instances of classes. • Constructors have a form reminiscent of methods—they have a name followed by a set of parentheses which may or may not contain parameters.
Constructors are not methods. • Their name is the same as the class they belong to. • As you can see, a single class may have more than one constructor. • The system can tell them apart because they have different parameter lists. • In the documentation the types of the parameters are shown. • For the time being we will restrict our attention to examples with parameters of the type “int”.
The last segment of the documentation is the “Method Summary”. • This gives all of the methods by name and all of their parameters by name, including their types. • There can be different methods with the same name. • The system tells them apart by their parameter lists. • It is only through these methods that a program can affect the instance variables, the x and y coordinates, of a Point object that has been created.
As you can see, int and double are two different numeric types. • The meaning of types will be covered in the next unit. • For the time being, examples will be restricted to the int, or integer type. • This type can hold whole number values.
Writing the code for classes of your own will come later. • At this time it is possible to understand and correctly write lines of code and small, complete programs that construct objects from system supplied classes and then make use of them.
Assuming that the Point class has been imported into a program, the following two lines of code 1) Declare a name which can be used for a Point object, and 2) Construct an object and give it that name, using the “=” sign, or assignment to do so: • Point myPoint; • myPoint = new Point(10, 20);