1) Basic Syntax & Data Types

1) Basic Syntax & Data Types

Fundamental Java Knowledge

As https://roadmap.sh/java suggests, we’re going to start from the basic syntax and data types in Java.

Java is mainly an object oriented language, which means we’re mostly going to utilize objects for our program logic.


Basic Syntax

When writing a java program, we need to write a class, with a method named main. main() method is the entry point for any program in Java. This is the method which is invoked by the JVM(Java Virtual Machine) to execute the program.

Main method shouldn’t be confused as a constructor method, since it’s just an entry point for the program and can be declared only once in a class and a constructor method is for instantiating a class and can be declared more than once in a class.

Assuming that we have a file named MarketShare.java , below would be it's generic content:

public class MarketShare {
    public static void main(String[] args) {

    }
}

As we can see, there’s a public class named the same as the file and a public static void main method which receives a parameter named args of type array of strings.

For now, just get yourself used to this structure since you’ll see it in every Java class. We’ll dive further into the public static void parts in the future articles.


Data Types

Java is a statically-typed language, therefore each variable has a property known as its data type which determines what kind of data can be stored in that variable.

Data types are the most essential tools that we’re going to use to create our programs in a programming language, since it directly affect what type of data you’ll be able to work with in the language.

The data types are divided into two categories, primitive data types and reference data types.

Primitive Data Types

Here’s a great table I found from Codecademy’s Java documentation on Data Types:

Byte

Represents small whole numbers.

byte myByte = 100;

Short

Stores smaller whole numbers.

short myShort = 30000;

Int

Represents whole numbers.

int myInt = 2000000000;

Long

Stores very large whole numbers.

long myLong = 900000000000L;

Float

Stores floating-point numbers with single precision.

float myFloat = 3.14f;

Double

Stores floating-point numbers with double precision.

double myDouble = 3.141592653589793;

Boolean

Represents true or false values.

boolean myBoolean = true;

Char

Stores a single character.

char myChar = 'A';

One thing I want to point out is that, all above primitive data types start with a lowercase letter. This means that they are not referencing an object, they are directly a part of the language syntax.

On the next section, we’re going to talk about the reference data types, which are going to reference objects and will be written with a uppercase letter.

Reference Data Types

Below is from the linked document of Codecademy:

  • Annotations - allow metadata to be associated with elements of a program

  • Arrays - store elements of the same type

  • Classes - provide a template for object creation

  • Enumeration - stores a fixed set of constants

  • Interfaces - store a template for a class

Please navigate above links for detailed explanation on each of the data types for further detail and example usage.