Sie sind auf Seite 1von 31

Java Programming Basics

CSE 110: Introduction to Computer Science SUNY at Stony Brook

Outline of Topics
The Java Environment Java Grammar A First Program
Parts of a Java Program

Compiling and Executing Java Code Experimenting With Code

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

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 on the 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.

is compiled by

executes program on

Source Code

Desktop PC

The Java Compilation Process

is compiled by

executes program on

Source Code

Desktop PC

Java compiler (javac)

100111 101010 110011 000110

Source Code

Java Bytecode

Java Compilation (Part 1)

is compiled by

executes program on

Source Code

Desktop PC

100111 101010 110011 000110 Java Bytecode

executes on Java Virtual Machine (JVM)

which is simulated by Desktop PC

Java Compilation (Part 2)

Why Is It So Complicated?
With most languages, a program must be compiled separately 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

Outline of Topics
The Java Environment Java Grammar A First Program
Parts of a Java Program

Compiling and Executing Java Code Experimenting With Code

Formal Languages
A computer language is a set of notation for giving instructions 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 wellformed Java programs

Parts of Speech
English has different parts of speech: nouns, verbs, adjectives, etc. Java has something similar: variables, statements, modiers, expressions, etc. Just like with English, these parts of speech can only be combined in certain ways

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

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

Outline of Topics
The Java Environment Java Grammar A First Program
Parts of a Java Program

Compiling and Executing Java Code Experimenting With Code

My First Java Program


public class MyProgram { public static void main (String [ ] args) { System.out.println(Hello, world!); } }

Line-by-Line Breakdown
Every Java program

public class MyProgram { } public static void main (String [ ] args) { System.out.println(Hello, world!); }

consists of one or more classes

A class is a block of
Java code

Classes are indicated


by the reserved word class

public is a modier that

tells Java that the class can be seen/used all over contents of the class

Curly braces surround the

Line-by-Line Breakdown
A method is a block of

public class MyProgram { } public static void main (String [ ] args) { System.out.println(Hello, world!); }

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 same header (rst line)

main() always has the The body of a method is

surrounded by curly braces

Line-by-Line Breakdown
System.out.println() is

public class MyProgram { } public static void main (String [ ] args) { System.out.println(Hello, world!); }

another Java method

It tells the program to

print something to the console (screen) goes inside the parentheses

The information to print

Here, were printing a


string of characters

This is a method call


main() was a method

denition

Outline of Topics
The Java Environment Java Grammar A First Program
Parts of a Java Program

Compiling and Executing Java Code Experimenting With Code

Compiling Your Java Code


1. Write and save your source code using a text editor

The source code MUST be a plain text le The le should be named after the class it contains (e.g., a
class named Foo must live in a le named Foo.java) 2. Run the Java compiler (javac) to compile your source code le into a Java bytecode le: javac myFile.java This will produce a le named myFile.class

Executing Your Java Code


To execute your program, run the JVM with your compiled .class le:
java myFile

Note that the .class extension is OMITTED (The JVM lls that in automatically) You can only call java on a class le that contains a main() method

Outline of Topics
The Java Environment Java Grammar A First Program
Parts of a Java Program

Compiling and Executing Java Code Experimenting With Code

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

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

Introduction to BlueJ

What is BlueJ?
BlueJ is a simple IDE (integrated development environment) 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

Projects
A project is the set of les 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 name as the project
A BlueJ project contains all of your .java and .class les, along with a few special BlueJ-specic les

The Main BlueJ Window


The project window has two main sections:
The top area shows all of the classes in your current project (arrows indicate relationships) The object workbench is at the bottom

Editing Source Code


To open a source code le for editing, double-click on the box for that class BlueJ includes a basic text editor BlueJ has auto-indent and syntax coloring

Executing Your Program


To execute your program, right-click on the class in the main window Select the main() method from the menu that appears Unlike most IDEs, BlueJ actually allows you to start execution from any method in your program
This lets you test a specic method without having to run your entire program

The Object Workbench


BlueJ allows you to work with your classes without actually 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

Das könnte Ihnen auch gefallen