Chapter 2 your first program
1 / 13

Chapter 2: Your First Program! - PowerPoint PPT Presentation

  • Uploaded on

Chapter 2: Your First Program!. Hello World: Let’s Program. All programs must have the extension .java Our first program will be named: Let’s get started…. The name of the class must be EXACTLY the same as the name of the file!. The class starts here.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Chapter 2: Your First Program!' - denise

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Chapter 2 your first program

Chapter 2: Your First Program!

Hello world let s program
Hello World:Let’s Program

  • All programs must have the extension .java

  • Our first program will be named:

  • Let’s get started…

Hello world a closer look

The name of the class must be EXACTLY the same as the name of the file!

The class starts here

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.

The main function:the code in here is what happens when you “run” the program.

And ends here.ALL code will be inside the class.

Hello World: a closer look

All Java programs are inside a “class”.

public class HelloWorld{ public static void main(String [ ] args){//Prints Hello World System.out.println("Hello World"); }}

That s nice but what did we just do
That’s nice, but what did we just do? of the file!

  • 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

  • Now we are able to run the program

    • java HelloWorld

Java what s the big deal
Java: of the file!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 deal1
Java: of the file!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.

Let s take a look of the file!


Byte Code

Source Code





Mr. PC

Mr. Mac

Let’s take a look

C vs java
C++ vs. Java of the file!

  • 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
Why Does Java Hate Me? of the file!

  • Java is case sensitive!

    • public class HelloWorld NOTpublic Class helloworld

    • System.out.println(“Hello World”);NOTsystem.out.Println(“Hello World”);

Capitalization errors and other typos (missing semicolons) will result in compile-time errors (aka syntax errors)

Hello world gone bad

Unfinished quote of the file!


Why, that’s a large “P” you have there.

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) }}

A note on readability
A Note on Readability… of the file!

publicclassVariableStuff{ publicstaticvoidmain(String[]args){//Haiku oneSystem.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 twoSystem.out.println("Words liberated"); System.out.println("escaping through the keyboard"); System.out.println("fleeing to the screen");


  • Look at this beautiful program:

And the lack thereof
…and the lack thereof of the file!

  • This actually compiles and provides the same output.

  • VERY bad style and indentation

publicclassVariableStuff{publicstaticvoidmain(String[]args){//Haiku oneSystem.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 twoSystem.out.println("Words liberated");System.out. println("escaping through the keyboard");System.out.println("fleeing to the screen");}}

Helloworld the sequel
HelloWorld: the sequel of the file!

  • Now write a file called

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