Chapter 2: Your First Program! Hello World: Let’s Program  All programs must have the extension.java  Our first program will be named: HelloWorld.java.

Slides:



Advertisements
Similar presentations
Begin Java Pepper. Objectives What is a program? Learn the basics of your programming tool: BlueJ Write a first Java program and see it run Make some.
Advertisements

JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING. OBJECTIVES FOR THIS UNIT Upon completion of this unit, you should be able to: Explain the Java virtual machine.
Your First Java Program: HelloWorld.java
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
CHAPTER 1 INTRODUCTION GOALS  To understand the activity of programming  To learn about the architecture of computers  To learn about machine code and.
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
Slide 1 of 40. Lecture A The Java Programming Language Invented 1995 by James Gosling at Sun Microsystems. Based on previous languages: C, C++, Objective-C,
Hello, world! Dissect HelloWorld.java Compile it Run it.
CS107 Introduction to Computer Science Java Basics.
1. 2 Chapter 1 Introduction to Computers, Programs, and Java.
Unit 2: Java Introduction to Programming 2.1 Initial Example.
IB Computer Science II Paul Bui
“Introduction to Programming With Java”
Computer Programming 12 Mr. Jean March 3 rd, 2014.
Introduction to Java Tonga Institute of Higher Education.
Hello AP Computer Science!. What are some of the things that you have used computers for?
An intro to programming. The purpose of writing a program is to solve a problem or take advantage of an opportunity Consists of multiple steps:  Understanding.
CS 106 Introduction to Computer Science I 01 / 25 / 2010 Instructor: Michael Eckmann.
CS107 Introduction to Computer Science Java Basics.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter One: Introduction.
Welcome to the Lecture Series on “Introduction to Programming With Java”
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
Introduction to Computers and Java Chapter 1.3. A Sip of Java: Outline History of the Java Language Applets A First Java Program Compiling a Java Program.
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
Lecture 1 Introduction to Java MIT-AITI Ethiopia 2004.
Chapter 1 CSIS-120: Java Intro. What is Programming?  A: It is what makes computer so useful.  The flexibility of a computer is amazing  Write a term.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Board Activity Find your seat on the seating chart Login – Remember your login is your first initial your last name and the last three numbers of your.
Programming Concept Chapter I Introduction to Java Programming.
Week 1 - Friday.  What did we talk about last time?  Our first Java program.
Compiling and the Java Virtual Machine (JVM). The syntax of Pseudocode is pretty loose –visual validation encourages a permissive approach –emphasized.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Fall 2006Slides adapted from Java Concepts companion slides1 Introduction Advanced Programming ICOM 4015 Lecture 1 Reading: Java Concepts Chapter 1.
Comments in Java. When you create a New Project in NetBeans, you'll notice that some text is greyed out, with lots of slashes and asterisks:
Output in Java Hello World!. Structure of a Java Program  All Java files in ICS3U1 have the following structure: class HelloWorld { }  Notice the open.
JAVA Practical Creating our first program 2. Source code file 3. Class file 4. Understanding the different parts of our program 5. Escape characters.
ECE 122 Feb. 1, Introduction to Eclipse Java Statements Declaration Assignment Method calls.
Lecture 1. Introduction to Programming and Java MIT- AITI 2003.
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java.
1 WELCOME TO CIS 1068! Instructor: Alexander Yates.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
Chapter 1 Introduction. Chapter Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.
CSc 201 Introduction to Java George Wells Room 007, Hamilton Building
CS 106 Introduction to Computer Science I 01 / 22 / 2007 Instructor: Michael Eckmann.
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
CHAPTER 1 INTRODUCTION. CHAPTER GOALS To understand the activity of programming To learn about the architecture of computers To learn about machine code.
3/5/2002e-business and Information Systems1 Java Java Java Virtual Machine (JVM) Java Application Program Interface (API) HW Kernel API Application Programs.
CS 106 Introduction to Computer Science I 01 / 22 / 2008 Instructor: Michael Eckmann.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
CS 177 Recitation Week 1 – Intro to Java. Questions?
ITP 109 Week 2 Trina Gregory Introduction to Java.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
Introduction to 1. What is Java ? Sun Microsystems Java is a programming language and computing platform first released by Sun Microsystems in The.
Introduction to programming in java
Computer Programming Your First Java Program: HelloWorld.java.
The eclipse IDE IDE = “Integrated Development Environment”
John Woodward A Simple Program – Hello world
Chapter 3 GC 101 Java Fundamentals.
Introduction to.
Writing Methods.
Hands-on Introduction to JAVA
Tonga Institute of Higher Education
Java Intro.
IB Computer Science II Paul Bui
Computer Programming-1 CSC 111
Week 1 - Friday COMP 1600.
How to Run a Java Program
Presentation transcript:

