I was giving a exam when i got this question What is the Output of following question
public class ClassicQuestion {
public static void main(String[] args)
{
int i = 25;
double j = 95.0;
System.out.println(j+=i);
System.out.println(i+=j);
}
}
And the option for answer was
I don't know the correct answer but i thought correct answer would be one, but showed me incorrect answer? how can we assign one variable to different types of data types without conversion ?
Correct Answer 3.
Because according to
Compound Assignment operators of the form
E1 op= E2
evalute toE1 = (T) ((E1) op (E2))
where T is the type of E1, except that E1 is evaluated only once
so your j+=i
changes to j=(double)(j+i)
and since now j
value is 120.0
next time i does i+=j
it changes to i=(int)(i+j)
hence 145
so OUTPUT is 120.0 145
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다