• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

compile error

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In the following code, compiler throws at line#2. Why is that? Shouldn't it complain about line#1 first. If you fix line#2 by using a cast operator, it then complains about line #1 that 'i' is uninitialized. Any explanation from you experts?
Thanks,
deep
public class Temp {
public static void main(String [] args) {
int i; //1
char t = i; //2
System.out.println(" t = " + t);
}
}
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Line no 1 is a perfectly valid line of code as it just declares a local int variable i. So when you try to assign it to char, it first checks whether the assignment is possible. It is not. So it cries out.
So if you type cast int to char and then assign, the first problem is resolved. But the thing is var i has not been initialised. So the assignment is not possible and the compiler cries foul again.
The point being:
1) The two errors are independent of each other. Thats why the compiler didn't report two erros the very first time.
2) Both the errors occur at line no 2. Line no one is perfectly OK
Rgds,
Anupreet
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anupreet is correct, This is my explanation:
In the following code, compiler throws at line#2. Why is that?
you're trying to fit an integer into a char, and that's not valid unless you cast it. By casting you're telling the compiler that you are aware of the risk of performing this operation.
Shouldn't it complain about line#1 first
No. You have declared a local variable which is not initialized. The compiler will only complain when you start using it (of course, when you're doing something different than initializing)
If you fix line#2 by using a cast operator, it then complains about line #1 that 'i' is uninitialized.
Yup, because of my last explanation
 
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic