Print ASCII Value of a Particular Character in Java



In this article, we will learn to print the ASCII value of a particular character in Java. ASCII (American Standard Code for Information Interchange) is a standard encoding system that assigns a unique numeric value to characters like letters, digits, and symbols.

We'll explain the concept of ASCII, demonstrate how to retrieve and display ASCII values in Java and provide practical examples to help you understand and apply this concept effectively.

What Is ASCII?

ASCII is a character encoding standard where each character (letters, digits, and symbols) is assigned a numeric value. For instance ?

  • The ASCII value of 'A' is 65.
  • The ASCII value of 'a' is 97.
  • The ASCII value of '0' is 48.
This encoding helps computers understand and manipulate textual data.

Approaches to Print ASCII Values

The following are the two methods to Print the ASCII Value of a Particular Character ?

Using Inline Conversion

Directly converting a character to its ASCII value using int casting within the main method is straightforward and requires minimal code. It is best suited for programs where modularity or reuse of the conversion logic is not a priority.

Convert the character to its ASCII value ?

int asciiValue = (int) character;
  • Scanner.next(Pattern pattern) method: Returns the next token if it matches the specified pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext(Pattern) returned true.
  • The charAt() method in Java is used to retrieve the character at a specific index within a string. It takes an integer index as input and returns the corresponding character, with indexing starting from 0. 

Example

Below is the Java program to Print the ASCII Value of a Particular Character using inline conversion ?

import java.util.Scanner;
public class AsciiValue {
    public static void main(String[] args) {
        // Create a Scanner object to get user input
        Scanner scanner = new Scanner(System.in);
        // Prompt the user to enter a character
        System.out.print("Enter a character: ");
        char character = scanner.next().charAt(0);
        // Convert the character to its ASCII value
        int asciiValue = (int) character;
        // Print the ASCII value
        System.out.println("The ASCII value of '" + character + "' is: " + asciiValue);
        // Close the scanner
        scanner.close();
    }
}

Output

Enter a character: A
The ASCII value of 'A' is: 65

Time Complexity: O(1) Converting a character to its ASCII value using (int) casting is a constant-time operation.
Space Complexity: O(1) Requires minimal additional memory since no extra methods or data structures are used.

Using Static Method

The above Java program prints the ASCII value of a given character using a static method for improved modularity. The getAsciiValue method takes a character as input and converts it to its ASCII value. The program then prompts the user for a character, calls the method, and displays the result.

  • The getAsciiValue method is a static function in Java that takes a single character as input and returns its ASCII value by casting the character to an integer.

Call the static method and print the ASCII value ?

int asciiValue = getAsciiValue(inputChar);

Example

Below is the Java program to Print the ASCII Value of a Particular Character using the static method ?
import java.util.Scanner;
public class AsciiValue {
    // Static method to get ASCII value of a character
    public static int getAsciiValue(char character) {
        return (int) character; // Convert character to ASCII
    }
    public static void main(String[] args) {
        // Create a Scanner object for input
        Scanner scanner = new Scanner(System.in);

        // Prompt the user for input
        System.out.print("Enter a character: ");
        char inputChar = scanner.next().charAt(0);

        // Call the static method and print the ASCII value
        int asciiValue = getAsciiValue(inputChar);
        System.out.println("The ASCII value of '" + inputChar + "' is: " + asciiValue);

        // Close the scanner
        scanner.close();
    }
}

Output

Enter a character: @
The ASCII value of '@' is: 64

Time Complexity: O(1) Calling the static method and performing the conversion are both constant-time operations.
Space Complexity: O(1) The method uses no extra data structures, and its memory footprint is small.

Conclusion

In conclusion, both approaches are efficient with O(1) time and space complexities. Inline conversion is simple and ideal for quick programs, while using a static method promotes reusability and modularity, making it better suited for larger or more complex applications. 
Updated on: 2024-12-17T03:33:55+05:30

668 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements