aspose file tools*
The moose likes Beginning Java and the fly likes calling method ----doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calling method ----doubt" Watch "calling method ----doubt" New topic
Author

calling method ----doubt

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 479
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

Joined: Mar 03, 2004
Posts: 101
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 479
Thankyou very much Ernest and Jean.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 479
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

Short is signed, and char is unsigned. char can therefore hold larger positive values than short.
Koh Khai Huat
Ranch Hand

Joined: Aug 05, 2005
Posts: 100
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


if (!WorkHard){
System.out.println("Fail!!!");
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Oct 20, 2005
Posts: 257

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

Joined: Oct 20, 2005
Posts: 257

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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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's_complement]in Wikipedia, [/url]if it is any use to you.

    CR
    [ May 12, 2006: Message edited by: Campbell Ritchie ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: calling method ----doubt