• Post Reply Bookmark Topic Watch Topic
  • New Topic

Auto-conversion from byte  RSS feed

 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Evening,

I am playing around with code in one of the questions in Mala Gupta's excellent certification guides.
The question asks you to identify which method accepts three integers and returns their sum as a decimal number.

One of the correct options is listed below and I'm trying to find out why it's correct.




My hypothesis was:
   *  that the value in byte arg1 would be auto-converted to int
   *  then then the sum would be cast to double and returned

However the code return this error:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at UseSomeClass.main(UseSomeClass.java:13)
Java Result: 1

Any help always greatly appreciated.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using the javac compiler program to get a better error message that shows what the problem is.
 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using NetBeans so I'll try and find out how I can get a better error message.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javac SomeClass.java
SomeClass.java:12: error: cannot find symbol
        System.out.println(sc.numbers(1, 120, 1));
                             ^
  symbol:   method numbers(int,int,int)
  location: variable sc of type SomeClass
1 error
You are passing an integer, which is a compile‑time constant. Check carefully what you can do with compile‑time constants; I am pretty sure you can assign that 1 to a byte. But you cannot pass it as a byte to an argument; it is passed as an int, and there is no method which can accept that int.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammed Azeem wrote:I'm using NetBeans so I'll try and find out how I can get a better error message.


Regardless, running a program that doesn't compile is *not* a good idea. Read this...

https://coderanch.com/wiki/660183/Fix-Compiler-Errors-Running-Application

And BTW, the compile error is in regards to not finding a method that matches your required signature.

Henry
 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I think I've got it

So:

*  a variable of type byte can be assigned a whole number (provided it is in the range of byte)
*  but, that whole number is passed to an argument as int and since the parameter expects a byte, an error results.

I recall that in a previous chapter Mala Gupta says that the default type of integer literals int.
So  byte value = 1 would mean that:

*  the type of value is byte
*  but the type of '1' is int.

I have a suspicion its connected to my query.

The fog is almost cleared. Thank you for your help.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All these comments and nothing about the compiler error re the method defined on line 2 and the method called on line 12 having different names.
 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Henry for sharing that very illuminating article.

I think I see the central message: running un-compilable code in an IDE will report the errors in the order in which they are encountered during execution which may/may not be the the order that the compiler encounters them. And, since we should correct the first error during compile time and re-compile (and repeat if necessary), focusing on errors reported when running un-compilable code could be misleading - and I bet, cause further errors until you get a can of worms!

Norman, a slip of the fingers - the two methods indeed have different names (method signatures).

Thanks to everyone.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a slip of the fingers

Are you saying the code posted here was not copied and pasted from the original code you have on your PC, but was entered separately with possible changes in its contents?
 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Norman, I'm not sure I understand.

The multiple choice question (Question 3.3 Chapter 3 to be exact) in Mrs. Gupta's book asks the reader to select the correct methods that would return the sum of three integers as a decimal number.
A choice of four methods is given.

In order to test one of the methods, I had to define it in a class.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code in post#1 would never compile or execute because the method called on line 12 does not exist.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public double addNumbers(byte arg1, int arg2, int arg3)
...
System.out.println(sc.numbers(1, 120, 1));
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammed Azeem wrote:Hello Norman, I'm not sure I understand. . . .
Norman said something very simple. If the code you posted here is even slightly different from what Ms Gupta's book contains, the answer will be different.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!