Java Self Study from here
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)
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.
java.util.Scanner used for getting input from users, files, networks. javax.swing.JOptionPane used for GUI-fied version of same
- 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()
- 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
- can be declared and initialized in same step
- use square bracket notation
while
- used with boolean that determines exit conditionfor
- iterate over a rangefor (Integer i = 0; i < 10; i++) {}
andfor 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.
- Like Python dictionaries
- Two implementations:
TreeMap
andHashMap
TreeMap
uses balanced binary tree to implement, whileHashMap
uses hash table
if (condition) {
<!-- do something -->
} else {
<!-- do something else -->
}
No explicit elif, but can use else if
without further nesting if block is single statement.
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');
}
like javascript, e.g., condition ? trueValue : falseValue
and ternaries can be used for assignment.
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));
}
}
- 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
- 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 Pythonclone
equals
finalize
getClass
hashCode
notify
notifyAll
toString
wait
- 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.
- 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>