* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Lecture 2 Slides
Falcon (programming language) wikipedia , lookup
Program optimization wikipedia , lookup
Library (computing) wikipedia , lookup
Class (computer programming) wikipedia , lookup
Comment (computer programming) wikipedia , lookup
Structured programming wikipedia , lookup
Indentation style wikipedia , lookup
Java syntax wikipedia , lookup
String (computer science) wikipedia , lookup
String literal wikipedia , lookup
Scala (programming language) wikipedia , lookup
Name mangling wikipedia , lookup
Go (programming language) wikipedia , lookup
Object-oriented programming wikipedia , lookup
One-pass compiler wikipedia , lookup
Java (programming language) wikipedia , lookup
Java performance wikipedia , lookup
Programming for Beginners Lecture 2: Variables & Data Types Martin Nelson Elizabeth FitzGerald Revision of Session 1 Differences between: Procedural and object-oriented languages Interpreted and compiled languages The basics of Java programming How computer programs are constructed Statements, comments and basic arithmetic Anatomy of a Java program – 1 class myprog { public static void main (String[ ] args) { System.out.println(“Hello world!”); } } Anatomy of a Java program – 2 class myprog Reserved words Identifier 'class' is a Java reserved word 'myprog' is an identifier This is a word we make up to identify part of the program (in this case, the program itself) Identifiers must be a single word Remember - Java is case sensitive! Anatomy of a Java program – 3 class myprog { } Code braces Braces { or } usually separate off a block of code All programs have several blocks of code Braces must be evenly balanced Braces are often nested Anatomy of a Java program – 4 class myprog { public static void main (String[ ] args) { } } Methods Methods contain blocks of functional code Methods are named by an identifier This is a method called 'main' (applications execute their main method on starting) Anatomy of a Java program – 5 class myprog { public static void main (String[ ] args) { System.out.println(“Hello world”); } } Statements This program contains a single statement Statements are terminated by a semi-colon Anatomy of a Java program – 6 class myprog { public static void main (String[ ] args) { System.out.println(“Hello world”); } } println This statement calls a 'print' method Methods can be given data (arguments) which are contained in brackets Anatomy of a Java program – 7 class myprog { public static void main (String[ ] args) { System.out.println(“Hello world”); } } The argument of println here is a string A string is a sequence of characters Java strings are bound in double quotes Code Presentation Add coments to clarify what the code does // comments a single line. /* and */ comment multiple lines. Comments should be brief and helpful! Use blank lines to seperate different tasks. Indent code inside curly braces One tab or three/four spaces. Session 2 - aims & objectives Find out how to declare variables and how to assign values to them Appreciate the main Java variable types: char byte boolean String integer double Perform arithmetic using variables Introduce concept of decision making Variables Symbolic representation of data of a specific type variables are named by an identifier the type must be declared before a variable can be used e.g. int a Values can be assigned to a variable Java assignment is = e.g. a = 10; b = 5; c = a + b; Variables can be modified during program execution (usually by assignment) Text-based variable types char a single ASCII character (all letters, all numbers, all punctuation marks etc) bound by single quotes e.g. ‘a’ String a series of characters i.e. text, of any length note capital S at start of the word String bound by double quotes e.g. “some text” Numeric variable types byte integer whole number in the range -2147483648 to 2147483647 double whole number in the range -128 to 127 floating point numbers (15 decimal places) scientific notation The letter 'e' means "times 10 raised to the power" e.g. 3.45e-3 = 0.00345; 1e6 = 1 000 000 Other variable types boolean Used for creating true or false variables Useful in program control and decision making e.g. if condition is true else then do this do something else Decision making Sometimes you will want the program to perform a function based on a decision e.g. withdrawing or depositing money into a bank account withdrawal - subtract sum from balance deposit - add sum to balance. A decision is required: if deposit then add sum to balance else subtract sum from balance Arithmetic Operations Addition x=x+10; Subtraction x=x-10; Multiplication x=x*10; Division x=x/10; Increment x++; (equivalent to x=x+1) Decrement x--; (equivalent to x=x-1) The modulo operator gives the remainder when dividing x by some number. Useful for deciding if x is odd/even: x=x%2; The ‘if’ statement This statement requires a boolean expression as part of its code. e.g. compare numeric variables a and b if (a > b) { ... } if (a > b | b == 0) { ... } if (a > b) { ... } else { ... } Relational operators > greater than < less than == is equal to != is not equal to >= greater or equal to <= less or equal to | or & and Coming up in Session 3... Flow control! How to easily make your code repeat a task many times.