Variables

Java is a statically-typed language which means that ALL variables must be DECLARED (given a type and a name) before they can be USED.

While variables exist in all programming languages, they take on different meanings and names in an object-oriented programming language like Java:

Naming Conventions

Like variables that exist in all programming languages, so do naming conventions for variables, etc. All variables (e.g., fields, local or parameters) follow the same naming conventions:

Primitive Data Types

Data types are as the name implies “types of data” used by the language.

There are eight primitive data types (byte, short, int, long, float, double, boolean, and char). The ones that you will use the most are int, float, double and boolean.

For a complete listing of the various data types, their definitions and examples go to:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

The String class is not a primitive data type but an object; however, it is treated as such. For example, enclosing a character string within double quotes will automatically create a new String object (e.g., String firstName = “Cornelius”). String objects are immutable which means that once created their values cannot be changed.

Default Values

If a field is declared (given a type and a name) but not assigned, the compiler will give it a default value which typically is zero for numeric types and null for a String or an object and false for a boolean.

Data Type

Default Value (for fields)

byte, short, int

0

float

0.0f

double

0.0d

char

‘\u0000’

String, object

null

boolean

false


TIP: However, it is best practice to assign a data type.

However, for local variables in a method, default values are never assigned by the compiler. If a local variable is not initialized when it is declared, it needs to be assigned a value before it can be used otherwise the compiler will throw an error.

Casting into another type

There are times when you need to cast a variable into anohter data type. Below is a common example that you may see:

TextView myText = (TextView)

Reference Data Types

Reference data types are used with strings, arrays and objects.

Array — is a container for an “array” of values of the same type. The array length is determined when the array is created. After it is created, its length becomes fixed.

Declaring a Variable to Refer to an Array

Unlike most programming languages where square brackets denotes an array and are placed AFTER the array name (e.g., var myArray[ ]; in JavaScript), in Java it is common convention to place them BEFORE the array name but AFTER the data type where the data type (int, in this case) specifies what type of data the array will hold:

int[ ] myIntegerArray;
String[ ] myStringArray;

In the example above, the array is empty denoted by the empty brackets. However, the declaration does not actually create an array but simply tells the compiler that this variable will hold an array of a specify type.

While it is discouraged in Java, if you place the brackets after the array's name it will still work:

int myIntegerArray[ ];
String myStringArray[ ];

Creating and Populating An Array

It is best practice to create and populate an array at the same time.  However, unlike other languages, Java uses curly braces instead of square brackets to populate an array in this manner:

Int[] myArray = {10,20,30,40};

The length of the array is determined by the number of elements in the array (in this case, 4).

Multidimensional Array

You can also insert multiple arrays inside of a single array which is called a multidimensional array by using two sets of square brackets where each element has to be accessed by using two index values. While not recommended, you can also have different number of elements in each array.

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

String[][] names = {
{"Cornelius", "Joshua ", "Josiah"},
{"Chopin", "Washington"}	
};

System.out.println(names[0][0] + names[1][0]);	      
System.out.println(names[0][2] + names[1][1]);
	    
}
}

The outputs are Cornelius Chopin and Josiah Washington.

Copying Arrays

You can use the System’s arraycopy() method to copy data from one array into another array using the following syntax where the two Object arguments specify the array to copy FROM and copy TO. The three int arguments specify the starting source and destination starting positions and the number of elements to copy:

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

The following example copies a portion of the first array into the second array:    

class ArrayCopyDemo {

public static void  main(String[] args) {
char[] copyFrom = { 'H', 'e', 'l', 'l', 'o', 'W', 'o','r', 'l', 'd'}; char[] copyTo = new char[5];        
System.arraycopy(copyFrom, 5, copyTo, 0, 4); System.out.println(new String(copyTo)); }
}

The output is World.

Array Manipulations

Like most programming languages, arrays has a list of methods used to manipulate (e.g., copy, sort, search) an array in the java.util.Arrays class.

However, there a few that are unique to Java:

Determining which array type to use: (e.g., Array, ArrayList, HashMap):


instanceof Operator

The instanceof operator is used to test if an object in “an instance of” a class, an instance, a subclass or an instance of a class that implements an interface.

See https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html for example.