umma.dev

Learning Java: Part Two

Looking at variable types and different data types in Java.

Variable Types

Local

This variable is declared inside the body of the method.

Static

Static variables are initalised first before instance variables.

Instance

These variables exist outside of method declarations and specific to objects.

Data Types

Primitive Data Types

Int

int num = 10;

Long

long num = 12390234;

Float

float floatNum = 3.58f

Double

double doubleNum = 0.25;

Boolean

boolean exampleBoo = false;

Char

char letters = 'k';

Non-Primitive Data Types

Strings

String pieceOfText = "test";

Arrays

int myArr = new int[10];

Putting It All Together

public class Example {
  static int test = 100;

  voice methodExample() {
    string myStr = "local variable";
  }

  public static void main(String args[]) {
    double instanceVar = 0.0038203;
  }
}

Part three will look at object oriented programming.