remove mkdocs specific syntax

This commit is contained in:
Marcello 2024-06-16 19:14:59 +02:00
parent 8d08c1964f
commit 8026e1465b
Signed by: m-lamonaca
SSH key fingerprint: SHA256:8db8uii6Gweq7TbKixFBioW2T8CbgtyFETyYL3cr3zk
77 changed files with 1128 additions and 1128 deletions

View file

@ -1,6 +1,6 @@
# Java
```java linenums="1"
```java
//single line comment
/* multi line comment */
/** javaDoc docstring */
@ -19,7 +19,7 @@ Package definition: `package <package_location>;`
### Main Method (entry point of algorithm)
```java linenums="1"
```java
public static void main (String[] args) {
//code here
}
@ -31,21 +31,21 @@ public static void main (String[] args) {
### Constant Definition (outside of main method/function)
```java linenums="1"
```java
public static final Type CONSTANT_NAME = value;
public static final double PI = 3.14159; //example
```
### Constant Definition (inside main method/function)
```java linenums="1"
```java
final Type CONSTANT_NAME = value;
final double PI = 3.14159; //example
```
### Screen Output
```java linenums="1"
```java
System.out.println(output_1 + _ + output_n); //newline at every invocation
System.out.print(output_1 + _ + output_n);
```
@ -54,7 +54,7 @@ System.out.print(output_1 + _ + output_n);
[String.format() Examples](https://dzone.com/articles/java-string-format-examples)
```java linenums="1"
```java
System.out.printf("string %..", variable);
System.out.println(String.format(format, args));
```
@ -65,7 +65,7 @@ Methods inherited from C. The value pf the variable substitutes %.
`NumberFormat` class is used to format a number output.
```java linenums="1"
```java
Locale locale = new Locale("language", "country"); // as defined by IETF lang tag, RCF 5646, RCF 4647
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale); // format a number as a currency based on a Locale
fmt.format(number); // apply format to a number, returns a String
@ -73,7 +73,7 @@ fmt.format(number); // apply format to a number, returns a String
## Keyboard Input
```java linenums="1"
```java
import java.util.Scanner; //package import
Scanner scanner = new Scanner(System.in); //Scanner obj init
scanner.useDelimiter("delimitatore"); //delimiter setting
@ -92,7 +92,7 @@ Thus when switching to a different input method is necessary to call `nextLine()
### Primitive Types
```java linenums="1"
```java
TYPE WRAPPER SIZE MIN_VALUE MAX_VALUE
int Integer -2147483648 2147483647
byte Byte 8 bit -128 127
@ -115,7 +115,7 @@ For high precision calcs is best to use `BigDecimal`.
### Type Conversion (casting) & Type checking
```java linenums="1"
```java
Type variable = (Type) <expression>; // convert to other Type
var instanceof Type; // true if var is an instance of Type
```
@ -125,7 +125,7 @@ var instanceof Type; // true if var is an instance of Type
Every primitive type has a corresponding wrapper class.
Wrapper classes permits the creation of an object with the same type of a primitive type but with added methods and constants.
```java linenums="1"
```java
WrapperClass objectName = new WrapperClass(primitiveValue); //declaration
WrapperClass objectName = primitiveValue; //shortened declaration
@ -143,7 +143,7 @@ WrapperClass.toString(primitive); // converts the wrapper class value to a stri
### String & Char
```java linenums="1"
```java
String string = "text"; //strings always in double quotes
char character = 'C'; //chars always in single quotes
```
@ -172,7 +172,7 @@ String are immutable. Concatenation creates a new string.
### String Conversion to Number
```java linenums="1"
```java
double d = Double.parseDouble(string);
float f = Float.parseFloat(string);
int i = integer.parseInt(string);
@ -180,7 +180,7 @@ int i = integer.parseInt(string);
### String Class Methods
```java linenums="1"
```java
string.equals(otherString); // returns TRUE if the strings are equal
string.equalsIgnoreCase(otherString); // returns TRUE if the strings are equals ignoring the case
string.charAt(index); // returns the character at position INDEX
@ -211,7 +211,7 @@ To compare in alphabetical order both strings must have the same case.
### Mathematical Operations
```java linenums="1"
```java
Math.PI // value of pi
Math.E // value of e
@ -317,7 +317,7 @@ Full evaluation can be forced using `&` and `|`.
### `If Else`
```java linenums="1"
```java
if (condition) {
//code here
} else {
@ -327,7 +327,7 @@ if (condition) {
### `If, Else If, Else`
```java linenums="1"
```java
if (condition) {
//code here
} else if (condition) {
@ -346,7 +346,7 @@ if condition is `true` executes instruction1 otherwise executes instruction2.
### `Switch`
```java linenums="1"
```java
switch (matchExpression) {
case matchingPattern:
//code here
@ -366,7 +366,7 @@ Omitting the `break` keyword causes multiple branches to execute the same code.
### `While` Loop
```java linenums="1"
```java
while (condition) {
//code here
}
@ -374,7 +374,7 @@ while (condition) {
### `Do While` Loop
```java linenums="1"
```java
do {
//code here
} while (espressione_booleana);
@ -384,7 +384,7 @@ Loop body executed *at least* one time
### `For` Loop
```java linenums="1"
```java
for (initializer; condition; iterator) {
//code here
}
@ -392,7 +392,7 @@ for (initializer; condition; iterator) {
### `Foreach` Loop
```java linenums="1"
```java
for (Type variable : iterable){
//code here
}
@ -400,7 +400,7 @@ for (Type variable : iterable){
### Multiple iterators in `For` Loop
```java linenums="1"
```java
for (initializer1, initializer2; condition; iterator1, iterator2) {
//code here
}
@ -417,7 +417,7 @@ The iterator declared in the for is a local variable and can be used only in the
If the assertion check is enabled (`java -ebableassetrions programName`) the execution of the algorithm is terminated if an assertion fails.
Assertions can be used to check if a variable has a wanted value in a precise point in the code (Sanity Check).
```java linenums="1"
```java
assert <booleanExpression>;
```
@ -427,7 +427,7 @@ Static methods are not bound to an *instance* of a class but they act on the cla
### Static Void Method Definition
```java linenums="1"
```java
static void methodName (parameters) {
//code here
}
@ -435,7 +435,7 @@ static void methodName (parameters) {
### Static Method Definition
```java linenums="1"
```java
static tipo_metodo methodName (parameters) {
//code here
return <espressione>; //returned type MUST match method type
@ -444,7 +444,7 @@ static tipo_metodo methodName (parameters) {
### Static Method Invocation
```java linenums="1"
```java
methodName(arguments);
ClassName.methodName(arguments); //if method is used outside its class
```
@ -453,7 +453,7 @@ ClassName.methodName(arguments); //if method is used outside its class
## Array Declaration
```java linenums="1"
```java
Type[] arrayName = new Type[dimension];
Type arrayName[] = new Type[dimension];
@ -463,7 +463,7 @@ arrayName.length //length of the array
Its possible to break the declaration in two lines
```java linenums="1"
```java
Type[] arrayName;
arrayType = new Type[dimension];
```
@ -475,7 +475,7 @@ Array dimension is determined by the number of values.
### Arrays as method parameters
```java linenums="1"
```java
static Type methodName (Type[] arrayName) {
//code here
}
@ -492,7 +492,7 @@ Array contents must be confronted by looping through the array.
### Methods returning Arrays
```java linenums="1"
```java
static Type[] methodName (parameters) {
Type[] arrayName = new Type[dimension]; //array declaration
//array valorization
@ -502,7 +502,7 @@ static Type[] methodName (parameters) {
## Variable numbers of parameters in a method
```java linenums="1"
```java
static Type methodName (parameters, tipo[] ArrayName) {
//code here
}
@ -512,14 +512,14 @@ It's not necessary to specify a dimension of the array, it's determined by Java
### Multi-Dimensional Arrays
```java linenums="1"
```java
Type[]...[] arrayName = new Type[dimension1]...[dimensionN];
Type arrayName[]...[] = new Type[dimension1]...[dimensionN];
```
### Multi-Dimensional Arrays as parameters
```java linenums="1"
```java
static Type methodName (Type[]...[] ArrayName) {
//code here
}
@ -527,7 +527,7 @@ static Type methodName (Type[]...[] ArrayName) {
### Methods returning multi-dimensional arrays
```java linenums="1"
```java
static Type[]...[] methodName (parameters) {
Type[]...[] array = new Type[dimension1]...[dimensionN];
//array valorization
@ -537,14 +537,14 @@ static Type[]...[] methodName (parameters) {
### Array Length of multi-dimensional arrays
```java linenums="1"
```java
array.length //row length
array[rowIndex].length //column length
```
### Irregular Table Visualization
```java linenums="1"
```java
static void viewTable (Type[][] matrix){
for (int row = 0; row < matrix.length; row++){ //run through the rows
for (int column = 0; column < arrayName[row].length; column++){ //run through the columns
@ -571,7 +571,7 @@ Unchecked exceptions usually mean that there is an error in the logic of the pro
This construct permits to monitor what happens in a block of code and to specify what to do in case of errors (*exceptions*).
```java linenums="1"
```java
try {
//monitored code
} catch (SpecificException e) {
@ -590,7 +590,7 @@ A `try-catch` construct can handle multiple exceptions at once. Every `catch` i
### Try with Resources
```java linenums="1"
```java
try (
//resource definition
){
@ -607,7 +607,7 @@ try (
The `throw` keyword is used to generate a custom exception in a point of the code.
`throw` is used together with an exception type.
```java linenums="1"
```java
Type methodName(parameters) {
if (condition) {
throw new Exception("error message");
@ -618,7 +618,7 @@ Type methodName(parameters) {
The `throws` keyword is used to indicate what exception Type may be thrown by a method.
`throws` is used together with a exception class. It's used to send the exception to the method caller.
```java linenums="1"
```java
Type methodName(parameters) throws ExceptionClass {
if (condition) {
throw new SpecificException("error message");
@ -630,7 +630,7 @@ Type methodName(parameters) throws ExceptionClass {
A user-defined exception has to inherit from `Exception` or one of it's descendants.
```java linenums="1"
```java
public class CustomException extends Exception {
public CustomException(){
@ -659,7 +659,7 @@ If not specified variables, methods and classes are *only* accessible from the s
### Instance Method Definition
```java linenums="1"
```java
Type methodName (parameters) {
//code here
}
@ -667,7 +667,7 @@ Type methodName (parameters) {
### `Void` Instance Method
```java linenums="1"
```java
void methodName (parameters) {
//code here
}
@ -675,7 +675,7 @@ void methodName (parameters) {
### Class Definition
```java linenums="1"
```java
public class ClassName {
//instance variables declaration
@ -711,7 +711,7 @@ Constructors are special methods that are invoked with the `new` operator when a
Constructors assign the starting values of the object attributes.
If a constructor id defined Java doesn't create the default constructor.
```java linenums="1"
```java
class ClassName (){
//attributes declaration (aka instance variables)
@ -736,7 +736,7 @@ Static variable do not belong to the class objects.
### Getters & Setter Methods
```java linenums="1"
```java
public void setAttribute(Type attribute){
this.attribute = attribute;
}
@ -750,7 +750,7 @@ public Type getAttribute(){
Automatically returns a string if the object is directly called in a print method.
```java linenums="1"
```java
@Override
Public String toString(){
return "string-representation-of-object"
@ -777,7 +777,7 @@ A child class can inherit from *only* one parent class.
Child class **must** implement a constructor that instantiates the parent (super) class.
`super()` instantiates the superclass of the child class.
```java linenums="1"
```java
class ChildClass extends ParentClass{
public ChildClass(parentParameters, childParameters){
@ -800,7 +800,7 @@ An overridden method can change the access modifier as long as the new modifier
A `ParentClass` type can contain a `ChildClass` object. This is useful for using collections and arrays of objects.
```java linenums="1"
```java
ParentClass objectName = ChildClass(); // upcast
(ChildClass)ParentClassObject; // downcast
```
@ -814,7 +814,7 @@ Abstract classes are marked by the `abstract` keyword.
An abstract method is a method without implementation.
The methods **must** be `public` and marked with the `abstract` keyword.
```java linenums="1"
```java
//abstract class
abstract class className{
//attributes here
@ -838,7 +838,7 @@ Interfaces' attributes are always `public static final`, no need for the keyword
A class can implement *more than one* Interface.
```java linenums="1"
```java
public interface InterfaceName{
//attributes here
@ -869,7 +869,7 @@ Enums are used to restrict the type of data to a set of the possible constant va
Enums are classes which constructor is private by default.
It's still possible to create a custom constructor to add values.
```java linenums="1"
```java
enum enumName {
value1,
value2,
@ -904,7 +904,7 @@ enumName variable; //creation of a variable of type enumName
*Anonymous classes* make the code more concise. They enable to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Useful if is needed a local class that is used once.
```java linenums="1"
```java
AnonymousClass objectName = new AnonymousClass(Type parameter, ...) {
// attributes
@ -915,7 +915,7 @@ AnonymousClass objectName = new AnonymousClass(Type parameter, ...) {
### Cloning
```java linenums="1"
```java
class ClassName implements Clonable {
}
@ -923,7 +923,7 @@ class ClassName implements Clonable {
## Generics
```java linenums="1"
```java
// WARNING: T is not instantiable, new t(), new t[] are INVALID
public class GenericClass<T> {
private T generic;
@ -949,7 +949,7 @@ GenericClass<Type>[] obj = new GenericClass<>[]; // invalid
### Multiple Generics
```java linenums="1"
```java
public class GenericClass<T1, T2, ...> { } // number of generic types is not limited
```
@ -957,7 +957,7 @@ public class GenericClass<T1, T2, ...> { } // number of generic types is not li
Specify an interface or class that the generic type must implement/inherit.
```java linenums="1"
```java
public class GenericClass<T extends Interface1 & Interface2> { }
public class GenericClass<T1 extends Interface1 & Interface2, T2 extends Class1> { }
public class GenericClass<T extends Class1> { }
@ -965,7 +965,7 @@ public class GenericClass<T extends Class1> { }
### Generic Methods
```java linenums="1"
```java
public class ClassName{
public <T> methodName() {
@ -1001,7 +1001,7 @@ obj.<Type>methodName(); // generic method call
#### Writing on a file
```java linenums="1"
```java
// opening/creating the file for writing
PrintWriter outStream = null; // output stream creation
try{
@ -1022,7 +1022,7 @@ outStream.close() // close stream and write buffer contents.
#### Reading from a file
```java linenums="1"
```java
Filereader filereader = new Filereader("filename"); //open the file
Scanner scanner = new Scanner(filereader); //scanner for the file
@ -1058,7 +1058,7 @@ public void close() throws IOException // closes the stream
The File class is an abstraction of the file and it's path. The abstraction is independent from the OS.
```java linenums="1"
```java
File("path/to/file") // UNIX like path
File("path\\to\\file") // Windows path
@ -1075,7 +1075,7 @@ file.length() // file length in bytes
#### Writing to a binary file
```java linenums="1"
```java
ObjectOutputStream outStream;
try {
outStream = new ObjectOutputStream(new FileOutputSteam(filename));
@ -1104,7 +1104,7 @@ public void close()
#### Reading from a binary file
```java linenums="1"
```java
ObjectInputStream inStream;
try {
inStream = new ObjectInputStream(new FileinputSteam(filename));
@ -1148,7 +1148,7 @@ Needed for a class to be *serializable*:
An array is serializable if it's base type is a serializable object.
```java linenums="1"
```java
SerializableObject[] array = (SerializableObject[])inStream.readObject(); // read returns Object, cast needed
```
@ -1160,7 +1160,7 @@ Functional interfaces provide target types for *lambda expressions*.
General purpose `@functionalInterfaces`:
```java linenums="1"
```java
// takes input, performs actions, return boolean
public interface Predicate<T> {
boolean test(T t);
@ -1215,7 +1215,7 @@ The features of Java stream are:
Usable only by a `@FunctionalInterface`'s method or a method of a *stream*.
```java linenums="1"
```java
lambda operator -> body;
//zero parameter