0

So in this basic Java program,

public class HelloWorld{
    public void testFunc(){
        System.out.println("Class = "+this);
    }

    @Override
    public String toString(){
        return "TEST";
    }


    public static void main(String[] args){
        int i = 5;
        HelloWorld hw = new HelloWorld();
        System.out.println("Hello, World");
        hw.testFunc();
        System.out.println(i);
    }
}  

the line System.out.println(i); automatically converts the int i into a String. I thought any function that requires a String type argument would automatically do this conversion in java (unlike in C). However, if I try this in Android
Toast.makeText(getApplicationContext(), i, Toast.LENGTH_SHORT).show();
where i declared as an int in the extended activity class, the app crashes upon launch. So how does this conversion work in Java?

user13267
  • 6,871
  • 28
  • 80
  • 138

3 Answers3

5

Its not an implicit conversion. The pintln() is overloaded by lots of datatypes:

public void println(int x) {
    synchronized (this) {
        print(x);
        newLine();
    }
}

And in your case makeText() is not.

rocketboy
  • 9,573
  • 2
  • 34
  • 36
  • so the gist is, the function has to designed to specifically convert any given type of argument to the required type of argument? – user13267 Aug 14 '13 at 05:20
  • It is not `converting` anything. The class has a suitable method for all supported datatypes and the appropriate implementation is picked up. If you look at the link in my answer you'll understand. – rocketboy Aug 14 '13 at 05:23
  • ok thanks. So there are multiple versions of println, and each one converts whatever the argument is, into a String. Is this correct? – user13267 Aug 14 '13 at 05:25
  • But then again, if the argument had been something like "value ="+i, in this case isn't the integer i typecasted to a String? How does this work? – user13267 Aug 14 '13 at 05:27
  • 1
    Yes `"value ="+i` uses an implicit conversion. For more [look here](http://stackoverflow.com/questions/11408427/how-does-the-string-class-override-the-operator) – rocketboy Aug 14 '13 at 05:31
  • I see that using `String.valueOf(i)` as well as `"here"+i` both work in the `Toast` example. Thank you very much for your help. – user13267 Aug 14 '13 at 05:43
2

System.out.println(int) does convert int to char, it converts int to String using String.valueOf(int). Try explicit conversion System.println((char)i) and you will see that it prints a different result, it will be U+0005 Unicode character

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1

To understand Type Conversions in JAVA:Refer

Every expression written in the Java programming language has a type that can be deduced from the structure of the expression and the types of the literals, variables, and methods mentioned in the expression.

It is possible, however, to write an expression in a context where the type of the expression is not appropriate. In some cases, this leads to an error at compile time. This holds true with Android Toast Example.

In other cases, the context may be able to accept a type that is related to the type of the expression; as a convenience, rather than requiring the programmer to indicate a type conversion explicitly, the Java programming language performs an implicit conversion from the type of the expression to a type acceptable for its surrounding context. And this holds true for System.out.println(i) Example

Nargis
  • 4,687
  • 1
  • 28
  • 45
  • Thanks, but I am now convinced that println() does not perform data conversion. Please see the link in @rocketboy 's answer – user13267 Aug 14 '13 at 05:37