Basics Flashcards

(64 cards)

1
Q

.class vs .java

A
  • .java is the source code
  • .class is the compiled version (byte code)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

JDK

A
  • Java Development Kit
  • Requred to create Java apps
  • Installed by developers
  • Converts source code to byte code
  • Consist of:
    • JRE
    • Debugger
    • Compiler
    • JavaDoc
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

JRE

A
  • Java Runtime Environment
  • Required to run java apps
  • Must be installed by end users
  • Contains JVM as well as core Java libraries used by JVM
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

JVM

A
  • Java Runtime Environment
  • Turns byte code into machine code
  • Allows byte code to run on your particular computer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

byte code

A
  • Similar to machine code
  • Compiled code that can be understood and intepreted by different JVMs (java runtime environment)
  • Platform independent
  • .class files
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Run java from command line

A

Steps

  1. Compile code
  2. Navigate to top-level folder of the package in the build
  3. Run java using class name (including package, without file ending)
    • ex: java com.company.Main
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

package names

A
  • Follow standard naming convensions
  • All lowercase by convention
  • Ideally unique (on a global scale)
    • Use reverse domain name
    • Add further qualifiers to ensure uniqueness within organization/group
    • ex: package created by Walmart would be package com.walmart.groupa
  • Class names are composed from package names
    • ex: com.walmart.groupa.Main
  • Java doesn’t impose any rules about package names and file structure
  • But most IDEs require each subfolder to use part of package name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Primitive Types

A
  • 8 primitive types
  1. char
  2. boolean
  3. int
  4. byte
  5. short
  6. long
  7. float
  8. double
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Primitive Number Types

A
  • 6 primitive number types
  • All numbers are signed
  1. byte
    • 1 byte (width of 8 bits)
    • Only olds integer
  2. short
    • 2 bytes (width of 16 bits)
    • Only holds integers
  3. int
    • 4 bytes (width of 32 bits)
    • Only holds integers
  4. long
    • 8 bytes
    • Add l to the end to denote long literal
    • Only holds intergers
  5. float
    • 4 bytes
    • 7 digits of precision (after decimal)
    • Add f to the end to denote float literal
  6. double
    • 8 bytes
    • 16 digits of precision (after decimal)
    • Denote double literals with d to the end
    • But decimals are doubles by default in Java, so not necessary
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Type Suffix

A
  • There are three type suffixes
    • L: long
    • D: double
    • F: float
  • Can be uppercase or lowercase
  • Used to differentiate different number literals
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

char

A
  • 2 bytes (width of 16 bits)
  • Only supports 1 character literal
  • Must use single quotes
  • Also supports unicode characters encoded with hexadecimal
    • Notation: \u followed by 4 digit hex value
    • ex: ‘\u1234’
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Scope

A
  • 3 levels of scope
  1. Instance variables (class-level scope)
    • Also known as class variables
    • Defined within the class, but outside any methods
    • Scope is determined by their access modifier
    • Can be used by any methods in class
  2. Local variables (method-level scope)
    • Defined within a method
    • Cannot be accessed by other methods
  3. Loop variables (block-level scope)
    • Defined with a block (if statement or for loop)
    • Cannot be accessed outside block
  • In general, a set of curly braces defines a scope
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Method Overloading

A
  • Using the same method name with different parameters
  • Java will perform automatic type conversions if no matching signatures can be found
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

this

A
  • A reserved keyword used to refer to the instance itself (of a class)
  • required when differentating between local variables and instance variables of the same name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

this() as a call

A
  • this can be involked in a constructor, if it’s the first statement
  • used to call another version of the constructor
  • useful when constructor is overloaded
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

access modifier

A
  • specify access of a data member, method, constructor or class

4 options:

  1. default
    • No access modifier provided
    • Makes item visible to any other class within package
    • Also called “package-private”
  2. private
    • Makes item visible only within class
  3. public
    • Makes item visible by anyone
  4. protected
    • Makes item visible to package and its subpackages

Notes:

  • Classes and inferaces cannot be private
  • Main method must be public (has to be invoked by Java Interpreter)
  • If a class is public, file name must be the same as class name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

widening conversion

A
  • converting from a smaller data type (in terms of bytes) to a larger data type
    • ex: convert long number = intVal (where b in an int)
  • compiler can perform widening conversion automatically without an explicit cast
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

narrowing conversions

A
  • converting from a larger data type to a smaller data type
  • ex: btye number = doubleVal (where b is a double)
  • compiler cannot perform narrowing conversions without ax explicit cast (statement above would throw error)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

arithmetic with two different data types

A
  • when performing arithmetic with different data types, output is always the larger type
  • if one if floating point and one is integer, output is always floating point (regardless of byte size)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

convert floatng point values to integer values

