Java conversions and 7 Golden rules of widening, boxing & varargs

By , last updated November 12, 2017

Here is a very useful java conversion table that I’ve created while preparing for the Certified SE Java Programmer exam.

primitives conversion

Read also: What is and how to use Java varargs.

And here is a complete overview of the Java Golden rules of widening, boxing & varargs:

1. Primitive Widening > Boxing > Varargs.
2. Widening and Boxing (WB) not allowed.
3. Boxing and Widening (BW) allowed.
4. While overloading Widening + vararg and boxing + vararg are mutually exclusive of each other.
5. Widening between wrapper classes not allowed
6. Widening+varArgs & Boxing+varargs are individually allowed (but not allowed in overloaded version of method)
7. Boxing+Widening is preferred over Boxing+Varargs.

Examples:

We are calling a function foo(5):

foo(5);

foo(Integer i) 	and foo(long l) 		// long (by Rule 1)
	//Explanation: 
	//int to Integer - boxing
	//int to long - widening
	// Rule 1: widening > boxing
	
foo(int...i) 	and foo(Integer i) 		// Integer (by Rule 1)
	//Explanation: 
	//int to int... - varargs
	//int to Integer - boxing
	// Rule 1: boxing > varargs

foo(long...l) 	and foo(Integer i)  	//  Integer (Rule 1)
	//Explanation: 
	//int to long... - varargs
	//int to Integer - boxing
	// Rule 1: boxing > varargs

foo(Object o) 	and foo(int...i)    	// Object o (Rule 1)
	//Explanation: 
	//int to int... - varargs
	//int to Object - boxing
	// Rule 1: boxing > varargs
	
foo(Object o) 	and foo(long l)     	// long l (Rule 1)
	//Explanation: 
	//int to long - widening
	//int to Object - boxing
	// Rule 1: widening > boxing

foo(Object o) 	and foo(Integer... l)	// Object o (Rule 1)
	//Explanation: 
	//int to Integer... - boxing + varargs
	//int to Object - boxing
	// Rule 1: boxing > varargs

foo(Long l) 	and foo(int...i)  		// int...i (Rule 2 )
	//Explanation: 
	//int to Long - widening (to long) + boxing (to Long) 
	//int to int... - varargs
	// Rule 2: widening + boxing is not allowed

foo(Long l) 	and foo(Integer...i)	// Integer...i(Rule 2)
	//Explanation: 
	//int to Long - widening (to long) + boxing (to Long) 
	//int to Integer... - boxing + varargs
	// Rule 2: widening + boxing is not allowed

foo(Object o) 	and foo(Long l)     	// Object o (Rule 2)
	//Explanation: 
	//int to Long - widening (to long) + boxing (to Long) 
	//int to Object - boxing
	// Rule 2: widening + boxing is not allowed
	
foo(long...l) 	and foo(Integer...i)	//  ambiguous (Rule 4)
	//Explanation: 
	//int to long... - widening + varargs
	//int to Integer... - boxing + varargs
	// Rule 4: widening + vararg and boxing + vararg are mutually exclusive of each other.

More explanation here.

Senior Software Engineer developing all kinds of stuff.

Comments

Be the first to comment.

Leave a Reply


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*