Introduction to Java Chapters 1 and 2 The Java Language – Section 1.1 Data & Expressions – Sections 2.1 – 2.5 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Scope 2 Introduce the Java programming language Program, Class, and Methods The Use of White Space and Comments Strings, Concatenation, and Escape Sequences Declaration and Use of Variables Java Primitive Data Types Syntax and Processing of Expressions Mechanisms for Data Conversion Scott Kristjanson – CMPT 125/126 – SFU Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase Wk01.2 Slide 2 And course material from Diana Cukierman, Lou Hafer, and Greg Baker
Java 3 A computer is made up of hardware and software • hardware – the physical, tangible pieces that support the computing effort • Program – a series of instructions that the hardware executes Programs are sometimes called Applications Software • consists of programs and the data those programs use • Data includes files on disk such as pictures, templates, and databases • Data can also be input from a user, the internet, or from devices Scott Kristjanson – CMPT 125/126 – SFU Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase Wk01.2 Slide 3 And course material from Diana Cukierman, Lou Hafer, and Greg Baker
Java 4 A programming language specifies the words and symbols that we can use to write a program A programming language employs a set of rules that dictate how the words and symbols can be put together to form valid program statements – this is called Syntax The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and its popularity grew quickly, it is now the #1 most widely used programming language [2] Scott Kristjanson – CMPT 125/126 – SFU Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase Wk01.2 Slide 4 And course material from Diana Cukierman, Lou Hafer, and Greg Baker
The Java Programming Language 5 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 These terms will be explored in detail throughout the course A Java application always contains a method called main Scott Kristjanson – CMPT 125/126 – SFU Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase Wk01.2 Slide 5 And course material from Diana Cukierman, Lou Hafer, and Greg Baker
A Java Program 6 public class MyProgram { class header class body Comments can be placed almost anywhere } Scott Kristjanson – CMPT 125/126 – SFU Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase Wk01.2 Slide 6 And course material from Diana Cukierman, Lou Hafer, and Greg Baker
A Java Program 7 // comments about the class public class MyProgram { // comments about the method public static void main(String[] args) { method header method body } } Scott Kristjanson – CMPT 125/126 – SFU Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase Wk01.2 Slide 7 And course material from Diana Cukierman, Lou Hafer, and Greg Baker
Comments 8 Comments should be included to explain the purpose of the program and describe processing Do not explain the obvious, explain the intent of the code at a higher level 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 */ Scott Kristjanson – CMPT 125/126 – SFU Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase Wk01.2 Slide 8 And course material from Diana Cukierman, Lou Hafer, and Greg Baker
A Very Simple Java Program 9 //****************************************************************** // Lincoln.java Java Foundations Comments about the class // // Demonstrates the basic structure of a Java application. //****************************************************************** class header public class Lincoln { //--------------------------------------------------------------- // Prints a presidential quote. Comments about the method //--------------------------------------------------------------- method header public static void main(String[] args) class { method body System.out.println("A quote by Abraham Lincoln:"); body System.out.println("Whatever you are, be a good one."); } } Output: A quote by Abraham Lincoln: Whatever you are, be a good one. Scott Kristjanson – CMPT 125/126 – SFU Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase Wk01.2 Slide 9 And course material from Diana Cukierman, Lou Hafer, and Greg Baker
Identifiers 10 Identifiers are the words a programmer uses in a program to name things • can be made up of letters, digits, the underscore character ( _ ), and the dollar sign • 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 Scott Kristjanson – CMPT 125/126 – SFU Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase Wk01.2 Slide 10 And course material from Diana Cukierman, Lou Hafer, and Greg Baker
Identifiers 11 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 he or she 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 Scott Kristjanson – CMPT 125/126 – SFU Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase Wk01.2 Slide 11 And course material from Diana Cukierman, Lou Hafer, and Greg Baker
Reserved Words 12 Java reserved words: Scott Kristjanson – CMPT 125/126 – SFU Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase Wk01.2 Slide 12 And course material from Diana Cukierman, Lou Hafer, and Greg Baker
White Space 13 In Java: • Spaces, blank lines, and tabs are called white space • White space is used to separate words and symbols in a program • A valid Java program can be formatted many ways • Extra white space and indenting is ignored by the Java compiler • Proper use of White Space is important – for people to understand it • Programs should be formatted to enhance readability, using consistent indentation Scott Kristjanson – CMPT 125/126 – SFU Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase Wk01.2 Slide 13 And course material from Diana Cukierman, Lou Hafer, and Greg Baker
A Poorly formatted version of Lincoln 14 Java may not care about format, but your reader does… Use White Space to highlight program structure Unclear White Space will lose marks for readability in your assignments! //***************************************************************** // Lincoln2.java Java Foundations // // Demonstrates a poorly formatted, though valid, program. //***************************************************************** public class Lincoln2{public static void main(String[]args){ System.out.println("A quote by Abraham Lincoln:"); System.out.println("Whatever you are, be a good one.");}} Scott Kristjanson – CMPT 125/126 – SFU Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase Wk01.2 Slide 14 And course material from Diana Cukierman, Lou Hafer, and Greg Baker
A Horribly formatted version of Lincoln 15 This use of White Space is horribly unclear and could get you ZERO on an assignment! //******************************************************************** // Lincoln3.java Java Foundations // // Demonstrates another valid program that is poorly formatted. //******************************************************************** public class Lincoln3 { public static void main ( String [] args ) { System.out.println ( "A quote by Abraham Lincoln:" ) ; System.out.println ( "Whatever you are, be a good one." ) ; Scott Kristjanson – CMPT 125/126 – SFU } } Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase Wk01.2 Slide 15 And course material from Diana Cukierman, Lou Hafer, and Greg Baker
16 Chapter 2 Data & Expressions – Sections 2.1 – 2.5 Scott Kristjanson – CMPT 125/126 – SFU Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase Wk01.2 Slide 16 And course material from Diana Cukierman, Lou Hafer, and Greg Baker
Scope 17 Character strings and concatenation Escape sequences Declaring and using variables Java primitive types Expressions Data conversions Scott Kristjanson – CMPT 125/126 – SFU Slides based on Java Foundations 3rd Edition, Lewis/DePasquale/Chase Wk01.2 Slide 17 And course material from Diana Cukierman, Lou Hafer, and Greg Baker
Recommend
More recommend