Introduction to Java.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Getting Started with Java.
Advertisements

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
The Web Warrior Guide to Web Design Technologies
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
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.
Chapter 1: Introduction
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
Getting Started with Java
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
 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.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Programming with Java standard classes. Java API Application Programming Interface Provides hundreds of standard classes that can be incorporated into.
Chapter 2: Introduction to C++.
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Introduction to scripting
A First Program Using C#
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
The Java Programming Language
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSCI-383 Object-Oriented Programming & Design Lecture 13.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Classes CS 21a: Introduction to Computing I First Semester,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Objective You will be able to define and identify the basic components of a java program by taking notes, seeing examples, and completing a lab. Construction.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Introduction to programming in the Java programming language.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use Swing components to build the GUI Use proper naming conventions for classes.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Chapter 2 Getting Started with Java. Objectives After you have read and studied this chapter, you should be able to Identify the basic components of Java.
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Introducing Java Chapter 3 Review. Why Program in Java? Java, is an object-oriented programming language. OOP languages evolved out of the need to better.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Chapter 6 JavaScript: Introduction to Scripting
2.1 Parts of a C++ Program.
elementary programming
Chapter 2: Introduction to C++.
Object Oriented Programming in java
Java objects: a first view
Classes CS 21a: Introduction to Computing I
Classes, Objects and Methods
Chap 2. Identifiers, Keywords, and Types
Getting Started with Java
Computer Programming-1 CSC 111
Presentation transcript:

Introduction to Java

Java program example Running the program produces this result: /* Introduction to OOP with Java 4th Ed, McGraw-Hill Wu/Otani Chapter 2 Sample Program: Displays a Window File: Ch2Sample1.java */ import javax.swing.*; class Ch2Sample1 { public static void main(String[] args) { JFrame myWindow; myWindow = new JFrame(); myWindow.setSize(300, 200); myWindow.setTitle("My First Java Program"); myWindow.setVisible(true); } } Running the program produces this result:

How it works Declares a class named Chapter2Sample1, including a single method called main which contains all of the instructions in the program Declares a JFrame object named myWindow Sends messages to the object requesting it to set its size and title, then display the window

Line-by-line analysis /* Introduction to OOP with Java 4th Ed, McGraw-Hill Wu/Otani Chapter 2 Sample Program: Displays a Window File: Ch2Sample1.java */ The program begins with a comment – all of the text between the /* and */ symbols is not, strictly speaking, code – the markers indicate that the computer can ignore this section, which contains information strictly for the human reader – later, we’ll look at comments in more depth

Line-by-line analysis import javax.swing.*; import statement: allows the program to use code from a package in the Java API – in this case, we’re importing code from the swing package, which contains class definitions for graphical objects, like windows

Import statements import statements always appear at the beginning of a program file syntax: import packagename.classname; example of this syntax: import javax.swing.JFrame; this syntax indicates that we want the JFrame class, which is part of the swing package, which is a subpackage of a larger package called javax a package is simply a grouping of related Java classes – later on, we’ll see how to create our own packages note the semicolon – in Java, this indicates the end of an instruction the word import is a Java reserved word – that is, this is a symbol that belongs to the Java language, and it can only be used in an import statement

Import statements We can choose to import an entire package rather than a single class – then the syntax would be import packagename.*; Our program example uses this syntax – the line import javax.swing.*; indicates we wish to import the entire swing package this is more common than importing a single class import statements are often used when we want to incorporate code from the Java standard classes it would have been possible to write this program without the import statement, but that would have made other statements more complicated – we’ll see how in a moment there are usually multiple import statements in a program

Analysis continued Class declaration: every Java program consists of one or more classes There must be at least one main class (that is, a class containing a main method) – program execution starts with the first instruction in this main method

