Java Basics: Functions, Loops, and Non-Primitive Data Types
iamdestinos
Posted on October 24, 2022
Intro
In a continuation from the previous blog this one will be going into functions, loops, and non-primitive data types.
Functions - But Not Really
In Java, there are technically no functions as Java is a class/object based programming language, so all functions are actually methods. Now, defining a method is rather simple, as there are four parts that make up the syntax for announcing a definition, as shown below:
static int add(int x, int y) {
return x + y;
};
In this example the line declaring the method has four pieces to it, static, return type, identifier, and parameters. Static is an unchanging part to method declarations as it tells Java that this is a method for the class it is inside rather than an object. The int in the example is the return type, which is simply saying what type of variable the method returns. While in this case the method returns an int, a method can return any data type or void, which means nothing is returned from the method. The identifier is the name of the method which will be used to invoke the method. An identifier can be named whatever the user wants and typically starts with a lowercase letter. The last piece of the method declaration is the parameter section, which tells the method what type of variables to take in and how many it can take in.
Method Overloading
An interesting feature to Java methods is overloading, which allows a method to have multiple definitions, unlike many other programming languages. This allows for a method to take in different arguments and return different data types.
static int add(int x, int y) {
return x + y;
};
static double add(double x, double y) {
return x + y;
};
static float add(float x, float y) {
return x + y;
};
Loops
Loops are rather simple, allowing a section of code to be repeated a number of times. There are two kinds of loops, while and for.
//while loop
int x = 0;
while(x != 10) {
System.out.println(x);
x++;
}
//for loop
for(int i = 0; i < 10; i++) {
System.out.println(i);
}
Non-Primitive Data Types
In Java, data types are split into two categories, primitive and non-primitive data types. Primitive data types are noted for taking up a predetermined amount of storage, lacking methods, and starting with a lowercase letter when typed and a number of other qualities. Non-primitive data types are notable for having variable storage size, having methods, and Starting with an uppercase letter when typed. Three notable kinds of non-primitive data types are Strings, Arrays, and Classes.
Strings
Strings are a rather simplistic non-primitive data type, storing a series of characters of variable length. Strings have a number of different methods but some commonly used ones includes the .length
method and the .toUpperCase
and .toLowerCase
methods which turn a string to all uppercase or lowercase letters respectively.
String example = "Hello World"
example.length() //returns 11
example.toUpperCase() //returns "HELLO WORLD"
example.toLowerCase() //returns "hello world"
Arrays
Arrays are an indexed list of variable length that hold one specific data type, as shown below:
int[] exampleArr = [1, 2, 3, 4];
System.out.println(exampleArr[0]); //prints 1
In the previous example, the array holds a number of int values. Arrays can hold any number of values but all values must be of the type in the declaration, so an array only holds one data type.
Multidimensional Arrays
It is possible to create an array that holds arrays, otherwise known as a multidimensional array, which is done like so:
int[][] exampleArr2 = { {1, 2, 3}, {2, 4, 6} };
System.out.println(exampleArr2[0][0]); //prints out 1
A notable difference from a normal array is the use of the curled brace ({}) instead of the square bracket ([]). It should also be noted that multidimensional arrays can have however many arrays nested within so long as the type declaration includes the number of layers the multidimensional array will have.
Classes
The last non-primitive data type to be covered are classes. Java is considered a class-based or object-based programming language as everything is contained within a class and classes can be considered objects. To declare a class, it can be done like so:
class Example {
int x = 5;
int y = 10;
}
Simply noting that a class definition is being declared followed up by the identifier (which must start with a capital letter) will serve as the class declaration. To create an instance of this class as an object is shown below:
Example objExample = new Example();
System.out.println(objExample.x); //prints 5
Closing
Overall, Java does not have functions, but rather methods which will perform a series of actions when invoked and possibly return a value of some given data type. Loops will perform the same series of actions a number of times, and non-primitive data types are a variably sized storage of data which can have methods and some of which can be defined by the user using classes.
Helpful Sources
Posted on October 24, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.