Java - Basic Flashcards
(49 cards)
What is Java and what are its main features?
- Java is a high-level, general-purpose programming language that was developed by Sun Microsystems (now owned by Oracle Corporation).
- Its main features include:
- platform independence,
- object-oriented programming (OOP) support,
- automatic memory management (garbage collection),
- robustness,
- and a large standard library.
Java SE
- Java SE (Standard Edition)
- is the core Java platform
- that provides the basic set of APIs and libraries for general-purpose development.
Java EE
- Java EE (Enterprise Edition)
- is a set of APIs and libraries built on top of Java SE,
- specifically designed for developing enterprise applications, such as web applications and distributed systems.
Java ME
- Java ME (Micro Edition)
- is a subset of Java SE,
- optimized for resource-constrained devices like mobile phones and embedded systems.
Explain the JVM (Java Virtual Machine) and its role in Java programming.
- It is responsible for executing Java bytecode, which is the compiled form of Java source code.
- The JVM acts as an abstraction layer between the Java program and the underlying hardware and operating system.
- It provides platform independence by translating bytecode into machine-specific instructions at runtime.
- The JVM also:
- manages memory,
- handles garbage collection,
- and provides various runtime services to Java programs.
What are the primitive data types in Java
- byte
- short
- int
- long
- float
- double
- char
- boolean
What are the reference data types in Java?
- classes
- interfaces
- arrays
- enumerations
class
a class is a blueprint or template that defines the structure and behavior of objects.
It specifies the:
1. properties (fields)
2. and actions (methods) that objects of that class can possess.
object
- An object, on the other hand, is an instance of a class.
- and can hold its own unique state and behavior.
inheritance
- allows a class to inherit the 1. properties and 2. methods of another class, known as the superclass or base class.
- The class that inherits from the superclass is called a subclass or derived class.
- The subclass can:
- reuse the code
- and extend the functionality of the superclass.
What is the purpose of the “static” keyword in Java?
- used to declare members (fields or methods) that belong to the class itself rather than specific instances of the class.
- Static members are:
- shared among all objects of the class
- and can be accessed directly using the class name, without creating an instance of the class.
- Static methods are commonly used for utility functions or operations that don’t require access to instance-specific data.
How do you create and use an array in Java?
- an array is a fixed-size, ordered collection of elements of the same type.
- To create an array, you specify the element type and the size of the array.
For Example:
int[] numbers = new int[5];
or
int[] numbers = {1, 2, 3, 4, 5};
Method Overloading
- the ability to have multiple methods in the same class with the same name but different parameters.
- The methods are distinguished by their parameter types, number, or order.
Method Overriding
- when a subclass provides a specific implementation of a method that is already defined in its superclass.
- The overridden method in the subclass must have the same:
- name,
- return type,
- and parameters as the method in the superclass.
How do you handle exceptions in Java?
- The try-catch-finally block is used to handle exceptions.
- The code that might throw an exception is placed in the try block, and the corresponding exception handlers are placed in catch blocks.
- If an exception occurs in the try block, the catch block that matches the type of the thrown exception is executed.
- The finally block, if present, is executed regardless of whether an exception occurred or not and is commonly used for cleanup tasks.
Checked Exceptions
-
exceptions that must be declared
- in the method signature using the “throws” keyword
- or caught using a try-catch block.
- They represent expected error conditions that the calling code must handle explicitly.
- checked exceptions are subclasses of Exception (excluding RuntimeException and its subclasses).
Unchecked Exceptions
- do not require explicit handling or declaration.
- They usually represent unexpected runtime errors or programming mistakes.
- Unchecked exceptions are subclasses of RuntimeException and its subclasses
abstract classes
- An abstract class can have both:
- method declarations (without implementation)
- and concrete methods,
- and it can be extended by other classes using the “extends” keyword
interface
- an interface only contains method declarations,
- and a class implements an interface using the “implements” keyword.
How many interfaces can a class implement?
multiple
How many abstract classes can a class extend?
one
What is a constructor?
- A constructor in Java is a special method that is used to initialize objects of a class.
- It has the same name as the class
- it does not have a return type.
- and use the “new” keyword to create an instance of the class.
For Example:
public class Person { private String name; public Person(String name) { this.name = name; } } Person person = new Person("John");
What is the purpose of the “final” keyword in Java?
- In Java, the “final” keyword is used to restrict certain entities from being modified.
- it makes a variable a constant.
- it prevents the method from being overridden in a subclass.
- It prevents the class from being subclassed.
What happens when you use the new keyword to create a string object (i.e. String str = new String(“Hello”))?
- a new string object is created on the heap, (i.e. it doesn’t reference the same pointer in memory when you assign the same string value to a new variable).
- and memory is allocated to store the characters “Hello”.