Introduction to Java Programming Introduction Java applications and

  • Slides: 24
Download presentation
Introduction to Java Programming

Introduction to Java Programming

Introduction – Java applications and applets – Primitive data types – Java control flow

Introduction – Java applications and applets – Primitive data types – Java control flow – Methods – Object-oriented programming

Fundamentals of Programming – Introduction to Java – Primitive Data Types and Operations –

Fundamentals of Programming – Introduction to Java – Primitive Data Types and Operations – Control Statements – Methods

Introduction to Java F What Is Java? F Getting Started With Java Programming –

Introduction to Java F What Is Java? F Getting Started With Java Programming – Compiling and Running a Java Application

What Is Java? F History F Characteristics of Java

What Is Java? F History F Characteristics of Java

History F James Gosling F Oak F Java, May 20, 1995, Sun World F

History F James Gosling F Oak F Java, May 20, 1995, Sun World F Hot. Java – The first Java-enabled Web browser

Characteristics of Java F Java is simple F Java is object-oriented F Java is

Characteristics of Java F Java is simple F Java is object-oriented F Java is distributed F Java is interpreted F Java is robust F Java is secure F Java is architecture-neutral F Java is portable F Java’s performance F Java is multithreaded F Java is dynamic

Getting Started with Java Programming FA Simple Java Application F Compiling Programs F Executing

Getting Started with Java Programming FA Simple Java Application F Compiling Programs F Executing Applications

A Simple Application Example 1. 1 //This application program prints Welcome //to Java! public

A Simple Application Example 1. 1 //This application program prints Welcome //to Java! public class Welcome { public static void main(String[] args) { System. out. println("Welcome to Java!"); } }

Compiling Programs F On command line – javac file. java

Compiling Programs F On command line – javac file. java

Executing Applications F On command line – java classname

Executing Applications F On command line – java classname

Example javac Welcome. java Welcome output: . . .

Example javac Welcome. java Welcome output: . . .

Primitive Data Types and Operations F Identifiers, Variables, and Constants F Primitive Data Types

Primitive Data Types and Operations F Identifiers, Variables, and Constants F Primitive Data Types – Byte, short, int, long, float, double, char, boolean F Operators – +, -, *, /, %, +=, -=, *=, /=, %=, ++, -F Expressions F Style and Documentation F Syntax Errors, Runtime Errors, and Logic Errors

Numerical Data Types byte 8 bits short 16 bits int 32 bits long 64

Numerical Data Types byte 8 bits short 16 bits int 32 bits long 64 bits float 32 bits double 64 bits

Control Statements FSelection Statements –Using if and if. . . else –Nested if Statements

Control Statements FSelection Statements –Using if and if. . . else –Nested if Statements –Using switch Statements –Conditional Operator FRepetition Statements –Looping: while, do, and for –Nested loops –Using break and continue

Selection Statements F if Statements F switch Statements F Conditional Operators

Selection Statements F if Statements F switch Statements F Conditional Operators

if Statements if (boolean. Expression) { statement(s); } Example: if ((i >= 0) &&

if Statements if (boolean. Expression) { statement(s); } Example: if ((i >= 0) && (i <= 10)) { System. out. println("i is an “ + “integer between 0 and 10"); }

The if. . . else Statement if (boolean. Expression) { statement(s)-for-the-true-case; } else {

The if. . . else Statement if (boolean. Expression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; }

Repetitions F while F do Loops F for F Loops break and continue

Repetitions F while F do Loops F for F Loops break and continue

Introducing Methods A method is a collection of statements that are grouped together to

Introducing Methods A method is a collection of statements that are grouped together to perform an operation. Method Structure

Declaring Methods public static int max(int num 1, int num 2) { if (num

Declaring Methods public static int max(int num 1, int num 2) { if (num 1 > num 2) return num 1; else return num 2; }

Passing Parameters void n. Println(String message, int n) { for (int i=0; i<n; i++)

Passing Parameters void n. Println(String message, int n) { for (int i=0; i<n; i++) System. out. println(message); }

Object-Oriented Programming – Objects and Classes – Class Inheritance

Object-Oriented Programming – Objects and Classes – Class Inheritance

Exception Handling F Exceptions and Exception Types F Claiming Exceptions F Throwing Exceptions F

Exception Handling F Exceptions and Exception Types F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F The finally Clause