• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Doubt in primitive assignment

 
Ranch Hand
Posts: 33
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This might be very silly question but I am not able to understand the concept.


this is ok..

However if I have method..


I cant invoke it with go(7), here I have to do a cast as, go((byte)7).

Why it worked in assignment but not for method invocation?
 
author & internet detective
Posts: 42160
937
Eclipse IDE VI Editor Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vivek,
In the second example, you have an int in go(7). Java "knows" it is an int because it is just the number 7. It so happens that there is no go(int) method so the compiler gives you an error. But there *could* be so Java can't make assumptions. Or go(int) could be added after. The problem is that if go(int) existed, go(7) would call it. And Java doesn't want the method called to change on you later.

Whereas with byte b = 7, there's no ambiguity. You obviously mean for it to be a byte because it is an assignment.
 
Ranch Hand
Posts: 125
Scala Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
keep in mind that byte b = 7; will compile and byte b = 133; won't. An explicit cast is necessary in the second case.
 
reply
    Bookmark Topic Watch Topic
  • New Topic