Java Associate Flashcards
What is /** comment
JavaDoc
Rules for java code file
At most 1 public class Filename must match the class name (case sensitive) and have java ext
How do you define main class
class Main { public static void main(String[] args) {} } Or String args[] Or String... args
What is the result of compiling java file
.class file with JVM bytecode of the same name as java class file
JDK vs JRE
JDK - includes compiler
JRE - only runtime, compiled code on one machine can be run on any machine with JRE
Java package
Logical grouping for classes Import statement tells java which package to look in for the class Import statements can collide with one another (java.util.Date, java.sql.Date), if using explicit and wildcard it works (java.util.Date, java.sql.*) Use fully-qualified name if conflict can't be avoided Default package - no name, only throwaway code
Precents class name clashes Packages starting with java/javax come from JDK Rules for naming dot-segments - same as var names java.util.* - wildcard import all, doesn't import child packages, imports only classes.
- > Compiler figures out what actually needs to be imported but is less readable
- > java.lang is autoimported
JAR file
zip-like file of java .class files mostly
java pkg.Class to compile
-cp to set class path (where to look for packages&classes)
Class path can include jars
Constructor
Special method, named the same as class name No return type Purpose - initialize fields, but can contain any code Compiler can supply default one that does nothing
References and priimitives
Primitives - 8 data types - building blocks for java objects
boolean
byte 8b
short 16b
int 32b
long 64b 123L
float 32b 123.0f
double 64b
char 16b unicode
Numeric types are signed
Literals 0b10 binary, 0xFF hex, 017 octal
Numeric underscores 1_000_000 - readability feature Java7
They hold value where varaible is allocated
Reference types - the value is a pointer to an object somewhere in memory Can be assigned null - they refer to no object Can be used to call methods Should be Uppercased type names Instance/class vars dont require initialization (unlike local vars)
Identifier names
Beings with letter, $ or _
Subsequent chars can be nums
Cannot use reserved words (they are case-sen)
Where java stores object
Program memory heap - pool of unused memory allocated to java app
Garbage Collection
Process of auto freeing the heap, deleting objects not reachable by the program
System.gc() not guaranteed to run (java can ignore the method call)
When object is unreachable:
- no references pointing to it
- references gone out of scope
Finalize - objects can implement, called when GC collects the object; it’s called only once (in case it fails the first time, won’t be second one - for example if we assign reference to GCed obj in finalize); called 0 or 1 time
Numeric promotion
- if 2 values have diff data types, java auto promotes one of them to the larger data type
- if one is integral and other is floating, java auto promotes integral to floating
- Smaller DTs (byet, short, char) are always first promoted to int when used with java binary operators (even if none operand is int) - unary ++ excluded
- Result will have data type of promoted operand
Switch statement
Switch(varToTest) - int - byte - short - char - String - enum Default executed if no case match Breaks are optional, fall through if no break
case 1 default case 2 break 1 will go through both cases and default
Values in case statement must be compile-time constant values of the same data type as switch value (final vals are ok)
Var initialization when multiple vars in same line caveat
int x = 0, int y = 0 -> wrong, data type can be once!
For-loop
for(datatype element : collection) {}
For objects that inherit java.lang.Iterable, there is a different, but similar, conversion.
For example, assuming values is an instance of List, as we saw in the second
example, the following two loops are equivalent:
for(java.util.Iterator i = values.iterator(); i.hasNext(); ) {
int value = i.next();
System.out.print(value + “, “);
}
Break/continue statement in loops
Loops can have labels,
break LABEL; can break out of outer most loop in the inner most :O
+ operator rules
- if both ops are numeric - addition
- if either is string - concatenation
- expression is evaled left->right
Strings in java are…
...immutable String s1 = "1"; String s2 = s1.concat("2"); s2.concat("3"); System.out.println(s2);
String pool
AKA intern pool
Contains literal string values that appear in program code
StringBuilder vs StringBuffer
The other is thread safe