A
  • Java will autoatically convert integer values to floaing points, but cannot automatically convert floating points to integers
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Arrays

A
  • must all be the same type
  • cannot be resized
  • declared using square brackets
    • ex: int[] arr = new int[4];
  • or with curly brackets
    • ex: int[] arr = {1, 2, 3};
  • elements in an array are always initialized with default initial values
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

calculate length of array

A
  • Array
    • use length property (not method) on an array
  • ArrayList
    • use .size() method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

for each loop

A
  • a shorthand approach to for loops
  • can be used on arrays
  • Syntax:
    for (type loopVar : array) {
    statement;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

switch statements

A
  • in java, will match and run all subsequent cases after first match (even if they don’t actually match)
  • can only switch (and case) on certain data types:
    • byte, short, int (and their wrapper classes)
    • char
    • boolean
    • Strings
    • enums
  • switch x is a variables, literals and expressions
  • case x is a literal or expression, but not variable or conditional
  • Syntax:
    switch (var) {
    case 1:
    staement;
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
default initial values
* values assigned to variables by Java automatically, under certain conditions * contents of an array are assigned default values when array is created * fields within a class also receive these vaues during instantiation (if no values are assigned) * by type: * 0 for numbers * false for boolean * null character for char * '\u0000' * null for objects
26
new keyword
* used to instantiate classes * allocates memory for the class * returns a reference to the instance
27
encapsulation
* information-hiding * restricting the ability of a user to access the internal data of a class * generally, only the class itself should have access to its data members, unless otherwise exposed via getters and setters * achieved using access modifiers
28
constructor
* Can use super() or this() inside, but not both * each has to be the first statement to be valid * Signature of constructors cannot have a return type * Compiler won't regard it as a constructor if it does * Constructor do actually return an instance of the class * Java will provide implicit no-args constructor if you don't make one * But implicit no-args constructor won't exist if you define even one cosntructor yourself * Constructors can have any access modifier
29
instance initialization block
* another approach to instantiating variables in a class * always runs before constructors * can access methods anywhere within the class * can only access instance variables if they've been declared above IIB * created by wrapping statements in curly braces on class level (beside instance variables) * if more than one is provided, they execute in order * if IIB is used in inheritance, order is: 1. IIB of parent 2. Constructor of parent 3. IIB of child 4. Constructor of child * Syntax: { statement; statement; }
30
varargs
* also called variable arguments * allows you to declare a method that accepts an arbitrary (0+) number of arguments * can only be used on the last parameter in a method * parameter treated as an array inside method body * with this syntax, can pass variables individually, or an an array (since java will make an array anyway) * Syntax: private void functionA(Type... var) { statement; } * spacing between ellipses doesn't matter, can go before, between or after (or not at all)
31
Inheritance
* Process of creating a new class that borrows fields and methods directly from another class **Steps (2)** 1. Extend child class * public class Child **extends Parent** { } 1. Invoke super method * public Child(int a, int b, int c) { **super(a, b, c);** } _Notes_ * Child does not inherit private methods/attributes of parent * A contructor from parent must be involked implicity or explicity in each of constructor in derived class * If you don't invoke super explicity, compiler will invoke a no-args super for you * No-args super invokes no-args constructor of parent (if one exists)
32
inheritance parent / child instantiation
* using a parent class to instantiate a child class * Example: Parent instanceA = new Child(); * the instance is of type Child, not Parent * the instance cannot access any methods or attributes of child except for overriden methods (methods defined in both parent and child classes) * instance can still access methods and attributes of child via casting
33
inheritance hiding attributes
* in java, a subclass will ***hide*** the fields of a superclass if fields are the same * child class will expose its version of the field * when using parent/child instantiation: * instance cannot access child's fields without a cast * method calls that use hidden attributes will rely on parent's verion of the fields in its implementation
34
inheritance overriding methods
* in java, a subclass will ***overrride*** the method of a superclass if names are the same * child class will expose overriden method * when instantiating child from parent * instance will return child's method * when using paren't method, instance will invoke child's version of any methods used in implementation
35
@Override
* tells compiler that this method is an override * will prevent compilation if there isn't a matching method available to override
36
Object class
* root of the class hierarchy * every class in java inherits directly or indirectly from Object class * defines basic functionality for all objects
37
Object class methods
* clone * duplicates instance * hashCode * generates a hash of current instance * getClass * returns type information for current instance * toString * returns string representation * equals * compares instances
38
equality
* == * if variables are primitives, will compare values * if variables are references, will compare memory addresses * equals * part of Object class * will compare memory addresses of references by default * typically, we override equals to work the way we want
39
non-access modifiers
* 7 in Java: * static * final * abstract * synchronized * transient * volatile * native * order of access and non-access modifiers doesn't matter
40
final
* a non-access modifier that has different effects in different contexts * **variables** * can only be initialized once * if a primitive, value cannot be changed * **methods** * prevents overriding of that mehod * **classes** * prevents inheritance of that class
41
abstract
* non-access modifier * ***class*** * must be inherited from in order to be used * methods within the class can or cannot be abstract (have actual implemenations) * if child does not implement *all* of the _abstract_ methods, it must be also be abstract * ***methods*** * declared without an implementation * no curly braces, just signature and semi-colon * cannot have a body * can only exist in abtract classes and interfaces * ***variables*** * cannot be applied to variables
42
assign child to parent
* in java, you can assign a child class reference to a parent * but, in doing so, you lose access to child-specific methods and attributes * even overridden methods and attributes now default to parent's version * you can get back the child-specific attributes by casting * you cannot assigned parent to child class * Syntax: Flight myFlight = new CargoFlight();
43
composition
* a type of OOP strategy * a way of modelling a 'has-a' relationship * basically, make complex objects by attaching objects to a class as instance variables * ex: usering node class in linked list
44
polymorphism
* refers to the ability of a method of the same name to have different behavior 3 situations: * ad-hoc polymorphism * two methods behave differently based on the parameters passed * aka: functional overloading * parametric polymorphics * a class does not specify the type of its parameters upfront and can accept objects of various types * aka: generics * subtyping * a subclass overrides its parent's method with a different implementation
45
interfaces
* basically an empty class that indicates what methods must be implemented if someone/something inherits from them * only contains only class variables and empty methods * interfaces are abstract * interface only has method signatures (no curly braces) * methods can only be public, and keyword is optional * prevents arbitrary changes in classes that implement it **Steps:** 1. Define interface * public interface MyInterface { } 2. Write abstract methods * void myMethod(); 3. In a separate class, implement Interface * public class MyClass implements MyInterface { } 4. Implement abstract methods in interface * public void myMethod() { // implementation }
46
super.method
* Allows you to invoke that method on the parent * Widely used when overriding methods in a child class
47
reference
* the memory address of an object in Java
48
super
* super() **as a call** * Must be the first statement in a constructor * If not written, no-args super() will be inserted automatically in subclass constructor * no-args super() invokes no-args constructor in parent (if it exists)
49
java.lang.object
50
Annotations
51
Scanner
* class for reading user input of primative types * part of java.util **Steps (3)** 1. Input the class * import java.util.Scanner 2. Instantiate a new scanner * Either as a class variable, or within main block * Should take System.in (reference to computer's input stream, ie: keyboard) * Scanner scanner = new Scanner(System.in); 3. Invoke method on scanner to read input from stream * scanner.nextLine() reads a line (up until newline char) and returns a string * scanner.nextInt() reads and returns an integer * scanner.next() reads tokens until a delimiter (usually a space)
52
cloning arrays in Java
* Use Arrays.copyOf * Arrays.copyOf(oldArray, newArrrayLength); * truncates array if necessary * pads array with 0 if necessary
53
List
* interface * ordered collection of items * extends the Collections interface
54
ArrayList
* extends AbstractList and implements List interface * part of java.util * build-in resizable array * a generic type
55
ArrayList methods
_7 situations_ 1. Add element * .add(item) 2. Update element * .set(index, item) 3. Remove element * .remove(index) * Returns the element that was removed from list 4. Retrieve ith element * .get(index) * cannot use brackets 5. Get size * .size() 6. Find element * .contains(obj) * Returns true if object is in array * .indexOf(obj) * Return index of object in array, or -1 7. Append elements * .addAll(collection)
56
cloning ArrayList
* instatiate newArray with oldArrary as input * ex: ArrayList newArrary = new ArrayList\<\>(oldArray);
57
convert ArrayList to Array
**Steps** 1. Instantiate new array with appropraite size * String[] newArray = new String[oldArrayList.size()] 2. Use .toArray method * newArray = oldArraryList.toArray(newArray) * passing newArray to .toArray() method tells it what type to return
58
generics
* Allows you to write one class that can accept multiple different types * Provides type safety * allows you to use different types with the same class without sacrificing type safety
59
Class
* member variables * Should usually be private
60
isntance vs static vs local variables
61
62
conditional logical operators
* && * Only resolves right side if left side is true * || * Only resolves right side if left side is false
63
intialization order
* order in which intialization processes take place 1. fields 2. instance initialization blocks 3. constructors
64
XML Schema
* XML definition * one root element * called **xs:schema** * include: xmlns, xmlns:xs, targetNamespace, * attribute definitions * called **xs:attribute** * include: name, type, default, fixed, use * element definitions * called **xs:element** * includes: name, type, default, fixed