Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa."— Presentation transcript:

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

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

3 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

4 4 Compiled Languages import java.lang.* import java.lang.* import java.lang.* import java.lang.* Source Code Print Hello World Compiler executable machine code High Level Language code Run Hello World!

5 5 Interpreted Languages import java.lang.* import java.lang.* import java.lang.* import java.lang.* Source Code Print Hello World Interpreter High Level Language code Run Hello World!

6 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

7 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 instructionscompiled Java 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

8 8 Running Java Programs javacjava

9 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

10 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

11 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 wish For now – just assume that this has to be there! This means that this process has no return This 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

12 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.

13 13 Typed Languages BankBalance HolderName 700  John Smith

14 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

15 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).

16 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));

17 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;

18 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.

19 19 Simple Program class 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

20 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); }

21 21 Recommended Reading utsandbolts/index.html utsandbolts/index.html


Download ppt "1 Introduction to Programming G51PRG University of Nottingham Revision 1 Essam Eliwa."

Similar presentations


Ads by Google