Introduction to Java The HelloWorld program Primitive data types Assignment and arithmetic operations User input Conditional statements Looping Arrays CSA0011 Matthew Xuereb 2008 1
Java Overview A high level programming language An object-oriented language A platform-independent language Java code is compiled into byte-code The byte-code is interpreted into machine code (Just-in-time compilation) using the JVM Java provides automatic memory allocation and garbage-collection CSA0011 - Matthew Xuereb 2008 2
The HelloWorld program[1/2] /* /* * * First First program program in in Java Java */ */ class class HelloWorld HelloWorld static static void void main(string[] main(string[] args) args) System.out.println("Hello System.out.println("Hello World!!"); World!!"); // // End End of of main main method method Save the code in a file named HelloWorld.java Java is a case sensitive language Comments are closed within /* */ Inline comments starts with a // CSA0011 - Matthew Xuereb 2008 3
The HelloWorld program[2/2] Compiling and running the HelloWorld program Using the OS shell Make sure that the JDK is properly installed and the class path is correctly configured. I recommend to use an IDE Eclipse, NetBeans, JCreator, BlueJ, CSA0011 - Matthew Xuereb 2008 4
Primitive data types There are 8 primitive data types Data Type Values boolean byte short int long float double Char true or false -128..127 [8-bit signed] -32,768..32,767 [16-bit signed] -2,147,483,648..2,147,483,647 [32-bit signed] -9,223,372,036,854,775,808..9,223,372,036,854,775,807 [64-bit signed] 1.40129846432481707e-45..3.40282346638528860e+38 [32-bit signed] 4.94065645841246544e-324d..1.79769313486231570e+308d [64-bit signed] 16-bit Unicode character A string is not a primitive data type String Java has special support for strings and many times they can used just as primitive data types. CSA0011 - Matthew Xuereb 2008 5
Variables declarations class class Variables Variables static static void void main(string[] main(string[] args) args) boolean boolean flag flag true; true; int int x, x, y y,, z; z; double double d d 4.346; 4.346; char char c c 'x'; 'x'; String String s s "Hello"; "Hello"; Identifiers: Must be composed of letters, numbers, the underscore _ and the dollar sign $ May only begin with a letter, the underscore or a dollar sign Reserved words cannot be used as identifiers CSA0011 - Matthew Xuereb 2008 6
Assignment statements The equal sign () is used as the assignment statement. int x 65; y x - 9 + (x * 4); // Assuming that y is an integer String s Hello + World!! ; CSA0011 - Matthew Xuereb 2008 7
Arithmetic Operators Arithmetic operation Addition Subtraction Multiplication Division Remainder Operator Rules of operator precedence + - * / % Algebraic expression f + 7 p - c bm x y or x / y r mod s Java expression F + 7 p c b * m x / y r % s *, / and % operators are applied first. If an expression contains several such operations, the operators are applied from left to right. + and operations are applied next. If an expression contains several such operations, the operators are applied from left to right. CSA0011 - Matthew Xuereb 2008 8
Getting input from console[1/3] Class Scanner is used A Scanner enables a program to read data for use in a program. The data can come from many sources, e.g. file on disk or the user at the keyboard. Syntax: Scanner input new Scanner(System.in); int num1 input.nextint(); // read an integer from user String str input.nextline(); // read a string from user CSA0011 - Matthew Xuereb 2008 9
Getting input from console[2/3] import java.util.scanner; class UserInput static void main(string[] args) // create Scanner to get user input from console Scanner input new Scanner(System.in); // Prompt user to enter his name System.out.println("What is your name?"); // Read name from console String name input.nextline(); // Output welcome message System.out.println("Hello " + name + " :-) Welcome to CSA 0011!!"); CSA0011 - Matthew Xuereb 2008 10
Getting input from console[3/3] Basic numeric and string methods for Scanner Method int nextint() long nextlong() float nextfloat() double nextdouble() String next() String nextline() void close() Returns Returns the next token as an int. If the next token is not an integer, InputMismatchException is thrown. Returns the next token as a long. If the next token is not an integer, InputMismatchException is thrown. Returns the next token as a float. If the next token is not a float or is out of range, InputMismatchException is thrown. Returns the next token as a long. If the next token is not a float or is out of range, InputMismatchException is thrown. Finds and returns the next complete token from this scanner and returns it as a string; a token is usually ended by whitespace such as a blank or line break. If not token exists, NoSuchElementException is thrown. Returns the rest of the current line, excluding any line separator at the end. Closes the scanner. CSA0011 - Matthew Xuereb 2008 11
Conditional statements[1/2] Equality and relational operators Algebraic equality or relational operator Java equality or relational operator Java example Meaning Equality operators x y x is equal to y! x! y x is not equal to y Relational operators > > x > y x is greater than y < < x < y x is less than y > x > y x is greater than or equal to y < x < y x is less than or equal to y Syntax if / if..else / if.. else if.. else switch.. case.. default CSA0011 - Matthew Xuereb 2008 12
Conditional statements[2/2] static void main(string[] args) // create Scanner to get user input from console Scanner input new Scanner(System.in); // Prompt user to enter his age System.out.println("What is your age?"); int age input.nextint(); if(age > 18) System.out.println("You can drive a car!"); else System.out.println("You cannot drive a car!"); CSA0011 - Matthew Xuereb 2008 13
Looping: A loop is a sequence of statements which is specified once but which may be carried out several times in succession. 3 types of loops: for loop while loop do.. while loop CSA0011 - Matthew Xuereb 2008 14
For loop An iteration statement with a counter Example: /* /* * For For loop loop example example */ */ class class ForLoop ForLoop static static void void main(string[] main(string[] args) args) // // A for for loop loop to to display display the the numbers numbers 1-10 1-10 for(int for(int i 1;i 1;i <10; <10; i++) i++) System.out.println(i); CSA0011 - Matthew Xuereb 2008 15
While loop A control flow statement that allows code to be executed repeatedly based on a given boolean condition. The condition is first evaluated - if the condition is true the code within the block is then executed static static void void main(string[] main(string[] args) args) Scanner Scanner input input new new Scanner(System.in); Scanner(System.in); System.out.println("Enter System.out.println("Enter an an integer integer less less then then 100"); 100"); int int num num input.nextint(); input.nextint(); while(num while(num > > 100) 100) System.out.println("Enter System.out.println("Enter an an integer integer less less then then 100"); 100"); num num input.nextint(); input.nextint(); CSA0011 - Matthew Xuereb 2008 16
Do.. While loop A control flow statement that allows code to be executed repeatedly based on a given boolean condition. First, the code within the block is executed, and then the condition is evaluated. If the condition is true the code within the block is executed again static static void void main(string[] main(string[] args) args) Scanner Scanner input input new new Scanner(System.in); Scanner(System.in); int int num; num; do do System.out.println("Enter System.out.println("Enter an an integer integer less less then then 100"); 100"); num num input.nextint(); input.nextint(); while(num while(num > > 100); 100); CSA0011 - Matthew Xuereb 2008 17
Arrays[1/3] A data structure consisting of a group of elements that are accessed by indexing. A collection of related data items. A group of variables containing values that all have the same type. CSA0011 - Matthew Xuereb 2008 18
Arrays[2/3] Declaring and creating arrays int nums[]; nums new int[10]; -- OR - int num[] new int[10]; Declaring array Creating array Declaring and creating array Accessing arrays nums[2] 36; System.out.println(nums[5]); An array of size 10 has an index range from 0 to 9 CSA0011 - Matthew Xuereb 2008 19
Arrays[3/3] Example class class Arrays Arrays static static void void main(string[] main(string[] args) args) Scanner Scanner input input new new Scanner(System.in); Scanner(System.in); // // Create Create an an array array of of size size 10 10 of of type type integer integer int int numbers[] numbers[] new new int[10]; int[10]; // // Ask Ask the the user user to to enter enter 10 10 integers integers to to fill fill the the array array for(int for(int i i 0; 0; i i < < 9;i++) 9;i++) System.out.println("Enter System.out.println("Enter an an integer integer - - "); "); numbers[i] numbers[i] input.nextint(); input.nextint(); // // Now Now display display the the whole whole array array for(int for(int i i 0;i 0;i < < 9;i++) 9;i++) System.out.println(numbers[i]); System.out.println(numbers[i]); CSA0011 - Matthew Xuereb 2008 20