Basics of Java Programming - Part 2
This article contains more information on some of the fundamental concepts of Java programming.
Table of contents
Arrays
Arrays in Java, like Python lists, Javascript arrays, and C arrays, store ordered values of the same data type. They are built-in. However, there are some differences.
Java arrays cannot be resized once created, and you must declare the type of the variables that the array will store in advance. There is a class you can use to work with arrays in Java: the Arrays
class. It is more flexible, and a frequent alternative to arrays, but first letβs see how to use arrays.
Example:
class ArrayExample {
public static void main(String[] args) {
// Declare and initialize an array of integers
int[] myCalorieIntake = {2000, 4500, 3000, 1500, 1000};
System.out.println(myCalorieIntake[2]);
// Arrays have an instance variable that stores
// the length of the array
System.out.println(myCalorieIntake.length);
// Array of strings
String[] myFruits = {"Bananas", "Mangoes", "Apples", "Oranges"};
System.out.println("Today, I would love to eat " + myStrings[3]);
// Arrays can be modified
myStrings[2] = "Pineapple";
}
}
Note: Although you can determine the length of a string using the method someString.length()
, you determine the length of an array using the instance variable someArray.length
.
new
keyword
new
is a special keyword in Java that can be used to create arrays. To use it, you first have to declare the array.
Example:
class NewArrayExample {
public static void main(String[] args) {
// Declare the array
int[] myCalories;
myCalorieIntake = new myCalories[7];
myCalorieIntake[0] = 1400;
myCalorieIntake[1] = 1460;
myCalorieIntake[2] = 2000;
myCalorieIntake[3] = 1390;
myCalorieIntake[4] = 2900;
myCalorieIntake[5] = 780
myCalorieIntake[6] = 2300
System.out.println("I have a list of my calorie intake for " + myCalories.length " days.");
}
}
The array in main (String[] args)
The main method is used to run Java programs. You have to have a main
method if you want to execute any Java program.
The syntax of the main
method is usually: public static void main (String[] args)
.
You can now see that main
takes a single parameter, args
, of type String
.
If you execute a program from the command line (terminal on Unix or Mac), you can pass parameters to the program. For example, the command
cat calories.txt
can be typed into the command line to display the information contained in calories.txt
. The first word, cat
, is the name of the program to execute. The second word, calories.txt
, is passed as a string as the first item in the array of strings, args
.
A similar mechanism is used in C, but since C arrays do not internally know their lengths, the syntax declaration of main
is a bit different. However, this can be determined using argc
, which is the count of the number of arguments passed to main
. In Python, sys.argv
can be imported to fetch command-line arguments.
The args array can be used to access the arguments that were passed to the program. For example, the following code will print the value of the second argument:
public static void main(String[] args) {
System.out.println(args[1]);
}
java.util.Arrays
You can work with arrays using the Arrays
class. This is sometimes a better alternative since they are more flexible. The Arrays
class in the java.util
package provides some methods that let you do things like copy, compare, sort, and search arrays.
Unlike some other classes, like Math
or String
that are available by default and hence don't need to be imported, you must import
the Arrays
class, using an import
statement at the top of any file in which you would like to use it.
You can find out more about the Arrays
class here: Arrays (Java Platform SE 8 ) (oracle.com)
import java.util.Arrays;
class ArraysExample {
public static void main(String[] args) {
int[] myCalorieIntake = {42, 1, 17, 27, 16};
Arrays.sort(myCalorieIntake);
System.out.println(Arrays.toString(myCalorieIntake));
}
}
With this information, you now have what it takes to use and manipulate arrays effectively! π Feel free to reach out if you have any questions and comment below! Happy coding! π