chapter 2: your first program! hello world: let’s program all programs must have the...

13
Chapter 2: Your First Program!

Upload: gerard-richards

Post on 17-Dec-2015

222 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: 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

Chapter 2: Your First Program!

Page 2: 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

Hello World:Let’s Program

All programs must have the extension .java

Our first program will be named: HelloWorld.java

Let’s get started…

Page 3: 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

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.

Page 4: 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

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

Page 5: 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

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.

Page 6: 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

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.

Page 7: 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

Compiler

Let’s take a look

HelloWorld.classByte Code

Mr. PC Mr. Mac

HelloWorld.javaSource Code

JVMJVM

You

Page 8: 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

C++ vs. JavaC++: 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)

Page 9: 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

Why Does Java Hate Me?Java is case sensitive!

public class HelloWorld NOT

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

Page 10: 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

Hello World Gone BadCan 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!!!

Page 11: 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

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");

}}

Page 12: 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

…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 twoSystem.out.println("Words liberated");System.out. println("escaping through the keyboard"); System.out.println("fleeing to the screen");}}

Page 13: 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

HelloWorld: the sequelNow 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.