Method Overloading with varargs - java

I am a bit confused about this topic, reason being in this code:
public class File
{
public static void main(String[] args)
{
numbers();
}
static void numbers(int...x)
{
System.out.println("Integers");
}
static void numbers(byte...x)
{
System.out.println("Byte");
}
static void numbers(short...x)
{
System.out.println("Short");
}
}
The output of this code is "Byte", and the reason is the most specific type is chosen, since among byte, short and int the most specific type is byte, that's why it is chosen.
But if I modify my code to-
public class File
{
public static void main(String[] args)
{
numbers(1, 4);
}
static void numbers(int...x)
{
System.out.println("Integers");
}
static void numbers(byte...x)
{
System.out.println("Byte");
}
static void numbers(short...x)
{
System.out.println("Short");
}
}
The output is "Integers", and I'm unable to figure out why? I know in case of arithmetic instructions byte and short are implicitly promoted to int, but here we are calling a method with values which is within the range of byte and short, then how the method with int arguments is invoked?
And also, if I comment out the method with int arguments, then the code shows an error of no suitable method found. Why???

1 and 4 are integer literals. So the int... version is called.
There are no byte or short literals, but if you were to call Numbers((byte)2, (byte)3); the byte... version would be called.

Related

shadowing static variable ( global variable)

Why in the next code I get pro.x=11?. it should be 22. Please somebody throws a light.
public class Pro {
static int x=11;
public static void main(String[] args) {
Pro pro=new Pro();
pro.call(5);
System.out.println(Pro.x);
System.out.println(pro.x);
}
public void call(int x){
x=22;
}
}
you are not setting the static/global variable to 22, but rather the value of the argument passed. Considering it's a primitive value, call by value is used and not call by reference.
Edit: In fact as pointed out in comments, java doesn't have have call by reference, but rather call by value of a reference.
If you just wisht to change the global variable, no argument is necessary for your function, you can do it this way:
public class Pro {
static int x=11;
public static void main(String[] args) {
Pro pro=new Pro();
pro.call();
System.out.println(Pro.x);
System.out.println(pro.x);
}
public void call(){
Pro.x=22;
}
}

Organize methods for each primitives in Java

I'm trying to structure a bunch of static methods I have coded such that they are easy to follow in groups. (I still have to read from and write in the console.)
In a "console" package, I have a Java class called "Read", which contains nested classes "Number", "NumberMinimum", "NumberMaximum"... that all contain methods to interpret each data types (Byte, Short, Int...) since generics don't apply.
Console
Read
Number
NumberMinimum
NumerMaximum
...
That way, I just import console.Read in my project class, and call my methods using Read.Number.getInt() for instance.
Is there a better way to organize my methods than using nested classes?
I think you should work with java.lang.Number class. It contains all method you want.
Take a look in this example:
public class Tests {
public static void main(String[] args) {
float number = 12;
Read.setNumber(number);
byte byteValue = Read.getNumber().byteValue();
System.out.println(byteValue);
}
public static class Read{
private static Number number;
private static Number numberMinimum;
private static Number numberMaximum;
public static Number getNumber(){
return number;
}
public static void setNumber(Number number){
Read.number = number;
}
public static Number getNumberMinimum() {
return numberMinimum;
}
public static void setNumberMinimum(Number numberMinimum) {
Read.numberMinimum = numberMinimum;
}
public static Number getNumberMaximum() {
return numberMaximum;
}
public static void setNumberMaximum(Number numberMaximum) {
Read.numberMaximum = numberMaximum;
}
}
}

In Java, why Object class object in method parameter can not accept null [duplicate]

