Getting Started with Java Programming, Slides of Algorithms and Programming

A comprehensive guide on how to get started with java programming, including the required tools, mechanics of writing java programs, and running applications and applets. It also covers java basics, classes, objects, strings, arrays, and concurrent multi-threaded programming.

Typology: Slides

2012/2013

Uploaded on 04/27/2013

netii
netii šŸ‡®šŸ‡³

4.4

(7)

91 documents

1 / 40

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java Crash Course
A Tutorial Introduction for C++
Programmers
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download Getting Started with Java Programming and more Slides Algorithms and Programming in PDF only on Docsity!

Java Crash Course

A Tutorial Introduction for C++

Programmers

So what the heck is Java (besides coffee, that

is)?

  • A programming language.
    • Syntax and constructs are very similar to C++
  • A virtual platform
    • Java Virtual Machine is a software ā€œmachineā€ or ā€œhypothetical

chipā€

  • Since it’s ā€œvirtualā€, it can be implemented on any hardware
  • Cross-platform distribution achieved via .class binary file of

bytecodes (instead of machine-dependent machine code) 

Write Once, Run Anywhere

  • A class library
    • Standard APIs for GUI, data storage, processing, I/O, and

networking.

Mechanics of Writing Java Programs

ļ‚§ Create a Java source file. Must have the .java extension and contain

only one public class.

ļ‚§ Compile the source file into a bytecode file. The Java compiler ,

javac, takes your source file and translates its text into instructions that the Java Virtual Machine (Java VM) can understand. The compiler puts these instructions into a .class bytecode file.

ļ‚§ Run the program contained in the bytecode file. The Java VM is

implemented by a Java interpreter , java. This interpreter takes your bytecode file and carries out the instructions by translating them into instructions that your computer can understand.

Putting it all together

public class Hello { public static void main(String args[]) { System.out.println(ā€œHello, world!ā€); } }

  1. Put in: Hello.java
  2. Compile with: javac Hello.java

ļ‚§ Creates Hello.class

  1. Run with: java Hello

Java notes for C++ programmers

  • Everything’s an object
    • Every object inherits from java.lang.Object
  • No code outside of the class definition!
    • No global variables (use static variables instead)
  • Single inheritance only
    • Instead, implement interfaces
  • All classes are defined in .java files
    • One top level public class per file
      • The file has to have the same name as the public class!
  • Syntax is similar (control structures are very similar).
    • Primitive data types are similar
    • But a bool is not an int
    • To print to stdout, use System.out.println()

Requisite First Program (Application

Version)

Put in HelloWorld.java:

public class HelloWorld {

public static void main(String args[]) {

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

}

}

So what’s going on?

The Java bytecode and interpreter at work!

  • Bytecode is an intermediate representation of

the program (the class file)

  • Think of it as the machine-code for the Java Virtual Machine
  • The Java interpreter (java) starts up a new

ā€œVirtual Machineā€

  • The VM starts executing the user’s class by

running its main() method

Requisite First Program (Applet Version)

Put in HelloWorld.java: import java.awt.Graphics; public class HelloWorld extends java.applet.Applet { public void paint(Graphics g) { g.drawString("Hello Worldā€œ, 35, 15); } }

**Put in test.html:

Test the applet

Test the applet

**

Java Language Basics

  • Data types same as in C++ (except bool)
    • bool,char,byte,short,int,long,float,

double,string, etc.

  • Operators (same as C++)
    • Assignment: =, +=, -=, *=, …
    • Numeric: +, -, *, /, %, ++, --, …
    • Relational: ==. !=, <, >, <=, >=, …
    • Boolean: &&, ||,!
    • Bitwise: &, |, ^, ~, <<, >>, …
  • Control Structures  more of what you expect:

1. conditional: if, if else, switch

2. loop: while, for, do

3.break and continue

Classes, References, & Packages

  • Classes and Objects
    • ā€œAll Java statements appear within methods, and all methods are defined within classesā€.
    • Java classes are very similar to C++ classes (same concepts).
    • Instead of a ā€œstandard libraryā€, Java provides a lot of Class implementations or packages
  • What are packages?
    • You can organize a bunch of classes and interfaces into a package (or library of classes) - defines a namespace that contains all the classes.
    • Use the import keyword to include the packages you need
      • import java.applet.*;
    • You need to use some java packages in your programs
      • java.awt (Abstract Windowing Toolkit), java.io (for Files, etc.), java.util (for Vectors, etc.)
  • References
    • No pointers  everything’s a reference!
    • classes
    • arrays

Exceptions

  • When a program carries out an illegal action, an

exception is generated.

  • Terminology:
    • throw an exception : signal (in the method header)

that some condition or error has occurred but we

want to pass the buck and not deal with it.

  • catch an exception : deal with the error (or whatever)

ourselves inside the function/method.

  • Catch it using a try/catch block (next slide).
  • In Java, exception handling is necessary (forced

by the compiler  compilation errors!)

Try/Catch/Finally

try {

// code that can throw an exception

} catch (ExceptionType1 e1) {

// code to handle the exception

} catch (ExceptionType2 e2) {

// code to handle the exception

} catch (Exception e) {

// code to handle other exceptions

} finally {

// code to run after try or any catch

}

This block is always run

Defining a Class

  • One top level public class per .java file.
    • Typically end up with many .java files for a single

program with at least one containing a static

public main() method (if they’re applications).

  • Class name must match the file name!
    • The compiler/interpreter use class names to figure out

what the file name is.

  • Classes have these three features:
    • A constructor that’s used to allocate memory for the

object, initiailize its elements, and return a reference to

the object

  • Methods (function members) Docsity.com

A Sample Class

public class Point {

public Point(double x, double y) {

this.x = x; this.y=y;

}

public double distanceFromOrigin(){

return Math.sqrt(xx+yy);**

}

private double x,y;

}