Java Summary

studied byStudied by 13 people
5.0(1)
Get a hint
Hint

Java

1 / 141

encourage image

There's no tags or description

Looks like no one added any tags here yet for you.

142 Terms

1

Java

An object-oriented programming language that uses objects and classes to organize and perform tasks.

New cards
2

James Gosling

Java was developed by James Gosling at Sun Microsystems, which is now owned by Oracle Corporation.

New cards
3

Platform-Independence

Java's Platform-Independence: Java code can run on any machine that has a Java Virtual Machine (JVM) installed, which makes it platform-independent.

New cards
4

Enterprise, Web & Android

Java's Applications: Java is widely used in enterprise applications, web development, and Android app development.

New cards
5

Syntax

Java syntax is similar to C and C++. Programs are written in plain text files with the ‘.java‘ extension.

New cards
6

Variables

Variables store data and have a type (e.g., ‘int‘, ‘float‘, ‘double‘, ‘boolean‘, ‘char‘, ‘String‘).

New cards
7

Operators

Java supports arithmetic (‘+’, ‘-’, ‘\*’, ‘/’, ‘%’), comparison (‘==’, ‘! =’, ‘>’, ‘≥’, ‘<’, ‘>=’) and logical (‘&&’, ‘||’, ‘!’) operators.
New cards
8

Classes

A class is a blueprint for creating objects. It defines the properties (attributes) and behaviors (methods) that an object can have.

New cards
9

Objects

An object is an instance of a class. It has its own state (attributes) and can perform actions (methods).

New cards
10

Methods

A method is a function defined inside a class that performs a specific task. Methods can have parameters and a return type.

New cards
11

If-else statement

Used to make decisions based on a condition. The code in the ‘if’ block will execute if the condition is true, otherwise the code in the ‘else’ block will execute.

New cards
12

Switch Statement

Allows the selection of multiple branches of code execution based on the value of a variable or expression.

New cards
13

For loop

Used to repeatedly execute a block of code a specific number of times.

New cards
14

While

Executes a block of code as long as a condition is true

New cards
15

Do-while loop

Executes a block of code at least once, and then repeats as long as a condition is true

New cards
16

Arrays

A collection of elements of the same data type, stored in contiguous memory allocations

New cards
17

Arrays Declaration

Arrays can be declared using square brackets, e.g., ‘int[] myArray;‘.

New cards
18

Arrays Initialization

Arrays can be initialized using the ‘new‘ keyword, e.g., ‘myArray = new int[5];‘, or with an initializer list, e.g., ‘int[] myArray = {1, 2, 3, 4, 5}

New cards
19

Arrays Accessing elements

Array elements can be accessed using indices, e.g., ‘myArray[0]‘ to access the first element.

New cards
20

Arrays Length

The length of an array can be obtained using the ‘.length‘ attribute, e.g., int arrayLength = myArray.length;

New cards
21

Arrays Multidimensional Arrays

Arrays can have multiple dimensions, e.g., ‘int[][] matrix = new int[3][4];‘ creates a 3x4 matrix

New cards
22

Standard output

‘System.out.println()‘ is used to print messages to the console with a newline character, while ‘System.out.print()‘ prints without a newline.

New cards
23

Standard input

The ‘Scanner‘ class from ‘java.util‘ package can be used to read user input from the console, e.g., ‘Scanner scanner = new Scanner(System.in);‘.

New cards
24

Reading data

‘Scanner‘ provides various methods to read different data types, e.g., ‘nextInt()‘, ‘nextFloat()‘, ‘nextLine()‘, and ‘next()‘.

New cards
25

Closing the scanner

It is important to close the ‘Scanner‘ object after use to prevent resource leaks, e.g., ‘scanner.close();‘.

New cards
26

File class

The ‘File‘ class from the ‘java.io‘ package represents a file or directory in the file system.

New cards
27

Reading from a file

The ‘FileReader‘ and ‘BufferedReader‘ classes can be used to read text from a file. The ‘BufferedReader‘ provides efficient reading with a buffer.

New cards
28

Writing to a file

The ‘FileWriter‘ and ‘BufferedWriter‘ classes can be used to write text to a file. The ‘BufferedWriter‘ provides efficient writing with a buffer

New cards
29

Handling exceptions

File I/O operations can throw exceptions, e.g., ‘FileNotFoundException‘ or ‘IOException‘. Use a ‘try‘-‘catch‘ block to handle these exceptions.

New cards
30

Closing resources

Always close file I/O resources in a ‘finally‘ block or use the try-with-resources statement, which automatically closes resources when the block exits.

New cards
31

Class Constructors

Constructors are special methods used to initialize objects. They have the same name as the class and do not have a return type, e.g., ‘public MyClass() { }‘.

New cards
32

