Java Overview

Photo by Nathan Dumlao on Unsplash
Photo by Nathan Dumlao on Unsplash

Introduction to Java

Java is a high-level, general-purpose, compiled programming language. Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture.  It is intended to let application developers “write once, run anywhere” (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.

History

Java was originally developed by James Gosling at Sun Microsystems (which has since been acquired by Oracle Corporation) and released in 1995 as a core component of Sun Microsystems’ Java platform. The language derives much of its syntax from C and C++, but it has fewer low-level facilities than either of them. The original and reference implementation Java compilers, virtual machines, and class libraries were originally released by Sun under proprietary licenses. As of May 2007, in compliance with the specifications of the Java Community Process, Sun relicensed most of its Java technologies under the GNU General Public License.

Java Resources

Quick Reference – Java

Here is a quick reference for the basics of Java…

Hello World in Java

App.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
					
public class App {
  public static void main(String[] args) { 
    System.out.println("What is your name?");
    try {
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
      String yourName = bufferedReader.readLine();
         System.out.println("Hello " + yourName + "!");
     } catch(IOException e) {
           e.printStackTrace();
     }
  }
}

Java: Basic Syntax

Variables & Data Types

Wrapper classes provide methods like .parseInt() or .toString(). Primitives are more efficient.

  
Primitives    Wrapper classes
  boolean       Boolean
  byte          Byte
  short         Short
  int           Integer
  long          Long
  float         Float
  double        Double
  char          Character, String

Branching

Note: == applies only to numbers, so to compare strings (or other objects), use string1.equals(string2)

if (condition) {
  // do something
} else if (condition) { 
  // do something else
} else { 
  // do something else
}

Switch statements:

int var = 0;

switch(var) {
  case 1:  // do first thing
           break;
  case 2:  // do second thing
           break;
  default: // what to do if nothing matches...
           break;
}

Arrays

Note: You cannot change the length of an array after it is declared.

int[] primeNumbers = { 2, 3, 5, 7, 11 };

Array Lists

import java.util.List;
import java.util.ArrayList;

List primeNumbers = new ArrayList {2, 3, 5, 7, 13};
primeNumbers.add(17);
primeNumbers.get(1); //returns 3 in this case...

Hash Maps

import java.util.Map;
import java.util.HashMap;

Map<String, String> mapName = new HashMap<string, string>();

Loops

You can loop through all the items in an array:

string[] myList = {"one", "two", "three", "four", "five"};

for (String item : myList) {
  System.out.println(item);
}

You can loop a fixed number of times:

for (int i = 0; i < 5; i++) {
  System.out.println(i);
}

You can loop while a condition is true:

while (true) {
  // do something that may change the condition
}

You can loop until a condition is false (always loops once):

do {
  // do something that may change the condition
} while (true);

Java: Object-Oriented Syntax

Classes

Classes are defined in a file with the same name. All names must be uppercase:

Animal.java

class Animal {
  public string Species;
  public string Name;
  public string Sound;
}

Objects

Animal myPet = new Animal();

myPet.Species = "dog";
myPet.Name = "Fido";
myPet.Sound = "Bow-wow";

Methods

MyCalculator.java

class MyCalculator {
  public int Add(int num1, int num2) {
    return num1 + num2;
  }
}

Constructors

Animal.java

class Animal {
  public string Species;
  public string Name;
  public string Sound;

  public Animal(string species, string name, string sound) {
    Species = species;
    Name = name;
    Sound = sound;
  }
}

Animal myPet = new Animal("dog", "Fido", "Bow-wow");
Animal yourPet = new Animal("cat", "Garfield", "Screech! Hiss!");

Favorite Books on Java