Upload
nahom-girma
View
225
Download
0
Embed Size (px)
Citation preview
8/2/2019 02 Java Programming Basics
1/31
Java Programming BasicsCSE 110: Introduction to Computer Science
SUNY at Stony Brook
8/2/2019 02 Java Programming Basics
2/31
Outline of Topics
The Java Environment
Java Grammar
A First Program
Parts of a Java Program
Compiling and Executing Java Code
Experimenting With Code
8/2/2019 02 Java Programming Basics
3/31
The Java Development Cycle
Standard edit-compile-execute cycle
Compilation translates source code into a form that the
computer can understand
With Java, this is slightly more complicated than with
other languages...
8/2/2019 02 Java Programming Basics
4/31
Two Faces of Java
The name Java actually refers to two things:
The programming language
The Java Virtual Machine (JVM)
In most languages, a compiled program runs directly onthe hardware (it speaks the CPUs language)
Java programs execute on a simulated CPU (the JVM).
The real CPU runs a program that simulates this CPU.
8/2/2019 02 Java Programming Basics
5/31
Desktop PCSource Code
is compiled by
executesprogram on
The Java Compilation Process
8/2/2019 02 Java Programming Basics
6/31
Java Compilation (Part 1)
Desktop PCSource Code
is compiled by
executesprogram on
Source Code
100111
101010
110011
000110
Java Bytecode
Java compiler(javac)
8/2/2019 02 Java Programming Basics
7/31
Java Compilation (Part 2)
Desktop PCSource Code
is compiled by
executesprogram on
100111
101010
110011
000110
Java Bytecode
Java VirtualMachine (JVM) Desktop PC
executeson
which issimulated by
8/2/2019 02 Java Programming Basics
8/31
Why Is It So Complicated?
With most languages, a program must be compiledseparately for every type of CPU
A new type of CPU requires another compilation
Java programs are compiled to a fake CPU (the JVM)
After one compilation, a Java program can run on any
CPU that can run the JVM
Write once, run anywhere reduces programmer effort
8/2/2019 02 Java Programming Basics
9/31
Outline of Topics
The Java Environment
Java Grammar
A First Program
Parts of a Java Program
Compiling and Executing Java Code
Experimenting With Code
8/2/2019 02 Java Programming Basics
10/31
Formal Languages
A computer language is a set of notation for givinginstructions to a computer
Different languages may express the same concepts in
different ways, just like human languages
Like any language, Java has rules of grammar
We need to follow these rules in order to produce well-
formed Java programs
8/2/2019 02 Java Programming Basics
11/31
Parts of Speech
English has different parts of speech: nouns, verbs,
adjectives, etc.
Java has something similar: variables, statements,
modifiers, expressions, etc.
Just like with English, these parts of speech can only be
combined in certain ways
8/2/2019 02 Java Programming Basics
12/31
Statements and Expressions
Statements are commands
They tell Java what action to perform
Expressions are values
They dont do anything on their own
Expressions are manipulated by statements
8/2/2019 02 Java Programming Basics
13/31
Syntax and Semantics
Syntax refers to grammatical structure
Semantics refers to meaning
These are independent qualities; you can have one
without the other (just as in English)
Java programs need both to work effectively, but Java
only checks syntax for you
8/2/2019 02 Java Programming Basics
14/31
Outline of Topics
The Java Environment
Java Grammar
A First Program
Parts of a Java Program
Compiling and Executing Java Code
Experimenting With Code
8/2/2019 02 Java Programming Basics
15/31
My First Java Program
public class MyProgram
{ public static void main (String [ ] args) { System.out.println(Hello, world!); }}
8/2/2019 02 Java Programming Basics
16/31
Line-by-Line Breakdown
public class MyProgram
{
public static void main (String [ ] args) { System.out.println(Hello, world!); }}
Every Java program
consists of one or more
classes
Aclass is a block of
Java code
Classes are indicated
by the reserved word
class
public is a modifier that
tells Java that the class can
be seen/used all over
Curly braces surround the
contents of the class
8/2/2019 02 Java Programming Basics
17/31
Line-by-Line Breakdown
public class MyProgram
{
public static void main (String [ ] args) { System.out.println(Hello, world!); }}
Amethod is a block of
Java statements
main() is a special method
It tells the JVM how to
start the program
Every Java program
(not every class) must
have a main() method
main() always has the
same header (first line)
The body of a method is
surrounded by curly braces
8/2/2019 02 Java Programming Basics
18/31
Line-by-Line Breakdown
public class MyProgram
{
public static void main (String [ ] args) { System.out.println(Hello, world!); }}
System.out.println() is
another Java method
It tells the program to
print something to the
console (screen)
The information to print
goes inside the
parentheses
Here, were printing a
string of characters
This is a method call
main() was a method
definition
8/2/2019 02 Java Programming Basics
19/31
Outline of Topics
The Java Environment
Java Grammar
A First Program
Parts of a Java Program
Compiling and Executing Java Code
Experimenting With Code
8/2/2019 02 Java Programming Basics
20/31
Compiling Your Java Code1. Write and save your source code using a text editor
The source code MUST be a plain text file
The file should be named after the class it contains (e.g., a
class named Foo must live in a file named Foo.java)
2. Run the Java compiler (javac) to compile your source code fileinto a Java bytecode file:
javac myFile.java
This will produce a file named myFile.class
8/2/2019 02 Java Programming Basics
21/31
Executing Your Java Code
To execute your program, run the JVM with yourcompiled .class file:
java myFile
Note that the .class extension is OMITTED (The JVMfills that in automatically)
You can only call java on a class file that contains a
main() method
8/2/2019 02 Java Programming Basics
22/31
Outline of Topics
The Java Environment
Java Grammar
A First Program
Parts of a Java Program
Compiling and Executing Java Code
Experimenting With Code
8/2/2019 02 Java Programming Basics
23/31
Learning by Tinkering Most methods change their behavior based on the input
that you give them
System.out.println() will print whatever you tell it to
Character sequences MUST be enclosed in double
quotes
Numbers can be supplied as-is
Use the plus sign (+) to combine (concatenate)
expressions of different types
8/2/2019 02 Java Programming Basics
24/31
Notes on Printing Behavior System.out.println() automatically adds a newline
character at the end, to move to the next line
System.out.print() (note the lack of ln in the name) does
not do this it stays on the same line
To go to the next line at any point in a print statement,insert the character sequence \n
\ indicates that the next character has special meaning
\n = newline, \t = tab, \\ = a single \ character
8/2/2019 02 Java Programming Basics
25/31
Introduction to BlueJ
8/2/2019 02 Java Programming Basics
26/31
What is BlueJ?
BlueJ is a simple IDE (integrated developmentenvironment) designed for new programmers
An IDE combines a source code editor and the compiler
BlueJ is written in Java, and runs on all platforms
You can download BlueJ for free from
http://www.bluej.org
8/2/2019 02 Java Programming Basics
27/31
Projects
Aproject is the set of files that make up a program
Before you can do anything in BlueJ, you must create a
new project (or open an existing one)
BlueJ creates a new folder on disk with the same nameas the project
A BlueJ project contains all of your .java and .class files,
along with a few special BlueJ-specific files
8/2/2019 02 Java Programming Basics
28/31
The Main BlueJ Window The project window has
two main sections:
The top area shows all
of the classes in your
current project (arrowsindicate relationships)
The object workbench is
at the bottom
8/2/2019 02 Java Programming Basics
29/31
Editing Source Code To open a source
code file for editing,
double-click on the
box for that class
BlueJ includes a basictext editor
BlueJ has auto-indent
and syntax coloring
8/2/2019 02 Java Programming Basics
30/31
Executing Your Program
To execute your program, right-click on the class in themain window
Select the main() method from the menu that appears
Unlike most IDEs, BlueJ actually allows you to startexecution from any method in your program
This lets you test a specific method without having to run
your entire program
8/2/2019 02 Java Programming Basics
31/31
The Object Workbench
BlueJ allows you to work with your classes withoutactually running your entire program
Right-click on a class and select New... to create a
new instance of that class
Instances appear in the object workbench at the bottom
Right-click on an object to call its methods
Double-click on an object to inspect its data