Chapter 2: Your First Program!

Hello World: Let’s Program  All programs must have the extension.java  Our first program will be named: HelloWorld.java  Let’s get started…

Hello World: a closer look public class HelloWorld{ public static void main(String [ ] args){ //Prints Hello World System.out.println("Hello World"); } } All Java programs are inside a “class”. The name of the class must be EXACTLY the same as the name of the file! The class starts here And ends here. ALL code will be inside the class. The main function: the code in here is what happens when you “run” the program. A Comment: the “//” tells the computer to ignore this line. The only line that DOES anything! Prints “Hello World” and goes to the next line. This is a statement and must end with a semicolon.

That’s nice, but what did we just do?  HelloWorld.java is written in a high-level language :  Easy for you to read  Not so easy for the computer to read  We used a program called a compiler to break the program down into something more computer-friendly.  javac HelloWorld.java  Now we are able to run the program  java HelloWorld

Java: what’s the big deal?  In a normal programming language, the compiler translates your program into machine language (1’s and 0’s).  Unfortunately, different machines speak different languages.  With these languages, code will need to be recompiled to run on different machines.

Java: what’s the big deal?  The Java compiler changes your code into byte code.  byte code is like machine language for a hypothetical machine.  HelloWorld.class: byte code output from compiler.  The Java Virtual Machine (JVM) is our hypothetical machine.  acts as an interpreter between byte code and the computer  There is a JVM for each computer architecture  The JVM must be installed before your computer can run a java program.

Compiler Let’s take a look HelloWorld.class Byte Code Mr. PCMr. Mac HelloWorld.java Source Code JVM You

C++ vs. Java  C++: I just wrote this beautiful code, but…  You wanna run it on your machine, you better find your own compiler and compile it yourself.  Java:  Write once, Compile once, run anywhere  (Anywhere that has the JVM installed)

Why Does Java Hate Me?  Java is case sensitive!  public class HelloWorld NOT public Class helloworld  System.out.println(“Hello World”); NOT system.out.Println(“Hello World”); Capitalization errors and other typos (missing semicolons) will result in compile-time errors (aka syntax errors )

Hello World Gone Bad  Can you spot the 3 syntax errors? public class HelloWorld{ public static void main(String [ ] args){ //Prints Hello World System.out.Println(“Hello World) } } Why, that’s a large “P” you have there. Unfinished quote SEMICOLON!!!

A Note on Readability…  Look at this beautiful program: public class VariableStuff { public static void main(String[] args) { //Haiku one System.out.println("Programmer's Hiaku"); System.out.println("an expression of the soul"); System.out.println("code flows from the heart"); System.out.println(); //Haiku two System.out.println("Words liberated"); System.out.println("escaping through the keyboard"); System.out.println("fleeing to the screen"); } }

…and the lack thereof  This actually compiles and provides the same output.  VERY bad style and indentation public class VariableStuff{public static void main(String[] args) {//Haiku one System.out.println ("Programmer's Hiaku");System.out.println("an expression of the soul"); System. out. println("code flows from the heart"); System. out. println();//Haiku two System.out. println("Words liberated");System.out. println("escaping through the keyboard"); System.out.println("fleeing to the screen");}}

HelloWorld: the sequel  Now write a file called HelloWorld2.java.  This program should print out the following:  A general greeting to the world  An introduction (“my name is…”)  Some general advice (e.g. “trust no one”)  A farewell  Don’t forget to compile and execute the program.