Introduction to Java Brief history of Java Sample Java Program - - PowerPoint PPT Presentation

introduction to java
SMART_READER_LITE
LIVE PREVIEW

Introduction to Java Brief history of Java Sample Java Program - - PowerPoint PPT Presentation

Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1 1 Introduction to Java History Invented in 1991 - Sun Microsystems, Inc (James Gosling). Sun


slide-1
SLIDE 1

1

Introduction to Java

 Brief history of Java  Sample Java Program  Compiling & Executing

Reading: => Section 1.1

slide-2
SLIDE 2

2

Introduction to Java

History

Invented in 1991 - Sun Microsystems, Inc (James Gosling).

Sun Microsystems was purchased by Oracle Corporation in 2010.

Originally a language for programming home appliances.

Later (1994) used for internet applications and general-purpose programming.

Why the name “Java”?

  • Supposedly came from a list of random words (wikipedia).
slide-3
SLIDE 3

3

A Java Application

import java.util.Scanner; public class FirstProgram { public static void main(String[] args) { int n1, n2; Scanner keyboard = new Scanner(System.in); System.out.println(“Enter an integer:”); n1 = keyboard.nextInt(); System.out.println(“Enter another integer:”); n2 = keyboard.nextInt(); System.out.println(“The sum is:”); System.out.println(n1 + n2); } }

>javac FirstProgram.java >java FirstProgram Enter an integer: 15 Enter another integer: 7 The sum is: 22 >

slide-4
SLIDE 4

4

Explanation of Code ...

Code at the beginning of the program (to be explained later):

import java.util.Scanner; public class FirstProgram { public static void main(String[] args) { 

Java applications all have similar code at the beginning

  • The name of the class differs from one program to another.
  • The name of the class is also the name of the file.
  • Notice that the blank line is gone!
slide-5
SLIDE 5

5

… Explanation of Code ...

The following creates two variables named n1, n2 for storing two whole numbers (integer): int n1, n2;

These are called “variable declarations.”

In this program they are used to store the user’s response.

slide-6
SLIDE 6

6

… Explanation of Code ...

The following creates an object called keyboard of the Scanner class:

Scanner keyboard = new Scanner(System.in); 

System.in refers to the keyboard

The Scanner class provides the program with access to keyboard input.

slide-7
SLIDE 7

7

Explanation of Code ...

Displays a “text” string to the screen:

System.out.println(“Enter an integer:”); 

Note the “dot” delimiter:

  • System is a class
  • out an object
  • println is a method that outputs something

Double-quoted text inside the parentheses is referred to as an argument or parameter to the method.

slide-8
SLIDE 8

8

… Explanation of Code ...

The following inputs (or “reads”) an integer typed in using the keyboard and stores it in the variable n1:

n1 = keyboard.nextInt(); 

The next two lines are similar:

System.out.println(“Enter another integer:”); n2 = keyboard.nextInt();

slide-9
SLIDE 9

9

… Explanation of Code

The following prints the sum to the console (or screen):

System.out.println(“The sum is:”); System.out.println(n1 + n2);

By the way, every character counts!

slide-10
SLIDE 10

10

Compiling and Running a Java Program

Type the program into a file:

  • FirstProgram.java

Make sure you get the file name correct! 

Compile (from the command-line, i.e., DOS):

  • javac <file>.java

Run (and link):

  • java <file>
slide-11
SLIDE 11

11

Extra Work – Yippee!

Type-in, compile and run the previous program.

Experiment with “what if” scenarios:

  • Add some syntax errors
  • Run the program incorrectly – input improper data
  • Modify the program to work with 3 ints

 Note that if you change the program, you have to recompile

before you rerun!

slide-12
SLIDE 12

12

Some Helpful Commands

Some helpful commands:

  • cd <dir-name>
  • change directory
  • cd ..
  • move up a directory
  • dir <pattern>
  • list directory contents matching the pattern
  • mkdir <dir-name>
  • create a new directory
  • del <pattern>
  • delete a file or files matching the pattern

Suggestion – create a new directory/subfolder on your “C” drive to store all class related programs.

Create subfolders as appropriate.