Classes Inheritance

Classes can inherit properties and methods from other classes using the ‘extends‘ keyword, e.g., ‘class Subclass extends Superclass { }‘.

New cards
33

Objects Instantiation

Objects can be created using the ‘new‘ keyword followed by the constructor, e.g., ‘MyClass myObject = new MyClass();‘.

New cards
34

Objects Accessing Fields

Fields of an object can be accessed using the dot (‘.‘) notation, e.g., ‘myObject.myField‘

New cards
35

Objects Accessing Methods

Methods of an object can be called using the dot (‘.‘) notation, e.g., ‘myObject.myMethod();‘.

New cards
36

Method declaration

Methods of an object can be called using the dot (‘.‘) notation, e.g., ‘myObject.myMethod();‘.

New cards
37

Method Parameters

Methods can have parameters, which are specified inside the parentheses as a commaseparated list of data types and variable names, e.g., ‘public void myMethod(int x, String y) { }‘.

New cards
38

Method Return Statement

Methods with a return type other than ‘void‘ must include a ‘return‘ statement followed by an expression that matches the return type, e.g., ‘return x + y;‘.

New cards
39

Fields

Fields are variables declared inside a class. They represent the state or the properties of an object

New cards
40

Fields Declaration

Fields are declared with an access modifier (e.g., ‘public‘, ‘private‘), a data type, and a variable name, e.g., ‘private int myField;‘.

New cards
41

Fields Inialization

Fields can be initialized during declaration or inside a constructor, e.g., ‘private int myField = 42;‘ or ‘public MyClass() { myField = 42; }‘.

New cards
42

Fields Access Modifiers

Fields can have different access levels, which control their visibility and accessibility:

  • ‘public‘: The field is accessible from any class.

  • ‘private‘: The field is accessible only within the class itself.

  • protected: The field is accessible within the class, its subclasses, and classes in the same package.

  • Default (no modifier): The field is accessible within the class and other classes in the same package.

New cards
43

Static Fields

Fields can be declared as static, which means they belong to the class itself rather than to individual objects. Static fields are shared among all instances of the class, e.g., private static int myStaticField;.

New cards
44

Command Line Arguments

Command line arguments are inputs passed to a Java program when it is executed from the command line or terminal.

New cards
45

Command Line Arguments Accessing Arguments

Command line arguments are passed as an array of ‘String‘ objects to the ‘main‘ method of the program, e.g., ‘public static void main(String[] args) { }‘.

New cards
46

Command Line Argument Example

To access the first command line argument, use ‘args[0]‘. To iterate over all arguments, use a ‘for‘ loop or an enhanced ‘for‘ loop (also known as a ”for-each” loop).

New cards
47

Packages

Packages are used to group related classes and interfaces in Java. They help in organizing the code and avoiding naming conflicts.

New cards
48

Packages Declaration

Packages are declared at the beginning of a Java source file using the ‘package‘ keyword followed by the package name, e.g., ‘package com.mycompany.myproject;‘.

New cards
49

Packages Importing Classes

To use a class from another package, you can either use its fully qualified name, e.g., ‘java.util.ArrayList‘, or import the class using the ‘import‘ keyword, e.g., ‘import java.util.ArrayList;‘.

New cards
50

Default Package

If no package is specified, the class belongs to the default (unnamed) package. It is generally not recommended to use the default package for anything other than simple experiments and tests.

New cards
51

JavaDocs

a standard way to generate documentation for Java classes, methods, and fields using specially formatted comments.

New cards
52

JavaDoc comments

written using a ‘/** ... */‘ notation and are placed immediately before the class, method, or field they document, e.g.,

Listing 1: JavaDoc Example

/∗ ∗

∗This method adds two integer.

∗ @param a t h e f i r s t i n t e g e r

∗ @param b t h e second i n t e g e r

∗ @re turn t h e sum o f a and b

∗/

public int add ( int a , int b ) { }

New cards
53

JavaDoc tags

JavaDoc comments can include tags such as ‘@param‘, ‘@return‘, ‘@throws‘, ‘@author‘, and ‘@since‘ to provide more information about the code element being documented.

New cards
54

Generating JavaDocs

JavaDocs can be generated using the ‘javadoc‘ command-line tool or through integrated development environments (IDEs) like IntelliJ IDEA or Eclipse.

New cards
55

Inheritance

allows a class to inherit properties (fields) and behaviors (methods) from another class.

New cards
56

Superclass and subclass

The class that is being inherited from is called the superclass, and the class that inherits from the superclass is called the subclass.

New cards
57

extends

Inheritance is implemented in Java using the ‘extends‘ keyword, e.g., ‘class Subclass extends Superclass { }‘.

New cards
58

Overriding methods

