Vous êtes sur la page 1sur 6

OUTLINE OF CONTENTS

JAVA: PUTTING THE PIECES • The Java Development Cycle


TOGETHER • Java Program Structure
CSE 114: Computer Science I
Stony Brook University • Processing vs. Java

•A First Java Program

• Compiling and Executing Your Code

THE DEVELOPMENT CYCLE


• Edit-Compile-Execute
cycle

• Compilation translates
THE JAVA DEVELOPMENT CYCLE source code into a form
that the computer can
understand

• With Java, this is slightly


more complicated than
with other languages...
THE TWO FACES OF JAVA
executes
• The name “Java” actually refers to two things: is compiled by program on

• The programming language


Source Code Desktop PC
• The Java Virtual Machine (JVM)

• In
most languages, a compiled program runs directly on the
hardware (it speaks the CPU’s language)

• Javaprograms execute on a simulated CPU (the JVM). The THE JAVA COMPILATION PROCESS
real CPU runs a program that simulates this CPU.

executes executes
is compiled by program on is compiled by program on

Source Code Desktop PC Source Code Desktop PC

100111 100111 executes which is


Java compiler 101010
101010 on simulated by
(javac) 110011
110011
000110 Java Virtual
000110
Machine (JVM) Desktop PC
Java Bytecode
Source Code Java Bytecode

JAVA COMPILATION (PART 1) JAVA COMPILATION (PART 2)


WHY IS IT SO COMPLICATED?

• Withmost languages, a program must be compiled


separately for every type of CPU

• Each new type of CPU requires a new compilation JAVA PROGRAM STRUCTURE
• Java programs are compiled to a fake CPU (the JVM)

• Afterone compilation, a Java program can run on any


CPU that can run the JVM

• “Write once, run anywhere” reduces programmer effort

CLASSES PROGRAMS AND CLASSES


class Class2
{
class MyClass Java Program variable declaration

•A class is a unit of code


variable declaration
{ class MyClass
variable declaration
{
variable declaration variable declaration
variable declaration
void method ()
{
variable declaration statement;

•A Java program is made


variable declaration
statement;
void method ()
variable declaration {
statement; }
statement;

statement;
statement;
}
void method ()
{

• Classes contain:
void method ()

up of one or more
void method () statement;
{ statement;
{ statement;
statement; statement;
statement; }
statement; }
void method ()
statement; void method ()

classes
{ {
statement; statement;
statement; statement;
statement;
statement;
statement;
} }
}
}
}

• Data (variables) void method ()


{
class MyClass
{
variable declaration
class Class3
{
variable declaration

• Programs use these


variable declaration variable declaration
statement; variable declaration variable declaration

statement; void method () void method ()


{ {
statement; statement; statement;

• Code (methods)
statement; statement;
}

classes to model different


statement; statement;
} }

void method () void method ()


void method () { {
statement; statement;
{ statement; statement;

parts of the problem


statement; statement;
statement; } }

statement; public static void main (String [] args) void method ()

• Classes are used to define


{ {
statement; statement;
statement;
statement;
statement;
} }
statement;
}
statement;

} } }

objects
THE MAIN() EVENT “HELLO, WORLD!” COMPARISON
• Atleast one class must
contain a special method Java Program
class Class2
{
variable declaration
variable declaration
• In Processing: • In Java:
named main()
variable declaration
class MyClass
{ void method ()
variable declaration
variable declaration {
variable declaration statement;

println(“Hello, World!”); public class Hello



statement;
void method ()
{ statement;
statement; }
statement;
statement;

{
}
void method ()
{
statement;

• Java
calls the main()
void method ()
{ statement;
statement;
statement; statement;
statement; }
}

void method ()
void method ()
{

method to start your


{
statement; statement;
statement;

public static void main (String [] args)



statement;
statement;
} statement;
} }
}

{
program class MyClass
{
variable declaration
class Class3
{
variable declaration
• In Python:


variable declaration variable declaration
variable declaration variable declaration

void method () void method ()

print(“Hello, World!”) System.out.println(“Hello, World!”);


{ {
statement; statement;
statement; statement;
statement; statement;

• main() always has the


} }

void method () void method ()


{ {
statement; statement;
statement; statement;

same, fixed header:
 }


statement; statement;
} }

public static void main (String [] args) void method ()


{ {
statement; statement;


statement; statement;
statement; statement;
} }
} }

}
public static void main (String [ ] args)

MY FIRST JAVA PROGRAM

public class MyProgram



A FIRST JAVA PROGRAM {

public static void main (String [ ] args)

{

System.out.println(“Hello, world!”);

}

}
LINE-BY-LINE BREAKDOWN LINE-BY-LINE BREAKDOWN

• Every Java program consists • A method is a block of Java


of one or more classes statements

 

• A class is a block of Java • main() is a special method
public class MyProgram
 code public class MyProgram

{
 {
 • It tells the JVM how to

 • Classes are indicated by 
 start the program
the reserved word class
public static void main (String [ ] args)
 public static void main (String [ ] args)
 • Every Java program (not
{
 • public is a modifier that tells {
 every class) must have a
Java that the class can be main() method
System.out.println(“Hello, world!”);
 seen/used all over
System.out.println(“Hello, world!”);

}
 }
 • main() always has the

 • Curly braces surround the 
 same header (first line)
contents of the class
} } • The body of a method is
surrounded by curly braces

NOTES ON PRINTING
LINE-BY-LINE BREAKDOWN
BEHAVIOR
• System.out.println() automatically adds a newline character
• System.out.println() is another
at the end, to move to the next line
Java method

• It tells the program to
• System.out.print()
public class MyProgram
 print something to the (note the lack of “ln” in the name) does
{
 console (screen) not do this — it stays on the same line

• The information to print
public static void main (String [ ] args)
 • To
goes inside the go to the next line at any point in a print statement,
{
 parentheses
System.out.println(“Hello, world!”);

insert the character sequence \n
• Here, we’re printing a
}
 string of characters

 •\ indicates that the next character has special meaning
• This is a method call — main()
} was a method definition
• \n = newline, \t = tab, \\ = a single \ character
COMPILING YOUR JAVA CODE
1. Write and save your source code using a text editor

COMPILING AND EXECUTING • The source code MUST be a plain text file

YOUR JAVA CODE • 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 file into a Java
bytecode file:

javac myFile.java

This will produce a file named myFile.class

EXECUTING YOUR JAVA


CODE
• To
execute your program, run the JVM with your
compiled .class file:

java myFile

• Note that the “.class” extension is omitted (The JVM fills


that in automatically)

• You
can only directly execute a class file that contains a
main() method

Vous aimerez peut-être aussi