Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling method ----doubt

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi



I would like to call go(int) method..but I don�t know how to call that.please help me.
 
Jean-Francois Briere
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no go(int) method in your Objective14 class but there is a go(short) method.
So you could call it by doing: myObject.go((short)99);

Regards
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "99" is ambiguous; it's an int, but it could equally well be converted to a char or to a short; because there's no way to decide, it's an error. All you need to do is apply a cast -- i.e., go((char) 99) or go((short) 99) . This tells Java which method you meant.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou very much Ernest and Jean.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a doubt in implicit casting when i working on the above coding.
char(16 unsigned ) can implicitly convert to Int,long,float,double.
but Short<------char
Ex short s=100;
char c;
s=c;//-------loss of precision error.

Is my understanding correct.Why i am asking this doubt,because both are 16 bits.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Short is signed, and char is unsigned. char can therefore hold larger positive values than short.
 
Koh Khai Huat
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest,

May I ask what is the meaning of Short is signed, and char is unsigned. What does the Signed and The Unsigned means???

regards
KKH
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by koh khai huat:
What does the Signed and The Unsigned means???


Both char and short are sixteen bits, but the bits in a char are used to represent the numbers from 0 to 65535, while those in a short represent -32768 to +32767 . Therefore, chars can take on many values that char cannot, and vice-versa.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by koh khai huat:
Hi Ernest,

May I ask what is the meaning of Short is signed, and char is unsigned. What does the Signed and The Unsigned means???

regards
KKH


Hi,

you can think about the Singed and Unsigned in this way also.
these are normal binary computational concepts. Here both short and char takes 2 bytes means 16 bits.
In those 16 bits the short will use only 15 bits to represent the original magnitude of the number and the remaing bit to store the sign, 1 for negative and 0 for positive so it will accomdate 2^15 only.
That is

<SingBit><0000 0000 0000 000> to <SingBit><1111 1111 1111 111>


But the char will use the whole 16 bits for magnitude means it will accomadte 2^16.
That is
<0000 0000 0000 0000> to <1111 1111 1111 1111>

These all must be instructed to host system how to treat them.

Hope you got it.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shiva Mohan:
Hi



I would like to call go(int) method..but I don�t know how to call that.please help me.



Hi, the error is not because the java is unable to call which method. please remember the error message ambigious has been removed from java.

The problem is auto conversion of the data types will be occur only for assignment operator but not for the mehtod parameters, so java will not be able to find a method with Int as the parameter.

You can do two things.

1) Explicit Cast it to call the corresponding method.

2) Create a method with Int as the parameter.

Thanx
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by j rangavallika:-
In those 16 bits the short will use only 15 bits to represent the original magnitude of the number and the remaing bit to store the sign, 1 for negative and 0 for positive so it will accomdate 2^15 only.


I am afraid you are mistaken there.

Java does not use that method of integer arithmetic, which is called sign and magnitude (S&M). An S&M integer in 16 bits runs from minus32767 (-2^15 + 1) to minus0, AND from 0 to 32767, so it has two values of zero, and a total range one less than -32768 to +32767, as Ernest Friedmann-Hill correctly quotes.

Java uses the two's complement format, where the bits from 0 to 15 (reading right to left, remember), represent
  • 1
  • 2
  • 4
  • 8
  • 16
  • 32
  • 64
  • 128
  • 256
  • 512
  • 1024
  • 2048
  • 4096
  • 8192
  • 16384
  • and minus32768, respectively.
  • This is a gross simplification of the two's complement format, but it will work.

    The two's complement format has at least these four advantages over S&M:
  • It is monotonic: each binary pattern represents a number one different from its neighbours.
  • It uses the whole of the available range, because there is no duplication
  • An adding circuit can add complement numbers very easily, and subtract them by entering a single bit representing "minus"
  • Nobody cracks bad jokes about its acronym

  • I found a nice article about two's complement [url=http://en.wikipedia.org/wiki/Two%27s_complement]in Wikipedia, [/url]if it is any use to you.

    CR
    [ May 12, 2006: Message edited by: Campbell Ritchie ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic