Method Call Order - java

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

Related

I need to use an object created in one static method in another static method but have issue calling it

(This is a repost of a previous question I have where I modified the code so it is much easier to understand.)
public class LaptopSaleApp{
public static void main(String[] args){
selectLaptop();
selectSupportOptions(laptopOrder);//This parameter cannot be resolved into a variable
}
public static void selectLaptop(){
LaptopSalesOrder laptopOrder = new LaptopSalesOrder("Dell Latitude", 1399.00);
}
public static void selectSupportOptions(LaptopSalesOrder laptopOrder){
laptopOrder.setBasicIndicator(true);
}
}
I need two static methods here. The object has to be declared in the first method, and then is used in the second method for the setBasicIndicator setter. I then need to call both methods in my main to run the program, but I don't know what to put in the parameters for the method in the main.
You can't quite do this if the object is declared in the first method.
Here's what you should do:
public static void main(String[] args){
LaptopSalesOrder laptopOrder = selectLaptop();
selectSupportOptions(laptopOrder);
}
public static LaptopSalesOrder selectLaptop(){
return new LaptopSalesOrder("Dell Latitude", 1399.00);
}
public static void selectSupportOptions(LaptopSalesOrder laptopOrder){
laptopOrder.setBasicIndicator(true);
}

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;
}
}

Java program not sure where and how to execute main method whilst using static boolean

I've recently started using java and I am trying to make a program that checks to see if an array can be changed to ascending order by returning true/false.
That is the gist of it however I am having problems with the main class. The error I am getting is that the main method is not found in class.
public static boolean solution (int[] A){
int count = 0;
.......
.......
.......
for(int i=0; i<A.length; i++)
{
if(A[i] != B[i]) count++;
}
if(count > 2) return false;
return true;
}
}
Since I am doing this as my java summer homework I am abit confused as to where I should add the main method. I know it is supposed to be
public static void main (String args[])
However if I were to add that from the start of the code after the class I get errors. Is it because I cannot have
public static boolean and public static void main
in the same class?
Thanks.
You can have public static boolean and public static void main in the same class. A main method can be added anywhere within a class as long as you define it as its own standalone method, don't declare inside another method. For Example:
public class Example{
public void method1(){
....
}
public void method2(){
....
}
public static void main(String[] args){
....
}
}
oke if you are using an ide like intellij you must also configure which class has the main method in you project.
You CAN have a main method and other static methods in the same class. In fact you can have any type of method in the class. The reason your program cannot "find a main method" is because there either isn't one there, or your run configuration is off.
A java program starts, with some exceptions of the initializer blocks, at the main method. So you MUST have it in at least one class in your program.
From what it sounds like you want to test if the array is already in ascending order. Since all arrays that are not empty, can be sorted.
public class Example{
public static void main(String[] args){
int[] test = {1,2,3,4,5};
System.out.println(solution(test));
}
public static boolean solution (int[] a){
//returns true if in a
for (int i = 0; i < a.length-2; i++) {
if (a[i] > a[i + 1])
return false;
}
return true;
}
}
If your cannot find main method error continues. It is probably your run configuration
If you are getting an error, based on the way you phrased your question, it sounds like you have a method with the exact same method signature.
You can not have:
public static void main(String[] args)
{
}
public static boolean main(String[] args)
{
return false;
}
If this is what you have, you should probably rename the second method.

Method Overloading with varargs

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.

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);

Categories

Resources