University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish
Outlines History of Java Java Class Libraries A First Program in Java: Printing a Line of Text Modifying Our First Java Program Displaying Text with printf
History of Java Sun Microsystems in 1991 funded an internal corporateresearch project code-named Green, which resulted in a C++-based language that its creator, James Gosling, called Oak after an oak tree outside his window at Sun It was later discovered that there already was a computer language by that name. When a group of Sun people visited a local coffee shop, the name Java was suggested, and it stuck.
History of Java The Green project ran into some difficulties. The marketplace for intelligent consumer- electronic devices was not developing in the early 1990s as quickly as Sun had anticipated. The project was in danger of being cancelled. but the World Wide Web exploded in popularity in 1993, and Sun people saw the immediate potential of using Java to add dynamic content, such as interactivity and animations, to web pages.
Java Class Libraries Java programs consist of pieces called classes. Classes include pieces called methods that perform tasks and return information when they complete them
Java Class Libraries Programmers can create each piece they need to form Java programs. However, most Java programmers take advantage of the rich collections of existing classes in the Java class libraries, which are also known as the Java APIs (Application Programming Interfaces).
A First Program in Java: Printing a Line of Text Every time you use a computer, you execute various applications that perform tasks for you. For example, your application helps you send and receive e- mail. Computer programmers create such applications by writing computer programs.
A First Program in Java: Printing a Line of Text A Java application is a computer program that executes when you use the java command to launch the Java Virtual Machine (JVM).
A First Program in Java: Printing a Line of Text
Line 1 begins with //, indicating that the remainder of the line is a comment This helps other people read and understand your programs. The Java compiler ignores comments, so they do not cause the computer to perform any action when the program is run.
A First Program in Java: Printing a Line of Text A comment that begins with // is called an end-of-line (or single-line) comment, because the comment terminates at the end of the line on which it appears. A // comment also can begin in the middle of a line and continue until the end of that line (as in lines 11 and 13).
A First Program in Java: Printing a Line of Text Traditional comments (also called multiple-line comments), such as This type of comment can be spread over several lines. It begins with the delimiter /* and ends with */. All text between the delimiters is ignored by the compiler.
A First Program in Java: Printing a Line of Text Java also provides Javadoc comments that are delimited by /** and */. As with traditional comments, all text between the Javadoc comment delimiters is ignored by the compiler. Javadoc comments enable programmers to embed program documentation directly in their programs. The javadoc utility program (part of the Java SE Development Kit) reads Javadoc comments and uses them to prepare your program’s documentation in HTML format
A First Program in Java: Printing a Line of Text Line 3 is a blank line. Programmers use blank lines and space characters to make programs easier to read. Together, blank lines, space characters and tab characters are known as white space. (Space characters and tabs are known specifically as white-space characters.) White space is ignored by the compiler. using white space enhance program readability.
A First Program in Java: Printing a Line of Text
Line 4 begins a class declaration for class Welcome1. Every program in Java consists of at least one class declaration that is defined by you—the programmer. These are known as programmer defined classes or user-defined classes. The class keyword introduces a class declaration in Java and is immediately followed by the class name (Welcome1). Keywords (sometimes called reserved words) are reserved for use by Java and are always spelled with all lowercase letters. The complete list of Java keywords is shown in Appendix C.
A First Program in Java: Printing a Line of Text By convention, all class names in Java begin with a capital letter and capitalize the first letter of each word they include (e.g., SampleClassName). A Java class name is an identifier an identifier a series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ($), it does not begin with a digit and does not contain spaces.
Class work Write five valid identifier and five invalid identifiers
Class work Some valid identifiers are: Welcome1, $value, _value, m_inputField1 button7. Some invalid identifiers are: 1 welcome, 3button, input field, m 3 n, $ value
A First Program in Java: Printing a Line of Text Java is case sensitive—that is, uppercase and lowercase letters are distinct, so a1 and A1 are different (but both valid) identifiers. A left brace (at line 5 in this program), {, begins the body of every class declaration. A corresponding right brace (at line 13), }, must end each class declaration.
A First Program in Java: Printing a Line of Text Line 7 (the main method) is the starting point of every Java application. The parentheses after the identifier main indicate that it is a program building block called a method. Java class declarations normally contain one or more methods. For a Java application, exactly one of the methods must be called main, otherwise, the JVM will not execute the application
A First Program in Java: Printing a Line of Text Methods are able to perform tasks and return information when they complete their tasks. Keyword void indicates that this method will perform a task but will not return any information when it completes its task The left brace, {, in line 8 begins the body of the method declaration. A corresponding right brace, }, must end the method declaration’s body (line 11 of the program).
A First Program in Java: Printing a Line of Text Line 9 System.out.println( "Welcome to Java Programming!" ); instructs the computer to perform an action— namely, to print the string of characters contained between the double quotation marks (but not the quotation marks themselves).
A First Program in Java: Printing a Line of Text A string is sometimes called a character string, a message or a string literal. We refer to characters between double quotation marks simply as strings. White-space characters in strings are not ignored by the compiler.
A First Program in Java: Printing a Line of Text System.out is known as the standard output object. System.out allows Java applications to display sets of characters in the command window from which the Java application executes Method System.out.println displays (or prints) a line of text in the command window. The string in the parentheses in line 9 is the argument to the method.
A First Program in Java: Printing a Line of Text Method System.out.println performs its task by displaying (also called outputting) its argument in the command window. When System.out.println completes its task, it positions the output cursor (the location where the next character will be displayed) to the beginning of the next line in the command window.
A First Program in Java: Printing a Line of Text The entire line 9, including System.out.println, the argument "Welcome to Java Programming!" in the parentheses and the semicolon (;), is called a statement.