Download what is java? and it explanation. and more Lecture notes Java Programming in PDF only on Docsity!
CS305j Introduction to Introduction to Java Programming (^) 1
Topic 2
Introduction to Java Programming
“When a programming
language is created that allows
programmers to program in
simple English, it will be
discovered that programmers
cannot speak English.”
- Anonymous Based on slides for Building Java Programs by Reges/Stepp, found at http://faculty.washington.edu/stepp/book/
CS305j Introduction to Introduction to Java Programming (^) 2
What We Will Do Today
What are computer languages?
Java editors
- (^) text editor and command line
- (^) BlueJ
First programming concepts
- (^) output with println statements
- (^) syntax and errors
structured algorithms with static methods
identifiers, keywords, and comments
CS305j Introduction to Introduction to Java Programming (^) 4
Definitions
(^) program : A set of instructions that are to be carried out by a computer. (^) program execution : The act of carrying out the instructions contained in a program.
- (^) this is done by feeding the instructions to the CPU (^) programming language : A systematic set of rules used to describe computations, generally in a format that is editable by humans.
- (^) in this class will are using Java
CS305j Introduction to Introduction to Java Programming (^) 5
High Level Languages
(^) Computers are fast
- (^) Pentium 4 chip from 2001 can perform approximately 1,700,000,000 computations per second
- (^) made up of 42,000,000 transistors (a switch that is on or off) (^) Computers are dumb
- (^) They can only carry out a very limited set of instructions
- (^) on the order of 100 or so depending on the computer's processor
- (^) machine language instructions, aka instruction set architecture (ISA)
- (^) Add, Branch, Jump, Get Data, Get Instruction, Store
CS305j Introduction to Introduction to Java Programming (^) 7
Say What?
(^) Programming with Strings of bits (1s or 0s) is not the easiest thing in the world. (^) Assembly language
- (^) mnemonics for machine language instructions .ORIG x LD R1, x AND R3, R3 # LD R4, R BRn x ADD R3, R3, R ADD R1, R1, # LD R4, R BRnzp x
CS305j Introduction to Introduction to Java Programming (^) 8
High Level Languages
Assembly language, still not so easy, and lots
of commands to accomplish things
High Level Computer Languages provide the
ability to accomplish a lot with fewer commands
than machine or assembly language in a way
that is hopefully easier to understand
int sum; int count = 0; int done = -1; while( list[count]!= -1 ) sum += list[count];
CS305j Introduction to Introduction to Java Programming (^) 10
A Picture is Worth…
The Interpreter's are sometimes referred to as the Java Virtual Machines The output of the compiler is .class file
CS305j Introduction to Introduction to Java Programming (^) 11
A Simple Java Program
public class Hello { public static void main(String[] args) { System.out.println("Hello World!"); } } This would be in a text file named Hello.java DEMO of writing and running a program via notepad and the command line
CS305j Introduction to Introduction to Java Programming (^) 13
Compiling and Running
(^) Compiler : a program that converts a program in one language to another language
- (^) compile from C++ to machine code
- (^) compile Java to bytecode (^) Bytecode : a language for an imaginary cpu (^) Interpreter : A converts one instruction or line of code from one language to another and then executes that instruction
- (^) When java programs are run the bytecode produced by the compiler is fed to an interpreter that converts it to machine code for a particular CPU
- (^) on my machine it converts it to instructions for a Pentium cpu
CS305j Introduction to Introduction to Java Programming (^) 14
The command line
To run a Java program using your Command Prompt:
change to the directory
of your program
cd
compile the program
javac Hello.java
execute the program
java Hello
source code (Hello.java) compile byte code (Hello.class) execute output
CS305j Introduction to Introduction to Java Programming (^) 16
Structure of Java programs
public class { public static void main(String[] args) { <statement(s)> ; } }
Every executable Java program consists of a
class ...
- (^) that contains a method named main...
- (^) that contains the statements to be executed
The previous program is a class named
Hello, whose main method executes one
statement named System.out.println
CS305j Introduction to Introduction to Java Programming (^) 17
Java terminology
class :
(a) A module that can contain executable code. (b) A description of a type of objects. (seen later)
statement : An executable piece of code that
represents a complete command to the
computer.
- (^) every basic Java statement ends with a semicolon ;
method : A named sequence of statements
that can be executed together to perform a
particular action or computation.
CS305j Introduction to Introduction to Java Programming (^) 19
Compiler Output
The program on the previous slide produces
the following output when we attempt to
compile it
H:\summer\Hello.java:2: expected pooblic static void main(String[] args) { ^ H:\summer\Hello.java:5: ';' expected } ^ 2 errors Tool completed with exit code 1 compiler output:
CS305j Introduction to Introduction to Java Programming (^) 20
Fixing syntax errors
(^) Notice how the error messages are sort of cryptic and do not always help us understand what is wrong: H:\summer\Hello.java:2: expected pooblic static void main(String[] args) { ^
- (^) We'd have preferred a friendly message such as, "You misspelled 'public' " (^) The compiler does tell us the line number on which it found the error, which helps us find the place to fix the code.
- (^) The line number shown is a good hint, but is not always the true source of the problem. (^) Java has a fairly rigid syntax.