0% found this document useful (0 votes)
5 views

Lecture 01 - Elementr Java

The document outlines the fundamentals of Java programming, covering topics such as identifiers, data types, operators, and control structures. It explains how Java programs are structured, how to handle input and output, and provides examples of basic programming constructs like loops and conditional statements. Additionally, it includes details on compiling and running Java programs, as well as formatting output and handling exceptions.

Uploaded by

nigelmutizwa17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 01 - Elementr Java

The document outlines the fundamentals of Java programming, covering topics such as identifiers, data types, operators, and control structures. It explains how Java programs are structured, how to handle input and output, and provides examples of basic programming constructs like loops and conditional statements. Additionally, it includes details on compiling and running Java programs, as well as formatting output and handling exceptions.

Uploaded by

nigelmutizwa17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Elementary Java Programming

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

Logical Operators Ouput and Input


Identifiers, types and constants Hello World

Conditional statements Programming Constructs


Java Data Types Logical Operators

Selection statements Conditional statements


Operators and numerical type Selection statements

conversion Iterators Iterators


Flow control

Character data Flow control


Ouput and Input

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

Command-line arguments may be passed to the main method via a mechanism


similar to that found in C++. Unlike C++ there is only one acceptable form of the
main method.

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

• An identifier is a sequence of characters consisting of letters, digits, under- Character data


Ouput and Input
scores and dollar signs. Hello World

• An identifier cannot start with a digit Programming Constructs

• An identifier cannot be a reserved word. Logical Operators


Conditional statements

• An identifier cannot be true, false or null. Selection statements


Iterators
• An identifier can be of any length. Flow control

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

byte −27 . . . 27 − 1 8 bits Byte none


Ouput and Input
Hello World

char 0 . . . 65535 16 bits Character none Programming Constructs

−215 . . . 215 − 1
Logical Operators
short 16 bits Short none Conditional statements

int −231 . . . 231 − 1 32 bits Integer none Selection statements


Iterators

long −263 . . . 263 − 1 64 bits Long L Flow control

float ±3.408235e+38 32 bits Float F


double ±1.79769e+308 64 bits Double D
Object — 32 bits — —

7
Outline
Operators and numerical type conversion The Basics
Identifiers, types and constants

• Operators behave in the same manner as C++.


Java Data Types
Operators and numerical type

• Operators in Java may not be overloaded.


conversion
Character data

• 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

alpha now contains the character α Logical Operators


Conditional statements
Selection statements
Escape characters need to be used in order to handle special characters: Iterators
Flow control

• \b (backspace) • \f (form feed) • \' (single quote)


• \t (tab) • \r (carriage return) • \" (double quote)
• \n (line feed) • \\ (backslash)

String is an object type which may be used to store sequences of characters.

9
Outline
Basic and formatted output I The Basics
Identifiers, types and constants

The System.out print stream contains: Java Data Types


Operators and numerical type
conversion

• 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

2 System.out.println("String Expression"); Conditional statements


Selection statements
3 // Formatted output
Iterators
4 int d = 108;
Flow control
5 boolean t = true;
6 System.out.printf("Favourite number is %d FM. The statement is %b", d, t);

10
Outline
Basic and formatted output II The Basics
Identifiers, types and constants
Java Data Types
Format specifiers are: Operators and numerical type
conversion

• %b - boolean Character data


Ouput and Input

• %d - decimal Hello World

• %c – char Programming Constructs


Logical Operators
• %f – floating point Conditional statements

• %e – scientific notation
Selection statements
Iterators

• %s – string. Flow control

In addition width and precision can be set as follows %Width.Precisionf e.g.


%10.2f has a width of 10 and two decimal places.

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

1 // This is an inline comment Java Data Types


2 // Importing Scanner class Operators and numerical type
conversion
3 import java.util.Scanner;
Character data
4 class HelloWorld
5 { Ouput and Input

6 /* Hello World
7 This is a block comment over many
8 lines. Programming Constructs
9 */ Logical Operators

10 // Constant declaration Conditional statements


11 public static final double PI = 3.1415926535897932384D; Selection statements
12 Iterators
13 // Main method with arguments Flow control
14 public static void main(String[] args)
15 {
16 // explicit type conversion
17 int humble_pi = (int) PI;
18 // Console output
19 System.out.println("Hello World!");
20 System.out.print("Whom shall I address?: ");
21 // Wrap Scanner around Sysytem input
22 Scanner scInput = new Scanner(System.in);
23 // Read a line
24 String strWho = scInput.nextLine();
25 System.out.println("Why hello " + strWho);
26 }
27 }
13
Outline
Compiling Hello World The Basics
Identifiers, types and constants
Java Data Types
To compile the class use the following command:
Operators and numerical type
conversion
c:\Code>javac HelloWorld.java Character data
Ouput and Input

Running the javac command will produce a HelloWorld.class class file. To run Hello World

the program HelloWorld program use: Programming Constructs


Logical Operators

c:\Code>java HelloWorld Conditional statements


Selection statements
Iterators
Flow control

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

OR || | Disjunction Programming Constructs


Logical Operators
XOR ^ ^ Exclusion Conditional statements
Selection statements
Iterators
Flow control

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

4 value = 1; // Perform truthful operation Ouput and Input


Hello World
5
6 } Programming Constructs
7 else Logical Operators
8 { Conditional statements

9 value = 2; // Perform false operation Selection statements

10 } Iterators

11 // Tenary operator shorthand Flow control

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

5 break; Conditional statements


Selection statements
6 case value1: //Code to handle value1;
Iterators
7 break;
Flow control
8 ...
9 ...
10 case valueN: //Code to handle valueN;
11 break;
12 default: //Code to handle no matches;
13 break;
14 }

18
Outline
Iterators The Basics
Identifiers, types and constants

1 // While loop Java Data Types


2 while(booleanCondition) Operators and numerical type
3 { conversion

4 // loop body Character data


5 } Ouput and Input
6 Hello World
7 // Do..while loop
8 do Programming Constructs
9 { Logical Operators
10 // loop body Conditional statements
11 } while(booleanCondition) Selection statements
12
Iterators
13 // For loop ‐ Basic
Flow control
14 for(initialisation; comparison; progression)
15 {
16 // loop body
17 }
18
19 for(int k = 0; k < 10; ++k)
20 {
21 System.out.println(k);
22 }
23
24 // For loop ‐ Collections
25 for(String s : args)
26 {
27 System.out.println(s);
28 } 19
Outline
Flow control The Basics
Identifiers, types and constants
Java Data Types
Iteration control is best performed by building the necessary logic into the loop’s
Operators and numerical type
continuation condition. However sometimes necessary to end a loop or current conversion
Character data
loop iteration: Ouput and Input
Hello World
• break – immediately ends the current loop.
Programming Constructs
• continue – immediately ends the current loop iteration. Logical Operators
Conditional statements
Selection statements
These keywords only operate in loops or switch statements. Iterators
Flow control

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.

1 // How many times will the outer loop run?


2 counting:
3 for(int k = 0; k < 10; ++k)
4 for(int j = 0; j < 10; ++j)
5 if(j == 9) break counting;

20

You might also like