Java
Simple Hello World program
-
Create a
helloWorld.javafileThis component was made by Stratis Dermanoutsos. The code can be found here. -
Compile
This component was made by Stratis Dermanoutsos. The code can be found here. -
Run
This component was made by Stratis Dermanoutsos. The code can be found here.
To run a program in Java, you must first define the
mainfunction as seen above.
Primitive data types
In Java, there are exactly 8 primitive data types.
| TYPE | DEFAULT | SIZE (in bits) | RANGE OF VALUES |
|---|---|---|---|
| boolean | false | 1 | true, false |
| char | \u0000 | 16 | [0, 255] ASCII |
| byte | 0 | 8 | [-128, 127] |
| short | 0 | 16 | [-32.768, 32.767] |
| int | 0 | 32 | [-2.147.483.648, 2.147.483.647] |
| long | 0 | 64 | Too large for this table |
| float | 0.0 | 32 | max 7 decimal places |
| double | 0.0 | 64 | max 16 decimal places |
Modifiers
Access modifiers
| Access modifier | within class | within package | outside package by subclass only | outside package |
|---|---|---|---|---|
| Private | Yes | No | No | No |
| Default | Yes | Yes | No | No |
| Protected | Yes | Yes | Yes | No |
| Public | Yes | Yes | Yes | Yes |
Non-Access Modifiers
| Modifier | Description |
|---|---|
| final | Classes cannot be inherited. Attributes and methods cannot be modified. |
| abstract | To access an abstract class, it must be inherited from another class. Abstract methods are declared without a body. The body is provided by the subclass. |
| static | Attributes and methods are the same for all instances of the class. |
| transient | Attributes and methods are not included when serializing the object. |
| synchronized | Methods can only be accessed by one thread at a time. |
| volatile | The value of an attribute is always read from the “main memory”. |
Record types
A record is also a special class type in Java. It is intended to be used in places where a class is created only to act as a plain data carrier.
Defining a record type
Java is simplier that C# in the way that it only allows for record definition using positional arguments.
Comparing records
The benefit of records over a class is that they are compared based on their values.
The above example outputs true while the following outputs false:
This is because record types are compares based on their values, while class types are compared based on their references stored in memory.
Lambda Expressions
Syntax
Expressions must immediately return a value. Thus, they are limited. To enable the lambda expression to run more commands/statements etc, do this:
Example use cases
-
Print all elements of an array
This component was made by Stratis Dermanoutsos. The code can be found here. -
Run math operations
This component was made by Stratis Dermanoutsos. The code can be found here.
Useful code snippets
Recyclable InputStream
The main problem with the InputStream class is that an instance can only be used once in its lifecycle.
A common solution to this is to convert the InputStream to a byte array, then iterate over that array as many times as you need.
For a quick snippet, use the following class:
Usage: