Numbers in Java (With 0 Prefix and with Strings)


Numbers in Java

It's important to understand that the number class isn't a tangible class, but rather an abstract one. Inside it, we have a set of wrapper classes that define its functions. These wrapper classes include Integer, Byte, Double, Short, Float, and Long. You may notice that these are the same primitive data types we previously discussed, but they're represented as individual classes with capitalized names in keeping with class naming conventions.

The compiler automatically converts primitive data types into objects and vice versa, based on the requirements of a particular function or program scope, and the number class is a part of the java.lang package.

This process is known as Autoboxing and Unboxing. By grasping the abstract nature of the number class and its corresponding wrapper classes, we can gain a deeper understanding of how Java processes information.

In this article we will discuss how to prefix 0 with string by breaking down the topic for better understanding.

How to Prefix Each Element in a Java Array?

Consider adding a prefix to each element of an array if you want to change any of its components. Here are a few simple methods to assist you in doing this −

  • Make an array first with the components you want to change. Give the array a name, such as "strArray," and give each member a variable name, such as "str."

  • Create a for loop to iterate through each member of the array next. Concatenate the desired prefix with the present element within this loop to change it.

  • The updated value of each member should then be displayed using a print statement after traversing the array using a for-each loop.

  • Use the "for (type var: collection)" syntax to define a variable, such as "iteration," if you want to use a for-each loop. The updated value of each element will then be displayed using a print command as you iterate through them.

These easy steps will help you quickly and successfully add your preferred prefix to the elements of an array.

Algorithm

  • Step 1 − Declare the "strArry" String array variable and initialise it with the titles of three animals: "Dog," "Cat," and "Rabbit."

  • Step 2 − To iterate through each member of the "strArry" array, create a for loop.

  • Step 3 − Concatenate the phrase "To Pet" with the current element while still in the for loop, then replace the element's value.

  • Step 4 − Construct a second for-each loop that iterates through each element in the "strArry" array and prints the element's most recent number.

  • Step 5 − When you run the Java application, the updated values for the array elements will be displayed in the console.

Syntax

Let us look at the syntax of prefixing each element in java.

class Main {
   public static void main (String[] args) {
      String[] strArry = {"Dog", "Cat", "Rabbit"};
      for (int i=0 ; i < strArry.length; i++) {
         strArry[i] = "To Pet " + strArry[i];
      }
      for (String str: strArry) {
         System.out.println (str);
      }
   }
}

Prefix 0 with Integers

Algorithm

  • Step 1 − Make a Java program to first transform a string with leading zeroes into an integer.

  • Step 2 − Create the string variable "str" and give it the value "0000000953".

  • Step 3 − To transform the string into a number, use the parseInt() method.

  • Step 4 − To convert the integer to a string with leading zeroes, use the format() function. To guarantee that the integer has five digits in this instance, the format specifier "%05d" is used.

  • Step 5 − Finally, print the output string "Output String:" concatenated with the modified value of "str" using the System.out.println() function.

Example 1

Here is an example of prefixing 0 with integers.

package com.tutorialspoint;
public class StringToInt {
   public static void main(String args[]){
      String str="0000000953";
      /* String to int conversion with leading zeroes
      * the %05 format specifier is used to have 5 digits in
      * the number, this ensures the leading zeroes
      */
      str = String.format("%05d", Integer.parseInt(str));
      System.out.println("Output String: "+str);
   }
}

Output

Output String: 00953

String Concatenation with Integers

In Java, when a string is concatenated with an integer, the integer is first converted to a string, and then the concatenation is performed.

Algorithm

  • Step 1 − Make a Java program to first transform a string with leading zeroes into an integer.

  • Step 2 − Create the string variable "str" and give it the value "0000000953".

  • Step 3 − To transform the string into a number, use the parseInt() method.

  • Step 4 − To convert the integer to a string with leading zeroes, use the format() function. To guarantee that the integer has five digits in this instance, the format specifier "%05d" is used.

  • Step 5 − Finally, print the output string "Output String:" concatenated with the modified value of "str" using the System.out.println() function.

Example 2

The below code explains how string concatenation works with integers.

import java.io.*;
public class tutorials {
   public static void main (String[] args) {
      String s = 5 + 4 + "World" + 7 + 5;
      System.out.print(s);
   }
}

Output

9World75

Conclusion

We have explored various aspects of working with numbers in Java, including the concept of wrapper classes, auto boxing and unboxing, and the methods to add prefixes to elements of an array or integers. We have seen how to concatenate strings with integers and how to convert strings to integers with leading zeroes.

Updated on: 15-May-2023

546 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements