• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Any function which return data type?

 
Rancher
Posts: 163
5
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am actually learning with the book from Jeanne & Scott, so there is an very interesting example for me:

x/y => results int.
But can I check it explicitly with some method saying it is really an int?

Thank you!
 
Rancher
Posts: 144
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you don't have to as it's specified so in the JLS: any math operation result in int if operand is smaller
 
Mike Savvy
Rancher
Posts: 163
5
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, thank you.
 
Marshal
Posts: 76866
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think there is any way to write 123.class or anything similar and actually have it compile.
 
Mike Savvy
Rancher
Posts: 163
5
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean something like this:

 
Campbell Ritchie
Marshal
Posts: 76866
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have never seen anything like that, sorry. I may be mistaken, but I don't think it is possible.
 
Saloon Keeper
Posts: 26290
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't really matter. Regardless of the size of the result, what matters is the size of the container (variable) that you store it in. And you will get a compiler error if you try and store something in a variable whose defined size might not be big enough to hold it. Dividing a short by a short using integer division should always yield a short value. Adding two shorts, however, can result in an arithmetic overflow if the results aren't widened to int. Even more so with multiplication, and even subtraction can have an issue.

Java is going to treat arithmetic as a mathematical abstraction while calculating, and the only "real world" considerations are when you exceed the capacity of the largest intermediate data type what the work can be widened to, it will throw an arithmetic exception. But, as I said, don't expect to get away with storing 2 tons of fertilizer in a 1-ton truck. Even if you "know" that the results will always fit, unless the compiler can 100% guarantee a fit, you're not going to be allowed to code for it.

 
Campbell Ritchie
Marshal
Posts: 76866
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . . Dividing a short by a short using integer division should always yield a short value.

That is what the mathematical abstraction would say, but Java has different ideas. Arithmetic is always done on types with width 32+ bits, so even if the value fits into the range of a short, it will be perceived as an int There is an exception: where the short is assigned from a compile‑time constant of the appropriate magnitude, in which case an implicit cast is applied. Otherwise the code will fail to compile.

. . . when you exceed the capacity of the largest intermediate data type what the work can be widened to, it will throw an arithmetic exception. . . .

Before Java8, all overflow errors occurred silently and didn't throw exceptions. Whether that was the right decision or wrong……well, I can see arguments on both sides. There are xyzExact() methods, introduced in Java8, which test for overflow and throw an exception if it happens.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Savvy wrote:I mean something like this:



This can be emulated using method overloading.

This prints:
short
short
int


Note that selecion of a method to call happens on compile time.
 
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could certainly write your own methods with signatures like boolean isByteSize(long var) and have them test the input to see if it will fit in the data type which is in the method name. They're mostly one-liners, I think.

What you would use them for I don't know, but I'm sure some Java programmer somewhere in the world might find a use for such a thing.
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic