Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only...

9
Hello Computer Science! Learning How To Talk With the Computer

Transcript of Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only...

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

Hello Computer Science!

Learning How To Talk With the Computer

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

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.

class HelloWorld {

public static void main(String [] arg) {

//This will print our message to the world

System.out.println("Hello World!");

}

}

Simple But Complicated

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

class - indicates that you are starting a class

HelloWorld - the name of the class

JAVA is what is known as an object oriented language. Because of that, ALL code is in some way attached to or as some would say encapsulated inside of a class. The main reason for this is organization although there are other benefits as well. There are other lower level programming languages that are procedural and ultimately all code behaves in a procedural manner on the CPU. You can think of this as filing papers in filing cabinets versus leaving them in a huge stack in the middle of the room.

JAVA – nothing but class

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

All code for both a class as well as a methods, loops, and if statements is demarcated with an open and close curly.

Programmer Tip – Be careful about the placement of a curly. Sometimes you will find that you will have multiple things nested inside of others and it becomes hard to tell when one thing starts and another ends. INDENTING helps with this!!!!!!

{Beginning and End of Our World}

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

Within each class are variables and methods. Inside methods we put our lines of code that actually run. The reason that we call these things methods is that they define how our classes do certain things.

Our method declaration is as follows:public static void main(String [] arg)

Method – It’s What We Do

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

public – indicates that this method is public which means that can be called from outside the class

static – indicates that this method is static which means we do not have to create an instance of the class before we can call the method

main – the name of the method(String [] arg) – indicates the input

variable(s), in this instance it is an array of Strings called arg. String variables can hold text and the brackets ([]) indicate that it is an array which can hold multiple values

Dissecting our method decleration

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

System.out.println – in this part of our code we are calling the println method of the out class which is contained inside the System class. Where this will print out to is dependent on the computer that we are running it on.

(“HelloWorld”) – The open and close parenthesis contain whatever we are passing into our method. We use the double quotes to start and end our String that we pass in to the method.

; - after every variable declaration and line of regular code, we have to include a semi colon

Our Output

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

Didn’t we miss something? What about that whole line of code that went:

//This will print our message to the worldWell that doesn’t do anything, for the computer anyway. That

is a comment. For the programmer it could do a lot. It lets you know what a class, method, code, or variable does without interfering with the working of code. Sometimes it even helps to write all your comments before you type a single line of code!

You can put comments into your code by putting a double slash. Or to put in comments with carriage returns you can use a slash star combo like so:

/*This will print

our message to

the world*/

A few last comments

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

Recital Time!

What did you learn?