Download presentation
Presentation is loading. Please wait.
Published byJoy Harrington Modified over 9 years ago
1
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types
2
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
3
Anatomy of a Java program – 1 class myprog { public static void main (String[ ] args) { System.out.println(“Hello world!”); }
4
Anatomy of a Java program – 2 Reserved words 'class' is a Java reserved word Identifier '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! class myprog
5
Anatomy of a Java program – 3 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 class myprog { }
6
Anatomy of a Java program – 4 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) class myprog { public static void main (String[ ] args) { }
7
Anatomy of a Java program – 5 Statements This program contains a single statement Statements are terminated by a semi-colon class myprog { public static void main (String[ ] args) { System.out.println(“Hello world”); }
8
Anatomy of a Java program – 6 println This statement calls a 'print' method Methods can be given data (arguments) which are contained in brackets class myprog { public static void main (String[ ] args) { System.out.println(“Hello world”); }
9
Anatomy of a Java program – 7 The argument of println here is a string A string is a sequence of characters Java strings are bound in double quotes class myprog { public static void main (String[ ] args) { System.out.println(“Hello world”); }
10
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.
11
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 Perform arithmetic using variables Introduce concept of decision making String integer double
12
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)
13
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”
14
Numeric variable types byte whole number in the range -128 to 127 integer whole number in the range -2147483648 to 2147483647 double 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
15
Other variable types boolean Used for creating true or false variables Useful in program control and decision making e.g. if condition is true then do this else do something else
16
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
17
Arithmetic Operations Addition x=x+10; Subtraction x=x-10; Multiplication x=x*10; Divisionx=x/10; Incrementx++;(equivalent to x=x+1) Decrementx--;(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;
18
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 {... }
19
Relational operators > greater than < less than == is equal to != is not equal to >= greater or equal to <= less or equal to | or & and
20
Coming up in Session 3... Flow control! How to easily make your code repeat a task many times.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.