Terminating the Java Program, Java Number, Character and String Classes, and Arrays in Java
Java is a powerful object-oriented programming language that provides various built-in classes and structures to simplify programming. Understanding how Java programs terminate and how built-in classes such as Number, Character, and String work is essential for developing efficient applications. In addition, arrays allow programmers to store multiple values in a single variable, making programs more efficient and easier to manage.
For students studying the ITI COPA (Computer Operator and Programming Assistant) trade, learning these Java concepts helps build strong programming skills and prepares them for real-world software development.
Terminating the Java Program
Every Java program starts execution from the main() method. The program continues running until all instructions inside the main method are executed. When the last statement of the program is executed, the program automatically terminates.
However, sometimes programmers may want to terminate the program before reaching the end of the code. Java provides methods that allow the program to stop execution immediately.
Normal Program Termination
Normally, a Java program ends automatically after executing the last statement inside the main method.
public class Example {
public static void main(String[] args) {
System.out.println("Program Started");
System.out.println("Program Finished");
}
}
In this example, the program terminates after printing the messages.
Using System.exit()
Java provides the System.exit() method to terminate a program immediately.
public class ExitExample {
public static void main(String[] args) {
System.out.println("Program Start");
System.exit(0);
System.out.println("This line will not execute");
}
}
The System.exit(0) statement stops program execution immediately.
The number inside System.exit() represents the exit status:
- 0 – Successful termination
- Non-zero value – Abnormal termination
Java Wrapper Classes
Java provides special classes called wrapper classes that convert primitive data types into objects. Wrapper classes allow primitive values to be used in object-oriented programming structures.
Some important wrapper classes include:
- Number class
- Character class
- String class
The Number Class
The Number class is an abstract class that represents numeric values. Many numeric wrapper classes such as Integer, Float, Double, and Long inherit from the Number class.
These classes allow primitive numbers to be treated as objects.
Example
public class NumberExample {
public static void main(String[] args) {
Integer num = 100;
System.out.println(num.intValue());
System.out.println(num.doubleValue());
}
}
The Number class provides methods for converting values into different numeric types.
Common Number Methods
| Method | Description |
|---|---|
| intValue() | Returns integer value |
| doubleValue() | Returns double value |
| floatValue() | Returns float value |
| longValue() | Returns long value |
The Character Class
The Character class is used to manipulate characters. It provides methods for checking character types and converting characters into different formats.
The Character class wraps the primitive data type char.
Example
public class CharacterExample {
public static void main(String[] args) {
char ch = 'A';
System.out.println(Character.isLetter(ch));
System.out.println(Character.isDigit(ch));
System.out.println(Character.toLowerCase(ch));
}
}
Common Character Methods
| Method | Description |
|---|---|
| isLetter() | Checks if character is a letter |
| isDigit() | Checks if character is a digit |
| toUpperCase() | Converts character to uppercase |
| toLowerCase() | Converts character to lowercase |
The String Class
The String class represents a sequence of characters. Strings are widely used in Java applications for handling text data.
Unlike primitive data types, strings are objects created using the String class.
Example
public class StringExample {
public static void main(String[] args) {
String text = "Java Programming";
System.out.println(text.length());
System.out.println(text.toUpperCase());
System.out.println(text.substring(0,4));
}
}
Common String Methods
| Method | Description |
|---|---|
| length() | Returns length of string |
| toUpperCase() | Converts string to uppercase |
| toLowerCase() | Converts string to lowercase |
| substring() | Extracts part of a string |
Arrays in Java
An array is a data structure used to store multiple values of the same type in a single variable.
Instead of creating many variables, arrays allow programmers to store large amounts of data efficiently.
Declaring an Array
int[] numbers;
Creating an Array
numbers = new int[5];
Initializing an Array
int[] numbers = {10, 20, 30, 40, 50};
Example Program
public class ArrayExample {
public static void main(String[] args) {
int[] numbers = {10,20,30,40,50};
for(int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
This program prints all elements stored in the array.
Advantages of Arrays
- Stores multiple values efficiently
- Reduces number of variables
- Easy data processing
- Improves program performance
Applications of Arrays
Arrays are used in many real-world applications such as:
- Storing student records
- Processing numerical data
- Game development
- Scientific calculations
Importance for ITI COPA Students
For students studying the ITI COPA trade, understanding Java classes and arrays is essential for building strong programming skills.
The Number, Character, and String classes allow efficient data manipulation, while arrays help manage multiple values efficiently.
These concepts form the foundation for advanced Java programming topics such as collections, data structures, and software development.
Conclusion
Java provides powerful tools for managing data and controlling program execution. Programs can terminate automatically or using the System.exit() method. Wrapper classes such as Number, Character, and String help manipulate different types of data effectively.
Arrays allow programmers to store and process multiple values in a structured way. Together, these features make Java a flexible and powerful programming language for developing modern applications.