1 / 21

Introduction to Programming G51PRG University of Nottingham Revision 1

Introduction to Programming G51PRG University of Nottingham Revision 1. Essam Eliwa. Revision 1. How Java works? Break up Of The HelloWorld program Java Data types Using Variables Expressions, Statements, and Blocks. Programming languages.

grant
Download Presentation

Introduction to Programming G51PRG University of Nottingham Revision 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to ProgrammingG51PRGUniversity of NottinghamRevision 1 Essam Eliwa

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

  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. Compiled Languages executable machine code import java.lang.* --------------------- -------------------- ------- --- ------------- ---- import java.lang.* --------------------- -------------------- ------- --- ------------- ---- Compiler 0010010100110 100101010101 101010101 10101010 import java.lang.* --------------------- -------------------- ------- --- ------------- ---- import java.lang.* --------------------- -------------------- ------- --- ------------- ---- Source Code Print Hello World ------- --- ------------- ---- Run High Level Language code Hello World!

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

  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. Java virtual machine • A Java virtual machine (JVM) interprets compiledJavabyte 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

  8. Running Java Programs javac java

  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. 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. Anything in these brackets is information given to the method to help it perform its job This means that this process has no return This is simply the name of the method For now – just assume that this has to be there! This simply means that other files can use this method if they wish Here is an example of using a method. In this case, we’re using the method that writes text on to the screen. A semi-colon is used like a full stop. It indicates the end of a single instruction This is us giving the method information it needs to complete it’s job. In this case, we’re telling it what to write! Hello World public static void main (String[] args) { System.out.println ("Hello World!") ; }// main end

  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. Typed Languages BankBalance ü 700 û HolderName John Smith

  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. 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. 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. 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. 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. 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. 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. Recommended Reading • http://java.sun.com/docs/books/tutorial/java/nutsandbolts/index.html

More Related