Use string representations of the values of primitive data types in string concatenation - Java Language Basics

Java examples for Language Basics:String

Description

Use string representations of the values of primitive data types in string concatenation

Demo Code

public class Main {
  public static void main(String[] args) {
    boolean b1 = true; 
    boolean b2 = false; 
    int num = 365; 
    double d = -0.0; 
    char c = 'A'; 
    String str1; /*from   w  ww.  j a va  2s  . c  o m*/
    String str2 = null; 
      
    str1 = b1 + " friends";  // Assigns "true friends" to str1
    System.out.println(str1);
    str1 = b2 + " identity"; // Assigns "false identity" to str1 
    System.out.println(str1);
    // Assigns "null and void" to str1. Because str2 is null, it is replaced 
    // by a string "null" by the concatenation operator 
    str1 = str2 + " and void"; 
    System.out.println(str1);
    str1 = num + " days"; // Assigns "365 days" to str1 
    System.out.println(str1);
    str1 = d + " zero";   // Assigns "-0.0 zero" to str1 
    System.out.println(str1);
    
    str1 = Double.NaN + " is absurd"; // Assigns "NaN is absurd" to str1 
    System.out.println(str1);
    
    str1 = c + " is a letter"; // Assigns "A is a letter" to str1
    System.out.println(str1);
    str1 = "This is " + b1;    // Assigns "This is true" to str1 
    System.out.println(str1);
    // Assigns "Beyond Infinity" to str1 
    str1 = "Beyond " + Float.POSITIVE_INFINITY ;
    System.out.println(str1);

  }
}

Result


Related Tutorials