COMP201 Topic 2 / Slide 2. Objective and Outline. â Objective. â« Show basic programming concepts. â Outline. â« W
COMP201 Java Programming
Topic 2: Java Basics
COMP201 Topic 2 / Slide 2
Objective and Outline
Objective
Show basic programming concepts
Outline
What do java programs look like? Basic ingredients – Java primitive types – Variables and constants – Operators and control flow
Simple commonly used built-ins. – Simple input/output – Arrays and Strings
COMP201 Topic 2 / Slide 3
What do java program look like? public class MyProgram { public static void main(String args[]) { System.out.println(“Hello world!”); } } //File: MyProgram.java
public: Access modifier
class: everything is inside a class MyProgram: class name.
matches file name.
Case sensitive
main: method of the wrapping class
Compilation and run: javac MyProgram.java => MyProgram.class java MyProgram
COMP201 Topic 2 / Slide 4
Objective and Outline
Outline
What do java programs look like? Basic ingredients – Java primitive types – Variables and constants – Operators and control flow
Simple commonly used built-ins. – Simple input/output – Arrays and Strings
COMP201 Topic 2 / Slide 5
Basic Ingredients/Primitive Types
Integers byte (1 byte, -128 to 127) short (2 bytes) For really large numbers use int (4 bytes) BigInteger and BigDecimal long (8 bytes) classes Floating-point types float (4 bytes, 6-7 significant decimal digits) double (8 bytes, 15 significant decimal digits) char (2 byte, Unicode) (ASCII 1 byte) boolean (true or false) Those are all the primitive types in Java. Everything else is an object.
COMP201 Topic 2 / Slide 6
Basic Ingredients/Primitive Types
Legal conversions between numeric types char bytes
short
int
float
double
Arrows indicate direction of legal and automatic conversion – double x = 123; – long x = 123456789; float y = x;
long
Solid arrow: no loss of precision Dotted arrow: might lose precision – z=1.234567E8
COMP201 Topic 2 / Slide 7
Basic Ingredients/Primitive Types char bytes
short
int
float
double
Conversion in the opposite direction required explicit cast.
long
double x = 9.997; int num = (int) x; int num = x; // does not compile
Can easily lead to the loss of precision (round-up errors) Cannot convert between boolean and numerical values.
COMP201 Topic 2 / Slide 8
Objective and Outline
Outline
What do java programs look like? Basic ingredients – Java primitive types – Variables and constants – Operators and control flow
Simple commonly used built-ins. – Simple input/output – Arrays and Strings
COMP201 Topic 2 / Slide 9
Basic Ingredients/Variables and Constants
Variables can be declared anywhere for (int i = 0; i < 20; i++) { System.out.println(“Hi”); char ch = ‘A’; } double pi = 3.14159;
Java compilers require initialization of local variables before use public void someMethod() { … int x; // does not compile System.out.println(“The value of x is “ + x); }
Instance variables of class automatically initialized.
COMP201 Topic 2 / Slide 10
Basic Ingredients/Variables and Constants
final marks a variable “read-only” Variable is assigned once and cannot be changed public void someMethod() { final double pi
=
3.14159;
.. .. ..
pi = 3.14; // illegal
}
Use static final to define constants which are available to multiple objects of the same class public class Time { static final int MinHour = 0; static final int MaxHour = 23; private int hour, minute; // these properties are set to 0 // unless overwritten by constructor … }
COMP201 Topic 2 / Slide 11
Objective and Outline
Outline
What do java programs look like? Basic ingredients – Java primitive types – Variables and constants – Operators and control flow
Simple commonly used built-ins. – Simple input/output – Arrays and Strings
COMP201 Topic 2 / Slide 12
Basic Ingredients/Operators
Basically the same as C++: +, -, *, /, %, ++, --, =, ==, !=, !, &&, || =, +=, -=, *=, /=, %=,
User cannot “overload” operators; although + is overloaded to do string concatenation
In C++: – Int x=0; x += 1; – String& String::operator+=( const String &s)..
Note that methods can be overloaded
COMP201 Topic 2 / Slide 13
Basic Ingredients /Operators No Pointers!
No explicit pointer types (although objects are implemented as references)
No & or * operators
In C++:
int *i = (int *) malloc( 3 * sizeof(int)); *(i+1) = 2; int& y = i;
No pointer arithmetic No function pointers
COMP201 Topic 2 / Slide 14
Basic Ingredients/Control flow Basically the same as C++: if (boolean-expr) statement; (has optional else) if ( x = 0 ) //leads to compiling error
for (expr; boolean-expr; expr) statement; while (boolean-expr) statement; (do-while variant also) switch (integer-expr) case constant: statement; break;
COMP201 Topic 2 / Slide 15
Basic Ingredients/Control flow Recursion Basically the same as C++: public class Factorial { public static int factorial( int n ) { if ( n