Java Flashcards
things an object knows about itself
instance variable
represents object’s state
things an object can do
methods
blueprint for an object
class
class describes what an object ………… and what an object ……….
knows
does
a method uses ……….. A caller passes ……….
parameter
arguments
java is pass-by-………..
value
that means pass-by-copy
getters and setters
accessors and mutators
access modifiers
public
private
hide data
private
declared inside class
instance variables
declared inside method
local variable
local variables …………. initialized before use
must be
check if two objects are equal
.equals()
to compare two primitives
==
a class can have any number of
instance variables
getter setter methods
a method can have only one
return
these help create encapsulation
getter
setter
private
public
convert String to int
Integer.parseInt(string);
iterate in array (enhanced for loop)
for(int cell : locationCells){……….}
get array length
array.length
Math.random()
returns a random number from 0 to just less than 1
while loop is good
when you don’t know how many times to loop and just want to keep going while some condition is true
ArrayList
add(Object elem) - adds elem to the list
remove(int index), remove(Object elem)
contains(Object elem) - returns true if there is a match for the object parameter
isEmpty()
indexOf(Object elem) - returns either the index of the object parameter or -1
size()
get(int index)
ArrayList over arrays
size changes dynamically
can remove elements
ArrayList is less efficient
in order to put primitives in ArrayList
wrap using primitive wrapper class unwrapping happens automatically when taking out the primitive from ArrayList
………… are faster to hold primitives
arrays
ArrayList has some extra load due to wrapping and unwrapping
new String[2];
new ArrayList();
short circuit operators
&&, ||
non short circuit operators
&, |
classes are grouped into
packages
packages importan
help the overall organization of a project or library
give you a name scoping
provide a level of security
java.lang
need not to be imported
preimported
import is not the same as ……….. in C
include
…………. extends …………….
subclass supeclass
add more stuff to super class method
public void method_name(){
super.method_name();
…………….
}
superclass can choose whether or not it wants a subclass to inherit a particular member by the level of access the particular member is given
private - not inherited
default
protected
public - inheritied
inherited methods can be overriden
instance variables cannot be overriden
by using inheritance
get rid of duplicate code by abstracting out the behavior common to a group of classes, and sticking that code in a superclass
with polymorphism, the reference type can be a ………
superclass of the actual object type
polymorphism makes use of
inheritance
with polymorphism
can write code that doesn’t have to change when you introduce new subclass
polymorphic arrays
polymorphic arguments
polymorphic return types
non-public class can be subclassed only by
classes in the same package
what if i don’t declare a class as public
then it is a non-public class
final modifier
end of the inheritance line
can’t extend a final class
can’t extend a class which has only ……….. constructor
private
if you want to protect a specific method from being overridden, mark the method with final modifier
mark the whole class as final if you want to guarantee that none of the methods in that class will ever be overridden
in order to overide
arguments must be same
return types must be compatible
the methods can’t be less accessible
method overloading
having two methods with the same name but different argument list
in order to overload
return types can be different
can’t change only the return type
can vary access levels in any directions
overloaded method has nothing to do with ……..
inheritance
abstract class
a class that can’t be instantiated
interface
100% abstract class
making a class abstract
abstract class Class_name{…..}
abstract class means
it must be extended
abstract method means
it must be overridden
creating an abstract method
public abstract void method_name();
no curly braces
if you declare an abstract method
you must mark the class abstract well
you must implement all ………. methods
abstract the first concrete class in the inheritance tree must implement all the abstract methods
every class we write extends
Object any class that doesn't explicitly extend another class, implicitly extends Object
Object class
boolean equlas() Class getClass() int hashCode() String toString()
is class Object abstrac
no
got method implementation code that all classes can inherit and use out-of-the-box, without having to overriide the methods
you are encouraged to override these methods in Object class
hashCode()
equals()
toString()
most common use of an instance of type Object
thread synchronization
mulitple inheritance problem
deadly diamond of death
to define an interface
public interface Interface_name{…..}
to implement an abstract class
public class Child_class extends Super_class{…..}
to implement an interface
public class Child_class extends Super_class implements Interface{…………..}
save object’s state to a file
implement Serializable
make methods run in separate thread of execution
implement Runnable
a java class can have only one parent (super class - extends) parent class define who you are
can implement multiple interfaces
interfaces define roles you can play
subclass
more specific version of a class and need to override or add new behaviors
use abstract class
when you want to define a template for a group of subclasses
you have at least some implementation code that all subclasses could use
when you want to gurantee that nobody can make objects of that type
use an interface
when you want to define a role that other classes can play, regardless of where those classes are in the inheritance tree
all interface methods are implicitly ………. and ……….
public
abstract
stack frame
holds the state of the method including which line of code is executing, and the values of all local variables
if the local variable is a reference to an object
only the variable (the reference) goes on the stack
java has two areas of memory we care about
Stack
Heap
instance variable lives in
heap
inside the object
constructor
has code that runs when you instantiate an object
java lets you declare a method with the same name as your class
if it doesn’t have a return type then it is a constructor
pirvate constructors are inherited
false
if you write a constructor that takes arguments, and you still want a no-arg constructor,
you’ll have to build the no-arg constructor yourself
to compile each constructor must have a ………. argument list
different
a constructor is the code that runs when you say ….. on a class type
new
you can use constructor to initialize the state of the object being constructed
i.e the instance variables
always provide a no-arg constructor if you can, to make it easy for programmers to make a working object
supply default values
subclass’ constructor is invoked first
but it is the superclass constructor that finishes first
…… calls the super constructor
super()
compiler puts in a call to super() if you don’t
by default it calls no arg constructor of the super class
the superclass parts of an object have to be fully-formed (completely built) before the subclass parts can be constructed
subclass object might depend n things it inherits from the superclass, so it’s important that those inherited things be finished
the call to …………. must be the first statement in each constructor
super()
a reference to the current object
this
you can say this() only within a ……………….
and it must be the first statement
constructor
every constructor can have a call to ………. or ………….. but never both
super()
this()
use ………. to call a constructor from another overloaded constructor in the same class
this()
an object is alive as long as
there are live references to it
when an object’s last reference disappears
- the reference goes out of scope, permanently - method ends
- the reference is assigned to another object
- the reference is explicitly set to null
if you use the dot operator on a null reference you’ll get a ………. at run time
NullPointerException
static class never affected by …………. variables
instance
static methods never use instance variable values
Math constructor is marked as ………….
private
lets a method run without any instance of the class
static
call a static method using ………… name
class
call a non-static method using ………….. ………. name
reference variable
the only way to get new object
new or deserialization (or something called the Java Reflection API)
static methods ………….. use non-static (instance) variables
CAN’T
static methods ………….. use non-static methods
CAN’T
static variable
value is the same for all instances of the class
static variable is initialized
only when the class is first loaded not each time a new instance is made
static variables are ………….
shared all instances of the same class share a single copy of the static variable
static variables in a class are initialized before any object of that class can be created
static variables in a class are initialized before any static method of the class runs
static final variables are
constants
a variable marked final means
once initialized it can never change
…………. is a block of code that runs when a class is loaded, before any other code can use the class, so it’s a great place to initialize a static final variable
static initializer clas Foo{ final static int x; static{x=42;} }
if you don’t give a value to a final variable
the compiler will catch it
final variable means
you can’t change its value
final method means
you can’t override the method
final class means
you can’t extend the class
if the class is final
you don’t need to mark the methods final
static method is good for
utility method that does not (and will never) depend on a particular instance variable value
when you need to treat a primitive like an object
wrap it
put integer in ArrayList
ArrayList list = new ArrayLIst(); list.add(3); int num = list.get(0);
converting a string to a primitive value
Integer.parseInt(string);
out in System.out is
a static variable
inner class can use all the methods and variables of the outer class
even the private ones
a …………. is an object that represents a network connection between two machines
socket
what is a connection
a relationship between two machines, where two pieces of software know about each other.
to make a socket connection you need to know
ip address
tcp port number
a socket connection means
the two machines have information about each other including ip address and tcp port
a tcp port is just a number
a 16-bit number that identifies a specific program on the server
without port number
the server would have no way of knowing which application a client wanted to connect to
a server can have up to ………………… different server apps running, one per port
65536
the tcp port numbers from 0 to 1023 are reserved for well-known services
don’t use them for your own server programs
if you try to bind a program to a port that is already in use
you’ll get a BindException
to communicate over a socket connection we use
streams
ip address of localhost
127.0.0.1
…………… is a bridge between low level byte stream (like the one coming from the socket) and a high level character stream (like the BufferedReader)
InputStreamReader
client and server communicate over a …………. connection
socket
unless you have multiple processors on your computer, each new Java thread is not actually a separate process running on the OS
but it almost feels as though it is
by creating a new Thread object, you’ve launched a separate thread of execution, with its very own call stack
but as it has nothing to do virtually it dies as it born
Thread t = new Thread(); t.start();
multiple threading in Java means we have to look at both ……….. and ……. that’s run by the thread
thread
job
every java application starts up a ………. thread
main
the thread that puts main method at the bottom of the stack
Thread class
void join() void start() static void sleep()
thread’s job
Runnable object
thread object - worker
runnable - thread job
to go from having just a Thread instance to having a new thread of execution you must call
thread.start();
all the methods in an interface are
public
once the thread becomes runnable, it can move back and forth between
runnable
running
blocked
blocked
sleeping
waiting for another thread to finish
waiting for an object’s lock
scheduler implementations are different for different JVMs
even running the same program on the same machine can give you different results
every thread in java has its own ………….
call stack
sleep method throws an InterruptedException
so all calls to sleep must be wrapped in a try/catch
try{Thread.sleep(2000);
}catch(InterruptException ex){
ex.printStackTrace();
}
when the thread wakes up, it always goes back to …………….. state
runnable
threads can lead to concurrency issues
concurrency issues lead to race conditions
race conditions lead to data corruption
the ……………. keyword means that a thread needs a key in order to access the synchronized code
synchronized
when code hits a synchronized method
there is going to be a performance hit
synchronization restricts concurrency
synchronized methods can lead to deadlock
names used for classes, variables and methods are called
identifiers
access modifiers
default
public
protected
private
non-access modifires
final
abstract
strictfp
enum
restrict a variable to have one of only a few predefined values
enum FreshJuiceSize{SMALL, MEDIUM, LARGE}
visible to the package
default - no modifiers are needed
visible to the class only
private
visible to the world
public
visible to the package and all subclasses
protected
final
is used to apply restrictions on class, method and variable
finally
used to place important code, it will be executed whether exception is handled or not
it is a block
finalize
is a method
used to perform clean up processing just before object is garbage collected