Getting Started with Java

Click here to load reader

download Getting Started with Java

of 25

description

Getting Started with Java. CS0007: Introduction to Computer Programming. Review. A program is… a set of instructions that tell a computer what to do. Programs can also be called… software . Hardware refers to… the physical components that a computer is made of . - PowerPoint PPT Presentation

Transcript of Getting Started with Java

PowerPoint Presentation

CS0007: Introduction to Computer ProgrammingGetting Started with JavaReviewA program isa set of instructions that tell a computer what to do.Programs can also be called software.Hardware refers to the physical components that a computer is made of.The CPU has two parts: Control UnitArithmetic and Logic Unit (ALU)A Memory Address isa unique identifying number associated with a byte in memory.

ReviewInput is any data the computer collects from the outside world.Output isany data the computer sends to the outside world.A User is a person who uses a computer program.Program Development CycleAnalyze Define the ProblemDesign Plan a Solution for the ProblemWrite the Code Implement a solutionAn algorithm isa precise sequence of steps to solve a problem

The Java Programming LanguageJava was created in 1991 by James Gosling (Sun Microsystems)Why?They needed a programming language that could be used to write a wide variety of applications and be processed on very different devices.Traditional programming languages cannot do this.Why?Most general-purpose programming languages at that time were platform specific.A General-Purpose Programming Language is a programming language designed to be used for writing software in a wide variety of application domains.A programming language is platform-specific, if once compiled, it can only be guaranteed to be able to run on the machine it was compiled for.Java is NOT platform-specific.From Source Code to a Running ProgramCode of any programming language must be saved into a file.This file contains the source code for the program, and thus is called a source file.Source Code is the human written code for a program.Java is known as a high-level language.A High-Level Programming Language is a language that is easily read and written by humans, and is needed to be translated before a machine can use it. It provided a high level of abstraction from the details of the workings of the computers hardwareMost programming by humans is done in high-level languagesThe source file for a high-level language needs to be translated into a form that a machine can understand through a process called compilation.Compilation is the process of translating high-level language into machine language for platform-specific languages or some other intermediate code representation for other languages. A program called a compiler does this.Machine Language is a sequence of instructions directly executed by a processing unit (CPU in most cases). When you compile Java source code it results in Java Byte Code, NOT Machine (Specific) LanguageFrom Source Code to a Running ProgramRunning a compiler successfully on source code results in an executable file.An Executable File is a file that can be directly executed or run by a processing unit (again, most often the CPU) or run by another program that executes the file.When a program executes (runs) the instructions of that program are carried-out.Executable files containing machine code can run without aid by a processing unit.Executable files created by the Java compiler are in Java Byte Code, which are not specific to any platform, and thus cannot be run directly by a processing unit.Java executable files must be run by a special program installed on a machine called the Java Virtual Machine (JVM).The Java Virtual Machine (JVM) is a program that reads instructions from Java Byte Code and executes them for the platform that the JVM is installed on.The JVM is often called an interpreter, because it interprets java byte code to be run on whatever machine it is installed on. For this reason, Java is often called an interpreted language, as opposed to a true compiled language

From Source Code to a Running ProgramSteps to create and run Java Program:Write the source code in the Java programming language.Run the source code through the Java Compiler (Interpreter)Run the resulting Java Byte Code through the JVMThe programs executes!Draw a diagram7ErrorsA compilation will not be successful if there are syntax errors in the program.Syntax Errors are grammatical errors in the Java programming language.Meaning, if you enter code that the java compiler does not understand, it will not complete compiling.There are two other kinds of errors that cannot be caught by the Java compiler:Run-Time Errors Errors that cause the execution of a program to end abnormally (crash).Logical Errors Errors in the programmers logic that cause incorrect results. PortabilitySo, why was Java created again?So the programs written in it can be run on different devices easily.This means that Java programs are very easily portable.Portability in programming means that a program can be written on one computer and run on many different ones.In traditional compiled languages, you must recompile your program from the source code for every different machine you want to run it on.TediousCan run into problems between systems As long as the machine you want to execute the program has a compatible version of the JVM, your Java program can be run on it without recompiling it!If the JVM is correctly installed, there is (almost) no problems.

Draw a diagram9Java Applications and AppletsThere are two kinds of programs that can be created with Java:Application stand alone programs that runs on a computer.Applets Small applications designed to be transmitted over the internet and executed in a web browser.Most web pages are written in Hypertext Markup Language (HTML)HTML is not very sophisticated, Java allows for much of the power of a normal application.Whats the main problem with allowing users to create full-fledged applications that can be sent over the internet and used in a web browser?Answer: Security!Applications have access to memory and secondary storage.Solution: Applets run in a secure part of memory and cannot access anything outside of that unless the user specifically states so.Procedural ProgrammingThere are two primary methods of programming in use todayThe first is Procedural Programming a programming methodology in which a program was made up of one of more procedures.A procedure is a set of programming statements that, together, perform a specific task.Typically, in this model, data is passed between procedures.Here data and the code that acts on it are entirely separate.Is there a problem with this?What if the form of the data changes? If the data and the code acting on it are completely separate, can it still function?draw11Object-Oriented ProgrammingThe response to this is Object-Oriented Programming (OOP) a programming methodology in which a programs parts are abstracted into the interaction between Objects.An object is an abstraction of a real-world (or sometimes not) entity that consists of data (attributes) and the procedures that act upon the objects data (methods).Attributes can be seen as the information kept about the object whether it be the current state of the object or unchanging characteristics about it, and the methods can be seen as the behavior of the object that change the state.In Java the generalization of all the possible individual objects of a kind is called a class. An individual instance of the class is called an object.You can think of a class as being a blueprint or recipe and an object as being a single result from the blueprint or recipe.

