PizzaStack.github.io

Java

Java is an object-oriented programming language and a platform developed by Sun Microsystems (eaten by Oracle). Using the principle of WORA (Write Once, Run Anywhere), a Java application can be compiled and executed on any platform supported by Java. Flexible, popular, and well-supported, Java has helps developers write scalable client-server web applications, desktop and mobile applications, and frameworks and libraries.

Features

Variables

A value is stored and identified in memory by a variable. Variables have a name that makes it possible to access the value, and a type that defines what sort of value it stores.

int variableName = 64;
String txtVar = "Hello World";

Primitive data types

Java handles two kinds of datatypes: primitives and references. Primitives are variables that store simple values. There are eight in Java.

Reference types

Reference types store the memory address location of more complex data types in the heap. Reference types include:

Naming variables

Scopes of a variable

A variable’s reference will only exist within the context of its declared scope, which is based on the location of its declaration.

Be aware of shadowing: when two variables in different scopes share names.

Methods

Methods accept a list of arguments known as parameters and return some value. They are used to implement repeatable, consistent actions on variable input, much like math functions.

public int myMethod(int a, int b);
public int myMethod(int a);

Constructors

Classes not only define object fields and methods, but how it should be instantiated through special methods called constructors. Constructors must have no return type and share the same name as its class. Java will automatically give you a noargs constructor. However, if you define any constructor, you will lose the automatically given constructor.

While a constructor may be private, used for singletons, it may not be final, static, or abstract.

Access modifiers

Classes should only be public or default. There are no cascading access levels, and unspecified fields will be default. Subclasses can only change inherited fields to be less restrictive.