aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Primitive Data Types Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Primitive Data Types" Watch "Primitive Data Types" New topic
Author

Primitive Data Types

Parimala Somasundaram
Ranch Hand

Joined: Mar 31, 2001
Posts: 41
What is the result of executing the following code:
class Test {
public static void main(String[] args) {
Test t = new Test();
t.test(1.0, 2L, 3);
}
void test(double a, double b, short c) {
System.out.println("1");
}
void test(float a, byte b, byte c) {
System.out.println("2");
}
void test(double a, double b, double c) {
System.out.println("3");
}
void test(int a, long b, int c) {
System.out.println("4");
}
void test(long a, long b, long c) {
System.out.println("5");
}
}
Select the one right answer.
1
2
3
4
5
The answer is given as 3. I thought both 1 and 3 seem applicable. Cannot the constant 3 fit into a short? Can someone please explain?
Siva Prasad
Ranch Hand

Joined: Feb 22, 2001
Posts: 104
Parimala!
Always constants match to integers.
that's why u get the answer as 3.
try this code
now you see the output as 1.
unless until u pass a short variable,
method test(double a, double b, short c) will be not be invoked
public static void main(String[] args)
{
MyTest t = new MyTest();
short s = 3;
long l = 4;
//t.test(1.0, 2L, 3);
t.test(2.0,l,s);
}
cheers
Siva
Parimala Somasundaram
Ranch Hand

Joined: Mar 31, 2001
Posts: 41
I am still not very clear about this.
Aren't the statements
short s = 3;
or
byte b = 3;
valid? Passing primitive type variables to a method is also similar. Isn't it?
Siva Prasad
Ranch Hand

Joined: Feb 22, 2001
Posts: 104
Parimala read the content in my post again
Always constants without any suffixes like L, f, d are treated as integers only
test(2.0,3L,5);
is different from
test(f,l,s);
where
float f = 2.0f;
long l = 6;
short s = 9;
here f,l,s are variables.
incase of the first method, the arguments passed are not variables they are constant values. i.e. primitive literals.
Still not clear?
Siva
Parimala Somasundaram
Ranch Hand

Joined: Mar 31, 2001
Posts: 41
Thanks Siva. I understand your point.
But isn't there a rule that in an assignment statement like
final int i=3;
short s=i; // valid though no cast is present
is valid because although we assign a type int to type short, i is a final( and so constant) value?
Similarly, when we pass 3(which though integer is a constant) as parameter to a short type (s in the method), it should be valid.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Parimala,
For assignments the compiler uses a special case so that the following can happen without casting:
short s = 100;
byte b = 56;
char c = 43;
The special case doesn't exist for parameter mapping. The compiler figures you better know what you want before you start passing things around. The compiler will only automatically perform widening while passing but never automatically perform narrowing conversions. Those are left to the programmer!
Regards,
Manfred.
Jonathan W. Brett
Ranch Hand

Joined: Mar 15, 2001
Posts: 36
Why is 2L and 3 reconized as a double?
Shouldn't there be a method that takes a double, long, int
I'm sorry for asking for another explanation but I can't get my head around what your saying.


Jonathan Brett, SCJP
Elijah Israel
Greenhorn

Joined: Mar 31, 2001
Posts: 3
Jonathan !
It would have been appropriate if there was a method that takes double long int.
Since its not there, as Manfred has said, the compiler does only widening conversion while passing parameters. so it cannot map 3 (int) to short.
Try taking out the double double double method. It would not even compile.
Hope this helps you.
[This message has been edited by Deepak Israel (edited April 03, 2001).]
[This message has been edited by Deepak Israel (edited April 03, 2001).]


Elijah
Parimala Somasundaram
Ranch Hand

Joined: Mar 31, 2001
Posts: 41
Thans Manfred.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Primitive Data Types
 
Similar Threads
method overloading
conversion and casting
Barry Boone #29
boone exam q -- assignments, help
Why this Output?