• 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

wrong thing in oca book

 
Greenhorn
Posts: 8
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ???
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic