mirror of
https://gitlab.com/djdietrick/docs
synced 2026-05-03 00:20:54 -04:00
Began java section
This commit is contained in:
9
docs/java/basics/classes.md
Normal file
9
docs/java/basics/classes.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# Classes
|
||||
|
||||
## Generics
|
||||
|
||||
Generics allow you to create a class that can hold many different kinds of data types, and specify an upper bound for what kind of interfaces are allowed to be stored and manipulated.
|
||||
|
||||
```java
|
||||
|
||||
```
|
||||
36
docs/java/basics/interfaces.md
Normal file
36
docs/java/basics/interfaces.md
Normal file
@@ -0,0 +1,36 @@
|
||||
# Interfaces
|
||||
|
||||
## Common Interfaces to Implement
|
||||
|
||||
### Comparator
|
||||
|
||||
Compares two instances of a class.
|
||||
|
||||
```java
|
||||
class StudentGPAComparator implements Comparator<Student> {
|
||||
@Override
|
||||
public int compare(Student o1, Student o2) {
|
||||
return (o1.gpa + o1.name).compareTo(o2.gpa + o2.name);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main() {
|
||||
Comparator<Student> gpaSorter = new StudentGPAComparator();
|
||||
Arrays.sort(students, gpaSorter);
|
||||
Arrays.sort(students, gpaSorter.reversed());
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Comparable
|
||||
|
||||
Similar to Comparator, except this is implemented directly on the classes you are comparing. Without specificing a type, you will receive an `Object` to your `compareTo` function.
|
||||
|
||||
```java
|
||||
class Student implements Comparable<Student> {
|
||||
@Override
|
||||
public int compareTo(Student o) {
|
||||
return this.name.compareTo(o.name);
|
||||
}
|
||||
}
|
||||
```
|
||||
1
docs/java/basics/types.md
Normal file
1
docs/java/basics/types.md
Normal file
@@ -0,0 +1 @@
|
||||
# Types
|
||||
@@ -2,8 +2,10 @@
|
||||
{
|
||||
"text": "Basics",
|
||||
"items": [
|
||||
{ "text": "Introduction", "link": "/java/" }
|
||||
{ "text": "Introduction", "link": "/java/" },
|
||||
{ "text": "Types", "link": "/java/basics/types" },
|
||||
{ "text": "Classes", "link": "/java/basics/classes" },
|
||||
{ "text": "Interfaces", "link": "/java/basics/interfaces" }
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user