1 introduction to programming g51prg university of nottingham revision 1 essam eliwa

21
1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

Upload: devin-jew

Post on 02-Apr-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

1

Introduction to Programming G51PRG

University of NottinghamRevision 1

Essam Eliwa

Page 2: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

2

Revision 1

How Java works? Break up Of The HelloWorld program Java Data types Using Variables Expressions, Statements, and Blocks

Page 3: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

3

Programming languages

An interpreted language is a programming language whose programs are translated to machine code at the time of execution through using an interpreter program

A compiled language is a programming language which need the use of compilers to generate executable machine code in order to run the program

Page 4: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

4

Compiled Languages

import java.lang.*--------------------------------------------------------------------

import java.lang.*--------------------------------------------------------------------

import java.lang.*--------------------------------------------------------------------

import java.lang.*--------------------------------------------------------------------

Source CodePrint Hello World---------------------------

0010010100110100101010101

10101010110101010

Compiler

executable machine code

High Level Language code

Run

Hello World!

Page 5: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

5

Interpreted Languages

import java.lang.*--------------------------------------------------------------------

import java.lang.*--------------------------------------------------------------------

import java.lang.*--------------------------------------------------------------------

import java.lang.*--------------------------------------------------------------------

Source CodePrint Hello World---------------------------

Interpreter

High Level Language code

Run

Hello World!

Page 6: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

6

Java language is both compiled and interpreted

In-stead of translating Java programs into machine language, the Java compiler generates Java byte code

Byte code is easy (and fast) to interpret, almost like machine language, yet it is also portable, thus it is possible to compile a Java program on one machine and run it on any other machine

Java virtual machine is needed to run any java program

Page 7: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

7

Java virtual machine

A Java virtual machine (JVM) interprets compiled Java byte code for a computer's CPU so that it can perform a Java program's instructions

Defines an abstract machine or processor.

Once a Java virtual machine has been implemented for a given platform, any Java program can run on that platform

Page 8: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

8

Running Java Programs

javac java

Page 9: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

9

HelloWorld Program

/** * The HelloWorld class implements an application that * simply prints "Hello World!" to standard output. */

class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello World!");}// main end

} // class end

Page 10: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

10

Break down of HelloWorld

/** * The HelloWorld class implements an application that * simply prints "Hello World!" to standard output. */class HelloWorld {

public static void main(String[] args) {

System.out.println("Hello World!"); }// main end} // class end

Comments are ignored by the compiler but are useful to other programmers. The Java programming language supports three kinds of comments

The keyword class begins the class definition for a class followed by its name.

The code for each class appears between the opening and closing curly braces

every application must contain a main method (The starting point of the program) The modifiers public and static can be written in either order yet the convention is to use public static

Page 11: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

11

Anything in these brackets is information given to the method to help it perform its job

Hello World

This simply means that other files can use this method if they wishFor now – just assume that this has to be there!

This means that this process has no returnThis is simply the name of the method

Here is an example of using a method. In this case, we’re using the method that writes text on to the screen.

This is us giving the method information it needs to complete it’s job. In this case, we’re telling it what to write!

A semi-colon is used like a full stop. It indicates the end of a single instruction

public static void main (String[] args) { System.out.println ("Hello World!") ; }// main end

Page 12: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

12

Notes Java is case-sensitive

public class HelloWorld is the declaration of a new class called HelloWorld.

main is the entry point for the program, that is the point at which execution starts.

The body of the class and main method is contained within the { and } symbols.

Every statement which is an instruction to the computer must be ended with a semi-colon.

main() and { and } are part of the layout of the program not instructions.

White space layout (tabs, newlines, spaces etc) is not enforced but should be used sensibly to make the code more readable.

Page 13: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

13

Typed LanguagesBankBalance

HolderName

700

John Smith

Page 14: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

14

Primitive Data Types all variables must first be declared before they can be used

byte: An 8-bit signed integer ( -128 to 127 )

short: A 16-bit signed integer. (-32,768 to 32,767 )

int: A 32-bit signed integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647

long: A 64-bit signed integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807

Page 15: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

15

Primitive Data Types

float: A single-precision 32-bit floating point.

double: a double-precision 64-bit floating point.

boolean: The boolean data type has only two possible values: true and false.

char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

Page 16: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

16

Reference Data Types - Strings Typically, a reference is the memory address

at which the object or array is stored.

String str =“cat”;

Can not use == for comparison use ‘equals’ method instead

String x = "Hello";

String y = "World";

String z = "HelloWorld";

String a = x + y;

System.out.println(a == z);

System.out.println(a.equals(z));

Page 17: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

17

Creating a Variable in Java

To create a variable in java we simply specify the type and the name of the variable.

The following defines a variable called “num” that holds integers. int num;

To initialize the variable: num = 0;

Can be done on one step: int num =0;

Page 18: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

18

Creating a Variable in Java

We have to create a variable before we can use it

Creating variables simply warns the computer that it needs to allocate some of the RAM to storing your data.

The more variables you create, the more RAM your program will use when it is run.

Page 19: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

19

Simple Programclass Demo {

public static void main (String[] args) {

int result = 1 + 2; // result is now 3

result = result - 1; // result is now 2 result = result * 2; // result is now 4

result = result / 2; // result is now 2

result = result + 8; // result is now 10

result = result % 7; // result is now 3

System.out.println(result); } // main end

} // class end

Page 20: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

20

Expressions, Statements, and Blocks Operators may be used in building expressions, which compute values.

5+(7*2) 5>9

Expressions are the core components of statements (ends with ; ) int x= 5+7; x++; System.out.println("Hello World!");

Statements may be grouped into blocks defined by starting { and ending with } {

int x; x =0; System.out.println(“x value is” + x);

}

Page 21: 1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa

21

Recommended Reading

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/index.html