1 / 131

Chapter 2 Java Fundamentals

Chapter 2 Java Fundamentals. Contents. The Parts of a Java Program The print and println Methods, and the Java API Variables and Literals Primitive Data Types Arithmetic Operators Combined Assignment Operators Conversion between Primitive Types Creating Named Constraints with final.

urbano
Télécharger la présentation

Chapter 2 Java Fundamentals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2Java Fundamentals

  2. Contents • The Parts of a Java Program • The print and println Methods, and the Java API • Variables and Literals • Primitive Data Types • Arithmetic Operators • Combined Assignment Operators • Conversion between Primitive Types • Creating Named Constraints with final

  3. Contents • The String Class • Scope • Comments • Programming Style • Reading Keyboard Input • Dialog Box

  4. 1. The Parts of a Java Program • Problem • Write a Java program to display a message (Programming is great fun!) on the screen.

  5. 1. The Parts of a Java Program (Cont’d) • Run eclipse • Create a new Java project • Enter the project name: MyFirstProject • Create a new Java class: • Enter the class name: Simple • This will create a source code file Simple.java • Enter the code

  6. 1. The Parts of a Java Program (Cont’d)

  7. 1. The Parts of a Java Program (Cont’d) • Run the program

  8. 1. The Parts of a Java Program (Cont’d) • Line 1: //This is a simple Java program • // : • marks the beginning of a comment. • The compiler ignores everything from the double-slash to the end of the line. You can type anything you want. • Comments help explain what’s going on.

  9. 1. The Parts of a Java Program (Cont’d) • Line 2: • A blank line • Programmers often insert blank lines in programs to make them easier to read. • Line 3: public class Simple • This is a class header, and it marks the beginning of a class definition.

  10. 1. The Parts of a Java Program (Cont’d) • A Java program must have at least one class definition. • public: • public is a Java keyword • must be written in all lowercase letters • public is an access specifier, and it controls where the class may be accessed from. The public specifier means access to the class is unrestricted (the class is “open to the public”).

  11. 1. The Parts of a Java Program (Cont’d) • class: • a Java keyword, must be written in lowercase letter. • indicates the beginning of a class definition. • Simple: • Simple is the class name which is made up by the programmer. Programmer-defined names may be written in lowercase letters, uppercase letters, or a mixture of both.

  12. 1. The Parts of a Java Program (Cont’d) public class Simple • tell the compiler that a public accessible class named Simple is being defined. • We can create more than one class in a file, but we may only have one public class per Java file. • When a Java file has a public class, the name of the public class must be the same as the name of the file (without the .java extension). • Java is a case-sensitive language. Public ≠ public ≠ puBlic ≠ pUblic …

  13. 1. The Parts of a Java Program (Cont’d) • Line 4: { • is a left brace, or an opening brace, and is associated with the beginning of the class definition. • All programming statements that are parts of class are enclosed in a set of braces. In line 9, we will have the closing brace. • Everything between the two braces is the body of the class named Simple.

  14. 1. The Parts of a Java Program (Cont’d)

  15. 1. The Parts of a Java Program (Cont’d) Name of the method • Line 5: public static void main(String[] args) • a method header, the beginning of a method. • main is a name of the method. Every Java program must have a method named main. The main method is the starting point of an application.

  16. 1. The Parts of a Java Program (Cont’d) • Line 6: { • This opening brace belongs to the main method. Every opening brace must have a accompanying closing brace. • We will have a closing brace in line 8 that corresponds with this opening brace. • Everything between these braces is the body of the main method.

  17. 1. The Parts of a Java Program (Cont’d) • Line 7: System.out.println("Programming is great fun!"); • This line is a statement. It displays a message on the screen. The message, “Programming is great fun!”, is printed without the quotation marks. • The group of characters inside the quotation marks is called a string literal. • There is a semicolon at the end of this line. Semicolon marks end of a statement in Java.

  18. 1. The Parts of a Java Program (Cont’d) • Not every line of code ends with a semicolon: • Comments do not have to end with a semicolon • Class headers and method headers do not end with a semicolon. • The brace characters, { and }, are not statements, so we do not place a semicolon after them. • Lines 8 and 9 contain the closing braces for main method and the class definition.

  19. 1. The Parts of a Java Program (Cont’d) • Java is a case-sensitive language. • All Java program must be stored in a file with a name that ends with .java • Comments are ignored by the compiler. • A .java file may contain many classes, but may only have one public class. • If a .java file has a public class, the class must have the same name as the file.

  20. 1. The Parts of a Java Program (Cont’d) • Every Java application program must have a method named main. • For every brace, or opening brace, there must be a corresponding right brace, or closing brace. • Statements are terminated with semicolons. This does not include comments, class headers, method headers, or braces.

  21. 2. The print and println Methods, and Java API • The print and println methods • are used to display text output. • are parts of the Java API (Application Programmer Interface). • API is a collection of prewritten classes and methods for performing specific operations. • The console • Standard output: the monitor • Standard input: the keyboard

  22. 2. The print and println Methods, and Java API System.out.println("Programming is great fun!"); Hierarchical Relationship among the System class, the out object, and the print and println methods

  23. 2. The print and println Methods, and Java API • The System class is part of the java API. It has member objects and methods for performing system-level operations, such as sending output to the console. • The out object is a member of the System class. It provides methods for sending output to the screen. • The print and println methods are members of the out object. They actually perform the work of writing characters on the screen. • We use the period to separate System, out and print or println. The period is pronounced “dot”.

  24. 2. The print and println Methods, and Java API • The value that is to be displayed on the screen is places inside the parentheses. This value is known as an argument. System.out.println(“King Arthur”); • The printlnmethod is that after it displays its message, it advances the cursor to the beginning of the next line.

  25. 2. The print and println Methods, and Java API

  26. The print Method • The print method • part of the System.out object. • serves a purpose to display output on the screen. • does not advance the cursor to the next line after its message is displayed.

  27. The print Method

  28. The print Method

  29. The print Method • There are two ways to fix the Unruly program: • use println method ? • use escape sequences to separate the output into different lines • An escape sequence starts with the blackslash character (\), and is followed by one or more control characters. • The escape sequence that causes the output cursor to go to the next line is \n • \n is called the newline escape sequence.

  30. The print Method

  31. The print Method

  32. Common Escape Sequences • See table 2-2 • \n New line • \t Horizontal Tab • \b backspace • \r Return • \\ Backslash • \’ Single quote • \” Double quote

  33. 3. Variables and Literals • A variable is a named storage location in the computer’s memory. • A literal is a value that is written into the code of a program.

  34. 3. Variables and Literals (Cont’d)

  35. 3. Variables and Literals (Cont’d) • Line 7: int value; • Variable declaration • Variables must be declared before they can be used. • A variable declaration tells the compiler • the variable’ name • the type of data the variable will hold variable’s name data type

  36. 3. Variables and Literals (Cont’d) • Line 9: value=5; • Assignment statement • The equal sign = is an operator that stores the value on its right (5) into the variable named on its left (value). • After this line executes, the value variable will contain the value 5.

  37. 3. Variables and Literals (Cont’d)

  38. 3. Variables and Literals (Cont’d) • Line 11: System.out.println(value); • The method println will display the variable’s contents on the console. • There are no quotation marks around variable value • Compare with System.out.println(“value”);

  39. Display Multiple Items with the + Operator • The + operator is used with strings: • String concatenation operator System.out.println(“This is ” + “one string.”); This is one string. • The + operator can be used to concatenate the contents of a variable to a string number = 5; System.out.println(“The value is ” + number); The value is 5

  40. Display Multiple Items with the + Operator (Cont’d) • A string literal cannot begin on one line and end on another System.out.println(“Enter a value that is greater than zero and less than 10.”); • Breaking the argument up into smaller string literals, and use the + operator System.out.println(“Enter a value that” + “ is greater than zero and less “ + “than 10.”); Error

  41. Display Multiple Items with the + Operator (Cont’d)

  42. Identifiers • Identifier • A programmer-defined name. • Do not use any of the Java keywords for identifiers. • Represents some element of a program. • Variable names, class names, name of methods, …

  43. Identifiers (Cont’d) • Should choose names that give an indication of what they are used for, what the purpose is • For example • Number of ordered items int x; not good intitemsOrdered;

  44. Identifiers (Cont’d) • There are some rules with all identifiers: • The first character must be one of the letters a-z, A-Z, _, $ • After the first character, we can use letters a-z, A-Z, digits 0-9, _, $ • Uppercase and lowercase characters are distinct. • Identifiers cannot include spaces

  45. Identifiers (Cont’d)

  46. Identifiers (Cont’d) • Variable Names • It is standard practice to begin variable names with a lowercase letter • Capitalize the first the first letter of each subsequent word. intitemOdered;

  47. Identifiers (Cont’d) • Class Names • It is standard practice to begin variable names with a uppercase letter • Capitalize the first letter of each subsequent word. public class PayRoll

  48. 4. Primitive Data Types • Each variable has a data type, which is the type of data that the variable can hold. • Selecting the proper type for variables is important • amount of memory • the way the variable formats and stores data

  49. 4. Primitive Data Types (Cont’d) • Primitive data types for numeric data Integer Data Types Floating-Point Data Types

  50. 4. Primitive Data Types (Cont’d) • General format of a variable declaration DataTypeVariableName; • DataType: name of the data type • VariableName: name of the variable • Examples: byte inches; int speed; short month; float salesComission; double distances;

More Related