22
OOSSE - Java Lecture 1 1 Jun 16, 2022 Introduction to Java OOSSE Programming with Java Lecture 1

02 introductionto java

  • Upload
    apu

  • View
    271

  • Download
    2

Embed Size (px)

Citation preview

Page 1: 02 introductionto java

OOSSE - Java Lecture 1 1Apr 12, 2023

Introduction to Java

OOSSE Programming with JavaLecture 1

Page 2: 02 introductionto java

OOSSE - Java Lecture 1 2Apr 12, 2023

Objectives

In this lecture, we will:• Introduce Java and the Java Virtual Machine • Discuss the basic object oriented concepts• Define classes and instances• Review the structure and syntax of a Java program• Introduce the Scanner class and simple I/O

Page 3: 02 introductionto java

OOSSE - Java Lecture 1 3Apr 12, 2023

Introduction to Java

• Java is a high level object oriented programming language

• Java was designed to be:– Simple– Object oriented– Distributed– Robust– Secure– Architecture-Neutral– Portable– Multithreaded

Page 4: 02 introductionto java

OOSSE - Java Lecture 1 4Apr 12, 2023

A Simple Java Application

// A simple Hello World Java Application

public class Hello{ public static void main (String[] args) {

System.out.println ("Hello World");System.out.println ("Welcome to Java");

}}

Page 5: 02 introductionto java

OOSSE - Java Lecture 1 5Apr 12, 2023

The Java Source File

• A Java program can be developed using any simple editor to generate the Java source file

• For example Notepad could be used• The source file is simple text but must be saved with

an extension of java

Page 6: 02 introductionto java

OOSSE - Java Lecture 1 6Apr 12, 2023

Compiling a Java Application

• The Java source file is in a suitable format for humans to read but must be compiled into bytecode before it can be used

• Sun provide a compiler called javac that is used to compile the source code into bytecode

• The compiler can be invoked from a command prompt in a windows environment– javac Hello.java

• The result of a successful compilation is a class file containing the bytecode– Hello.class

Page 7: 02 introductionto java

OOSSE - Java Lecture 1 7Apr 12, 2023

Executing a Java Application

• The bytecode can be executed on the Java Virtual Machine using the interpreter provided by Sun: java Hello

• Note the bytecode is held in a file called Hello.class but the extension class is not included in the call to the interpeter: java Hello

Page 8: 02 introductionto java

OOSSE - Java Lecture 1 8Apr 12, 2023

Java Application Development Process

Create/ Modify Source Code

Source Code

Compile Source Code

Bytecode

Execute Bytecode

notepad

Hello.java

Hello.class

javac Hello.java

java Hello

Page 9: 02 introductionto java

OOSSE - Java Lecture 1 9Apr 12, 2023

The Java Virtual Machine

• The bytecode produced by the Java compiler is not targeted at a specific machine– It is targeted at the Java Virtual Machine

• The Java Runtime Environment executes the virtual machine– The Java Bytecode is executed on the virtual

machine

• Hence the bytecode is portable– It will execute on any machine that is running the

Java Virtual Machine– The JVM needs to be target specific

Page 10: 02 introductionto java

OOSSE - Java Lecture 1 10Apr 12, 2023

Pitfalls

• Java is case sensitive– Hello and hello are not the same

• All Java programs must have the java extension• The extension is specified when the compiler is

used– javac Hello.java

• The bytecode produced by the compiler is held in a file with a .class extension but the extension is NOT specified when the interpreter is used– java Hello

Page 11: 02 introductionto java

OOSSE - Java Lecture 1 11Apr 12, 2023

Java and Classes

• Java is an object oriented programming language• All code is wrapped in the form of a class:

public class Hello{

…}

• Note the keywords public and class• The class is given a name, Hello in this case, and

must be saved in a file called Hello.java • Code similar to this will appear in each of your

applications– The name of the class will change

Page 12: 02 introductionto java

OOSSE - Java Lecture 1 12Apr 12, 2023

The Method main

• Classes use methods to specify what can be done• A main method is required in a Java application and

defines where the application begins• The structure of main is fixed and will be the same

