Chapter 1 Introduction to JAVA. Why Learn JAVA? Java is one of the fastest growing programming language in the world. Java is one of the fastest growing.

Slides:



Advertisements
Similar presentations
Chapter 2 First Java Programs
Advertisements

IT151: Introduction to Programming
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Lecture 2 Introduction to C Programming
University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter2: Introduction to Java Applications 1.
Chapter 1: Introduction
Chapter 2 - Introduction to Java Applications
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
CMT Programming Software Applications
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
Chapter 2 - Introduction to Java Applications
1. 2 Chapter 1 Introduction to Computers, Programs, and Java.
Chapter 2 First Java Programs
Computer Programming 12 Mr. Jean March 3 rd, 2014.
1.1 History of Computers 1940s: The ENIAC was one of the world’s first computers. Large stand-alone machine Used large amounts of electricity Contained.
Java Applications & Program Design
Lesson 2: First Java Programs
 2005 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
(C) 2010 Pearson Education, Inc. All rights reserved.  Java programs normally go through five phases  edit  compile  load  verify  execute.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Chapter 2 First Java Programs Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Computer Programming 12 Mr. Jean March 19 th, 2013.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
1.8History of Java Java –Based on C and C++ –Originally developed in early 1991 for intelligent consumer electronic devices Market did not develop, project.
Lesson 2: First Java Programs. Objectives: –Discuss why Java is an important programming language. –Explain the Java virtual machine and byte code. –Choose.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
8/31: Intro to Java, Languages, and Environments Types of programming languages –machine languages –assembly languages –high-level languages Java environment.
1 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Introduction to Java Applications Part I. In this chapter you will learn:  Why Java?  History of Java.  To write simple Java applications. 2.
1/16: Intro to Java, Languages, and Environments Types of programming languages –machine languages –assembly languages –high-level languages Java environment.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
Chapter 2 First Java Programs Fundamentals of Java.
Introduction to 1. What is Java ? Sun Microsystems Java is a programming language and computing platform first released by Sun Microsystems in The.
1 Sections Java Virtual Machine and Byte Code Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Lesson 2: First Java Programs. 2.1 Why Java? Java is one of the most popular programming languages in the world. Java is a modern object-oriented programming.
1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing a Line of Text 2.2.1Compiling and Executing.
2.5 Edit, Compile, and Execute Figure 2-3 illustrates the edit, compile and execute steps. Java bytecode.
Introduction to Java Applications
CSC201: Computer Programming
GC101 Introduction to computer and program
Chapter 3 GC 101 Java Fundamentals.
Introduction to.
Chapter 2 Introduction to Java Applications
Text by: Lambert and Osborne
Chapter 2 Introduction to Java Applications
Chapter 2 First Java Programs
Java programming lecture one
Chapter 2 - Introduction to Java Applications
Fundamentals of Java Programs, Input/Output, Variables, and Arithmetic
Chapter 2 - Introduction to Java Applications
MSIS 655 Advanced Business Applications Programming
Chapter 2 - Introduction to Java Applications
Introduction to Java Applications
Chapter 2 - Introduction to Java Applications
Computer Programming-1 CSC 111
Presentation transcript:

Chapter 1 Introduction to JAVA

Why Learn JAVA? Java is one of the fastest growing programming language in the world. Java is one of the fastest growing programming language in the world. Java is a modern object-oriented programming language. Java is a modern object-oriented programming language. Java is secure, robust, and portable. Java is secure, robust, and portable. Enables the construction of virus-free, tamper free systems (secure) Enables the construction of virus-free, tamper free systems (secure) Supports the development of programs that do not overwrite memory (robust) Supports the development of programs that do not overwrite memory (robust) Yields programs that can be run on different types of computers without change (portable) Yields programs that can be run on different types of computers without change (portable)

Why Learn JAVA? Java supports the use of advanced programming concepts such as threads. Java supports the use of advanced programming concepts such as threads. Thread – a process that can run concurrently with other processes. Thread – a process that can run concurrently with other processes. Java bears a superficial resemblance to C++, which is currently the world’s most popular industrial strength programming language. Java bears a superficial resemblance to C++, which is currently the world’s most popular industrial strength programming language.

The JAVA Virtual Machine Compilers usually translate a higher-level language into the machine language of a particular type of computer. Compilers usually translate a higher-level language into the machine language of a particular type of computer. However, the Java compiler translates Java not into machine language, but into a pseudo- machine language called Java byte code. However, the Java compiler translates Java not into machine language, but into a pseudo- machine language called Java byte code. Byte code - is the machine language for an imaginary Java computer. To run Java byte code on a particular computer, you must install a Java virtual machine (JVM) on that computer. Byte code - is the machine language for an imaginary Java computer. To run Java byte code on a particular computer, you must install a Java virtual machine (JVM) on that computer. A JVM is a program that behaves like a computer. Such a program is interpreter. A JVM is a program that behaves like a computer. Such a program is interpreter.

