Skip to content

Instantly share code, notes, and snippets.

@benjaminEwhite
Last active March 15, 2016 00:43
Show Gist options
  • Save benjaminEwhite/828d8fdd128d162a99ee to your computer and use it in GitHub Desktop.
Save benjaminEwhite/828d8fdd128d162a99ee to your computer and use it in GitHub Desktop.
notes from java4python

Java Self Study from here

Data types

Primitive / Object versions (Java has both)

  • int / Integer
  • float / Float
  • double / Double
  • char / Char
  • boolean / Boolean

you used to have to explicitly convert back and forth between, now compiler knows to do this (autoboxing)

Imports

Some classes need not be imported. That's if javac and java already know existence of class, and you use full name of class.

Java knows about: all * classes defined in .java and .class files in cwd * all classes shipped with java * classes included in CLASSPATH env var, which names jar file with classes or a directory with class files

Imports do not load imported class into memory. That only occurs in the loading class using it.

So the import statement is really just about using a shortened version of the name. If you wanna refer to java.util.Scanner you can do that without import, but if you want just Scanner you gotta import it.

i/o and Scanner objects

java.util.Scanner used for getting input from users, files, networks. javax.swing.JOptionPane used for GUI-fied version of same

Strings

  • Immutable with
  • Don't support indexing or slicing with an operator, you do it with method calls
  • In general, no operator overloading.

Python/Java

indexing str[3] => str.charAt(3) slicing str[2:5] => str.substring(2, 4) length len(str) => str.length() index of char str.find('x') => str.indexOf('x') splitting str.split() => str.split('\s') concatenation str + str => str.concat(str) stripping str.strip() => str.trim()

ArrayList

  • java's equiv of python list
  • when declared, must indicate what type it will hold ysing <Type> syntax: ArrayList<Integer> count
  • use .get() and .set() methods

Primitive Arrays

  • can be declared and initialized in same step
  • use square bracket notation

Loops

  • while - used with boolean that determines exit condition
  • for - iterate over a range for (Integer i = 0; i < 10; i++) {} and for each variant for lists and strings etc: for (Char i : word) {} iterating over a declared collection: for(Integer i: count) {} where count is an ArrayList, for instance.
  • also do/while where you define unit of work to be done in do, and condition secondly. this ensures that do block is always executed at least once, even if while condition never evaluates to true.

Maps

  • Like Python dictionaries
  • Two implementations: TreeMap and HashMap
  • TreeMap uses balanced binary tree to implement, while HashMap uses hash table

Conditionals

if /else

if (condition) {
    <!-- do something -->
} else {
    <!-- do something else -->
}

No explicit elif, but can use else if without further nesting if block is single statement.

switch

Basically looks like Javascript switch:

int tempgrade = grade / 10;
switch(tempgrade) {
case 10:
case 9:
    System.out.println('A');
    break;
case 8:
    System.out.println('B');
    break;
case 7:
    System.out.println('C');
    break;
case 6:
    System.out.println('A');
    break;
default:
    System.out.println('F');
}

ternary operators

like javascript, e.g., condition ? trueValue : falseValue and ternaries can be used for assignment.

classes

public class Something {
    // instance vars could be declared at top or bottom of class
    private Integer foo;
    private String bar;

    // constructor method just uses the class name
    public Something(Integer foo, String bar) {
        // because param names and instance vars have same name, need
        // to use this, if they had diff names, we wouldn't need `this`
        // cause they would be in scope from var declaration above
        this.foo = foo;
        this.bar = bar;
    }

    // getters
    public Integer getFoo() {
        return foo;
    }
    // setters
    public void setFoo(Integer newFoo) {
        foo = newFoo; 
    }

    public Something evolveSomething(Integer foo, String bar) {
        return new Something(this.foo + foo, this.bar.concat(bar));
    }

}

oddities

  • in Java you can have multiple of the same method name with different signature. This is how you accomplish overloading in Java.
  • java only supports single inheritance

the Object class

  • all java objects inherit from this class, by default
  • you get the following methods for free (which you're free to override):
    • toString: equiv of __str__ in Python
    • clone
    • equals
    • finalize
    • getClass
    • hashCode
    • notify
    • notifyAll
    • toString
    • wait

abstract classes

  • if you were implementing a Fraction class, and wanted it to behave like other numeric types, you'd need to implement all of the methods required by the parent abstract class Number.
  • Java abstract classes define a set of common functions that all children must implement.
  • You do this by using extends and implementing the methods required by base class
  • Note that if you're using a new child of an abstract class in some method, and you have typed that param as the parent class, you will get an error if you try to call any method on child other than those set out in abstract parent.

the interface mechanism

  • Like a combo of inheritance and contracts
  • It's a specification for what all objects claiming the interface must implement
  • In some sense similar to abstract class, but you can't construct instance of an interface
  • use implements InterfaceName<ParamType>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment