Java Building Blocks Flashcards
class
A description of an object. A class in Java is defined in a source file with the .java extension and compiled into a file with the .class extension.
object
A runtime instance of a class in memory
members of a class
methods and fields
Method Signature
The full declaration of a method
/* */ vs. /** */
The first is a regular multiline comment, the second is a special Javadoc comment
When you have two classes defined in the same java file, how many can be public?
1
Java Virtual Machine (JVM)
Runs Java class files.
main() method declaration
public static void main(String[] args)
How to compile and run a program called Zoo.java
javac Zoo.java
java Zoo
bytecode
Compiled Java code. Bytecode appears in files with the .class extension.
access modifier
Defines what other code can refer to the code. Choices are private,
protected, public, and default (no modifier.)
static
Binds a method or field to its class so it can be called by just the class name, as in, for example, Zoo.main(). Java doesn't need to create an object to call the main() method Makes a variable shared across all instances of this class.
What type does java read in arguments as by default?
String
package
A grouping of classes, interfaces, enumerations, and annotated types.
What is the package that is automatically imported?
java.lang
default package
An unnamed package used when no package statement is present in the code. This is a special unnamed package that you should use only for throwaway code. In real life, always name your packages to avoid naming conflicts and to allow others to reuse your code.
JAR file
Like a zip file of mainly Java class files.
What return type does a constructor have?
None! Not even void
Instance Initializers
Code blocks (between braces { }) outside of methods
Code Block
Code between { }
When creating a new object, when does the constructor run?
After all fields and instance initializer blocks have run
Two types of data
Primitive types
and
Reference types
byte type
8-bit integral value (whole number)
short type
16-bit integral value (whole number)
int type
32-bit integral value (whole number)
long type
64-bit integral value (whole number) (should have a capital L at the end e.g. 463728642L)
float type
32-bit floating point value (could be decimal) (must have the letter f after the number, e.g. 100.5f)
double
64-bit floating point value (could be decimal)
char type
16-bit Unicode value
literal
A numeric, character, boolean, or String value that is typed into the code.
How to specify using octal notation
prefix the number with 0 e.g. 017
How to specify using hexadecimal notation
prefix the number with 0x e.g. 0xFF
How to specify using binary notation
prefix the number with 0b e.g 0b10
What can you use to separate a number into sections to make it easier to read?
Underscores
1000000 = 1_000_000
(cannot have _ at the beginning, ending, or right before or after a decimal point)
Reference Type
Basically an object, conceptually kinda like a pointer
What can be assigned the value “null”?
Reference types
Is String a primitive or a reference type?
Reference type
What can an identifier be made up of?
letters, numbers, $, or _
note: cannot start with a number
identifier
The name of a variable, method, class, interface, or enum.
local variable
A variable defined within a method
instance variable
Variable outside of a method (also called fields)
class variable
An instance variable with the word “static” before it, shared across multiple objects
Which variables are given default values and which are not?
Instance and class variables are, and local variables are not
Default value of boolean
false
Default value of byte, short, int and long
0
Default value of float and double
0.0
Default value of objects
null
Order of elements in a class
1) Package Declaration
2) Import Statements
3) Class Declaration
4) Field Declarations and Method Declarations
heap
Represents a large pool of unused memory allocated to your Java application. All
objects in Java reside in the heap memory.
When is an object considered to be no longer reachable and thus eligible for garbage collection?
1) The object no longer has any references pointing to it
or
2) All references to the object have gone out of scope
finalize()
The method that gets called if the garbage collector tries to collect an object.
Could run 0 or 1 time
Why is Java secure?
It runs in a virtual machine, the JVM. This creates a sandbox that makes it hard for Java code to do evil things to the computer it is running on.
What may a variable name begin with?
The name must begin with a letter, $, or _
Default value of boolean
false
Default value of byte, short, int and long
0
Default value of float and double
0.0
Default value of objects
null
Benefits of Java
1) Object Oriented: Java is an object-oriented language, which means all code is defined in classes and most of those classes can be instantiated into objects.
2) Encapsulation: Java supports access modifiers to protect data from unintended access and modification.
3) Platform Independent: A key benefit is that Java code gets compiled once rather than needing to be recompiled for different operating systems.
4) Robust: Prevents memory leaks by doing its own garbage collecting.
5) Secure: Java code runs inside the JVM. This creates a sandbox that makes it hard for Java code to do evil things to the computer it is running on.