Coin163

首页 > Spring MVC文件上传

Spring MVC文件上传

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1073

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
入口地址https://www.aliyun.com/minisite/goods

相关推荐:JQuery Uploadify v3.2.1 上传图片并预览(基于spring mvc框架开发)

http://blog.csdn.net/java0311/article/details/44885933 Controller后台处理: [java]  view plain   copy   package com.news.controller;      import java.io.File;   import java.io.FileInputStream;   import java.io.IOException;   import java.io.

up vote 0 down vote favorite

This question already has an answer here:

I'm doing an assignment in java about overloading, overriding and interfaces. One of the parts requires me to create overloading methods, with short, int, float and double. The methods only need to sum the numbers, easy enough.

Here is where I'm stuck:

public short sum(short x, short y)
{
    System.out.println("I'm method on line 10");
    return x+y;
}

I have this method, eclipse kept flagging by suggesting I do a (int) type cast. If I did that, it would defeat the purpose of this assignment. So after doing some googling, I wrote this:

public short sum(short x, short y)
{
    System.out.println("I'm method on line 10");
    short t = (short)(x+y);
    return t;
}

In my main method, I created an object:

public static void main(String[] args) 
{
    OverloadingSum obj1 = new OverloadingSum();     
    System.out.println(obj1.sum(3,3));
}

So, when I run this it'll tell me that it used the int sum(int x, int y) method for the values (3,3) and not the short sum method i want it to use. My question is what numbers can I test so that the short method is called and not the int method.

java methods overloading short share | improve this question edited Nov 18 '15 at 6:33 EJP 211k 18 156 274 asked Nov 18 '15 at 6:29 Naman 77 1 2 8

marked as duplicate by RC., Community♦ Nov 19 '15 at 6:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

     use Short.valueOf("3") for example (but using short is usually a bad idea) –  RC. Nov 18 '15 at 6:33      @RC. No and no. You don't need to use a string literal, it is just wasteful, and this is not a question about instantiating Short objects. –  EJP Nov 18 '15 at 6:36 add a comment  | 

4 Answers 4

active oldest votes up vote 1 down vote accepted

To do this, you can explicitly cast the parameters to shorts to force the short method to be called.

For example, change:

System.out.println(obj1.sum(3,3));

to:

System.out.println(obj1.sum((short) 3,(short) 3));
share | improve this answer answered Nov 18 '15 at 6:33 Lambda Ninja 8,599 10 32 66      I guess thats one way of doing it, thanks for the help. –  Naman Nov 18 '15 at 6:39 add a comment  |  up vote 1 down vote
obj1.sum(3,3)

You need to either cast the values:

obj1.sum((short)3,(short)3)

or use variables:

short v1 = 3;
short v2 = 3;
// ...
... obj1.sum(v1,v2)...
share | improve this answer answered Nov 18 '15 at 6:34 EJP 211k 18 156 274 add a comment  |  up vote 0 down vote

type casting is required while passing arguments, use:

public static void main(String[] args) 
{
OverloadingSum obj1 = new OverloadingSum();     
System.out.println(obj1.sum((short)3,(short)3));
}

Integer literals implicitly have the type int, and converting from an int to byte or short potentially loses information, so it requires explicit casting.

share | improve this answer edited Nov 18 '15 at 6:34 answered Nov 18 '15 at 6:33 amit dayama 1,870 1 3 19      Nice! Posted the same answer 2 seconds before I did =) –  Lambda Ninja Nov 18 '15 at 6:34      It is required when passing numeric literal arguments. –  EJP Nov 18 '15 at 6:34 add a comment  |  up vote 0 down vote

You can try something like this

public static void main(String[] args) {
        short x=3;
        short y=3;
        addShort(x,y);

    }

    public static void addShort(short a,short b)
    {
        short c=(short)(a+b);

    }

Other than this,you need to explicitly cast while passing them like this

addShort((short) 3,(short)3);
share | improve this answer answered Nov 18 '15 at 6:36 Prince Mani Gupta 2,091 2 8 25 add a comment  | 

Not the answer you're looking for? Browse other questions tagged java methods overloading short or ask your own question.

原文

up vote 0 down vote favorite This question already has an answer here: Java instantiate Short object in Java 2 answers I'm doing an assignment in java about overloading, overriding

------分隔线----------------------------