Java

Simple Hello World program

  • Create a helloWorld.java file

    This 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 main function as seen above.

Primitive data types

In Java, there are exactly 8 primitive data types.

TYPEDEFAULTSIZE (in bits)RANGE OF VALUES
booleanfalse1true, false
char\u000016[0, 255] ASCII
byte08[-128, 127]
short016[-32.768, 32.767]
int032[-2.147.483.648, 2.147.483.647]
long064Too large for this table
float0.032max 7 decimal places
double0.064max 16 decimal places

Modifiers

Access modifiers

Access modifierwithin classwithin packageoutside package by subclass onlyoutside package
PrivateYesNoNoNo
DefaultYesYesNoNo
ProtectedYesYesYesNo
PublicYesYesYesYes

Non-Access Modifiers

ModifierDescription
finalClasses cannot be inherited. Attributes and methods cannot be modified.
abstractTo 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.
staticAttributes and methods are the same for all instances of the class.
transientAttributes and methods are not included when serializing the object.
synchronizedMethods can only be accessed by one thread at a time.
volatileThe 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.

This component was made by Stratis Dermanoutsos. The code can be found here.

Comparing records

The benefit of records over a class is that they are compared based on their values.

This component was made by Stratis Dermanoutsos. The code can be found here.

The above example outputs true while the following outputs false:

This component was made by Stratis Dermanoutsos. The code can be found here.

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

This component was made by Stratis Dermanoutsos. The code can be found here.

Expressions must immediately return a value. Thus, they are limited. To enable the lambda expression to run more commands/statements etc, do this:

This component was made by Stratis Dermanoutsos. The code can be found here.

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:

This component was made by Stratis Dermanoutsos. The code can be found here.

Usage:

This component was made by Stratis Dermanoutsos. The code can be found here.

Resources