User Interface Styles There are two types of user interfaces a programmer can develop. There are two types of user interfaces a programmer can develop. Terminal I/O interface – A text based DOS console window. Terminal I/O interface – A text based DOS console window. Graphical User Interface (GUI) – A windowed based environment with text and graphics displayed in the window. Graphical User Interface (GUI) – A windowed based environment with text and graphics displayed in the window. We will focus on Terminal I/O in this class. We will focus on Terminal I/O in this class.

JAVA Class Libraries Java Programs Java Programs Classes contain methods, which perform tasks Classes contain methods, which perform tasks Consist of pieces called classes Consist of pieces called classes Class libraries Class libraries Rich collection of predefined classes, which you can use Rich collection of predefined classes, which you can use Two parts of learning Java Two parts of learning Java Learning the language itself, so you can create your own classes Learning the language itself, so you can create your own classes Learning how to use the existing classes in the libraries Learning how to use the existing classes in the libraries

Basics of a Typical Java Environment Java Systems Java Systems Consist of environment, language, Class libraries Consist of environment, language, Class libraries Java programs have five phases Java programs have five phases - Edit Use an editor to type Java program Use an editor to type Java program Notepad, Jbuilder, Visual J++, Jcreator, BlueJ, Eclipse Notepad, Jbuilder, Visual J++, Jcreator, BlueJ, Eclipse.java extension.java extension - Compile Translates program into bytecodes, understood by Java interpreter Translates program into bytecodes, understood by Java interpreter Creates.class file, containing bytecodes (MyProgram.class) Creates.class file, containing bytecodes (MyProgram.class)

Basics of a Typical Java Environment Java programs have five phases (continued) Java programs have five phases (continued) - Loading Class loader transfers.class file into memory Class loader transfers.class file into memory Applications - run on user's machine Applications - run on user's machine Applets - loaded into Web browser, temporary Applets - loaded into Web browser, temporary Classes loaded and executed by interpreter with java command Classes loaded and executed by interpreter with java command HTML documents can refer to Java Applets, which are loaded into web browsers. HTML documents can refer to Java Applets, which are loaded into web browsers. appletviewer is a minimal browser, can only interpret applets appletviewer is a minimal browser, can only interpret applets

Basics of a Typical Java Environment Java programs have five phases (continued) Java programs have five phases (continued) - Verify Bytecode verifier makes sure bytecodes are valid and do not violate security Bytecode verifier makes sure bytecodes are valid and do not violate security Java must be secure - Java programs transferred over networks, possible to damage files (viruses) Java must be secure - Java programs transferred over networks, possible to damage files (viruses) - Execute Computer (controlled by CPU) interprets program one bytecode at a time Computer (controlled by CPU) interprets program one bytecode at a time Performs actions specified in program Performs actions specified in program Program may not work on first try Program may not work on first try Make changes in edit phase and repeat Make changes in edit phase and repeat

Example Program Java program //Example Program Welcome.java //A first program in Java public class Welcome { public static void main(String [ ] args { System.out.println(“Welcome to Java Programming!”); } Program Output Welcome to Java Programming!

Commenting Code //Example Program Welcome.java // indicates the remainder of the line is a comment // indicates the remainder of the line is a comment Comments are ignored by the compiler Comments are ignored by the compiler Use comments to document and describe code Use comments to document and describe code Commenting code is GOOD PROGRAMMING STYLE Commenting code is GOOD PROGRAMMING STYLE Can also use multiple line comments: /*... */ Can also use multiple line comments: /*... */ ex. /* This is a multiple line comment. It can line comment. It can be split over many lines */ be split over many lines */

public class Welcome { -Begins a class definition for class Welcome Every Java program has at least one user- defined class Every Java program has at least one user- defined class class keyword immediately followed by class name class keyword immediately followed by class name Keyword: words reserved for use by Java Keyword: words reserved for use by Java Naming classes: capitalize every word Naming classes: capitalize every word SampleClassName SampleClassName Beginning a Program

public class Welcome { Identifier Names Identifier Names Series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ ) Series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ ) Does not begin with a digit Does not begin with a digit Contains no spaces Contains no spaces Examples: Welcome1, $value, _value, button7 Examples: Welcome1, $value, _value, button7 7button is invalid 7button is invalid Case sensitive (capitalization matters) Case sensitive (capitalization matters) a1 and A1 are different a1 and A1 are different

Beginning a Program public class Welcome { Saving files Saving files File name is class name with.java extension File name is class name with.java extension Welcome.java Welcome.java Braces Braces Left brace starts every class Left brace starts every class Right brace ends every class Right brace ends every class public static void main(String [] args) { Part of every Java application Part of every Java application Applications begin executing at main Applications begin executing at main Parenthesis indicate main is a method Parenthesis indicate main is a method Java applications contain one or more methods Java applications contain one or more methods

A Simple Program: Printing a Line of Text public static void main(String [] args) { Exactly one method must be called main Exactly one method must be called main Methods can perform tasks and return information Methods can perform tasks and return information void means main returns no information void means main returns no information For now, mimic main's first line For now, mimic main's first line Left brace begins body of method definition Left brace begins body of method definition

A Simple Program: Printing a Line of Text System.out.println(“Welcome to Java Programming!”); Instructs computer to perform an action Instructs computer to perform an action Prints string of characters between double quotes Prints string of characters between double quotes String - series characters inside double quotes String - series characters inside double quotes White spaces in strings are not ignored by compiler White spaces in strings are not ignored by compiler System.out - standard output object System.out - standard output object Allows java to print to command window (i.e., MS-DOS prompt) Allows java to print to command window (i.e., MS-DOS prompt) Method System.out.println displays a line of text Method System.out.println displays a line of text Argument inside parenthesis Argument inside parenthesis Entire line known as a statement Entire line known as a statement All statements must end with a semicolon ; All statements must end with a semicolon ;

A Simple Program: Printing a Line of Text } Ends method definition Ends method definition } Ends class definition Ends class definition Some programmers add comments to keep track of ending braces Some programmers add comments to keep track of ending braces The last two lines could be rewritten as: The last two lines could be rewritten as: } //end of method main() } //end of class welcome Remember that the compiler ignores comments Remember that the compiler ignores comments

