Download presentation
1
The Java Programming Language
import java.util.*; public class Greeter1 { public static void main (String [] args) { Date currentDate = new Date(); String today = currentDate.toString(); Screen theScreen = new Screen(); theScreen.println ("Welcome! today, " today + ", you begin to study Java!"); }
2
Introduction to Java Application Programs
Java is an object oriented programming language Uses objects to carry out the tasks Sends messages to the objects to perform the tasks Objects interact with each other to do the tasks An actual object is called an instance of a class The class is the declaration of or blueprint for the object Object oriented programs: A collection of object interactions that solve a problem
3
Java Program Structure
In the Java programming language: A program is made up of one or more classes A class contains one or more methods A method contains program statements
4
Lincoln.java //******************************************************************** // Lincoln.java // Demonstrates the basic structure of a Java application. public class Lincoln { // // Prints a presidential quote. public static void main (String[] args) System.out.println ("A quote by Abraham Lincoln:"); System.out.println ("Whatever you are, be a good one."); }
5
The Class A class is the basic building block of an object-oriented language Is a template that describes the data and behavior associated with instances of that class Each class represented by a single file When you instantiate a class you create an object that looks and feels like other instances of the same class The data associated with a class or object is stored in variables The behavior associated with a class or object is implemented with methods
6
Java Program Structure
// comments about the class public class MyProgram { } class header class body Comments can be placed almost anywhere
7
Class Definition Block
In the Java language, the simplest form of a class definition is public class name { } The keyword class begins the class definition for a class named name. The variables and methods of the class are embraced by the curly brackets that begin and end the class definition block. public class MyProgram { }
8
The Method Java lets a programmer break down complex tasks into simpler units called methods A method definition must occur inside a class A method can occur anywhere inside the class although custom places all the other methods of a class before the main method. The method body consists of one or more method variables and program statements
9
Java Program Structure (methods)
// comments about the class public class lotteryodds { } method header // comments about the method public static void long lotteryodds (int high, int number) { } long r = 1; int i; for (i = 1; i <= number; i++) { r = r * high/i; high--; } return r; method body
10
signature or method signature
What variable types and in what order they are passed to a method. The return type is not a part of the signature. In the following method "String, double" is the signature: public int returnAnInt (String stringIn, double doubleIn) { int intToReturn = 1; if (stringIn.equals(String.valueOf(doubleIn)) intToReturn = 2; } return intToReturn; }
11
Main Method Every Java application must contain a main method whose signature looks like this public static void main(String[] args) The method signature for the main method contains three modifiers: public indicates that the main method can be called by any object. static indicates that the main method is a class method. void indicates that the main method doesn't return any value.
12
Java Program Structure (methods)
// comments about the class public class MyProgram { } // comments about the method public static void main (String[] args) { } method header method body
13
How main Gets Called When the Java interpreter executes an application (by being invoked upon the application's controlling class), it starts by calling the class's main method. The main method then calls all the other methods required to run your application. If you try to invoke the Java interpreter on a class that does not have a main method, the interpreter refuses to run your program and displays an error message
14
Comments Comments in a program are called inline documentation
They should be included to explain the purpose of the program and describe processing steps They do not affect how a program works Java comments can take three forms: // this comment runs to the end of the line /* this comment runs to the terminating symbol, even across line breaks */ /** this is a javadoc comment */
15
Identifiers Identifiers are the words a programmer uses in a program
An identifier can be made up of letters, digits, the underscore character ( _ ), and the dollar sign Identifiers cannot begin with a digit Java is case sensitive - Total, total, and TOTAL are different identifiers By convention, programmers use different case styles for different types of identifiers, such as title case for class names - Lincoln upper case for constants - MAXIMUM
16
Identifiers Sometimes we choose identifiers ourselves when writing a program (such as Lincoln) Sometimes we are using another programmer's code, so we use the identifiers that they chose (such as println) Often we use special identifiers called reserved words that already have a predefined meaning in the language A reserved word cannot be used in any other way
17
Reserved Words The Java reserved words: abstract boolean break byte
case catch char class const continue default do double else enum extends false final finally float for goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while
18
White Space Spaces, blank lines, and tabs are called white space
White space is used to separate words and symbols in a program Extra white space is ignored A valid Java program can be formatted many ways Programs should be formatted to enhance readability, using consistent indentation
19
Syntax and Semantics The syntax rules of a language define how we can put together symbols, reserved words, and identifiers to make a valid program The semantics of a program statement define what that statement means (its purpose or role in a program) A program that is syntactically correct is not necessarily logically (semantically) correct A program will always do what we tell it to do, not what we meant to tell it to do
20
Errors A program can have three types of errors
The compiler will find syntax errors and other basic problems (compile-time errors) If compile-time errors exist, an executable version of the program is not created A problem can occur during program execution, such as trying to divide by zero, which causes a program to terminate abnormally (run-time errors) A program may run, but produce incorrect results, perhaps using an incorrect formula (logical errors)
21
Basic Program Development
Edit and save program errors errors Compile program Execute program and evaluate results
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.