Lecture 01 - Elementr Java
Lecture 01 - Elementr Java
CSC02A2
Outline
Outline
Outline The Basics
Identifiers, types and constants
Java Data Types
Operators and numerical type
2 Programming Constructs conversion
1 The Basics Character data
Hello World
3
The Basics
Outline
The Basics The Basics
Identifiers, types and constants
Java programs consist of sets of objects which interact with each other via method Java Data Types
Operators and numerical type
invocation. conversion
Character data
A Java class is created within a textual .java source file. The name of the Ouput and Input
Hello World
outermost, public class must match the name of the Java source file.
Programming Constructs
Java source files are compiled into Java byte-code .class files by way of the Logical Operators
Conditional statements
javac program and run by way of the java program command. In order to be Selection statements
directly executed by the JVM a Java class must have a public main method. Iterators
Flow control
5
Outline
Identifiers, types and constants The Basics
Identifiers, types and constants
Identifiers in Java follow roughly the same rules as in C++. Java Data Types
Operators and numerical type
conversion
In Java variables are either reference types or fixed length primitive types (see
next slide).
Constants in Java are declared using the keywords final (which means that the
variable’s value cannot be altered) and static (which ensures that only one copy
of the variable exists in memory). Constants are typically names in ALL_CAPS.
6
Outline
Java Data Types The Basics
Identifiers, types and constants
Java Data Types
Name Range Storage size Wrapper class Suffix
Operators and numerical type
conversion
boolean true or false not defined Boolean none Character data
−215 . . . 215 − 1
Logical Operators
short 16 bits Short none Conditional statements
7
Outline
Operators and numerical type conversion The Basics
Identifiers, types and constants
• Numerical data types may be automatically converted by Java as follows Ouput and Input
Hello World
• If one operand is a double then the other is converted into a double Programming Constructs
• Otherwise if one operand is a float the other is converted into a float Logical Operators
Conditional statements
• Otherwise if one operator is a long the other is converted into a long Selection statements
Iterators
• Otherwise both operands are converted into type int Flow control
Take note
Data types may be manually converted via type casting. Beware of loss of inform-
ation when moving from wider to shorter ranged data types and from higher to
lower precision.
8
Outline
Character data The Basics
Identifiers, types and constants
Java uses the Unicode character encoding scheme as opposed to the conventional Java Data Types
Operators and numerical type
ASCII scheme. The first block of Unicode values contains the entire ASCII table. conversion
Character data
The \u escape sequence may be used when setting a char variable’s value: Ouput and Input
Hello World
char alpha = "\u03b1";
Programming Constructs
9
Outline
Basic and formatted output I The Basics
Identifiers, types and constants
• print() and println() for basic output to the console. Character data
Ouput and Input
• printf() for formatted output. Hello World
Programming Constructs
1 // Basic output Logical Operators
10
Outline
Basic and formatted output II The Basics
Identifiers, types and constants
Java Data Types
Format specifiers are: Operators and numerical type
conversion
• %e – scientific notation
Selection statements
Iterators
11
Outline
Basic Input The Basics
Identifiers, types and constants
Java provides a useful Scanner class for dealing with user input from devices Java Data Types
Operators and numerical type
such as the keyboard. conversion
Character data
In order to convert between String and primitive data type values each primitive Ouput and Input
Hello World
Wrapper class provides appropriate parsing methods.
Programming Constructs
Parsing the text may result in an Exception. Exception handling will be Logical Operators
Conditional statements
discussed in a future lecture. Selection statements
Iterators
Flow control
12
Outline
Hello World class The Basics
Identifiers, types and constants
6 /* Hello World
7 This is a block comment over many
8 lines. Programming Constructs
9 */ Logical Operators
Running the javac command will produce a HelloWorld.class class file. To run Hello World
14
Programming Constructs
Outline
Logical Operators The Basics
Identifiers, types and constants
Java Data Types
Operator Short circuit Bit-wise Description
Operators and numerical type
conversion
NOT ! ! Negation Character data
Ouput and Input
AND && & Conjunction Hello World
16
Outline
Conditional statements The Basics
Identifiers, types and constants
Java Data Types
1 int value = 0;
Operators and numerical type
2 if(booleanCondition) conversion
3 { Character data
10 } Iterators
12 value = booleanCondition ? 1 : 2;
13
14 // Dangling else
15 if(false)
16 if(false)
17 System.out.println("A");
18 else
19 System.out.println("B");
20 // What is the output?
17
Outline
Selection statements The Basics
Identifiers, types and constants
Java Data Types
Switch statements allow different paths of execution for many different values
Operators and numerical type
without the clutter of many if else statements. conversion
Character data
Ouput and Input
1 // Strings work in JDK7
Hello World
2 switch(char|byte|short|int|enum|String)
3 { Programming Constructs
4 case value0: //Code to handle value0; Logical Operators
18
Outline
Iterators The Basics
Identifiers, types and constants
For nested loops it is possible to use the labelled versions of the break and
continue statements. This practice is discouraged as it dramatically reduces the
readability of the code.
20