A subclass can override a method of the superclass by providing a new implementation with the same method signature and the java @Override tag.

New cards
59

Super

The ‘super‘ keyword can be used to call a method or constructor of the superclass from within the subclass, e.g., ‘super.myMethod();‘ or ‘super();‘.

New cards
60

Polymorphism

s the ability of an object to take on different forms or behaviors depending on the context. In Java, polymorphism is implemented through method overriding and interfaces.

New cards
61

Subtype polymorphism

Also known as dynamic method dispatch or runtime polymorphism, allows objects of a subclass to be treated as objects of the superclass. This enables a single interface to be used for a general class of actions, making the code more flexible and extensible.

New cards
62

Method overriding (Polymorphism)

When a subclass provides a new implementation for a method of the superclass, it overrides the method. At runtime, the appropriate method implementation is called based on the actual type of the object.

New cards
63

Dynamic Dispatch

the process by which the Java Virtual Machine (JVM) selects the appropriate method implementation to call at runtime, based on the actual type of the object.

New cards
64

Method Overriding(Dynamic Dispatch)

Dynamic dispatch is used when a method is overridden in a subclass. The JVM determines the correct method to call based on the object’s type at runtime, even if the reference type is that of the superclass.

New cards
65

Virtual methods

In Java, all non-static and non-private methods are considered virtual by default, which means they can be overridden in subclasses and participate in dynamic dispatch.

New cards
66

Abstract Classes

e classes that cannot be instantiated and are meant to be subclassed. They can contain both abstract and non-abstract methods.

New cards
67

Abstract Class Declaration

Abstract classes are declared using the ‘abstract‘ keyword, e.g., ‘abstract class MyAbstractClass { }‘.

New cards
68

Abstract Methods

Abstract methods are methods without a body that must be implemented by any concrete (non-abstract) subclass. They are declared using the ‘abstract‘ keyword, e.g., ‘public abstract void myMethod();‘.

New cards
69

Interfaces

a collection of abstract methods (and optionally default and static methods) that define a contract or set of behaviors that implementing classes must adhere to.

New cards
70

Interfaces Declaration

Interfaces are declared using the ‘interface‘ keyword, e.g., ‘interface MyInterface { }‘.

New cards
71

Implementing interfaces

A class can implement an interface using the ‘implements‘ keyword, e.g., ‘class MyClass implements MyInterface { }‘. The class must provide an implementation for all abstract methods in the interface.

New cards
72

Multiple inheritance

Java does not support multiple inheritance of classes, but a class can implement multiple interfaces, e.g., ‘class MyClass implements InterfaceA, InterfaceB { }‘.

New cards
73

Default Methods

Interfaces can provide default method implementations using the ‘default‘ keyword. This allows new methods to be added to interfaces without breaking existing implementations.

New cards
74

Enums

a special type of class that represents a fixed set of named constants. They are useful for representing a group of related values, such as days of the week or colors.

New cards
75

Enums Declaration

declared using the ‘enum‘ keyword, followed by the enum name and a list of named constants, e.g., ‘enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }‘.

New cards
76

Enum Methods

Enums automatically inherit several useful methods, such as ‘values()‘, which returns an array of the enum’s constants, and ‘valueOf(String)‘, which returns the enum constant with the specified name.

New cards
77

Enum Custom Behaviors

Enums can have constructors, fields, and methods, allowing them to have custom behavior associated with each constant.

New cards
78

Exceptions

e events that occur during the execution of a program, signaling an abnormal condition or error. Java provides a built-in mechanism for handling exceptions using try-catchfinally blocks and the ‘throw‘ keyword.

New cards
79

Try-catch-finally blocks

To catch and handle exceptions, surround the code that may throw an exception with a try block, followed by one or more catch blocks to handle different types of exceptions, and an optional finally block for cleanup code, e.g.,

Listing 2: Try-Catch-Finally Example

try {

// code t h a t m igh t throw an e x c e p t i o n

} catch ( IOException e ) {

// h a n dle IOExcep t ion

} catch ( Excep ti on e ) {

// h a n dle o t h e r e x c e p t i o n s

} f i n a l l y {

// cle a n u p code t h a t alw ay s runs

}

New cards
80

Throwing Exceptions

You can throw an exception using the ‘throw‘ keyword followed by an instance of the exception class, e.g., ‘throw new IllegalArgumentException(”Invalid argument”);‘.

New cards
81

Custom Exceptions

You can create custom exception classes by extending the ‘Exception‘ class or one of its subclasses.

New cards
82

Unit Testing

Unit testing is the process of testing individual units or components of a software application to ensure they work correctly. In Java, unit tests are typically written using testing frameworks like JUnit or TestNG.

New cards
83

JUnit

