Training Java – part 2 – concatenate char, float, double and integer

It is second topic given execises in Java.

In this example you can see how will be result in print method if it will be set types of number,char or string parametr into it. Write this code:

public class Proba {

	public static void main(String[] ar ) {
 int a = 12;
 float b = 15.26f;
 double c = 15.26;
System.out.println("int = "+a+", float = "+b+", double = "+c);  	
    System.out.print("(1) int+float ");   System.out.println(a+b);	   
    System.out.print("(2) float+int ");   System.out.println(b+a);
    System.out.print("(3) string+int+float ");   System.out.println(""+a+b); 
    System.out.print("(4) int+float+string ");   System.out.println(a+b+"");   
    System.out.print("(5) int+double ");   System.out.println(a+c);	
    System.out.print("(6) double+int ");   System.out.println(c+a);                
    System.out.print("(7) string+int+double ");   System.out.println(""+a+c); 
    System.out.print("(8) int+double+string ");   System.out.println(a+c+"");
    System.out.print("(9) float+double ");   System.out.println(b+c);	
    System.out.print("(10) double+float ");   System.out.println(c+b);  
    System.out.print("(9) string+float+double ");   System.out.println(""+b+c);
    System.out.print("(10) letter a has code: ");   System.out.println((int)'a');
    System.out.print("(11) only letter: ");   System.out.println('a');
    System.out.print("(12) letter+int ");   System.out.println('a'+a);    
    System.out.print("(13) int+letter ");   System.out.println(a+'a');  
    System.out.print("(14) letter+float ");   System.out.println('a'+b);    
    System.out.print("(15) float+letter ");   System.out.println(b+'a');      
    System.out.print("(16) letter+double ");   System.out.println('a'+c);    
    System.out.print("(17) double+letter ");   System.out.println(c+'a'); 

It displays result:

As you can notice, that float and double gives double, int and float gives float. And int and double gives double.

int + double = double
float + double = double
int + float = float
char + int = int
char + float = float
char + double = double
string + { int or float or double } = string
{ int or float or double } + string = { int or float or double } with added string

First occurence string datatype concatenates all values. If first is letter and the next is int and double, it gives float value(the letter is change into int code ). If this letter will be the last value, the result will be double value.


Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.