Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

return type problem

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test {

public static void main(String[] args) {
m1();
}
static byte m1( ) {
final char c1 ='2';
return c1; //line 1
}
static byte m2(final char c2) {
c2='3';
return c2; //line 2
}
}

while i am compiling this code i am getting compiler error at line 2 only.i am not getting compiler error at line 1 why.please help anybody.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please do not post the same question multiple times.

Note that if you want to edit your post after submitting it, you can do so by clicking the button.
[ September 02, 2007: Message edited by: Jesper Young ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Sam!

Welcome to the ranch!




I added a little comment to your code

You won't get a compiler error on line 1. char c1 is final and has one value once and for all. The char value of '2' can be converted into an int value of 50. This is a value that fits also into the byte range of a byte.

But you will get an error in line 2. The compiler sees that it gets a value that might have changed.
You should also get an error in the "line in the middle" because you try to change a final variable. But the compiler sees the error in line 2 first and does not report the other one.
If you change line 2 to return 1; (which is a legal return for a byte) then compiler would mark an error in the line in the middle: "final parameter c2 may not be assigned".

If you change the whole method to

Then the former line 2 is also wrong, because you cannot give a char back because it doesn't fit always into a byte.
It does not help now that the char is final. Because you could easily invoke the method with a char value of - say '\u0080' (128, just one too big).



Yours,
Bu.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic