Skip to content
Anthony Christe edited this page Aug 26, 2013 · 1 revision

Introduction

  • Anthony Christe
  • https://github.com/anthonyjchriste/ics211f13
  • Finishing MS and soon start on PhD in Computer Science
  • Research involving the detection of overlapping communities in networks and how to parallelize these problems on large GP:GPUs
  • Designing the front end of a Smart Grid aggregation system
  • B.S. Computer Science from Slippery Rock University
  • Worked with robotic arm
  • "Boulders, the poor man's super computer"
  • Minor in theater
  • I like to hike
  • Big Pittsburgh sports fan
Office Hours

Wednesdays from 1:30 - 2:30 PM in POST 303-5

History of Java

  • Created in 1990 - 1992 as replacement to C++
  • Designed to run a multitude of platforms (like kitchen appliances)
  • 1994, Java applets
History of Java Versions
Release Year Features
JDK 1.0 1996 Many additions to standard library
J2SE 1.1 1997 Improvements to AWT events, inner classes, JDBC
J2SE 1.2 1998 Reflection, collections, Swing, JIT compiler
J2SE 1.3 2000 HotSpot JVM, JNDI, JPDA
J2SE 1.4 2002 RegEx modeled after Perl, exception chaining, XML parser, XSLT processor
J2SE 5.0 2004 for-each loops, generics, autoboxing, var-args
Java SE 6 2006 Scripting in JVM, pluggable annotations, GUI improvements
Java SE 7 2011 Strings in switch, try-with-resources, type inference for generics

Java Overview

Java Virtual Machine
  • Java source code is compiled to platform independent byte code
  • The byte-code is then interpreted by the platform dependent JVM and converted into the actual machines instructions
Java is Object Oriented
  • In Object Oriented programming, the class is the fundamental programming unit
  • An object is an instance of a class
  • Classes contain related attributes and methods that act on an object
Example of a class
/**
 * Represents a person by storing their first and last name.
 */
public class Person {
  /**
   * The first name of this person.
   */
  private String firstName;
 
  /**
   * The last name of this person.
   */
  private String lastName;
 
  /**
   * Creates an instance of a person.
   */
  public Person(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
  
  /**
   * Get the first name of this person.
   * @return The first name of this person.
   */
  public String getFirstName() {
    return this.firstName()
  }
  
  /**
   * Sets the first name of this person.
   * @param firstName The first name of this person.
   */
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
 
  /**
   * Gets the last name of this person.
   * @return The last name of this person.
   */
  public String getLastName() {
    return this.lastName;
  }
 
  /**
   * Sets the last name of this person.
   * @param lastName The last name of this person.
   */
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
  
  /**
   * Returns a string representing a person by their first and last name.
   * @return This persons first and last name.
   */
  @Override
  public String toString() {
    return this.firstName + " " + this.lastName; 
  }
}
Creating instances of a class
public static void main(String[] args) {
  Person ta = new Person("Anthony", "Christe");
  Person professor = new Person("Martha", "Crosby");
}
The Java API
  • Java contains a large amount of packages which contain an even larger amount of pre-defined classes
  • Access to the Java API is done through the use of import statements
import java.util.Random;

public class RandomNum {
  public static void main(String[] args) {
    Random random = new Random();
    System.out.println(random.nextInt());
  }
}
Primitive Data Type
Type Bits Values
byte 8 (1 signed) -128 to 127
short 16 (1 signed) -32,768 to 32,767
int 32 (1 signed) -2,147,483,648 to 2,147,483,647
long 64 (1 signed) -9,223,972,036,854,775,808 to 9,223,972,036,854,775,807
float 32 (IEEE 754) ~ (+/-) 10^(-38) to (+/-) 10^(38) with ~ 6 digits of precision
double 64 (IEEE 754) ~ (+/-) 10^(-308) to (+/-) 10^(308) with ~ 15 digits of precision
char 16 Unicode character set
boolean not defined true, false
Constants good, magic numbers bad
  • Constants can be created in Java by using the "final" keyword.
  • Constants are also usually made static, as the value does not change for different instances of a class
  • Constant names should be in all caps
  • Multiple words in a constant name should be separated with an underscore "_".
public static final double PI = 3.14159265358979;

public double getAreaOfCircle(double radius) {
  return PI * (radius * radius);
}
Operators
Pre vs. Post increment/decrement
int z = 0;

// These three statements are equivalent
z = z + 1
z++;
++z;

// But what about the following code?
int i = 5;
int j = 5;
int x = i++;
int y = ++j;

Can you figure out the values for i, j, x, and y in the previous code snippet?

Casting with primitives
  • It's perfectly fine to change a small type into a larger type (widening, i.e. int -> float, byte -> int, etc)
  • In order to change from a larger type to a smaller type, casting must take place (i.e. float -> int, double -> float, etc)
  • When casting occurs, accuracy is lost
int a = 5;
double b = a; // Here, b receives the value of a, but widens it into a double.

// The code below is incorrect
float x = 1.5;
int y = x;

// To correct the above, we need to "cast" the larger type into the smaller type
float x = 1.5;
int y = (int) x;

System.out.println(y); // 1 instead of 1.5

Clone this wiki locally