diff --git a/concepts/basics/about.md b/concepts/basics/about.md index 3711d08ab..508ae41ef 100644 --- a/concepts/basics/about.md +++ b/concepts/basics/about.md @@ -1,12 +1,14 @@ # About -Java is a statically-typed language, which means that everything has a type at compile-time. Assigning a value to a name is referred to as defining a variable. +Java is a statically-typed language, which means that everything has a type at compile-time. +Assigning a value to a name is referred to as defining a variable. ```java int explicitVar = 10; // Explicitly typed ``` -The value of a variable can be assigned and updated using the [`=` operator][assignment]. Once defined, a variable's type can never change. +The value of a variable can be assigned and updated using the [`=` operator][assignment]. +Once defined, a variable's type can never change. ```java int count = 1; // Assign initial value @@ -16,7 +18,13 @@ count = 2; // Update to new value // count = false; ``` -Java is an object-oriented language and requires all functions to be defined in a _class_, which are defined using the [`class` keyword][classes]. A function within a class is referred to as a _method_. Each [method][methods] can have zero or more parameters. All parameters must be explicitly typed, there is no type inference for parameters. Similarly, the return type must also be made explicit. Values are returned from functions using the [`return` keyword][return]. To allow a method to be called by other classes, the `public` access modifier must be added. +Java is an object-oriented language and requires all functions to be defined in a _class_, which are defined using the [`class` keyword][classes]. +A function within a class is referred to as a _method_. +Each [method][methods] can have zero or more parameters. +All parameters must be explicitly typed, there is no type inference for parameters. +Similarly, the return type must also be made explicit. +Values are returned from functions using the [`return` keyword][return]. +To allow a method to be called by other classes, the `public` access modifier must be added. ```java class Calculator { @@ -32,7 +40,8 @@ Invoking a method is done by specifying its class and method name and passing ar int sum = new Calculator().add(1, 2); ``` -If a method does not use any class _state_ (which is the case in this exercise), the method can be made _static_ using the `static` modifier. Similarly, if a class only has static methods, it too can be made static using the `static` modifier. +If a method does not use any class _state_ (which is the case in this exercise), the method can be made _static_ using the `static` modifier. +Similarly, if a class only has static methods, it too can be made static using the `static` modifier. ```java static class Calculator { @@ -44,7 +53,8 @@ static class Calculator { Scope in Java is defined between the `{` and `}` characters. -Java supports two types of [comments][comments]. Single line comments are preceded by `//` and multiline comments are inserted between `/*` and `*/`. +Java supports two types of [comments][comments]. +Single line comments are preceded by `//` and multiline comments are inserted between `/*` and `*/`. Integer values are defined as one or more (consecutive) digits and support the [default mathematical operators][operators]. diff --git a/concepts/basics/introduction.md b/concepts/basics/introduction.md index c8b459037..e38461c07 100644 --- a/concepts/basics/introduction.md +++ b/concepts/basics/introduction.md @@ -1,12 +1,17 @@ # Introduction -Java is a statically-typed language, which means that the type of a variable is known at compile-time. Assigning a value to a name is referred to as defining a variable. A variable is defined by explicitly specifying its type. +Java is a statically-typed language, which means that the type of a variable is known at compile-time. +Assigning a value to a name is referred to as defining a variable. +A variable is defined by explicitly specifying its type. ```java int explicitVar = 10; ``` -Updating a variable's value is done through the `=` operator. Here, `=` does not represent mathematical equality. It simply assigns a value to a variable. Once defined, a variable's type can never change. +Updating a variable's value is done through the `=` operator. +Here, `=` does not represent mathematical equality. +It simply assigns a value to a variable. +Once defined, a variable's type can never change. ```java int count = 1; // Assign initial value @@ -16,7 +21,8 @@ count = 2; // Update to new value // count = false; ``` -Java is an [object-oriented language][object-oriented-programming] and requires all functions to be defined in a _class_. The `class` keyword is used to define a class. +Java is an [object-oriented language][object-oriented-programming] and requires all functions to be defined in a _class_. +The `class` keyword is used to define a class. ```java class Calculator { @@ -24,7 +30,12 @@ class Calculator { } ``` -A function within a class is referred to as a _method_. Each method can have zero or more parameters. All parameters must be explicitly typed, there is no type inference for parameters. Similarly, the return type must also be made explicit. Values are returned from methods using the `return` keyword. To allow a method to be called by other classes, the `public` access modifier must be added. +A function within a class is referred to as a _method_. +Each method can have zero or more parameters. +All parameters must be explicitly typed, there is no type inference for parameters. +Similarly, the return type must also be made explicit. +Values are returned from methods using the `return` keyword. +To allow a method to be called by other classes, the `public` access modifier must be added. ```java class Calculator { @@ -37,11 +48,12 @@ class Calculator { Invoking/calling a method is done by specifying its class and method name and passing arguments for each of the method's parameters. ```java -int sum = new Calculator().add(1, 2); // here the "add" method has been called to perform the task of addition +int sum = new Calculator().add(1, 2); // here the "add" method has been called to perform the task of addition ``` Scope in Java is defined between the `{` and `}` characters. -Java supports two types of comments. Single line comments are preceded by `//` and multiline comments are inserted between `/*` and `*/`. +Java supports two types of comments. +Single line comments are preceded by `//` and multiline comments are inserted between `/*` and `*/`. [object-oriented-programming]: https://docs.oracle.com/javase/tutorial/java/javaOO/index.html diff --git a/concepts/booleans/about.md b/concepts/booleans/about.md index 168d75de0..4edee1936 100644 --- a/concepts/booleans/about.md +++ b/concepts/booleans/about.md @@ -4,13 +4,14 @@ Booleans in Java are represented by the `boolean` type, which values can be eith Java supports three [boolean operators][operators]: -- `!` (NOT): negates the boolean -- `&&` (AND): takes two booleans and results in true if they're both true -- `||` (OR): results in true if any of the two booleans is true +- `!` (NOT): negates the boolean +- `&&` (AND): takes two booleans and results in true if they're both true +- `||` (OR): results in true if any of the two booleans is true The `&&` and `||` operators use _short-circuit evaluation_, which means that the right-hand side of the operator is only evaluated when needed. -These are also known as conditional or logical operators. `!` is sometimes classified as a bitwise operation in the documentation but it has the conventional _NOT_ semantics. +These are also known as conditional or logical operators. +`!` is sometimes classified as a bitwise operation in the documentation but it has the conventional _NOT_ semantics. ```java true || false // => true @@ -22,7 +23,9 @@ true && true // => true !false // => true ``` -The three boolean operators each have a different [_operator precedence_][precedence]. As a consequence, they are evaluated in this order: `not` first, `&&` second, and finally `||`. If you want to 'escape' these rules, you can enclose a boolean expression in parentheses (`()`), as the parentheses have an even higher operator precedence. +The three boolean operators each have a different [_operator precedence_][precedence]. +As a consequence, they are evaluated in this order: `not` first, `&&` second, and finally `||`. +If you want to 'escape' these rules, you can enclose a boolean expression in parentheses (`()`), as the parentheses have an even higher operator precedence. ```java !true && false // => false diff --git a/concepts/chars/about.md b/concepts/chars/about.md index b9de623d9..ff5616166 100644 --- a/concepts/chars/about.md +++ b/concepts/chars/about.md @@ -1,7 +1,6 @@ # About -`char`s are generally easy to use. They can be extracted from strings, added back -(by means of a string builder), defined and initialised using literals with single quotes, as in `char ch = 'A';` -, assigned and compared. +`char`s are generally easy to use. +They can be extracted from strings, added back (by means of a string builder), defined and initialised using literals with single quotes, as in `char ch = 'A';`, assigned and compared. The Character class encapsulates the char value. diff --git a/concepts/chars/introduction.md b/concepts/chars/introduction.md index 6a24eae39..c78f90c0a 100644 --- a/concepts/chars/introduction.md +++ b/concepts/chars/introduction.md @@ -1,17 +1,14 @@ # Introduction The Java `char` type represents the smallest addressable components of text. -Multiple `char`s can comprise a string such as `"word"` or `char`s can be -processed independently. Their literals have single quotes e.g. `'A'`. +Multiple `char`s can comprise a string such as `"word"` or `char`s can be processed independently. +Their literals have single quotes e.g. `'A'`. -Java `char`s support Unicode encoding so in addition to the Latin character set -pretty much all the writing systems in use worldwide can be represented, -e.g. the Greek letter `'β'`. +Java `char`s support Unicode encoding so in addition to the Latin character set pretty much all the writing systems in use worldwide can be represented, e.g. the Greek letter `'β'`. -There are many builtin library methods to inspect and manipulate `char`s. These -can be found as static methods of the `java.lang.Character` class. +There are many builtin library methods to inspect and manipulate `char`s. +These can be found as static methods of the `java.lang.Character` class. `char`s are sometimes used in conjunction with a `StringBuilder` object. -This object has methods that allow a string to be constructed -character by character and manipulated. At the end of the process -`toString` can be called on it to output a complete string. +This object has methods that allow a string to be constructed character by character and manipulated. +At the end of the process `toString` can be called on it to output a complete string. diff --git a/concepts/classes/about.md b/concepts/classes/about.md index 722952fec..e0a173e76 100644 --- a/concepts/classes/about.md +++ b/concepts/classes/about.md @@ -1,17 +1,20 @@ # About -The primary object-oriented construct in Java is the _class_, which is a combination of data ([_fields_][fields]), also known as instance variables, and behavior ([_methods_][methods]). The fields and methods of a class are known as its _members_. +The primary object-oriented construct in Java is the _class_, which is a combination of data ([_fields_][fields]), also known as instance variables, and behavior ([_methods_][methods]). +The fields and methods of a class are known as its _members_. Access to members can be controlled through access modifiers, the two most common ones being: - [`public`][public]: the member can be accessed by any code (no restrictions). - [`private`][private]: the member can only be accessed by code in the same class. -In Java if no access modifier is specified, the default is _package visibility_. In this case, the member is visible to all classes defined into the same package. +In Java if no access modifier is specified, the default is _package visibility_. +In this case, the member is visible to all classes defined into the same package. The above-mentioned grouping of related data and behavior plus restricting access to members is known as [_encapsulation_][encapsulation], which is one of the core object-oriented concepts. -You can think of a class as a template for creating instances of that class. To [create an instance of a class][creating-objects] (also known as an _object_), the [`new` keyword][new] is used: +You can think of a class as a template for creating instances of that class. +To [create an instance of a class][creating-objects] (also known as an _object_), the [`new` keyword][new] is used: ```java class Car { @@ -34,7 +37,9 @@ class Car { } ``` -One can optionally assign an initial value to a field. If a field does _not_ specify an initial value, it will be set to its type's [default value][default-values]. An instance's field values can be accessed and updated using dot-notation. +One can optionally assign an initial value to a field. +If a field does _not_ specify an initial value, it will be set to its type's [default value][default-values]. +An instance's field values can be accessed and updated using dot-notation. ```java class Car { @@ -53,7 +58,8 @@ newCar.year; // => 0 newCar.year = 2018; ``` -Private fields are usually updated as a side effect of calling a method. Such methods usually don't return any value, in which case the return type should be [`void`][void]: +Private fields are usually updated as a side effect of calling a method. +Such methods usually don't return any value, in which case the return type should be [`void`][void]: ```java class CarImporter { @@ -67,9 +73,11 @@ class CarImporter { } ``` -Note that is not customary to use public fields in Java classes. Private fields are typically used which are accessed through [_getters_ and _setters_][so-getters-setters]. +Note that is not customary to use public fields in Java classes. +Private fields are typically used which are accessed through [_getters_ and _setters_][so-getters-setters]. -Within a class, the [`this` keyword][this] will refer to the current class. This is especially useful if a parameter has the same name as a field: +Within a class, the [`this` keyword][this] will refer to the current class. +This is especially useful if a parameter has the same name as a field: ```java class CarImporter { diff --git a/concepts/classes/introduction.md b/concepts/classes/introduction.md index 774eefc4f..7a488babe 100644 --- a/concepts/classes/introduction.md +++ b/concepts/classes/introduction.md @@ -1,13 +1,15 @@ # Introduction -The primary object-oriented construct in Java is the _class_, which is a combination of data (_fields_) and behavior (_methods_). The fields and methods of a class are known as its _members_. +The primary object-oriented construct in Java is the _class_, which is a combination of data (_fields_) and behavior (_methods_). +The fields and methods of a class are known as its _members_. Access to members can be controlled through access modifiers, the two most common ones being: - `public`: the member can be accessed by any code (no restrictions). - `private`: the member can only be accessed by code in the same class. -You can think of a class as a template for creating instances of that class. To create an instance of a class (also known as an _object_), the `new` keyword is used: +You can think of a class as a template for creating instances of that class. +To create an instance of a class (also known as an _object_), the `new` keyword is used: ```java class Car { @@ -30,7 +32,9 @@ class Car { } ``` -One can optionally assign an initial value to a field. If a field does _not_ specify an initial value, it wll be set to its type's default value. An instance's field values can be accessed and updated using dot-notation. +One can optionally assign an initial value to a field. +If a field does _not_ specify an initial value, it wll be set to its type's default value. +An instance's field values can be accessed and updated using dot-notation. ```java class Car { @@ -49,7 +53,8 @@ newCar.year; // => 0 newCar.year = 2018; ``` -Private fields are usually updated as a side effect of calling a method. Such methods usually don't return any value, in which case the return type should be `void`: +Private fields are usually updated as a side effect of calling a method. +Such methods usually don't return any value, in which case the return type should be `void`: ```java class CarImporter { diff --git a/concepts/constructors/about.md b/concepts/constructors/about.md index 6f8be6668..0ad5b101e 100644 --- a/concepts/constructors/about.md +++ b/concepts/constructors/about.md @@ -1,6 +1,8 @@ # About -Creating an instance of a _class_ is done by calling its [_constructor_][constructors] through the [`new` operator][new]. A constructor is a special type of method whose goal is to initialize a newly created instance. Constructors look like regular methods, but without a return type and with a name that matches the class's name. +Creating an instance of a _class_ is done by calling its [_constructor_][constructors] through the [`new` operator][new]. +A constructor is a special type of method whose goal is to initialize a newly created instance. +Constructors look like regular methods, but without a return type and with a name that matches the class's name. ```java class Library { @@ -15,7 +17,9 @@ class Library { var library = new Library(); ``` -Like regular methods, constructors can have parameters. Constructor parameters are usually stored as (private) [fields][fields] to be accessed later, or else used in some one-off calculation. Arguments can be passed to constructors just like passing arguments to regular methods. +Like regular methods, constructors can have parameters. +Constructor parameters are usually stored as (private) [fields][fields] to be accessed later, or else used in some one-off calculation. +Arguments can be passed to constructors just like passing arguments to regular methods. ```java class Building { @@ -32,7 +36,8 @@ class Building { var largeBuilding = new Building(55, 6.2) ``` -Specifying a constructor is optional. If no constructor is specified, a parameterless constructor is generated by the compiler: +Specifying a constructor is optional. +If no constructor is specified, a parameterless constructor is generated by the compiler: ```java class Elevator { @@ -42,7 +47,8 @@ class Elevator { var elevator = new Elevator(); ``` -If fields have an initial value assigned to them, the compiler will output code in which the assignment is actually done inside the constructor. The following class declarations are thus equivalent (functionality-wise): +If fields have an initial value assigned to them, the compiler will output code in which the assignment is actually done inside the constructor. +The following class declarations are thus equivalent (functionality-wise): ```java class UsingFieldInitialization { diff --git a/concepts/constructors/introduction.md b/concepts/constructors/introduction.md index 22cf45ec2..69c09e5f3 100644 --- a/concepts/constructors/introduction.md +++ b/concepts/constructors/introduction.md @@ -1,6 +1,8 @@ # Introduction -Creating an instance of a _class_ is done by calling its _constructor_ through the `new` operator. A constructor is a special type of method whose goal is to initialize a newly created instance. Constructors look like regular methods, but without a return type and with a name that matches the class's name. +Creating an instance of a _class_ is done by calling its _constructor_ through the `new` operator. +A constructor is a special type of method whose goal is to initialize a newly created instance. +Constructors look like regular methods, but without a return type and with a name that matches the class's name. ```java class Library { @@ -16,7 +18,9 @@ class Library { var library = new Library(); ``` -Like regular methods, constructors can have parameters. Constructor parameters are usually stored as (private) fields to be accessed later, or else used in some one-off calculation. Arguments can be passed to constructors just like passing arguments to regular methods. +Like regular methods, constructors can have parameters. +Constructor parameters are usually stored as (private) fields to be accessed later, or else used in some one-off calculation. +Arguments can be passed to constructors just like passing arguments to regular methods. ```java class Building { diff --git a/concepts/exceptions/about.md b/concepts/exceptions/about.md index 607f1e0a8..96e2bc4a5 100644 --- a/concepts/exceptions/about.md +++ b/concepts/exceptions/about.md @@ -37,7 +37,8 @@ All exceptions in Java that inherit from `RuntimeException` are considered unche _Errors_ are exceptional conditions that are external to an application. An example of an error is the `OutOfMemoryError` which occurs when an application is trying to use more memory than is available on the system. -Like unchecked exceptions, errors are not checked at compile-time. They are not usually thrown from application code. +Like unchecked exceptions, errors are not checked at compile-time. +They are not usually thrown from application code. All exceptions in Java that inherit from `Error` are considered errors. diff --git a/concepts/exceptions/introduction.md b/concepts/exceptions/introduction.md index 1d02bddae..586f6f1ea 100644 --- a/concepts/exceptions/introduction.md +++ b/concepts/exceptions/introduction.md @@ -37,7 +37,8 @@ All exceptions in Java that inherit from `RuntimeException` are considered unche _Errors_ are exceptional conditions that are external to an application. An example of an error is the `OutOfMemoryError` which occurs when an application is trying to use more memory than is available on the system. -Like unchecked exceptions, errors are not checked at compile-time. They are not usually thrown from application code. +Like unchecked exceptions, errors are not checked at compile-time. +They are not usually thrown from application code. All exceptions in Java that inherit from `Error` are considered errors. diff --git a/concepts/inheritance/about.md b/concepts/inheritance/about.md index 52b72cfbd..4d1698bb8 100644 --- a/concepts/inheritance/about.md +++ b/concepts/inheritance/about.md @@ -9,14 +9,13 @@ A class can extend another class using `extends` keyword and can inherit from an ## Access Modifiers -The access modifiers define rules for the member variables and methods of a class about their access from other classes (or anywhere in -the code). +The access modifiers define rules for the member variables and methods of a class about their access from other classes (or anywhere in the code). -There are four access modifiers: +There are four access modifiers: -- private -- public -- protected +- `private` +- `public` +- `protected` - default (No keyword required) You can read more about them [here][access-modifiers] @@ -25,7 +24,7 @@ You can read more about them [here][access-modifiers] These concepts are very similar and are often confused. -- Inheritance means that the child has IS-A relationship with the parent class. +- Inheritance means that the child has an IS-A relationship with the parent class. ```java interface Animal() { diff --git a/concepts/inheritance/introduction.md b/concepts/inheritance/introduction.md index 0864dfa61..94a78b745 100644 --- a/concepts/inheritance/introduction.md +++ b/concepts/inheritance/introduction.md @@ -1,7 +1,8 @@ # Introduction -Inheritance is a core concept in OOP (Object-Oriented Programming). It donates IS-A relationship. -It literally means in programming as it means in english, inheriting features from parent(in programming features is normally functions and variables). +Inheritance is a core concept in OOP (Object-Oriented Programming). +It represents an IS-A relationship. +It literally means in programming as it means in english, inheriting features from parent (in programming features is normally functions and variables). Consider a class, `Animal` as shown, @@ -45,7 +46,7 @@ The output will look like Lion here!! ``` -According to OOP, there are many types of inheritance, but Java supports only some of them(Multi-level and Hierarchical). +According to OOP, there are many types of inheritance, but Java supports only some of them (Multi-level and Hierarchical). To read more about it, please read [this][java-inheritance]. [java-inheritance]: https://www.javatpoint.com/inheritance-in-java#:~:text=On%20the%20basis%20of%20class,will%20learn%20about%20interfaces%20later. diff --git a/concepts/interfaces/about.md b/concepts/interfaces/about.md index 9e6cda2ea..9a3e78ec2 100644 --- a/concepts/interfaces/about.md +++ b/concepts/interfaces/about.md @@ -1,6 +1,7 @@ # About -[`interfaces`][interfaces] are the primary means of [decoupling][wiki-loose-coupling] the uses of a class from its implementation. This decoupling provides flexibility for maintenance of the implementation and helps support type safe generic behavior. +[`interfaces`][interfaces] are the primary means of [decoupling][wiki-loose-coupling] the uses of a class from its implementation. +This decoupling provides flexibility for maintenance of the implementation and helps support type safe generic behavior. The syntax of an interface is similar to that of a class except that methods appear as the signature only and no body is provided. @@ -25,9 +26,12 @@ The implementing class must implement all operations defined by the interface. Interfaces typically do one or more of the following: -- allow a number of different classes to be treated generically by the using code. In this case interfaces are playing the same role as a base class. An example of this is [java.io.InputStream][input-stream], -- expose a subset of functionality for some specific purpose (such as [`Comparable`][comparable]) or -- expose the public API of a class so that multiple implementations can co-exist. One example is that of a [test double][wiki-test-double] +- Allow a number of different classes to be treated generically by the using code. + In this case interfaces are playing the same role as a base class. + An example of this is [java.io.InputStream][input-stream], +- Expose a subset of functionality for some specific purpose (such as [`Comparable`][comparable]) +- Expose the public API of a class so that multiple implementations can co-exist. + One example is that of a [test double][wiki-test-double] ```java public class ItalianTraveller implements ItalianLanguage { @@ -104,9 +108,9 @@ public class DocumentTranslator implements ScriptConverter { Code which uses the above interfaces and classes can: -- treat all speakers in the same way irrespective of language. -- allow some subsystem handling script conversion to operate without caring about what specific types it is dealing with. -- remain unaware of the changes to the italian speaker which is convenient if the class code and user code are maintained by different teams +- Treat all speakers in the same way irrespective of language. +- Allow some subsystem handling script conversion to operate without caring about what specific types it is dealing with. +- Remain unaware of the changes to the italian speaker which is convenient if the class code and user code are maintained by different teams. Interfaces are widely used to support testing as they allow for easy [mocking][so-mocking-interfaces]. @@ -114,7 +118,9 @@ Interfaces can extend other interfaces with the `extend` keyword. Members of an interface are public by default. -Interfaces can contain nested types: `interfaces`, `enums` and `classes`. Here, the containing interfaces act as [namespaces][wiki-namespaces]. Nested types are accessed outside the interface by prefixing the interface name and using dot syntax to identify the member. +Interfaces can contain nested types: `interfaces`, `enums` and `classes`. +Here, the containing interfaces act as [namespaces][wiki-namespaces]. +Nested types are accessed outside the interface by prefixing the interface name and using dot syntax to identify the member. By design, Java does not support multiple inheritance, but it facilitates a kind of multiple inheritance through interfaces. diff --git a/concepts/interfaces/introduction.md b/concepts/interfaces/introduction.md index bbc85a441..171b0efde 100644 --- a/concepts/interfaces/introduction.md +++ b/concepts/interfaces/introduction.md @@ -1,6 +1,7 @@ # Introduction -An interface is a type containing members defining a group of related functionality. It distances the uses of a class from the implementation allowing multiple different implementations or support for some generic behavior such as formatting, comparison or conversion. +An interface is a type containing members defining a group of related functionality. +It distances the uses of a class from the implementation allowing multiple different implementations or support for some generic behavior such as formatting, comparison or conversion. The syntax of an interface is similar to that of a class except that methods appear as the signature only and no body is provided. @@ -34,4 +35,5 @@ All operations defined by the interface must be implemented by the implementing Interfaces usually contain instance methods. -An example of an interface found in the Java Class Library, apart from `Cloneable` illustrated above, is `Comparable`. The `Comparable` interface can be implemented where a default generic sort order in collections is required. +An example of an interface found in the Java Class Library, apart from `Cloneable` illustrated above, is `Comparable`. +The `Comparable` interface can be implemented where a default generic sort order in collections is required. diff --git a/concepts/nullability/about.md b/concepts/nullability/about.md index 88bfd3255..b2443051b 100644 --- a/concepts/nullability/about.md +++ b/concepts/nullability/about.md @@ -3,16 +3,15 @@ In Java, the [`null` literal][null-keyword] is used to denote the absence of a value. [Primitive variables][primitive-data-types] in java all have a default value and therefore can never be `null`. -By convention, they start with a lowercase letter e.g `int` +By convention, they start with a lowercase letter e.g `int`. [Reference variables][reference-data-types] contain the memory address of an object and can have a value of null. -These variables usually start with an uppercase e.g `String` +These variables usually start with an uppercase e.g `String`. -Attempting to assign a primitive variable a value of `null` will result in a compile time error as the variable always holds -a default value of the type assigned. +Attempting to assign a primitive variable a value of `null` will result in a compile time error as the variable always holds a default value of the type assigned. ```java -//Throws compile time error stating the required type is int, but null was provided +// Throws compile time error stating the required type is int, but null was provided int number = null; ``` diff --git a/concepts/nullability/introduction.md b/concepts/nullability/introduction.md index cf59dc1d8..82683b4c3 100644 --- a/concepts/nullability/introduction.md +++ b/concepts/nullability/introduction.md @@ -3,13 +3,12 @@ In Java, the `null` literal is used to denote the absence of a value. Primitive data types in java all have a default value and therefore can never be `null`. -By convention, they start with a lowercase letter e.g `int` +By convention, they start with a lowercase letter e.g `int`. Reference types contain the memory address of an object can have a value of null. -These variables usually start with an uppercase e.g `String` +These variables usually start with an uppercase e.g `String`. -Attempting to assign a primitive variable a value of `null` will result in a compile time error as the variable always holds -a primitive value of the type assigned. +Attempting to assign a primitive variable a value of `null` will result in a compile time error as the variable always holds a primitive value of the type assigned. ```java //Throws compile time error stating the required type is int, but null was provided diff --git a/concepts/numbers/about.md b/concepts/numbers/about.md index 53e057db4..842043580 100644 --- a/concepts/numbers/about.md +++ b/concepts/numbers/about.md @@ -2,19 +2,19 @@ One of the key aspects of working with numbers in Java is the distinction between integers (numbers with no digits after the decimal separator) and floating-point numbers (numbers with zero or more digits after the decimal separator). -Java has other [datatypes][numeric-datatypes] apart from `int` and `double` +Java has other [datatypes][numeric-datatypes] apart from `int` and `double`: ```java -//8-Bit Integer +// 8-Bit Integer byte a = 127; -//16-Bit Integer +// 16-Bit Integer short b = 262143; -//64-Bit Integer +// 64-Bit Integer long d = 18446744073709551999L; -//32-bit Single-Precision Floating-Point +// 32-bit Single-Precision Floating-Point float e = 5409.29f; ``` @@ -28,7 +28,8 @@ double largeDouble = 9_876_543.21; // => 9876543.21 ``` -Arithmetic is done using the standard [arithmetic operators][arithmetic-operators] (`+`, `-`, `*`, etc.). Numbers can be compared using the standard [comparison operators][comparison-operators] (`<`, `>=`, etc.) along with the equality operator (`==`) and inequality operator (`!=`). +Arithmetic is done using the standard [arithmetic operators][arithmetic-operators] (`+`, `-`, `*`, etc.). +Numbers can be compared using the standard [comparison operators][comparison-operators] (`<`, `>=`, etc.) along with the equality operator (`==`) and inequality operator (`!=`). ```java 5 * 6 @@ -49,7 +50,8 @@ When converting between numeric types, there are two types of numeric conversion 1. Implicit conversions: no data will be lost and no additional syntax is required. 2. Explicit conversions: data could be lost and additional syntax in the form of a _cast_ is required. -As an `int` has less precision than a `double`, converting from an `int` to a `double` is safe and is thus an [implicit conversion][type-casting]. However, converting from a `double` to an `int` could mean losing data, so that requires an [explicit conversion][type-casting]. +As an `int` has less precision than a `double`, converting from an `int` to a `double` is safe and is thus an [implicit conversion][type-casting]. +However, converting from a `double` to an `int` could mean losing data, so that requires an [explicit conversion][type-casting]. ```java int i = 9; diff --git a/concepts/numbers/introduction.md b/concepts/numbers/introduction.md index 352a45eb5..0fe2df18b 100644 --- a/concepts/numbers/introduction.md +++ b/concepts/numbers/introduction.md @@ -2,16 +2,21 @@ There are two different types of numbers in Java: -- Integers: numbers with no digits behind the decimal separator (whole numbers). Examples are `-6`, `0`, `1`, `25`, `976` and `-500000`. -- Floating-point numbers: numbers with zero or more digits behind the decimal separator. Examples are `-20.4`, `0.1`, `2.72`, `16.984025` and `1024.0`. +- Integers: numbers with no digits behind the decimal separator (whole numbers). + Examples are `-6`, `0`, `1`, `25`, `976` and `-500000`. +- Floating-point numbers: numbers with zero or more digits behind the decimal separator. + Examples are `-20.4`, `0.1`, `2.72`, `16.984025` and `1024.0`. -The two most common numeric types in Java are `int` and `double`. An `int` is a 32-bit integer and a `double` is a 64-bit floating-point number. +The two most common numeric types in Java are `int` and `double`. +An `int` is a 32-bit integer and a `double` is a 64-bit floating-point number. -Arithmetic is done using the standard arithmetic operators. Numbers can be compared using the standard numeric comparison operators (eg. `5 > 4` and `4 <= 5`) and the equality (`==`) and inequality (`!=`) operators. +Arithmetic is done using the standard arithmetic operators. +Numbers can be compared using the standard numeric comparison operators (eg. `5 > 4` and `4 <= 5`) and the equality (`==`) and inequality (`!=`) operators. Java has two types of numeric conversions: 1. Implicit conversions: no data will be lost and no additional syntax is required. 2. Explicit conversions: data could be lost and additional syntax in the form of a _cast_ is required. -As an `int` has less precision than a `double`, converting from an `int` to a `double` is safe and is thus an implicit conversion. However, converting from a `double` to an `int` could mean losing data, so that requires an explicit conversion. +As an `int` has less precision than a `double`, converting from an `int` to a `double` is safe and is thus an implicit conversion. +However, converting from a `double` to an `int` could mean losing data, so that requires an explicit conversion. diff --git a/concepts/strings/about.md b/concepts/strings/about.md index 85169cf96..1dc4e8205 100644 --- a/concepts/strings/about.md +++ b/concepts/strings/about.md @@ -1,14 +1,17 @@ # About -The key thing to remember about Java strings is that they are immutable objects representing text as a sequence of Unicode characters (letters, digits, punctuation, etc.). Double quotes are used to define a `String` instance: +The key thing to remember about Java strings is that they are immutable objects representing text as a sequence of Unicode characters (letters, digits, punctuation, etc.). +Double quotes are used to define a `String` instance: ```java String fruit = "Apple"; ``` -Manipulating a string can be done using method of class [`String`][string-class]. As string values can never change after having been defined, all string manipulation methods will return a new string. +Manipulating a string can be done using method of class [`String`][string-class]. +As string values can never change after having been defined, all string manipulation methods will return a new string. -A string is delimited by double quote (`"`) characters. Some special characters need escaping using the backslash (`\`) character. +A string is delimited by double quote (`"`) characters. +Some special characters need escaping using the backslash (`\`) character. Characters to be escaped in Java: - `"` @@ -19,7 +22,8 @@ String escaped = "c:\\test.txt"; // => c:\test.txt ``` -Finally, there are many ways to concatenate a string. The simplest one is the `+` operator +Finally, there are many ways to concatenate a string. +The simplest one is the `+` operator: ```java String name = "Jane"; diff --git a/concepts/strings/introduction.md b/concepts/strings/introduction.md index 127e0a0e0..129e3b00e 100644 --- a/concepts/strings/introduction.md +++ b/concepts/strings/introduction.md @@ -1,10 +1,13 @@ # Introduction -A `String` in Java is an object that represents immutable text as a sequence of Unicode characters (letters, digits, punctuation, etc.). Double quotes are used to define a `String` instance: +A `String` in Java is an object that represents immutable text as a sequence of Unicode characters (letters, digits, punctuation, etc.). +Double quotes are used to define a `String` instance: ```java String fruit = "Apple"; ``` -Strings are manipulated by calling the string's methods. Once a string has been constructed, its value can never change. Any methods that appear to modify a string will actually return a new string. +Strings are manipulated by calling the string's methods. +Once a string has been constructed, its value can never change. +Any methods that appear to modify a string will actually return a new string. The `String` class provides some _static_ methods to transform the strings. diff --git a/concepts/switch-statement/about.md b/concepts/switch-statement/about.md index 91a41b677..14e0f8432 100644 --- a/concepts/switch-statement/about.md +++ b/concepts/switch-statement/about.md @@ -1,15 +1,19 @@ # About -Like an _if/else_ statement, a `switch` statement allows you to change the flow of the program by conditionally executing code. The difference is that a `switch` statement can only compare the value of a primitive or string expression against pre-defined constant values. +Like an _if/else_ statement, a `switch` statement allows you to change the flow of the program by conditionally executing code. +The difference is that a `switch` statement can only compare the value of a primitive or string expression against pre-defined constant values. Some keywords are useful when using a switch statement. -- `switch` : this keyword allows you to declare the structure of the switch. It is followed by the expression or the variable that will make the result change. -- `case` : you will use this one to declare the differents possibilties for the result. -- `break` : the `break` keyword is very useful in order to stop the execution of the switch at the end of the wanted flow. If you forget it, the program will continue and may lead to unexpected results. -- `default` : as it's name says, use it as a default result when no other case matchs your expression's result. +- `switch`: this keyword allows you to declare the structure of the switch. + It is followed by the expression or the variable that will make the result change. +- `case`: you will use this one to declare the differents possibilties for the result. +- `break`: the `break` keyword is very useful in order to stop the execution of the switch at the end of the wanted flow. + If you forget it, the program will continue and may lead to unexpected results. +- `default`: as it's name says, use it as a default result when no other case matchs your expression's result. -At their simplest they test a primitive or string expression and make a decision based on its value. For example: +At their simplest they test a primitive or string expression and make a decision based on its value. +For example: ```java String direction = getDirection(); @@ -21,7 +25,7 @@ switch (direction) { goRight(); break; default: - //otherwise + // otherwise markTime(); break; } @@ -30,37 +34,39 @@ switch (direction) { Starting with Java 14 (available as a preview before in Java 12 and 13) it is possible to use the "enhanced" switch implementation. 1. You have the possiblity to assign multiple value in a single case. - In the traditional switch-statement you can use fall-through. In the following example `case 1` and `case 3` will execute the same stuff. This is done by `case 1` not using the `break` keyword. + In the traditional switch-statement you can use fall-through. In the following example `case 1` and `case 3` will execute the same stuff. + This is done by `case 1` not using the `break` keyword. ```java switch (number) { case 1: case 3: - //do same stuff + // do same stuff break; case 2: - //do different stuff + // do different stuff break; - (...) + // (...) } ``` In the enhanced `switch expression` you can directly assign multiple value to a `case`. - Look at the following example : + Look at the following example: ```java switch (number) { case 1, 3: - //do stuff + // do stuff break; case 2: - //do other stuff + // do other stuff break; - (...) + // (...) } ``` -2. You can now write a `switch-statement` or a `switch expression`. What is the difference ? +2. You can now write a `switch-statement` or a `switch expression`. + What is the difference? Basicly a statement is expecting some strict logic where an expression can return a value. Instead of : @@ -68,69 +74,76 @@ Starting with Java 14 (available as a preview before in Java 12 and 13) it is po String result = ""; switch (expression) { case "bob": - result = "bob; + result = "bob"; break; - (...) + // (...) } ``` - You can do : + You can do: ```java - String result = switch(expression) { + String result = switch (expression) { case "bob": yield "bob"; - (...) + // (...) } ``` - The [`yield`][yield-keyword] works like a `return` except it's for switch expression. As `yield` terminates the expression `break` is not needed here. + The [`yield`][yield-keyword] works like a `return` except it's for switch expression. + As `yield` terminates the expression `break` is not needed here. -3. Another difference between _switch statements_ and _switch expressions_: in _switch expressions_ you _**MUST**_ cover all cases. Either by having a `case` for all possible values or using a `default` case. +3. Another difference between _switch statements_ and _switch expressions_: in _switch expressions_ you _**MUST**_ cover all cases. + Either by having a `case` for all possible values or using a `default` case. -4. You can use `->` instead of `:`. The `->` allow you to not include the `break` keyword. Both notations can be used but in a switch you have to stick with only one. +4. You can use `->` instead of `:`. + The `->` allow you to not include the `break` keyword. + Both notations can be used but in a switch you have to stick with only one. ```java - switch(expression) { - case 1 -> yield "one" - case 2 -> yield "two" - default: yield "other number" // Removing this will result in a compile error - } + switch(expression) { + case 1 -> yield "one" + case 2 -> yield "two" + default: yield "other number" // Removing this will result in a compile error + } ``` -5. The scope. Traditional `switch` can lead to some unexpected behavior because of its scope as there is only one scope for the whole `switch`. +5. The scope. + Traditional `switch` can lead to some unexpected behavior because of its scope as there is only one scope for the whole `switch`. ```java - switch(expression) { - case 1: - String message = "something"; - break; - case 2: - String message = "anything"; - break; - (...) - } + switch(expression) { + case 1: + String message = "something"; + break; + case 2: + String message = "anything"; + break; + // (...) + } ``` This example is not working because message is declared twice in the `switch`. - It could be solved using : + It could be solved using: ```java - switch(expression) { - case 1: { - String message = "something"; - break; - } - case 2: { - String message = "anything"; - break; - } - (...) - } + switch (expression) { + case 1: { + String message = "something"; + break; + } + case 2: { + String message = "anything"; + break; + } + // (...) + } ``` - As the `{}` is delimiting the scope of the `case`. However it's not intuitive because `{}` are not mandatory. - However if you use the new `->` notation it must be followed by either : a single statement/expression, a `throw` statement or a `{}` block. No more confusion! + As the `{}` is delimiting the scope of the `case`. + However it's not intuitive because `{}` are not mandatory. + However if you use the new `->` notation it must be followed by either: a single statement/expression, a `throw` statement or a `{}` block. + No more confusion! You can find more information on enhanced switch [here][switch1], [here][switch2] and on the [oracle documentation][oracle-doc]. diff --git a/concepts/switch-statement/introduction.md b/concepts/switch-statement/introduction.md index 92dcaacca..f9e7b275f 100644 --- a/concepts/switch-statement/introduction.md +++ b/concepts/switch-statement/introduction.md @@ -1,15 +1,19 @@ # Introduction -Like an _if/else_ statement, a `switch` statement allows you to change the flow of the program by conditionally executing code. The difference is that a `switch` statement can only compare the value of a primitive or string expression against pre-defined constant values. +Like an _if/else_ statement, a `switch` statement allows you to change the flow of the program by conditionally executing code. +The difference is that a `switch` statement can only compare the value of a primitive or string expression against pre-defined constant values. Some keywords are useful when using a switch statement. -- `switch` : this keyword allows you to declare the structure of the switch. It is followed by the expression or the variable that will change the result. -- `case` : you will use this keyword to declare the different possibilities for the result. -- `break` : the `break` keyword is very useful in order to stop the execution of the switch at the end of the wanted flow. If you forget it, the program will continue and may lead to unexpected results. -- `default` : as its name says, use it as a default result when no other case matches your expression's result. +- `switch`: this keyword allows you to declare the structure of the switch. + It is followed by the expression or the variable that will change the result. +- `case`: you will use this keyword to declare the different possibilities for the result. +- `break`: the `break` keyword is very useful in order to stop the execution of the switch at the end of the wanted flow. + If you forget it, the program will continue and may lead to unexpected results. +- `default`: as its name says, use it as a default result when no other case matches your expression's result. -At their simplest they test a primitive or string expression and make a decision based on its value. For example: +At their simplest they test a primitive or string expression and make a decision based on its value. +For example: ```java String direction = getDirection(); @@ -21,7 +25,7 @@ switch (direction) { goRight(); break; default: - //otherwise + // otherwise markTime(); break; } diff --git a/concepts/ternary-operators/about.md b/concepts/ternary-operators/about.md index a1f2dc113..461494b83 100644 --- a/concepts/ternary-operators/about.md +++ b/concepts/ternary-operators/about.md @@ -1,6 +1,7 @@ # Ternary Operator -The _ternary operators_ can be thought of as being a compact version of _if-else_. It's usually used in (but not restricted to) return statements, it needs just one single line to make the decision, returning the left value if the expression is `true` and the right value if `false`. +The _ternary operators_ can be thought of as being a compact version of _if-else_. +It's usually used in (but not restricted to) return statements, it needs just one single line to make the decision, returning the left value if the expression is `true` and the right value if `false`. A lot of simple _if/else_ expressions can be simplified using _ternary operators_. @@ -16,9 +17,12 @@ if ( 5 > 4 ) { } ``` -So, how to decide between _if-else_ and _ternary_ ? Well, _ternary operators_ are used in simple scenarios, where you just need to return a value based on a condition and no extra computation is needed. Use an _if-else_ for everthing else, like nested conditions, big expressions and when more than one line is needed to decide the return value. +So, how to decide between _if-else_ and _ternary_? +Well, _ternary operators_ are used in simple scenarios, where you just need to return a value based on a condition and no extra computation is needed. +Use an _if-else_ for everthing else, like nested conditions, big expressions and when more than one line is needed to decide the return value. -While you can nest _ternary operators_, the code often becomes hard to read. In these cases, nested if's are preferred. +While you can nest _ternary operators_, the code often becomes hard to read. +In these cases, nested if's are preferred. ```java // hard to read @@ -41,7 +45,7 @@ return val4; _Ternary operators_ and _if/else_ statements are a good example that you have different ways of achieving the same result when programming. -For more examples check out [this][ternary-operator-first] and [this][ternary-operator-second] sources. +For more examples check out [this][ternary-operator-first] and [this][ternary-operator-second]. [ternary-operator-first]: https://www.programiz.com/java-programming/ternary-operator [ternary-operator-second]: https://www.baeldung.com/java-ternary-operator diff --git a/concepts/ternary-operators/introduction.md b/concepts/ternary-operators/introduction.md index 606435f9c..b2e4776ab 100644 --- a/concepts/ternary-operators/introduction.md +++ b/concepts/ternary-operators/introduction.md @@ -1,6 +1,7 @@ # Introduction -The _ternary operator_ is a lightweight, compact alternative for simple _if/else_ statements. Usually used in (but not restricted to) return statements, it needs just one single line to make the decision, returning the left value if the expression is `true` and the right value if `false`, as follows: +The _ternary operator_ is a lightweight, compact alternative for simple _if/else_ statements. +Usually used in (but not restricted to) return statements, it needs just one single line to make the decision, returning the left value if the expression is `true` and the right value if `false`, as follows: ```java boolean expr = 0 != 200; diff --git a/exercises/concept/calculator-conundrum/.docs/introduction.md b/exercises/concept/calculator-conundrum/.docs/introduction.md index fd208573e..e9b19e65f 100644 --- a/exercises/concept/calculator-conundrum/.docs/introduction.md +++ b/exercises/concept/calculator-conundrum/.docs/introduction.md @@ -39,7 +39,8 @@ All exceptions in Java that inherit from `RuntimeException` are considered unche _Errors_ are exceptional conditions that are external to an application. An example of an error is the `OutOfMemoryError` which occurs when an application is trying to use more memory than is available on the system. -Like unchecked exceptions, errors are not checked at compile-time. They are not usually thrown from application code. +Like unchecked exceptions, errors are not checked at compile-time. +They are not usually thrown from application code. All exceptions in Java that inherit from `Error` are considered errors. diff --git a/exercises/concept/cars-assemble/.docs/introduction.md b/exercises/concept/cars-assemble/.docs/introduction.md index 50fc496e1..8569aa973 100644 --- a/exercises/concept/cars-assemble/.docs/introduction.md +++ b/exercises/concept/cars-assemble/.docs/introduction.md @@ -4,19 +4,24 @@ There are two different types of numbers in Java: -- Integers: numbers with no digits behind the decimal separator (whole numbers). Examples are `-6`, `0`, `1`, `25`, `976` and `-500000`. -- Floating-point numbers: numbers with zero or more digits behind the decimal separator. Examples are `-20.4`, `0.1`, `2.72`, `16.984025` and `1024.0`. +- Integers: numbers with no digits behind the decimal separator (whole numbers). + Examples are `-6`, `0`, `1`, `25`, `976` and `-500000`. +- Floating-point numbers: numbers with zero or more digits behind the decimal separator. + Examples are `-20.4`, `0.1`, `2.72`, `16.984025` and `1024.0`. -The two most common numeric types in Java are `int` and `double`. An `int` is a 32-bit integer and a `double` is a 64-bit floating-point number. +The two most common numeric types in Java are `int` and `double`. +An `int` is a 32-bit integer and a `double` is a 64-bit floating-point number. -Arithmetic is done using the standard arithmetic operators. Numbers can be compared using the standard numeric comparison operators (eg. `5 > 4` and `4 <= 5`) and the equality (`==`) and inequality (`!=`) operators. +Arithmetic is done using the standard arithmetic operators. +Numbers can be compared using the standard numeric comparison operators (eg. `5 > 4` and `4 <= 5`) and the equality (`==`) and inequality (`!=`) operators. Java has two types of numeric conversions: 1. Implicit conversions: no data will be lost and no additional syntax is required. 2. Explicit conversions: data could be lost and additional syntax in the form of a _cast_ is required. -As an `int` has less precision than a `double`, converting from an `int` to a `double` is safe and is thus an implicit conversion. However, converting from a `double` to an `int` could mean losing data, so that requires an explicit conversion. +As an `int` has less precision than a `double`, converting from an `int` to a `double` is safe and is thus an implicit conversion. +However, converting from a `double` to an `int` could mean losing data, so that requires an explicit conversion. ## If-Else Statements diff --git a/exercises/concept/elons-toy-car/.docs/introduction.md b/exercises/concept/elons-toy-car/.docs/introduction.md index 1de7cda5a..3bab24744 100644 --- a/exercises/concept/elons-toy-car/.docs/introduction.md +++ b/exercises/concept/elons-toy-car/.docs/introduction.md @@ -2,14 +2,16 @@ ## Classes -The primary object-oriented construct in Java is the _class_, which is a combination of data (_fields_) and behavior (_methods_). The fields and methods of a class are known as its _members_. +The primary object-oriented construct in Java is the _class_, which is a combination of data (_fields_) and behavior (_methods_). +The fields and methods of a class are known as its _members_. Access to members can be controlled through access modifiers, the two most common ones being: - `public`: the member can be accessed by any code (no restrictions). - `private`: the member can only be accessed by code in the same class. -You can think of a class as a template for creating instances of that class. To create an instance of a class (also known as an _object_), the `new` keyword is used: +You can think of a class as a template for creating instances of that class. +To create an instance of a class (also known as an _object_), the `new` keyword is used: ```java class Car { @@ -32,7 +34,9 @@ class Car { } ``` -One can optionally assign an initial value to a field. If a field does _not_ specify an initial value, it wll be set to its type's default value. An instance's field values can be accessed and updated using dot-notation. +One can optionally assign an initial value to a field. +If a field does _not_ specify an initial value, it wll be set to its type's default value. +An instance's field values can be accessed and updated using dot-notation. ```java class Car { @@ -51,7 +55,8 @@ newCar.year; // => 0 newCar.year = 2018; ``` -Private fields are usually updated as a side effect of calling a method. Such methods usually don't return any value, in which case the return type should be `void`: +Private fields are usually updated as a side effect of calling a method. +Such methods usually don't return any value, in which case the return type should be `void`: ```java class CarImporter { diff --git a/exercises/concept/football-match-reports/.docs/introduction.md b/exercises/concept/football-match-reports/.docs/introduction.md index e04875846..ff20beb45 100644 --- a/exercises/concept/football-match-reports/.docs/introduction.md +++ b/exercises/concept/football-match-reports/.docs/introduction.md @@ -2,16 +2,20 @@ ## Switch Statements -Like an _if/else_ statement, a `switch` statement allows you to change the flow of the program by conditionally executing code. The difference is that a `switch` statement can only compare the value of a primitive or string expression against pre-defined constant values. +Like an _if/else_ statement, a `switch` statement allows you to change the flow of the program by conditionally executing code. +The difference is that a `switch` statement can only compare the value of a primitive or string expression against pre-defined constant values. Some keywords are useful when using a switch statement. -- `switch` : this keyword allows you to declare the structure of the switch. It is followed by the expression or the variable that will change the result. -- `case` : you will use this keyword to declare the different possibilities for the result. -- `break` : the `break` keyword is very useful in order to stop the execution of the switch at the end of the wanted flow. If you forget it, the program will continue and may lead to unexpected results. -- `default` : as its name says, use it as a default result when no other case matches your expression's result. +- `switch`: this keyword allows you to declare the structure of the switch. + It is followed by the expression or the variable that will change the result. +- `case`: you will use this keyword to declare the different possibilities for the result. +- `break`: the `break` keyword is very useful in order to stop the execution of the switch at the end of the wanted flow. + If you forget it, the program will continue and may lead to unexpected results. +- `default`: as its name says, use it as a default result when no other case matches your expression's result. -At their simplest they test a primitive or string expression and make a decision based on its value. For example: +At their simplest they test a primitive or string expression and make a decision based on its value. +For example: ```java String direction = getDirection(); @@ -23,7 +27,7 @@ switch (direction) { goRight(); break; default: - //otherwise + // otherwise markTime(); break; } diff --git a/exercises/concept/lasagna/.docs/introduction.md b/exercises/concept/lasagna/.docs/introduction.md index 083c0dfb0..71fb191d7 100644 --- a/exercises/concept/lasagna/.docs/introduction.md +++ b/exercises/concept/lasagna/.docs/introduction.md @@ -2,13 +2,18 @@ ## Basics -Java is a statically-typed language, which means that the type of a variable is known at compile-time. Assigning a value to a name is referred to as defining a variable. A variable is defined by explicitly specifying its type. +Java is a statically-typed language, which means that the type of a variable is known at compile-time. +Assigning a value to a name is referred to as defining a variable. +A variable is defined by explicitly specifying its type. ```java int explicitVar = 10; ``` -Updating a variable's value is done through the `=` operator. Here, `=` does not represent mathematical equality. It simply assigns a value to a variable. Once defined, a variable's type can never change. +Updating a variable's value is done through the `=` operator. +Here, `=` does not represent mathematical equality. +It simply assigns a value to a variable. +Once defined, a variable's type can never change. ```java int count = 1; // Assign initial value @@ -18,7 +23,8 @@ count = 2; // Update to new value // count = false; ``` -Java is an [object-oriented language][object-oriented-programming] and requires all functions to be defined in a _class_. The `class` keyword is used to define a class. +Java is an [object-oriented language][object-oriented-programming] and requires all functions to be defined in a _class_. +The `class` keyword is used to define a class. ```java class Calculator { @@ -26,7 +32,12 @@ class Calculator { } ``` -A function within a class is referred to as a _method_. Each method can have zero or more parameters. All parameters must be explicitly typed, there is no type inference for parameters. Similarly, the return type must also be made explicit. Values are returned from methods using the `return` keyword. To allow a method to be called by other classes, the `public` access modifier must be added. +A function within a class is referred to as a _method_. +Each method can have zero or more parameters. +All parameters must be explicitly typed, there is no type inference for parameters. +Similarly, the return type must also be made explicit. +Values are returned from methods using the `return` keyword. +To allow a method to be called by other classes, the `public` access modifier must be added. ```java class Calculator { @@ -39,11 +50,12 @@ class Calculator { Invoking/calling a method is done by specifying its class and method name and passing arguments for each of the method's parameters. ```java -int sum = new Calculator().add(1, 2); // here the "add" method has been called to perform the task of addition +int sum = new Calculator().add(1, 2); // here the "add" method has been called to perform the task of addition ``` Scope in Java is defined between the `{` and `}` characters. -Java supports two types of comments. Single line comments are preceded by `//` and multiline comments are inserted between `/*` and `*/`. +Java supports two types of comments. +Single line comments are preceded by `//` and multiline comments are inserted between `/*` and `*/`. [object-oriented-programming]: https://docs.oracle.com/javase/tutorial/java/javaOO/index.html diff --git a/exercises/concept/log-levels/.docs/introduction.md b/exercises/concept/log-levels/.docs/introduction.md index eb7d4212a..6fa63ad07 100644 --- a/exercises/concept/log-levels/.docs/introduction.md +++ b/exercises/concept/log-levels/.docs/introduction.md @@ -2,11 +2,14 @@ ## Strings -A `String` in Java is an object that represents immutable text as a sequence of Unicode characters (letters, digits, punctuation, etc.). Double quotes are used to define a `String` instance: +A `String` in Java is an object that represents immutable text as a sequence of Unicode characters (letters, digits, punctuation, etc.). +Double quotes are used to define a `String` instance: ```java String fruit = "Apple"; ``` -Strings are manipulated by calling the string's methods. Once a string has been constructed, its value can never change. Any methods that appear to modify a string will actually return a new string. +Strings are manipulated by calling the string's methods. +Once a string has been constructed, its value can never change. +Any methods that appear to modify a string will actually return a new string. The `String` class provides some _static_ methods to transform the strings. diff --git a/exercises/concept/need-for-speed/.docs/introduction.md b/exercises/concept/need-for-speed/.docs/introduction.md index bf602c35c..5a63e3196 100644 --- a/exercises/concept/need-for-speed/.docs/introduction.md +++ b/exercises/concept/need-for-speed/.docs/introduction.md @@ -2,7 +2,9 @@ ## Constructors -Creating an instance of a _class_ is done by calling its _constructor_ through the `new` operator. A constructor is a special type of method whose goal is to initialize a newly created instance. Constructors look like regular methods, but without a return type and with a name that matches the class's name. +Creating an instance of a _class_ is done by calling its _constructor_ through the `new` operator. +A constructor is a special type of method whose goal is to initialize a newly created instance. +Constructors look like regular methods, but without a return type and with a name that matches the class's name. ```java class Library { @@ -18,7 +20,9 @@ class Library { var library = new Library(); ``` -Like regular methods, constructors can have parameters. Constructor parameters are usually stored as (private) fields to be accessed later, or else used in some one-off calculation. Arguments can be passed to constructors just like passing arguments to regular methods. +Like regular methods, constructors can have parameters. +Constructor parameters are usually stored as (private) fields to be accessed later, or else used in some one-off calculation. +Arguments can be passed to constructors just like passing arguments to regular methods. ```java class Building { diff --git a/exercises/concept/remote-control-competition/.docs/introduction.md b/exercises/concept/remote-control-competition/.docs/introduction.md index e31911456..d7cecddb4 100644 --- a/exercises/concept/remote-control-competition/.docs/introduction.md +++ b/exercises/concept/remote-control-competition/.docs/introduction.md @@ -2,7 +2,8 @@ ## Interfaces -An interface is a type containing members defining a group of related functionality. It distances the uses of a class from the implementation allowing multiple different implementations or support for some generic behavior such as formatting, comparison or conversion. +An interface is a type containing members defining a group of related functionality. +It distances the uses of a class from the implementation allowing multiple different implementations or support for some generic behavior such as formatting, comparison or conversion. The syntax of an interface is similar to that of a class except that methods appear as the signature only and no body is provided. @@ -36,4 +37,5 @@ All operations defined by the interface must be implemented by the implementing Interfaces usually contain instance methods. -An example of an interface found in the Java Class Library, apart from `Cloneable` illustrated above, is `Comparable`. The `Comparable` interface can be implemented where a default generic sort order in collections is required. +An example of an interface found in the Java Class Library, apart from `Cloneable` illustrated above, is `Comparable`. +The `Comparable` interface can be implemented where a default generic sort order in collections is required. diff --git a/exercises/concept/salary-calculator/.docs/introduction.md b/exercises/concept/salary-calculator/.docs/introduction.md index 785d13bab..9087ad290 100644 --- a/exercises/concept/salary-calculator/.docs/introduction.md +++ b/exercises/concept/salary-calculator/.docs/introduction.md @@ -3,7 +3,7 @@ ## Ternary Operators The _ternary operator_ is a lightweight, compact alternative for simple _if/else_ statements. -Usually used in (but not restricted to) return statements, it needs just one single line to make the decision, returning the left value if the expression is `true`, and the right value if the expression is `false`. +Usually used in (but not restricted to) return statements, it needs just one single line to make the decision, returning the left value if the expression is `true` and the right value if `false`, as follows: ```java boolean expr = 0 != 200; diff --git a/exercises/concept/squeaky-clean/.docs/introduction.md b/exercises/concept/squeaky-clean/.docs/introduction.md index 0f3b029c2..26dbb9fba 100644 --- a/exercises/concept/squeaky-clean/.docs/introduction.md +++ b/exercises/concept/squeaky-clean/.docs/introduction.md @@ -3,17 +3,14 @@ ## Chars The Java `char` type represents the smallest addressable components of text. -Multiple `char`s can comprise a string such as `"word"` or `char`s can be -processed independently. Their literals have single quotes e.g. `'A'`. +Multiple `char`s can comprise a string such as `"word"` or `char`s can be processed independently. +Their literals have single quotes e.g. `'A'`. -Java `char`s support Unicode encoding so in addition to the Latin character set -pretty much all the writing systems in use worldwide can be represented, -e.g. the Greek letter `'β'`. +Java `char`s support Unicode encoding so in addition to the Latin character set pretty much all the writing systems in use worldwide can be represented, e.g. the Greek letter `'β'`. -There are many builtin library methods to inspect and manipulate `char`s. These -can be found as static methods of the `java.lang.Character` class. +There are many builtin library methods to inspect and manipulate `char`s. +These can be found as static methods of the `java.lang.Character` class. `char`s are sometimes used in conjunction with a `StringBuilder` object. -This object has methods that allow a string to be constructed -character by character and manipulated. At the end of the process -`toString` can be called on it to output a complete string. +This object has methods that allow a string to be constructed character by character and manipulated. +At the end of the process `toString` can be called on it to output a complete string. diff --git a/exercises/concept/tim-from-marketing/.docs/introduction.md b/exercises/concept/tim-from-marketing/.docs/introduction.md index 87a92e293..8d7497357 100644 --- a/exercises/concept/tim-from-marketing/.docs/introduction.md +++ b/exercises/concept/tim-from-marketing/.docs/introduction.md @@ -5,13 +5,12 @@ In Java, the `null` literal is used to denote the absence of a value. Primitive data types in java all have a default value and therefore can never be `null`. -By convention, they start with a lowercase letter e.g `int` +By convention, they start with a lowercase letter e.g `int`. -Reference types contain the memory address of an object can -have a value of null. These variables usually start with an uppercase e.g `String` +Reference types contain the memory address of an object can have a value of null. +These variables usually start with an uppercase e.g `String`. -Attempting to assign a primitive variable a value of `null` will result in a compile time error as the variable always holds -a primitive value of the type assigned. +Attempting to assign a primitive variable a value of `null` will result in a compile time error as the variable always holds a primitive value of the type assigned. ```java //Throws compile time error stating the required type is int, but null was provided diff --git a/exercises/concept/wizards-and-warriors/.docs/introduction.md b/exercises/concept/wizards-and-warriors/.docs/introduction.md index b6ca66535..121854892 100644 --- a/exercises/concept/wizards-and-warriors/.docs/introduction.md +++ b/exercises/concept/wizards-and-warriors/.docs/introduction.md @@ -2,8 +2,9 @@ ## Inheritance -Inheritance is a core concept in OOP (Object-Oriented Programming). It donates IS-A relationship. -It literally means in programming as it means in english, inheriting features from parent(in programming features is normally functions and variables). +Inheritance is a core concept in OOP (Object-Oriented Programming). +It represents an IS-A relationship. +It literally means in programming as it means in english, inheriting features from parent (in programming features is normally functions and variables). Consider a class, `Animal` as shown, @@ -47,7 +48,7 @@ The output will look like Lion here!! ``` -According to OOP, there are many types of inheritance, but Java supports only some of them(Multi-level and Hierarchical). +According to OOP, there are many types of inheritance, but Java supports only some of them (Multi-level and Hierarchical). To read more about it, please read [this][java-inheritance]. [java-inheritance]: https://www.javatpoint.com/inheritance-in-java#:~:text=On%20the%20basis%20of%20class,will%20learn%20about%20interfaces%20later.