Wednesday, 20 January 2021

Is below statement compile?

Is below statement compile.

Serializable data = (10 > 3) ? 1 : "10 is greater than 3";

 

Yes, above statement compile and run also. Since Serializable is implemented by both Integer and String, above statement can compile and run without any issues.

 

App.java

package com.sample.app;

import java.io.Serializable;

public class App {

	public static void main(String args[]) {
		Serializable data1 = (10 > 3) ? 1 : "10 is greater than 3";
		Serializable data2 = (10 < 3) ? 1 : "10 is greater than 3";

		System.out.println(data1 + " " + data1.getClass().getName());
		System.out.println(data2 + " " + data2.getClass().getName());
	}
}

 

Output

1 java.lang.Integer

10 is greater than 3 java.lang.String

 

You may like

Miscellaneous

Interview Questions

Programming Questions

How to declare ArrayList with values?

How to initialize string array?

Implement re-try-catch

NumberFormatException Explained

Initial heap size set to a larger value than the maximum heap size


No comments:

Post a Comment