Documente Academic
Documente Profesional
Documente Cultură
JAVA Programming
Lecture 1 From C to Java a one way ticket!
Contents
A simple Java program Data types Variables Assignment/initialization Operators Control Flow Strings Arrays
Main points
Everything in a Java program is a class Keyword public is an access modifier Program starts execution from the main method We will worry about what static void means later The program prints the string Java is fun! System.out.println() means call the println() method of the object System.out (which is part of the class System)
Data types
Like C, Java is strongly typed Java has 8 primitive data types Machine independent storage requirements
Range
-2,147,483,648 .. 2,147,483,647
-32768 .. 32767 Approx 9x1018 -128 .. 127 38 Approx 3.4x10 308 Approx 1.8x10 false, true
char represented by a 2-byte Unicode value 2Designed to represent all characters in the written world Allows 65536 characters (35000 are in use) whereas ascii only allows 255 Expressed as hexidecimal \u0000 to \uFFFF \ \ (\u0000 to \u00FF is the ascii set) (\ \ \u indicates a Unicode value Check out www.unicode.org for more details
Variables
Variables must be declared before use Variable names must begin with a letter but can contain letters and digits Variable names are case sensitive
Assignment/initialization
Constant variables
In Java, the keyword final denotes a constant Constant variables cannot be assigned to
Operators
Usual arithmetic operators + - * / are used in Java as in C Integer divide / and modulus % as in C Increment ++ and decrement - Exponentiation uses pow() function which is part of the Math class
== (equal to) != (not equal to) <, >, <=, >= (less, greater, less or equal, greater or equal) & (and) | (or) ^ (xor) ~ (not)
Boolean expressions
In Java the result of a boolean expression is a boolean type (true or false) This cant be converted to an int (as in C)
if (x == y) {} // Result is a boolean // Ok in C, wont compile in // Java
Control flow
if (..) {} if (..) {} else if (..) {} . else {} switch (..) { case 1: break; default: break; } for (..) {} while (..) {} do {} while (..);
Iteration statements
root_old=root; root=(root_old+a/root_old)/2; } while (Math.abs(root_old-root)>1.0E-6); } System.out.println("The square root of " + a + " is " + root);
Computes the square root of an inputted number using a simple algorithm Same control structure as in C Note the use of indentation to indicate control In Java, keyboard input is not straightforward Done by the readDouble() method in readDouble() class Console
Strings
Strings are sequences of characters as in C The standard Java library has a predefined class String
Strings are immutable (unlike in C) individual characters in the string cannot be changed
name[0] = m; // Not allowed!
The == operator should not be used it tests to see if the strings are stored in the same location! int length() returns the length of the string There are more than 50 methods in the Java String class! (java.lang.String) (java.lang.String)
Arrays
Array variable is effectively a pointer to an array allocated on the heap (hence arrays passed by reference) BUT Cant do pointer arithmetic (as in C)
// NOT ALLOWED!
Copying arrays
newArray
2 4 6 8
evenNumbers
2 4 6 8
2 4 6 8
newArray
Class java.util.Arrays has a number of java.util. convenience utility functions for arrays Arrays.sort(a) - sorts array a into ascending order Arrays.fill(a,val) fills array a with value val Arrays.binarySearch(a, key) searches for a value key in array a Arrays.equals(a1,a2) test for equivalence of arrays a1 and a2
And finally
Basic Java programming is less error prone than C No pointers to worry about There is a genuine boolean type We have yet to think about object oriented concepts Classes are the subject of the next lecture
All the Java programming assignments for this semester are now available on my web site http://www.eee.bham.ac.uk/spannm/Courses/ee2e.htm Lab structure
Swing and GUIs (2 weeks), assessed Semester 2 Major programming assignment, assessed
Organisation of the lab You will work in pairs The programming assignments cover material already done in lectures Please carry out the preparatory work before the lab with your partner You will need to put in some time outside the lab slots to finish each exercise
Assessment : Makes up 85% of the 2E1 mark There will be 3 programming assignments this semester Assessed by submission of code + program outputs per lab group More details will follow and submission will be at the end of the semester There will be 1 major programming assignment next semester Assessed by a formal lab report per lab group