Download ppt

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Reactive programming wikipedia , lookup

Program optimization wikipedia , lookup

Abstraction (computer science) wikipedia , lookup

Class (computer programming) wikipedia , lookup

Scala (programming language) wikipedia , lookup

Comment (computer programming) wikipedia , lookup

Programming language wikipedia , lookup

Library (computing) wikipedia , lookup

Go (programming language) wikipedia , lookup

Name mangling wikipedia , lookup

Compiler wikipedia , lookup

History of compiler construction wikipedia , lookup

C++ wikipedia , lookup

C Sharp syntax wikipedia , lookup

Object-oriented programming wikipedia , lookup

Structured programming wikipedia , lookup

Interpreter (computing) wikipedia , lookup

Java (programming language) wikipedia , lookup

Java performance wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
CS 177 Recitation
Week 1 – Intro to Java
Questions?
Computers

Computers can do really complex stuff. How? By
manipulating data according to lists of instructions.


Fundamentally, this is all that a computer can actually do.
Different kinds of computers: supercomputers, desktops,
laptops, embedded.

Different kinds of computer have different sizes, computing
powers, uses, etc.
Computers

What is hardware? What are some examples?



The physical components of the computer (stuff you can
touch).
monitor, CPU, keyboard, memory, hard drive, etc.
What is software? What are some examples?


Programs that run on the hardware and make it do stuff.
Windows, Firefox, MS Powerpoint, World of Warcraft, etc
Computers

What are some important (hardware) parts of a
computer?



CPU – does calculations (the brain)
Memory – stores data (cache, RAM, hard drive, etc)
I/O devices – allows the user and the computer to
communicate (monitor, mouse, keyboard, etc)
Programming


Computers only manipulate data according to a list of
instructions.
Computers are stupid, but really fast.


Computers can only do very simple things (like adding two
numbers), but they can do millions or billions of simple things a
second.
Programming – the process of creating lists of
instructions for the computer to follow.
Programming

The process of software development:
1)
2)
3)
4)
Understand the problem that you need to solve.
Plan a solution to the problem.
Implement the solution (write the program).
Test the program (compile and run it), and fix any errors.
Programming Languages

We have to use programming languages to give
computers instructions because human languages (like
English) are too vague.

We are going to write programs in a programming
language called Java.

There are lots of other programming languages too.
Programming Languages

We classify programming languages as high or low level.

Low level languages are closer to (or are) what the
computer actually understands, and have very simple
commands.


ex) machine code, assembly languages
High level languages are easier for humans to
understand and have more complex commands.


Programs written in high level languages have to be translated
to a low level language before they can be run.
ex) Java, Python
Compiling

What special program translates a program written in a
high-level language into a low-level language?


a compiler
How things usually work:

The compiler translates the program directly into machine
code and the program can be run.
Compiling

Java is weird:




The Java compiler translates the program into bytecode, a
sort of intermediate language.
When the Java program is run, the Java Virtual Machine
compiles/translates the bytecode into machine code, and then
the program runs.
The machine code produced is different depending on the
computer. The bytecode will be the same no matter what
computer the program is compiled on.
The “Whatever.class” file you get after compiling your
“Whatever.java” file contains the bytecode.
Java


Java programs are just text files, and can be written in any
text editor or DrJava.
Java is a language, and therefore has rules that you have to
learn and obey.

If you do not follow Java’s rules, your program will not compile
or won’t run correctly.
Anatomy of a Java Program
public class NameOfClass {
public static void main(String[] args) {
}
}



Every Java program must be inside a class. The keyword
class tells Java that you are making a new class.
Replace NameOfClass with what you want to call your
program. The program will need to be saved in a file
called NameOfClass.java.
For now, the rest of the stuff is magic words. Just
remember to type them in, or your program won’t work.
Simple Program
public class HelloWorld {
public static void main(String[] args) {
System.out.print(“Hello, world! ”);
System.out.println(100.34);
System.out.println(“Programming is fun!”);
}
}



This program just prints some stuff on the screen.
You can use System.out.println() to print anything in
quotes (“fish”, “foobar”) or numbers (5, 20.6).
System.out.print() does the same thing, but does not
put a newline after the printed stuff.
Running a Program

Let’s run our HelloWorld program and see it in action!
There are two different ways to do it.

In the terminal or command prompt:


Change to the directory where your Java file is saved.
To compile, type “javac HelloWorld.java”



If you don’t see any messages, your program compiled successfully.
Compiling creates a “class” file with the same name as your Java file
(in this case, “HelloWorld.class”).
To run, type “java HelloWorld”.
Running a Program

In DrJava:

To compile, push the “Compile” button (upper right corner of
the window).



If it says “Compilation completed”, everything worked.
To run, push the “Run” button (near the Compile button).
NOTE: You don’t have to use DrJava if you don’t want to,
especially at home. It will probably make your life easier,
though.
Some Java Details

All statements in Java must end with a semicolon (;).



Think of it like the period at the end of an English sentence.
All classes and methods must have curly braces ({ and })
around their contents.
Java is case sensitive (“class” is not the same as “Class” or
“CLASS” or “cLAss”).
Comments




Programs can be confusing to read.
Use comments to leave notes for yourself or anyone
else who reads your code.
Comments do not affect how the program runs (they’re
ignored by the computer).
Two ways to make comments:

// I’m a comment


Use for single-line comments
/* I’m a comment. */

Use for comments that are multiple lines long
Coding with Style!

Java ignores whitespace, so use whitespace to make your
code more readable.

Indent all the lines of code between a class’ or method’s
curly brackets.


This makes your code MUCH easier to read. Really. It does.
We WILL take points off on assignments if you do not indent
your code nicely!
Indentation Examples

Good
public class NameOfClass {
public static void main(String[] args) {
System.out.println(“Hello, world!”);
}
}

Bad
public class NameOfClass {
public static void main(String[] args) {
System.out.println(“Hello, world!”);
}
}

Horrible!
public class NameOfClass {
public static void main(String[] args) {
System.out.println(“Hello, world!”);
}
}
Questions?