Académique Documents
Professionnel Documents
Culture Documents
• Compilation translates
THE JAVA DEVELOPMENT CYCLE source code into a form
that the computer can
understand
• 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
• Each new type of CPU requires a new compilation JAVA PROGRAM STRUCTURE
• Java programs are compiled to a fake CPU (the JVM)
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;
} }
}
}
}
• Code (methods)
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;
{
}
void method ()
{
statement;
• Java
calls the main()
void method ()
{ statement;
statement;
statement; statement;
statement; }
}
void method ()
void method ()
{
{
program class MyClass
{
variable declaration
class Class3
{
variable declaration
• In Python:
variable declaration variable declaration
variable declaration variable declaration
statement; statement;
statement; statement;
} }
} }
}
public static void main (String [ ] args)
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
java myFile
• You
can only directly execute a class file that contains a
main() method