Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java Brief history of Java Sample Java Program

Similar presentations


Presentation on theme: "Introduction to Java Brief history of Java Sample Java Program"— Presentation transcript:

1 Introduction to Java Brief history of Java Sample Java Program
Compiling & Executing

2 Introduction to Java History
Invented in James Gosling, Sun Microsystems, Inc. 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). Sun Microsystems was purchased by Oracle Corporation in 2010.

3 Applets vs. Java Applications
Java programs intended to be downloaded via the internet and run immediately. Typically embedded in a web-page and run in a web browser. “little applications” Applications: Java programs intended to be installed on a system and then executed. Often larger, more complex applications. Applets and applications are programmed slightly differently.

4 A Java Application import java.util.Scanner; public class FirstProgram
{ public static void main(String[] args) int n1, n2; System.out.println(“Hello”); System.out.println(“Enter two integers”); Scanner keyboard = new Scanner(System.in); n1 = keyboard.nextInt(); n2 = keyboard.nextInt(); System.out.println(“The sum is”); System.out.println(n1 + n2); }

5 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.

6 … 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.

7 Explanation of Code ... Display text strings to the screen:
System.out.println(“Hello”); System.out.println(“Enter two integers”); 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.

8 … 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.

9 … Explanation of Code ... The following inputs (or “reads”) two integers typed in from the keyboard and stores them in the variables n1 and n2: n1 = keyboard.nextInt(); n2 = keyboard.nextInt();

10 … 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!

11 Compiling and Running a Java Program
Type the program into a file: FirstProgram.jav Make sure you get the file name correct! Compile (from the command-line, i.e., DOS): javac <file>.java Run (and link): java <file> <file> must have a main method

12 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


Download ppt "Introduction to Java Brief history of Java Sample Java Program"

Similar presentations


Ads by Google