Java Basics Flashcards
Method Overloading
- (if a class has) multiple methods having the same name but different parameters
- it increases the readability and reusability of the program
- there are two ways to overload the method: changing the number of arguments; changing the data type
- a good example is System.out.println() method
Constant
- it is a value that can’t be changed after assigning it
- We use static and final modifiers (aka non-access modifiers), to declare any variable as constant
- its identifier name must be in capital letters
“final” keyword
- it is a non-access modifier applicable only to a variable, a method, or a class
- when a variable is declared with the final keyword, its value can’t be modified.
- when a method is declared with the final keyword, it can’t be overridden.
- when a class is declared with the final keyword, it can’t be extended (inherited).
Modifiers
- are specific keywords that we can use to make changes to the characteristics of a variable, method, or class and limit its scope
- Java has two types of modifiers: access modifiers and non-access modifiers
Non-Access Modifiers
- provide information about the characteristics of a class, method, or variable.
What are the seven types of Non-Access Modifiers?
- static
- final
- abstract
- synchronized
- volatile
- transient
- native
static methods
- when a method is declared with the static keyword, it is known as the static method
- Any static member can be accessed before any objects of its class are created and without reference to any object.
- the most common example of a static method is the main() method
What are the restrictions of the static methods?
- they can only directly call other static methods
- they can only directly access static data
- they can’t refer to this or super in any way
private keyword
an access modifier used for attributes, methods, and constructors, making them only accessible within the declared class
protected keyword
used for attributes, methods, and constructors, making them accessible in the same package and subclasses
int vs. Integer
- int is a primitive data type while Integer is a type of class
“new” keyword
- it is used to create an instance of the class.
- it instantiates a class by allocating memory for a new object and returning a reference to that memory.
- we can also use the new keyword to create the array object
What are the two types of data types Java provides?
- primitive
- reference
Examples of reference data types
- Array
- class
- interface
- String
- Enumeration
- Annotations
Serializable
In Java, it is a marker (empty) interface and has no fields or methods to implement.
Serialization
In Java, it is a process of converting the Java code Object into a Byte Stream, to transfer the Object Code from one JVM to another and recreate it using the process of Deserialization.
Why need serialization
- communication
- Persistence
- Deep Copy
- Caching
- Cross JVM Synchroniaztion
Instantiation
Creating an object of the class
- to call the constructor of a class that creates an instance (object)
- it occupies the initial memory for the object and returns a reference.
- two ways to create an instance: using the new keyword; using the static factory method
Example: // defines a reference (variable) that can hold an object ClassName reference; // instantiation reference = new Constructor();