×

Java String format() method

format() method returns a formatted String based on the given locale,specified format and arguments.

Syntax:

public static String format(String format , Object… args)

Parameter:

locale : It specifies locale value to be applied on the format() method.

format: It specifies the format of the output String

args : It specifies the number of arguments for the format String.It may be zero or more

Returns:

It returns a formatted String

Java String format() Example 1

public class JavaStringFormatEx1 {
    public static void main(String args[])
    {
        String s1 = "Tutorial And Example";
        // Concatenation of two strings
        String str1 = String.format("Welcome to %s", s1);
        // Output is given upto 11 decimal places
        String str2 = String.format("value is %.11f", 75.23456);
        System.out.println(str1);
        System.out.println(str2);
    }
}

Output:

Welcome to Tutorial And Example
value is 75.23456000000
value is 47.65734000

Java String format() Example 2

public class JavaStringFormatEx2
{ 
public static void main(String args[])
{ 
String s1="Nitish"; 
String f1=String.format("name is %s",s1); 
String f2=String.format("value is %f",54.15789); 
String f3=String.format("value is %54.15f",54.15789);
System.out.println(f1); 
System.out.println(f2); 
System.out.println(f3); 
  }
}

Output:

name is Nitish
value is 54.157890
value is 54.157890000000000

Java String format() Example 3

public class JavaStringFormatEx3 { 
    public static void main(String[] args) { 
        String s1 = String.format("%d", 101);          // Integer value
        String s2 = String.format("%f", 101.00);       // Float value
        String s3 = String.format("%x", 101);          // Hexadecimal value
        String s4 = String.format("%c", 'N');          // Char value 
        String s5 = String.format("%s", "Roy Nitish Nk"); // String value 
        System.out.println(s1); 
        System.out.println(s2); 
        System.out.println(s3); 
        System.out.println(s4); 
        System.out.println(s5); 
    } 
}

Output:

101101.
00000065
N
Roy Nitish Nk

Java String format() Example 4

public class JavaStringFormatEx4 { 
    public static void main(String[] args) {         
        String str1 = String.format("%d", 101); 
        String str2 = String.format("|%10d|", 101);  // Specifying length of integer 
        String str3 = String.format("|%-10d|", 101); // Left-justifying within the specified width 
        String str4 = String.format("|% d|", 101);  
        String str5 = String.format("|%010d|", 101); // Filling with zeroes 
        System.out.println(str1); 
        System.out.println(str2); 
        System.out.println(str3); 
        System.out.println(str4); 
        System.out.println(str5); 
    } 
}

Output:

101
101
101
101
0000000101

Related Topics

Contextual keywords in Java

Contextual keywords were earlier known as restricted identifiers and restricted keywords. Context keywords are chosen based on their expected placement in the syntactic grammar. These are the keywords in the code...

3 minutes read.

Java String startsWith() method

Java String startsWith() method checks whether current String starts with given prefix or not . Syntax: public boolean startsWith(String prefix) public boolean startsWith(String prefix, int offset) Parameters: prefix : It is sequence of character Returns: It returns...

2 minutes read.

Difference between throw and throws in java

This article shows you the core difference between “throw” and “throws”keywords in Java programming language.The throw keyword tells Java you want another part of the code to deal withthe exception,...

2 minutes read.

Difference between = = and equals ( ) in java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

6 minutes read.

Automorphic Number Program in Java

We will learn about automorphic numbers through examples in this article, and we'll also make Java programmes that can determine whether a specific number was automorphic or not. What is an...

3 minutes read.

Java extend multiple classes

In Java, what does extend mean? One of several Java inheritance keywords is extended, meaning we pass all or most of the Parent class's characteristics through to the Child class. The...

3 minutes read.

Java Clone Array

We frequently need to copy an array to back up its original components. We have a few unique numbers and strings, including Armstrong numbers, palindrome numbers, and palindrome strings. To...

4 minutes read.

Java Imageio

The javax.imageio package contains the final class known as Java ImageIO. For easy image reading, writing, and simple encoding and decoding, the class offers a convenient way. The class offers...

4 minutes read.

Arithmetic exception in Java

Exception Handling is one of the most potent ways of handling runtime faults and preserving the application's normal flow. In Java, an exception is an out-of-the-ordinary state, and exceptions are...

3 minutes read.

Java String substring() method

Java String substring() method returns a part of the String. Syntax: public String substring(int startIndex) public String substring(int startIndex, int endIndex) Parameters: startIndex : starting index endIndex : ending index Returns: It returns a specified String. Throws: StringIndexOutOfBoundsException if start...

2 minutes read.

Level order Traversal of a Binary Tree in Java

In Java, a level order traversal of a tree structure is also referred to as the breadth-first traversal of the binary tree. Regarding the subsequent binary tree: Level order traversal is as...

5 minutes read.

Java Print Writer

PrintWriter: It is used to write output data from the file. It is the class of Java.io package. It inherits the properties of the Writer class. Writer Class: It is a class of...

4 minutes read.

Heap Sort in Java

Heap Sort in JavaHeap sort in Java uses the data structure binary heap, min-heap, or max heap to do the sorting of elements. Since min-heap always gives the minimum element first,...

8 minutes read.

Java Integer doubleValue() method

The doubleValue() method of Integer class returns a double value for this Integer after a widening primitive conversion. Syntax public double doubleValue() Parameters NA Specified by This method is specified by doubleValue in class Number Return Value This...

1 minute read.

How to initialize string array in Java

In this tutorial, we will learn how the string array is initialized in java. The initialization of string array in the java by using the NEW (it creates the object for...

3 minutes read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

Non-primitive data types in Java

The kind of data stored in the variable is determined by its type. The type describes the data category (different sizes and values). These are not already built into the devices....

4 minutes read.

Java Integer compareUnsigned() method

The compareUnsigned() method of Integer class compares two int objects numerically by treating the values as unsigned. Syntax public static int compareUnsigned(int x , int y) Parameters The parameters ‘x’ and ‘y’ represent the...

1 minute read.

Find next greater number with same set of digits in Java

It contains a number (num). Finding the smallest number that has the same number of elements as num that is also larger than num is the task at hand. If...

8 minutes read.

Java Do While Loop

When we wish to test the exit condition at the end of the loop, we use a do-while loop. The do-while loop always executes its body at least once, because...

1 minute read.