Top Banner
Java goes wild Lesson 1: Introduction to Java Thierry Wasylczenko
32

Java goes wild, lesson 1

Jul 02, 2015

Download

Engineering

An introcution to,Java. What are JDK, JRE, JVM? Where to start? This presentation is a brief introduction to Java development and how to set a developer's environment properly.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java goes wild, lesson 1

Java goes wildLesson 1: Introduction to Java

Thierry Wasylczenko

Page 2: Java goes wild, lesson 1

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

Page 3: Java goes wild, lesson 1

Agenda• Bascis

• How does all of this work?

• Developer’s environment

3

Page 4: Java goes wild, lesson 1

BasicsSuccess is neither magical nor mysterious. Success is the natural

consequence of consistently applying the basic fundamentals.

Jim Rohn“4

Page 5: Java goes wild, lesson 1

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

Page 6: Java goes wild, lesson 1

A Java file

public class Foo {

public static void main(String[] args) {

System.out.println("Hello World!");

}

}

6

Page 7: Java goes wild, lesson 1

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

Page 8: Java goes wild, lesson 1

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

Page 9: Java goes wild, lesson 1

Java Archive• aka JAR

• A collection of compiled Java classes

• Package of a Java program

• Can be runnable

java -jar Foo.jar

9

Page 10: Java goes wild, lesson 1

That'sfreaking

AWESOME !

Page 11: Java goes wild, lesson 1

That’s awesome dude. But how

does all of this work?

You“11

Page 12: Java goes wild, lesson 1

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

Page 13: Java goes wild, lesson 1

The source code• Plain text file

• File extension: .java

• Rules for naming each file (covered later)

13

Page 14: Java goes wild, lesson 1

The bytecode• Result of compilation

• Something in between the source code and the machine’s language

• Interpreted by the Java Virtual Machine

14

Page 15: Java goes wild, lesson 1

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

Page 16: Java goes wild, lesson 1

A little schemaSourcecode

Sourcecode

JVMByte code

JVMByte code

turns on

turns on

java

java

javac

javac

16

Page 17: Java goes wild, lesson 1

Well man, you’re talking about

JVM, javac, bytecode and cool

stuff. But how do I set them up?

You“

17

Page 18: Java goes wild, lesson 1

Developer's environmentWhen people think about computer science, they imagine people with

pocket protectors and thick glasses who code all night.

Marissa Mayer“18

Page 19: Java goes wild, lesson 1

Java Development Kit• aka JDK

• Provides tools

• javac: java compiler

• javadoc: generates the documentation

• ...

• Includes a Java Runtime Environment

19

Page 20: Java goes wild, lesson 1

Java Runtime Environment• aka JRE

• Provides tools:

• java: for running Java applications

• ...

20

Page 21: Java goes wild, lesson 1

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

Page 22: Java goes wild, lesson 1

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

Page 23: Java goes wild, lesson 1

Avoid spaces in the path of your

JAVA_HOME.

JAVA_HOME

23

Page 24: Java goes wild, lesson 1

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

Page 25: Java goes wild, lesson 1

What does JRE stand for?

• Java Runtime Embeded

• Java Routine Exclusive

• Java Runtime Environment

Start Stop

25

Page 26: Java goes wild, lesson 1

What is the correct command to

execute a Java program?

• java Foo.java

• java Foo

• java Foo.class

Start Stop

26

Page 27: Java goes wild, lesson 1

1. Set the JAVA_HOME in your

environment

2. Verify your setup

Practice #1

27

Page 28: Java goes wild, lesson 1

Practice #2Given the following code:

public class Foo {

public static void main(String[] args) {

System.out.println("Hello World!");

}

}

28

Page 29: Java goes wild, lesson 1

1. Copy it in your favorite editor

2. Save the file as Foo.java

3. Compile it

4. Run it

Practice #2

29

Page 30: Java goes wild, lesson 1

30

Page 31: Java goes wild, lesson 1

Appendix

31

Page 32: Java goes wild, lesson 1

Appendix• Java Code conventions

• Not maintained since 1997, may not be accurate

32