Tutorial: Intro to Java Getting started. 1 CS 349 - Java tutorial - - PowerPoint PPT Presentation

tutorial intro to java
SMART_READER_LITE
LIVE PREVIEW

Tutorial: Intro to Java Getting started. 1 CS 349 - Java tutorial - - PowerPoint PPT Presentation

Tutorial: Intro to Java Getting started. 1 CS 349 - Java tutorial Background Designed by James Gosling Released by Sun Microsystems in 1995. Originally a proprietary license, but made open source under GNU GPL in 2007. Sun and


slide-1
SLIDE 1

Tutorial: Intro to Java

Getting started.

1 CS 349 - Java tutorial

slide-2
SLIDE 2

Background

  • Designed by James Gosling
  • Released by Sun Microsystems in 1995.

– Originally a proprietary license, but made open source under GNU GPL in 2007. – Sun and Java acquired by Oracle in 2010.

  • General-purpose, portable language.

– Cross-platform, and able to scale from small devices to large applications. – Dynamic, interpreted*

  • Broadly adopted, and extremely successful.

– Java is currently (2017) TIOBE's most popular Programming Language! (https://www.tiobe.com/tiobe-index/)

2 CS 349 - Java tutorial

slide-3
SLIDE 3

3 CS 349 - Java tutorial

slide-4
SLIDE 4

Object Oriented

  • C++ syntax

– Meant to be familiar to legions of C++ developers that would migrate to Java.

  • Class-based, object-oriented design.

– Explicitly object-oriented – Cannot build procedural applications!

  • Extensive class libraries included

– Cross-platform! – Support for everything from threading to database access to building user interfaces. – This makes Java unique; many languages rely

  • n third-party libraries.

4 CS 349 - Java tutorial

slide-5
SLIDE 5

Java Virtual Machine (JVM)

  • Portability is achieved through virtualization

– Java compiles to bytecode (IR). – Bytecode is executed by a Java virtual machine (JVM) on the target platform. – Interpreted bytecode is slower than native code BUT just-in-time compilation can give near-native performance.

http://viralpatel.net/blogs/java-virtual-machine-an-inside-story/

5 CS 349 - Java tutorial

slide-6
SLIDE 6

Garbage Collection (GC)

  • In Java, there’s no need to free memory

– Garbage collection runs periodically and frees up memory that’s not in use. – JVM attempts to do this without impacting performance.

http://www.ibm.com/developerworks/library/j-jtp10283/

6 CS 349 - Java tutorial

slide-7
SLIDE 7

Java Platform (JDK)

  • Includes tools, and libraries - everything from

threading, to database access, to UI toolkits – all cross platform and portable.

7 CS 349 - Java tutorial

slide-8
SLIDE 8

Installing the Platform

  • There are two main Java implementations

– Oracle Java: https://docs.oracle.com/javase/8/ – Open JDK: FOSS implementation for Linux.

  • JRE: standalone JVM installation (runtime).
  • JDK: JRE plus development tools and libraries.

– This gives you command-line tools (javac compiler and java runtime).

  • Third-party support is excellent

– Editor support in VIM, Sublime, etc. – IDEs like IntelliJ, Eclipse.

8 CS 349 - Java tutorial

slide-9
SLIDE 9

Building Applications

  • Source code is written in text files ending with

the .java extension (one class per source file).

  • Source files are compiled into .class files

(bytecode) by the javac compiler.

  • Class files (.class) can be executed on any

platform with an appropriate JVM.

  • Often applications include many class files,

which we bundle into a JAR (.jar) file (basically a zip file with metadata).

9 CS 349 - Java tutorial

slide-10
SLIDE 10

Structure of a Program

10

Bicycle.java

CS 349 - Java tutorial

public class Bicycle { private String owner = null; private int speed = 0; private int gear = 1; // constructor public Bicycle() { } public Bicycle(String name) { this.owner = name; } // methods public void changeSpeed(int newSp) { this.speed = newSp; } public void changeGear(int newGear) { this.gear = newGear; } public int getSpeed() { return this.speed; } public int getGear() { return this.gear; } // static entry point – main method public static void main(String[] args) { Bicycle adultBike = new Bicycle("Jeff"); adultBike.changeSpeed(20); System.out.println("speed=" + adultBike.getSpeed()); Bicycle kidsBike = new Bicycle("Austin"); kidsBike.changeSpeed(15); System.out.println("speed=" + kidsBike.getSpeed()); } }

slide-11
SLIDE 11

Command-Line Tools To compile on the command line:

$ javac CountArgsApp.java

To run compiled app:

$ java CountArgsApp one two three You provided 3 args.

11

This works when you have a single source file, but starts to fall apart when you have multiple files and dependencies (files that depend on one another).

CS 349 - Java tutorial

slide-12
SLIDE 12

Apache Ant

  • An open-source build tool for Java applications
  • Installation and manual:

http://ant.apache.org/manual/index.html

  • Build projects are specified in build.xml
  • Think of it as a “makefile for Java”

CS 349 - Java tutorial 12

slide-13
SLIDE 13

Example Ant build.xml

<project name="CS349-A0" default="run" basedir="."> <target name="compile" description="compile the source"> <javac srcdir="." destdir="."/> </target> <target name="run" depends="compile" description="run program"> <java classname="Check" classpath="."/> </target> <target name="clean" description="clean up"> <delete file="Check.class"/> <delete file="results.txt"/> </target> </project>

Using Ant: ant # run default target (‘run’) ant compile # run ‘compile’ target ant clean # run ‘clean’ target

13

target task

CS 349 - Java tutorial

task

slide-14
SLIDE 14

Recommended Resources

  • Required

– Java SE 8 JDK : http://www.oracle.com/technetwork/java/javase/

  • Reference

– Java 8 SE Platform SDK Documentation:

https://docs.oracle.com/javase/8/docs/api/overview-summary.html

– Java 8 Tutorials: http://docs.oracle.com/javase/tutorial/java/index.html – Apache Ant: http://ant.apache.org/manual/index.html

  • IDE

– IntelliJ (Jetbrains Student Licenses): https://www.jetbrains.com/student/

14 CS 349 - Java tutorial