a widely-used testing framework for Java applications. It provides annotations, such as ‘@Test‘, ‘@Before‘, and ‘@After‘, to define test methods and setup/teardown methods.

New cards
84

Test Methods

s are annotated with ‘@Test‘ and typically follow the Arrange-Act-Assert pattern: arrange the objects and inputs, act by calling the method under test, and assert that the expected outcome occurred

New cards
85

Setup and teardown

Setup methods, annotated with ‘@Before‘, run before each test method, and teardown methods, annotated with ‘@After‘, run after each test method. These methods are useful for initializing and cleaning up resources needed for the tests.

New cards
86

Assertions

statements that check whether a condition is true, such as ‘assertEquals(expected, actual)‘, ‘assertTrue(condition)‘, or ‘assertFalse(condition)‘. If an assertion fails, the test fails, and an error is reported.

New cards
87

Generics

a language feature in Java that allows you to create classes, interfaces, and methods that work with different types while maintaining type safety. They are often used to create generic containers, like collections, that can store any type of object.

New cards
88

Type parameters

placeholders for actual types, represented by uppercase single-letter names, such as ‘T‘ for a generic type, ‘E‘ for elements in a collection, and ‘K‘ and ‘V‘ for keys and values in a map

New cards
89

Generic classes

include the type parameter in angle brackets after the class name, e.g., ‘class MyGenericClass< T > { }‘. You can then use the type parameter as a type within the class, e.g., ‘private T myField;‘.

New cards
90

Generic Methods

Methods can also have their own type parameters, which are declared before the return type, e.g., ‘ T myGenericMethod(T input) { }‘.

New cards
91

Bounded Type Parameters

You can restrict the types that can be used as type arguments by specifying an upper bound using the ‘extends‘ keyword, e.g., ‘class MyGenericClass { }‘.

New cards
92

Using Generic Classes

To use a generic class or method, provide the actual type argument in angle brackets when declaring a variable or calling a method, e.g., ‘MyGenericClass myInstance = new MyGenericClass<>();‘.

New cards
93

Type Ensure

Generics are implemented in Java using type erasure, which means that the generic type information is removed at runtime. This allows for backward compatibility with older versions of Java but can lead to some limitations, such as not being able to create an array of a generic type.

New cards
94

Collections

a group of Java classes and interfaces that provide a framework for storing and manipulating groups of objects. The Java Collections Framework is part of the ‘java.util‘ package.

New cards
95

Main interface

The primary interfaces in the Java Collections Framework are ‘Collection‘, ‘List‘, ‘Set‘, ‘Queue‘, and ‘Map‘. Each interface represents a different way of organizing and storing data.

New cards
96

Collections Common Operations

The ‘Collection‘ interface defines common operations for working with collections, such as ‘add‘, ‘remove‘, ‘contains‘, ‘size‘, and ‘isEmpty‘.

New cards
97

Lists

are ordered collections that can contain duplicate elements. Elements in a list can be accessed by their index.

New cards
98

ArrayList

a resizable array implementation of the ‘List‘ interface. It provides fast random access to elements but can be slow when inserting or removing elements in the middle of the list.

New cards
99

LinkedList

is a doubly-linked list implementation of the ‘List‘ and ‘Deque‘ interfaces. It provides fast insertion and removal of elements at the beginning and end of the list but can be slow when accessing elements by index

New cards
100

List Operations

Lists support additional operations, such as ‘get‘, ‘set‘, ‘indexOf‘, and ‘lastIndexOf‘.

New cards

Explore top notes

note Note
studied byStudied by 12 people
... ago
5.0(1)
note Note
studied byStudied by 6 people
... ago
5.0(1)
note Note
studied byStudied by 107 people
... ago
5.0(1)
note Note
studied byStudied by 15 people
... ago
5.0(1)
note Note
studied byStudied by 6 people
... ago
5.0(1)
note Note
studied byStudied by 15 people
... ago
5.0(1)
note Note
studied byStudied by 30 people
... ago
5.0(1)
note Note
studied byStudied by 5051 people
... ago
5.0(31)

Explore top flashcards

flashcards Flashcard (21)
studied byStudied by 9 people
... ago
5.0(1)
flashcards Flashcard (168)
studied byStudied by 1 person
... ago
5.0(1)
flashcards Flashcard (78)
studied byStudied by 8 people
... ago
5.0(1)
flashcards Flashcard (113)
studied byStudied by 15 people
... ago
5.0(2)
flashcards Flashcard (31)
studied byStudied by 1 person
... ago
5.0(1)
flashcards Flashcard (111)
studied byStudied by 5 people
... ago
5.0(1)
flashcards Flashcard (30)
studied byStudied by 97 people
... ago
5.0(1)
flashcards Flashcard (162)
studied byStudied by 14 people
... ago
5.0(1)
robot