A Simple Program: Printing a Line of Text Compiling a program Compiling a program Click compile button. Located on toolbar by an upside down arrow Click compile button. Located on toolbar by an upside down arrow If there are no errors, file Welcome.class is created If there are no errors, file Welcome.class is created Contains Java bytecodes that represent application Contains Java bytecodes that represent application Bytecodes passed to Java interpreter Bytecodes passed to Java interpreter Executing a program Executing a program Click Execute button. Located on toolbar by an arrow pointing to the right. F5 will also Execute Click Execute button. Located on toolbar by an arrow pointing to the right. F5 will also Execute Launches interpreter to load.class file for class Welcome Launches interpreter to load.class file for class Welcome.class extension omitted from command.class extension omitted from command Interpreter calls method main Interpreter calls method main

A Simple Program: Printing a Line of Text Other methods Other methods System.out.println System.out.println Positions cursor on new line after displaying argument Positions cursor on new line after displaying argument System.out.print System.out.print Keeps cursor on same line after displaying argument Keeps cursor on same line after displaying argument

A Simple Program: Printing a Line of Text // Example Welcome.java // Printing a line with multiple statementes public class Welcome { public static void main(String [ ] args) { System.out.print(“Welcome to “); System.out.println(“Java Programming!”); } Program Output Welcome to Java Programming! System.out.print keeps the cursor on the same line, so System.out.println continues on the same line.

A Simple Program: Printing a Line of Text Escape characters Escape characters Backslash ( \ ) Backslash ( \ ) Indicates that special characters are to be output Indicates that special characters are to be output Backslash combined with a character makes an escape sequence Backslash combined with a character makes an escape sequence \n - newline \n - newline \t - tab \t - tab Usage Usage Can use in System.out.println or System.out.print to create new lines Can use in System.out.println or System.out.print to create new lines System.out.println( "Welcome\nto\nJava\nProgramming!" ); System.out.println( "Welcome\nto\nJava\nProgramming!" );

A Simple Program: Printing a Line of Text Notice how a new line is output for each \n escape sequence. // Example Welcome.java // Printing multiple lines with a single statement public class Welcome { public static void main(String [ ] args) { System.out.println(“Welcome\nto\nJava\nProgramming!”); } } Program Output Welcome to Java Programming!

Template for Java File //Name //Project Name and # //Date public class Welcome { public static void main(String [ ] args) { Code that makes your program work } This section contains statements that complete the program.

Programming Style One thing to remember when developing applications is that typically programs have a long life and are usually maintained by many people other than their original authors. Therefore, the developer should take into account the following items: One thing to remember when developing applications is that typically programs have a long life and are usually maintained by many people other than their original authors. Therefore, the developer should take into account the following items: Layout Layout Readability Readability Indentation Indentation Name usage Name usage

Example Program with Readability import TerminalIO.KeyboardReader; public class Convert { public static void main(String [] args) { KeyboardReader reader = new KeyboardReader(); KeyboardReader reader = new KeyboardReader(); double fahrenheit; double fahrenheit; double celsius; double celsius; System.out.print("Enter degrees Fahrenheit: "); System.out.print("Enter degrees Fahrenheit: "); fahrenheit = reader.readDouble(); fahrenheit = reader.readDouble(); celsius = (fahrenheit ) * 5.0 / 9.0; celsius = (fahrenheit ) * 5.0 / 9.0; System.out.print("The equivalent in Celsius is "); System.out.print("The equivalent in Celsius is "); System.out.println(celsius); System.out.println(celsius); reader.pause(); reader.pause(); }}

Example Program without Readability import TerminalIO.KeyboardReader; public class Convert {public static void main(String [] args) { KeyboardReader reader = new KeyboardReader();double KeyboardReader reader = new KeyboardReader();double fahrenheit; fahrenheit; double celsius;System.out.print("Enter degrees Fahrenheit: "); double celsius;System.out.print("Enter degrees Fahrenheit: "); fahrenheit fahrenheit=reader.readDouble(); celsius = (fahrenheit celsius = (fahrenheit ) * 5.0 / 9.0; ) * 5.0 / 9.0; System.out.print System.out.print ("The equivalent in Celsius is "); System.out System.out.println(celsius); reader.pause(); reader.pause(); }}