in all applications: public static void main (String[] args)

{…

}• The syntax will be discussed in detail later

– For now please just accept that it must be as it is

Page 13: 02 introductionto java

OOSSE - Java Lecture 1 13Apr 12, 2023

The Body of main

• The code that you write to specify what the application should do makes up the body of main

• A block of code; that is one or more program statements wrapped in braces { }

• For example: {

System.out.println ("Hello World");System.out.println ("Welcome to Java");

}• Note that each statement is terminated by a

semicolon

Page 14: 02 introductionto java

OOSSE - Java Lecture 1 14Apr 12, 2023

Simple Input – The Scanner Class

Page 15: 02 introductionto java

OOSSE - Java Lecture 1 15Apr 12, 2023

Input in Java – the Scanner class

• Java 1.5 introduces adequate support for input– Prior to this input from the keyboard was not trivial

• The Scanner class can be used for keyboard input• Consider the following code extract:

// build an object that knows how to obtain keyboard data

Scanner kybd = new Scanner(System.in);int num1;// input the next integer and assign to num1num1 = kybd.nextInt();

• The object kybd knows how to obtain the next integer from the keyboard that is identified by System.in

Page 16: 02 introductionto java

OOSSE - Java Lecture 1 16Apr 12, 2023

Objects and Classes

• Using classes makes performing complex tasks simple

• In order to use a class you need to know:– Where the class is located– How to build an instance of the class – an object– What instances of the class can do– How to ask the instance to do something

• A class has a set of methods that define the functionality that it can provide– For example nextInt is a method of the Scanner class

• A method is invoked by sending a message to an object – num1 = kybd.nextInt();

Page 17: 02 introductionto java

OOSSE - Java Lecture 1 17Apr 12, 2023

Using the Scanner Class

• The Scanner class is contained in a Java package– A package is a library of classes

• The package must be imported into an application that uses the Scanner class– So that the compiler can find it

import java.util.*; // the package containing Scannerpublic class TestScan{

public static void main (String [] args){

// build an instance of Scanner, that is an object

Scanner kybd = new Scanner(System.in);

Page 18: 02 introductionto java

OOSSE - Java Lecture 1 18Apr 12, 2023

Methods of the Scanner Class

• Some of the methods of the Scanner class are:– nextInt() reads an integer – nextDouble() reads a double– next() reads a word– nextLine() reads the rest of the current input line

• The two methods next and nextLine both return a value of type String– String name;– name = kybd.next(); // assumes kybd is an

instance of // the Scanner class

Page 19: 02 introductionto java

OOSSE - Java Lecture 1 19Apr 12, 2023

Pitfalls

• The nextLine method inputs the REST of a line of text– It starts wherever the last input finished

• Consider the following section of code:Scanner kybd = new Scanner(System.in);String s1, s2;int num1 = kybd.nextInt();s1 = kybd.nextLine();s2 = kybd.nextLine();

• What would s1 and s2 be if you entered the following?42The answer to Life the Universe and everything

Page 20: 02 introductionto java

OOSSE - Java Lecture 1 20Apr 12, 2023

Pitfalls

• The variable s1 would be set to an empty string• The variable s2 would be set to “The answer to”

• Why?

• What would s1 and s2 be if you entered the following?42 The answer to Life the Universe and everything

Page 21: 02 introductionto java

OOSSE - Java Lecture 1 21Apr 12, 2023

Coding Style Guidelines

• Java is a free format language but layout makes a huge difference to understanding

• Adopt a good program layout to improve readability– Generous use of space– Vertical alignment of keywords– Indentation as appropriate

• Use meaningful comments– Level of intent– Particularly where the code is not obvious

• Use meaningful identifiers• Avoid complex program structures where possible• Try not to sacrifice clarity and simplicity for efficiency

Page 22: 02 introductionto java

OOSSE - Java Lecture 1 22Apr 12, 2023

Summary

In this lecture we have:• Introduced Java and the Java Virtual Machine • Discussed the basic object oriented concepts• Defined classes and instances• Reviewed the structure and syntax of a Java

program• Introduced the Scanner class and simple I/O