Draw Diagram Here12Object ExampleExample Class: CupAttributes:Total VolumeCurrent VolumeColorDimensionsMethods:FillPour

Object-Oriented ExerciseI want you to create a car class.Think about what information about the state and characteristics of a car can be kept.Also, think about what behaviors a car has that change its state.Encapsulation and Data HidingTwo important terms that describe how OOP addresses the data/code separation that Procedural Programming presents:Encapsulation refers to the combining of data and code into a single object.Advantages:When the data in the object changes, the methods that interact with the data can be easily located and changed.Data Hiding refers to the objects ability to hide its data from outside the object.Things outside of the object (objects, or other code) can access and manipulate the data inside of an object through the methods defined by the object.This has many advantages:When using an object, you do not need to know the details about how it was implemented, you just need to know the methods needed to do a task.Outside sources can only manipulate the data of an object in the ways the object designer intends.In Java, almost everything is an object, but you can still program procedurally.Your First Java ProgramHello WorldEvery programming language has a Hello World program.Its purpose is to do the most basic functionality of a program: Simply display text.No computationsNo information keptJust spit out textOften languages are compared by the Hello World programs in terms of verbosity. Compiling and Running From Command LineYou can compile a java source file from either a Windows or Unix command prompt with the following command:javac YourProgramName.javaThis will create a file called YourProgramName.class. This contains the Java Byte Code for your program.You can run a java program from either a Windows or Unix command prompt with the following command:java YourProgramNameNotes:Your source file should end in .java When you run your Java program just use the name of your source file WITHOUT .java

Hello World At a GlanceLine 1: // Hello World ProgramThis line is called a commentA Comment is a line(s) in a program that is ignored by the compiler entirely.Why do we have comments?Include documentation inside of the code to allow other programmers (or ourselves) to understand what the code does, who wrote it, when, etc.To temporarily make lines of code not executed instead of removing them outrightComments in Java start with // You can have multiple line comments starting with /* and ending with *//* Here is a comment on one line......and it continues here */Hello World At a GlanceLine 2: BlankBlank lines, spaces, and tabs are known as whitespace in programming.Java ignores whitespace. Programmers often include whitespace in order to make the code more readable.Hello World At a GlanceLine 3: public class HelloWorld {This line is known as a class header.A Class Header starts a class definition and describes certain features of a class.A Class Definition includes all the attributes and methods for a class.In Java almost everything is a class. Here, this class is a container for our application. A program MUST have at least one class definition.This line has three words and a character:publicclassHelloWorld{Hello World At a Glancepublic is a key word in Java that is known as an access specifier.A Key Word (Reserved Word) are words that have special meaning in a programming language and can only be used for their intended purpose.An Access Specifier determines where an element (a class in this case) can be accessed from.Here public means that the class is unrestricted (open to the public).We will talk more about access specifiers later.Note that public is all lowercase. Java is case-sensitive.A language is Case-Sensitive if it differentiates between the same elements with different cases.For instance public and Public are not the same (Public is not a keyword)Hello World At a Glanceclass is another keyword indicating the beginning of a class definition.HelloWorld is the name of the class.This was determined by me, the programmer.It is known as Programmer-Defined-Name or an identifier.A Programmer-Defined-Name (Identifier) is a name created and used by the programmer. It identifies some place in memory and parts in the program created by the programmer.Identifiers follow naming rules which we will go over later.I could just as easily named it Dog or Pizza or BananaHello World At a GlanceIn short the three words tell the compiler that we are creating a public class that is named Hello WorldNotes on Classes:You may have multiple classes per source file, but you may only have one public class per source file.If there is a public class in a source file, the name of the file must be the same name as the class (Note the HelloWorld class was in HelloWorld.java)The { indicates the beginning of the class definition for the HelloWorld class.All of the lines of code associated with the class are between it and the corresponding } (Line 8)Everything inside the braces is called the body of the class.Lines are grouped together as such are called a block.

Hello World At a GlanceLine 5: public static void main(String[] args) {This is known as a method header, because it marks the beginning of a method.A Method can be thought of as a group of one or more programming statements the collectively has a name.This line needs to tell the compiler many things about the method.LOTS going on here!At this point what I want you to focus on is the name of the method main.Every Java application needs a main method, this is where the execution of the program starts.For now, just use this line to start the main method of all of your Java applications (Sorry).Everything between the { and the } on line 7 is the body of the method.Hello World At a GlanceLine 6: System.out.println("Hello World");This is the first line that actually does something of purpose.In short, this displays the words Hello World on the screen (without the quotation marks).The group of characters inside of the quotation marks is called a string literalWe will talk more about literals and strings later.Note that the line ends with a semicolon. This is part of the syntax of the Java language that marks the end of a statement. The semicolon is known as a terminating character in Java, because it terminates the statement.The Syntax of a programming language determines the rules that must be followed when writing the programs. Syntax is the grammatical rules that need to be followed (punctuation in this case).A Statement in a programming language is a line of code that ends in a semicolon.Not every line ends with a semicolon, and at first it may seem not clear at all when to use one and when not to. Here are some cases for when not to use one (you should get a feel for it with experience)Comments ignored by the compiler anywayHeaders terminated by bodyBraces simply not statements