Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.

Slides:



Advertisements
Similar presentations
Introduction to Java.
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
IT151: Introduction to Programming
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
CMT Programming Software Applications
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2: Introduction to C++.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Week 1 Algorithmization and Programming Languages.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Control Structures (B) Topics to cover here: Sequencing in C++ language.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Bill Tucker Austin Community College COSC 1315
Programming what is C++
Chapter 6 JavaScript: Introduction to Scripting
Chapter 2: Introduction to C++
Chapter 2 - Introduction to C Programming
Introduction to Computer Science / Procedural – 67130
Chapter 2, Part I Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Data types and variables
Chapter 2 - Introduction to C Programming
Introduction to the C Language
IDENTIFIERS CSC 111.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Java Tutotrial for [NLP-AI] 2
Statements and expressions - java
Anatomy of a Java Program
Chapter 2: Introduction to C++.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Instructor: Alexander Stoytchev
Presentation transcript:

Java An introduction

Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int x = 5; int y = 6; int z = x + y; System.out.println (“I just did a calculation”); System.out.println(“” + x + “ + ” + y + “ = ” + z); } } // that’s all, folks!

Program result

Class definition The example program contains the definition of a class named Example 1 class headingThe class definition begins with the class heading: public class Example1 { end bracketThe class definition ends with an end bracket : } block of codebodyAny code that appears between brackets {} is called a block of code; the body of a class is a block

Method definition The body of the class consists of a single method The method begins with a method heading: public static void main (String [] args) { The body of the method, which is also a block of code, begins with the begin bracket and ends with a corresponding end bracket The end bracket for the method appears just before the end bracket for the class – the method is inside the class

Program instructions The body of the method consists of a set of instructions for the computer to perform There are three kinds of instructions in this program: –Method calls (messages) –Data declarations –Assignments Each instruction ends with the same symbol: a semicolon (;)

Method calls There are three examples of method calls in the program: System.out.println (“This is the first example”); … System.out.println (“I just did a calculation”); System.out.println(“” + x + “ + ” + y + “ = ” + z); A method call consists of: calling object –The name of the calling object (System.out in this case) method name –The method name (println) argument list –An argument list in parentheses

The println method System.out is the name of an object defined in Java’s standard library –Represents the standard output stream –On most systems, this is the screen The println method sends output to the stream object that calls it –Arguments to the method are the data to be output –Arguments can be quoted strings or values of simple data types

Arguments to println The arguments to println, like arguments to other methods, are expressions expression –An expression is a symbol or set of symbols that represents a value literal value –Expressions take many forms – the simplest expression is a literal value String literalString literal values in Java are any set of characters enclosed with double quotes (“”)

Arguments to println concatenateThe + operator is used to concatenate strings with other strings or other expressions; for example: System.out.println(“” + x + “ + ” + y + “ = ” + z); –The entire contents of the parentheses is interpreted as a single string –The string concatenates the value of x with a plus sign, the value of y, an equals sign, and the value of z –Note that the plus sign in quotes is interpreted as a string, not as an operator

Data declaration statements In order to store values in memory, the programmer needs to set aside memory space to hold those values data declarationAn instruction that allocates memory is a data declaration statement In the example program, there are three such statements, which set aside memory to hold three numbers

Data declaration statements variablesThe three spaces set aside for the numbers are called variables –Variables are memory spaces that hold single values –The value held in a variable can be changed many times during the course of a program

Data declaration statements The general syntax for a data declaration statement is: dataType identifier; // or dataType id1, id2, id3, … idn; –“dataType” is one of the standard Java data types described on the next slide –“identifier” (or “id1,” etc.) is a name chosen by the programmer to uniquely identify the memory space

Data types in Java Recall from the hardware lecture that different kinds of data values (whole numbers, real numbers, characters, etc.) require different amounts of memory Data types are reserved words in the Java language to indicate how much memory must be allocated to hold a particular named value

Data types in Java

Assignment statements Once a variable has been declared, a value can be stored in the named memory space One way to store a value in a variable is via an assignment statement Syntax: Variable = expression; –“Variable” is a previously-declared variable –“expression” represents the value to be stored

Examples In the example program, there are three statements that combine data declaration and assignment: int x = 5; int y = 6; int z = x + y; Variables x and y are assigned literal integer values; variable z is assigned the sum of x and y

Comments There is one more kind of statement in the program, at the very end: // that’s all folks! This is an example of a comment Comments are a form of documentation, not code Unlike this silly example, comments are usually used to explain what is happening in the program

Comment types A single-line comment begins with two slash marks and ends at the end of the line; example: // this is a single-line comment A multi-line comment begins with a slash followed by an asterisk, and ends with an asterisk followed by a slash: /* this is a multi-line comment this is part of the same comment and this is the end of it */

Comments Comments are not code; they are ignored by the compiler and do not become part of the executable program Comments are useful when well-written; they can go a long way toward making source code maintainable