Java goes wild Lesson 1: Introduction to Java Thierry Wasylczenko
Jul 02, 2015
Java goes wildLesson 1: Introduction to Java
Thierry Wasylczenko
After this course ...You will:
• Know what is the JDK/JRE
• Be able to set up your development environment
• Know how to compile and run a Java program
2
Agenda• Bascis
• How does all of this work?
• Developer’s environment
3
BasicsSuccess is neither magical nor mysterious. Success is the natural
consequence of consistently applying the basic fundamentals.
Jim Rohn“4
Basics• Source code is inside files with .java extension
• The source code is compiled into .class files
• The class files are executed by a strange thing (covered later)
5
A Java file
public class Foo {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
6
How do I get class files?• By compiling all Java files
$ cd /location/of/java/files
$ javac Foo.java
$ ls
Foo.class Foo.java
$
7
How do I run it?• With the java command
$ java Foo
Hello World!
$
• Yes you DON’T put the .class extension when you run a class !
8
Java Archive• aka JAR
• A collection of compiled Java classes
• Package of a Java program
• Can be runnable
java -jar Foo.jar
9
That'sfreaking
AWESOME !
That’s awesome dude. But how
does all of this work?
You“11
How does all of this work?It is not the beauty of a building you should look at; it's the construction
of the foundation that will stand the test of time.
David Allan Coe“12
The source code• Plain text file
• File extension: .java
• Rules for naming each file (covered later)
13
The bytecode• Result of compilation
• Something in between the source code and the machine’s language
• Interpreted by the Java Virtual Machine
14
The Java Virtual Machine• aka the JVM
• Kind of a black box
• Interprets the Java bytecode
• Is available on major platforms
• "Compile once, run everywhere"
15
A little schemaSourcecode
Sourcecode
JVMByte code
JVMByte code
turns on
turns on
java
java
javac
javac
16
Well man, you’re talking about
JVM, javac, bytecode and cool
stuff. But how do I set them up?
You“
17
Developer's environmentWhen people think about computer science, they imagine people with
pocket protectors and thick glasses who code all night.
Marissa Mayer“18
Java Development Kit• aka JDK
• Provides tools
• javac: java compiler
• javadoc: generates the documentation
• ...
• Includes a Java Runtime Environment
19
Java Runtime Environment• aka JRE
• Provides tools:
• java: for running Java applications
• ...
20
JAVA_HOME• An environment variable
• Points to the JDK installation
• Required by some tools such as:
• IDEs
• build tools (maven, gradle, ...)
• ...
• Used to update the PATH variable
21
JAVA_HOME: example
# Unix
export JAVA_HOME="/path/to/jdk"
export PATH=$JAVA_HOME/bin:$PATH
REM Windows
set JAVA_HOME="C:\path\to\jdk"
set PATH=%JAVA_HOME%\bin;%PATH%
22
Avoid spaces in the path of your
JAVA_HOME.
JAVA_HOME
23
JAVA_HOME: test the setupTo test the setup you can execute the following command:
$ java -version
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)
24
What does JRE stand for?
• Java Runtime Embeded
• Java Routine Exclusive
• Java Runtime Environment
Start Stop
25
What is the correct command to
execute a Java program?
• java Foo.java
• java Foo
• java Foo.class
Start Stop
26
1. Set the JAVA_HOME in your
environment
2. Verify your setup
Practice #1
27
Practice #2Given the following code:
public class Foo {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
28
1. Copy it in your favorite editor
2. Save the file as Foo.java
3. Compile it
4. Run it
Practice #2
29
30
Appendix
31
Appendix• Java Code conventions
• Not maintained since 1997, may not be accurate
32