• Post Reply Bookmark Topic Watch Topic
  • New Topic

wrong thing in oca book  RSS feed

 
Yazeed Hamdan
Greenhorn
Posts: 8
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi every one
in the oca book they write that this code will not compile (page 195-196)
public static void main(String[] args) {
        a(5);
       
    }
    public static void a(long c){
        System.out.println(c);
    }
    public static void a(long... c){
        System.out.println(c);
    }

because java do only one step convert while search for best overlode method
well
i tried to write it and it work with out prob
i want to ask
which result i can lay on for the exam ???
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can assign an int literal to a long variable as compiler will do an optimization for you here,it will put a "L"(or lowercase) after 5.long is 64 bits(so it can hold 32 bit int).may be in a book it is "Long" despite of "long"(in that case it would not compile).
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yazeed Hamdan wrote:java do only one step convert while search for best overlode method
well
i tried to write it and it work with out prob


I don't know what you mean by "one step convert" ... but ... there are quite a few rules that apply to resolving the overloaded methods. And due to backward compatibility reasons, one of the rules states that it is done in 3 phases. Specifically, if a phase fails, then the next phase is used.

Anyway, the second method doesn't even apply until the third phase, because it is using varargs. As for the first method, it passes in the first phase. So, there is nothing complex here. Most of the overloaded method resolution rules aren't even needed.

Henry

PS... Here is the relevant part in the Java Language Specification ... link to JLS
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell us which book it is, so we can confirm the original code for ourselves.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case if it was Long it would not compile as Long is a reference type and it can only refer objects of type Long though 5 is an int literal(primitive data type) which does not have any relation with class Long.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My book covers that topic on pages 195 and 196, but with different method names and Long instead long. Not sure if this was a changed example from my book or another book happens to cover the same concept on the same pages.

In any case, Praveen is right. This works with long because it is a primitive which only requires one conversion (int to long). It does not work with Long because that would require two conversion (int to long and long to Long)
 
Yazeed Hamdan
Greenhorn
Posts: 8
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi every one
i am really really sorry
it seem like i try my own version of code that i did it Without having to be aware of the difference between long and Long
so it work without prob
I did not notice that, but when I read the reply minute ago,


Jeanne Boyarsky
it seems like that they are the same book
Honestly I do not know how to apologize for this hasty act
i tried to delete the post once i tried the code again and realized my mistake but i couldn't
i am really really sorry
pleas accept my apologize
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that Jeanne will agree with me when I say, there's no need to apologize. Learning Java has plenty of ways to make errors, plenty of picky details which are easy to get wrong. Many people make these errors and that's exactly what this site is for. You can come here and ask your question and improve your Java knowledge and it's a win for everybody. So carry on with learning Java -- it's quite likely you will have more questions, so don't be afraid to come and ask them here.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I know that Jeanne will agree with me ...

Yes, I agree. It is a million times better to be wrong on the forum and learn something so you get it right on the exam and when you code for real!
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thumbsup to everybody and a cow to OPP for showing us how things ought to be discussed on a forum
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!