Class declaration Class declaration syntax: class classname { class member declarations } In our example program, the class declaration looks like this: class Ch2Sample1 { /* main method declaration here */  

Class declaration class classname { class member declarations } Note the curly brackets – these indicate the beginning (left bracket) and ending (right bracket) of a block of code The contents of a class are always contained within a code block The same is true for the body of a method – in fact, a method is always a subblock within the block containing the class Most programs contain additional subblocks, which are enclosed within methods

Analysis continued Method declaration: a method declaration consists of a heading and a body containing a set of instructions to be executed to perform a task The general syntax for method declaration is: modifier(s) returntype methodname (parameter(s)) { instructions }

Method declaration The main method header always looks exactly like this: public static void main (String [ ] args) { For the record, public and static are modifiers, void is the return type, main is the name of the method, and String[] and args are the data type and name of the parameter, respectively The purpose and meaning of all of these things will become clear during the semester – for now, just memorize this sequence of words and symbols, you’ll be using them a lot

Analysis continued Instructions: the body of main consists of a sequence of program instructions – when the program executes, each instruction will be performed exactly once, in the order given Every instruction ends with a semicolon (;) Although it isn’t necessary from the computer’s point of view that every instruction be on its own line of code, it’s important in terms of human readability, which is an important factor in writing code that is easy to maintain

Instructions The instructions in this program include: an object declaration an object creation three messages

Object declaration Object declaration: designates the name of an object and the class to which it belongs syntax: classname objectname; example: JFrame myWindow; you can declare more than one object in a single line of code – for example, the following code declares two JFrame objects: JFrame window1, window2;

Object declaration In order to declare an object of a particular class, the class must already be defined – in this instance, we are using one of the standard classes from the Java API JFrame is a class defined in the swing package, which is part of the javax package – the import statement we looked at earlier made JFrame and all other swing classes available, as you may recall Without the import statement, we still could have declared a JFrame object, but instead of just the class name we would have had to use JFrame’s fully qualified name, which lists the package(s) it belongs to – in that case, our declaration would have looked like this: javax.swing.JFrame myWindow;

Object declaration The name of an object is an identifier chosen by the programmer – a valid Java identifier has the following characteristics: A sequence of one or more letters, digits, underscores (_) and/or dollar signs ($) – no spaces are allowed The first character must be a letter Java reserved words can’t be used for identifiers – for example, you can’t name an object “import” or “public” or “class”

Java identifiers Java is case-sensitive: this means the language distinguishes between upper and lowercase characters (therefore, although it’s not advisable, you could create an object named “Class”) The Java standard naming convention adds these additional guidelines: Class names start with a capital letter Object names start with a lowercase letter If an identifier contains multiple words, the first letter of every word (except the first if the thing being named isn’t a class) is capitalized

Analysis continued object creation: a declaration simply reserves a name; to actually create an object, memory must be allocated for storage – this operation is accomplished using the new operator syntax: objectname = new classname(argument(s)); example: myWindow = new JFrame();

Object creation The object creation statement is a message to a special method of the class called the class constructor In this example, there are no arguments passed to the constructor – we will look at arguments when we examine messages, below It is possible to declare and create an object using a single line of code, as in the example below: JFrame myWindow = new JFrame();

Analysis continued Messages: a message is a request to an object (or class) for some task to be performed – the message specifies the name of the method requested (which must be defined within the specified object’s class) and any data that must be passed to the object in order for it to accomplish the task Syntax: objectname.methodname(argument(s));

Messages There are messages in the sample program – they are: myWindow.setSize(300, 200); myWindow.setTitle("My First Java Program"); myWindow.setVisible(true);

Messages Each of the messages is addressed to an object named myWindow the first one calls the setSize method and sends it 2 arguments which specify the width and length of the window to be created the second one calls the setTitle method and passes it a single argument in the form of the string literal “My First Java Program” – this argument specifies the title of the window the third one calls the setVisible method, with the argument true – true is a Java reserved word – this method displays the window – if we passed the opposite argument to the method (false), the window would disappear

Comments Comments: provide internal documentation for your code - we have already seen an example of a comment – there are 3 types: multiline comments: this is the kind we have seen – any text enclosed between the 2-character symbol /* and the symbol */ is considered a comment single line comments: a comment that begins with the 2-character symbol // and ends at the end of the line of text containing it – this type of comment is often used to provide narrative explanation for code on the same line – for example: myWindow.setVisible(true); // displays window javadoc comments: specialized type of comment that can appear before class declarations, method declarations, and other program elements – this type of comment is used to create external documentation in the form of a web page– we will look at this type of comment in depth later in the semester