File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Math.max(), Math.min() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Math.max(), Math.min()" Watch "Math.max(), Math.min()" New topic
Author

Math.max(), Math.min()

M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
Math.max( 5, (long) 5);

what primitive type would the result be in? Is there a way to check the primitive type, like we check the Objects with instanceof?
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
You don't have to check primitive types. Whatever you write it as that's what it is. For example you can put an int value into a long, but then it is a long and not an int.

In your example, because you cast one to a long, it should call the method Math.min(long, long) (closest match) which returns a long.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
The result is a long.

To get the compiler's view of the expression type, put this in your program and read the error message:
boolean t = Math.max( 5, (long) 5);


Mike Gershman
SCJP 1.4, SCWCD in process
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
Hi rama

Its return type is long

to justify my statement

if u assign to int it shows error like this

int a = Math.max( 5, (long) 5);
System.out.println(a);

test.java:31: possible loss of precision
found : long
required: int
int a = Math.max( 5, (long) 5);


if we go with long ...no prob

long a = Math.max( 5, (long) 5);
System.out.println(a);

we checked it in indirect way...
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
While a primitive variable's type may be saved in a debugging symbol table (javac -g), the type of an expression is a compile-time notion, so there is nothing to test at execution time.

You can look at the generated bytecode with javap and see what the compiler did with your expression. In this case, it's pretty obvious that Java created bytecode for a long expression. Try it yourself.
[ April 12, 2005: Message edited by: Mike Gershman ]
M Rama
Ranch Hand

Joined: Mar 04, 2005
Posts: 91
Thanks Guys.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Math.max(), Math.min()