This question already has answers here:
How to do method overloading for null argument?
(7 answers)
Closed 8 years ago.
Please explain why i'm getting "Method with String param" in output.
And when i remove the comments from display(Test x) method, it says "Reference to display is ambiguous".
class Test
{
int a;
int b;
}
public class TestIt
{
public static void display(String x)
{
System.out.println("Method with String param");
}
public static void display(Object x)
{
System.out.println("Method with Object param");
}
/*
public static void display(Test x)
{
System.out.println("Method with Test param");
}
*/
public static void main(String args[])
{
display(null);
}
}
Because null is a valid value for Object and String. You can cast,
display((String) null);
Will output
Method with String param
or
display((Object) null);
for
Method with Object param
Because when figuring out which method to call, the compiler picks the most specific method it can find that matches the argument. Both display(String) and display(Object) match a call to display(null), but display(String) is more specific than display(Object), so the compiler uses that. When you uncomment the display(Test) version, though, the compiler can't make a choice because both display(String) and display(Test) are equally specific.
For all the gory details, see §15.12 of the JLS.

Confusion in Function Overloading in JAVA [duplicate]

This question already has answers here:
Calling overloaded functions with "null" reference
(2 answers)
Closed 8 years ago.
I am new learner of Java. I am trying to understand the concept of passing argument in function and function overloading. I found few example on a java web site which where following code is given, my doubt is if null is passed to nh() then how "string" is displayed in output. Here is the code
public class CLI_APP
{
public static void main(String[] args)
{
jh(null);
}
public static void jh(String s)
{
System.out.print("String");
}
public static void jh(Object o)
{
System.out.print("Object");
}
}
In same code if below lines are added
public static void jh(Integer s)
{
System.out.print("Integer");
}
I got an compilation error of
"Method is ambiguous"
WHY this happen?
my doubt is if null is passed to nh() then how "string" is displayed in output
Overloaded methods are matched from bottom to top level of classes. Object class sits at the top level so it will be matched at the last. Having said that, null is first match to the String parameter method and a String can be null so this method is called.
If you also add the following method
public static void jh(Integer s)
to your code then jh(null) call introduces the ambiguity between Integer and String as both can be null.
Lean more here : Java Language Specification: Choosing the Most Specific Method
Integer and String both support null , so it generate ambiguity error at compile time,
Now, if you use int instead of Integer then it will work because int not support null.
public class testJava {
public static void main(String[] args)
{
jh(null);
}
public static void jh(String s)
{
System.out.print("String");
}
public static void jh(Object o)
{
System.out.print("Object");
}
public static void jh(int o)
{
System.out.print("Object int");
}
}
Java always use the most specific method. In your first example it will print "String" instead of "Object" because String is more specific than Object.
In your second example, java can´t choose if null is better for Integer or String. You should cast your call or use primitives to remove your ambiguity.
This would work:
public static void jh(int s)
{
System.out.print("Integer");
}
Also this would work:
jh((String) null);

Method Call Order

The following code compiles fine in Java:
public static void main(String[] args) {
int i =5;
call(i);
}
static void call(int i){
System.out.println("int");
}
static void call(long i){
System.out.println("long");
}
static void call(Integer i){
System.out.println("Integer");
}
static void call(Object i){
System.out.println("Object");
}
But the following code gives compile time error:
public static void main(String[] args) {
int i =5;
call(i);
}
static void call(int... i){
System.out.println("int...");
}
static void call(long... i){
System.out.println("long...");
}
static void call(Integer... i){
System.out.println("Integer...");
}
static void call(Object... i){
System.out.println("Object...");
}
Why the similar call mechanism is not maintained by Java while working with var-args? In the second example also, the call should go to method static void call(int... i)
Here is answer
Java doesnot work well with overloading varargs method.
Here is what Specs provide :
So when should you use varargs? As a client, you should take advantage of them whenever the API offers them. Important uses in core APIs include reflection, message formatting, and the new printf facility. As an API designer, you should use them sparingly, only when the benefit is truly compelling. Generally speaking, you should not overload a varargs method, or it will be difficult for programmers to figure out which overloading gets called.
As you are declaring the method as Static void call(int... i) and the method expects the int array but while calling this method you are sending only one integer value.
Static void call(int... i) is Same as Static void call(int[] i)
Variable arguments are treated as arrays in java. So instead of passing an int value, pass it like an array . For eg.
int[] i ={5};
call(i);
The link given by #NilsH explains the reason clearly. Please find the details at
http://www.xyzws.com/Javafaq/why-overloading-a-varargs-method-doesnt-work-for-the-primitive-type-and-its-object-wrapper-type/50

Categories

Resources