intro to java for c++ developers

21
Zachary Blair Zachary Blair April 10, 2012 April 10, 2012

Upload: zachary-d-blair

Post on 22-Jun-2015

2.640 views

Category:

Technology


6 download

DESCRIPTION

An Intro to Java for C++ Developers

TRANSCRIPT

Page 1: Intro to Java for C++ Developers

Zachary BlairZachary BlairApril 10, 2012April 10, 2012

Page 2: Intro to Java for C++ Developers

Intro to Java and the JVM

Basic Types/Arrays Classes/Inheritance Nested Classes Exceptions Enums Autoboxing/unboxing Annotations Generics

Page 3: Intro to Java for C++ Developers

Originally developed by Sun for embedded devices, version 1.0 released in 1996

James Gosling (Father of Java, Officer of the Order of Canada), photo by Peter Campbell

Page 4: Intro to Java for C++ Developers

Intended as an alternative to C++ that is:◦ Simpler◦ Higher-level◦ Multithreaded◦ Dynamic◦ Object-oriented◦ Trivially portable (“Write Once, Run Everywhere”)

◦ No pointer arithmetic◦ Automatic garbage collection

Page 5: Intro to Java for C++ Developers

Emulates a “virtual” CPU

Executes “bytecode” (each opcode is one byte long) stored in ‘.class’ files

The same “bytecode” can run on any machine with a JVM implemented for it

Java Source Files (.java)

Java bytecode

files (.class)

x86 Java Virtual

Machine (JVM)

ARM Java Virtual

Machine (JVM)

javac

Page 6: Intro to Java for C++ Developers

Not specific to the Java language. Other languages compile for the JVM:

◦ Groovy◦ Clojure◦ Jython◦ JRuby◦ Dozens of others…

Page 7: Intro to Java for C++ Developers

Similar to C:

Numeric types are always signed ‘char’ is a 16 bits instead of 8! No conversion between int and boolean as in C++. if(0) or while(1) are compile-time errors in Java.

byte -> 8 bitsshort -> 16 bitsint -> 32 bitslong -> 64 bitsfloat -> 32 bitsdouble -> 64 bitschar -> 16 bitsboolean

Page 8: Intro to Java for C++ Developers

Eight bytes walk into a bar.  The bartender asks, “Can I get you anything?”

“Yeah,” reply the bytes.  “Make us a double.”

** I didn’t come up with this joke, but it’s comedy Gold.

Page 9: Intro to Java for C++ Developers

Similar to C in syntax

Arrays are Objects with members and methods (member functions)!

Trying to index past the end of an array results in ArrayIndexOutOfBoundsException

int[] numbers = new int[10];numbers[0] = 1000;

for (int i = 0; i < numbers.length; i++) {System.out.println(numbers[i]);

}

Page 10: Intro to Java for C++ Developers

Method implementations must be defined in the class body. No ‘.h’ files, just ‘.java’!

Classes themselves can have access modifiers! Each ‘public’ class must reside in a ‘.java’ file of

the same name (e.g. Vector2D.java).

public class Vector2D{

public int x;public int y;public float magnitude(){

return Math.sqrt(x * x + y * y);}

}

Page 11: Intro to Java for C++ Developers

Uses ‘extends’ to specify a base class Only single inheritance is supported All inheritance equivalent to ‘public’ in C+

+ All classes implicitly inherit from Object

public class Velocity extends Point2D{

public boolean isTooFast(){

return (magnitude() > 60);}

}

Page 12: Intro to Java for C++ Developers

Non-static inner classes have access to the outer class’s members!

Instantiated using ‘this.new’ instead of ‘new’.

public class OuterClass{

public int var;class InnerClass{

public void foo(){

System.out.println(var);}

}}

Page 13: Intro to Java for C++ Developers

You can even declare a class inside a method!

public void foo(){

class Point{

public int x;public int y;

}

Point p = new Point();}

Page 14: Intro to Java for C++ Developers

Defined and instantiated an anonymous class that implements an “ActionListener” interface, all in the parameter list to a method call!

Used as Java’s alternative to function pointers in C for callbacks.

button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println(“The button was pressed”); }}

Page 15: Intro to Java for C++ Developers

Handle error conditions, similar to C++’s try/catch. Some exceptions are ‘checked’, meaning it is a

compile-time error to not either catch them, or explicitly mark your method as possibly throwing that exception to its caller.

try {int a[] = new int[2];System.out.println(“A three:" + a[3]);

}catch (ArrayIndexOutOfBoundsException e) {

System.out.println("Exception:" + e);}

Page 16: Intro to Java for C++ Developers

Like C enums, but in Java, enum is a sort of class with enum values as static members.

You can add data and methods to the enum!

public enum Color{

RED, ORANGE, YELLOW, GREEN, BLUE}…Color c = Color.RED;

Color c = Color.RED;if (c.isWarmColor()) System.out.println(c.toString());

Page 17: Intro to Java for C++ Developers

Automatically convert between primitive types (e.g. int, double, char) to their Object-based (boxed) types (e.g. Integer, Double, Character).

Useful because boxed types can be stored in collection classes just like any other Object

int x = 10;Integer y = new Integer(x);Integer z = x;

int a = z;

list list = new List();list.append(x); // x converted to Integer

Page 18: Intro to Java for C++ Developers

@Override marks a method as explicitly overriding a base class method, triggering a compilation error if it doesn’t!

public class Base{

public void foo() { }}…public class Subtype extends Base{

@Override public void foo() { bar(); }}

Page 19: Intro to Java for C++ Developers

A bit like templates in C++ (except that internally only one implementation is created).

class<T> Pair{

public T first;public T second;

}

Pair<int> p = new Pair<int>();p.first = 10;p.second = 20;

Page 20: Intro to Java for C++ Developers

Java has some similar syntax to C++ Rather than compiling to native code, it

compiles to bytecode for the JVM to execute Java makes it more difficult to make certain

mistakes (automatic garbage collection and no pointer arithmetic).

Learn more at http://docs.oracle.com/javase/

Page 21: Intro to Java for C++ Developers