 This week's book giveaways are in the Cloud and AI/ML forums.We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!See this thread and this one for details. Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Devaka Cooray
• Liutauras Vilda
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Paul Clapham
• Knute Snortum
• Rob Spoor
Saloon Keepers:
• Tim Moores
• Ron McLeod
• Piet Souris
• Stephan van Hulst
• Carey Brown
Bartenders:
• Tim Holloway
• Frits Walraven
• Ganesh Patekar

# numeric promotion understanding for oca exam

Greenhorn
Posts: 22
1
• • • • Hi,

I am triying to understand this :

What is the data type of x * y / z?
short x = 14;
float y = 13;
double z = 30;

It says in the OCA book p56 :p

x become int then float
x * y become double
result is double

I would say :

In fact y become int because of 'f' absence...but  we'll say that float y = 13f;
Now I would rather say :
x become int
x * y become int ( because int is prefered between int and float)
x * y * z become double (because double is larger than int)

Am I wrong or right ?

If I am wrong, can you explain me why ?

Thand you in advance

Sorry for my bad english :O

Alion Bada
Greenhorn
Posts: 22
1
• • • • Finally i found my error,

x * y become int ( because int is prefered between int and float) -> FALSE, float is prefered and not int
In fact y become int because of 'f' absence...but  we'll say that float y = 13f; -> FALSE but I am not sure,can I say that in float y = 13 -> y = 13.0 and is float and the literal 13 was double ?

Alion Bada wrote:Hi,

I am triying to understand this :

What is the data type of x * y / z?
short x = 14;
float y = 13;
double z = 30;

It says in the OCA book p56 :p

x become int then float
x * y become double
result is double

I would say :

In fact y become int because of 'f' absence...but  we'll say that float y = 13f;
Now I would rather say :
x become int
x * y become int ( because int is prefered between int and float)
x * y * z become double (because double is larger than int)

Am I wrong or right ?

If I am wrong, can you explain me why ?

Thand you in advance

Sorry for my bad english :O

author Posts: 23834
140         • • • • Alion Bada wrote:
x * y become int ( because int is prefered between int and float)

When multiplying two different primitive (typed) operands, the operation will implicitly cast to the type with the larger range. In this case, the short is cast to a float, and the result of "x * y" should be a float type.

Henry

Alion Bada
Greenhorn
Posts: 22
1
• • • • I don't think it's correct in this specific case
x * y become float because floating point has precedence on integral.
if you have for example float * long the result will be float even if long is larger...

Henry Wong wrote:

Alion Bada wrote:
x * y become int ( because int is prefered between int and float)

When multiplying two different primitive (typed) operands, the operation will implicitly cast to the type with the larger range. In this case, the short is cast to a float, and the result of "x * y" should be a float type.

Henry

Henry Wong
author Posts: 23834
140         • • • • Alion Bada wrote:
if you have for example float * long the result will be float even if long is larger...

A float type has a larger range than a long type. So, a float * long would also be implicitly cast to a float*.

Henry

* Just being pedantic, but it is more than the larger range. The larger range also has to fully encompass the other range.

Alion Bada
Greenhorn
Posts: 22
1
• • • • float : 32 bit
long : 64 bit

Henry Wong wrote:

Alion Bada wrote:
if you have for example float * long the result will be float even if long is larger...

A float type has a larger range than a long type. So, a float * long would also be implicitly cast to a float*.

Henry

* Just being pedantic, but it is more than the larger range. The larger range also has to fully encompass the other range.

Henry Wong
author Posts: 23834
140         • 2
• • • • Alion Bada wrote:float : 32 bit
long : 64 bit

Implicit casts are not based on the size (number of bits) -- it is based on the range. And a float type has a larger range than a long type.

Anyway...

long : from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float : from (-3.4 * 10^38) to (3.4 * 10^38)

Henry

Alion Bada
Greenhorn
Posts: 22
1
• • • • good point
I didn't know this
maybe I will learn it after...I am in chapter 2 :p
thank you for your detailed explanation
I will try to figure out how it 's possible

Henry Wong wrote:

Alion Bada wrote:float : 32 bit
long : 64 bit

Implicit casts are not based on the size (number of bits) -- it is based on the range. And a float type has a larger range than a long type.

Henry

Alion Bada
Greenhorn
Posts: 22
1
• • • • Hi Henry,

do you know why th value of j does never change ?

int j = 0;
while(true){
j = j++;
System.out.println("Hello World"+j);
}

I know that j++ is post incremental and the old value is assigned first.
What I do not understand is that in my head :
j = j++; means action 1 : j = j = 0 which is the old value - then action 2 : j++ means j = j +1

because in this case, j changes

int j = 0;
while(true){
j++;
System.out.println("Hello World"+j);
}

Do you have an explanation ?

Thanks in advance.

Henry Wong wrote:

Alion Bada wrote:float : 32 bit
long : 64 bit

Implicit casts are not based on the size (number of bits) -- it is based on the range. And a float type has a larger range than a long type.

Anyway...

long : from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float : from (-3.4 * 10^38) to (3.4 * 10^38)

Henry

Bartender Posts: 1243
86         • 1
• • • • • Java Virtual Machine Stack stores frames.
• Each Java Virtual Machine thread has a private Java Virtual Machine stack, created at the same time as the thread.
• A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes, whether that completion is normal or abrupt (it throws an uncaught exception).

• Example1: Refer diagram Example 1: Test

• Only one frame, the frame for the executing method, is active at any point in a given thread of control. This frame is referred to as the current frame, and its method is known as the current method.
• The class in which the current method is defined is the current class.

• In example1 and eample 2
• When main method is invoked a new frame is created known as current frame where main method is the current method and Test class is the current class.
• main thread(non-daemon) is in control ( I mean thread which is executing this main method).

• Example 1 explanation:
• First compile this program using javac then you get Test.class file then use javap command like this
• C:\>javap -c Test.class
you get following output about main method:

public static void main(java.lang.String[]);
Code:
0: iconst_0
1: istore_1
2: iload_1
3: iinc     1, 1
6: istore_1
7: goto    2

• At the beginning when frame is created operand stack is always empty. At this moment operand stack is empty.
• In array of local variables we have args at index 0 and j at index 1.

• 0:iconst_0
This instruction pushes the int constant 0 onto the operand stack ( where i stands for int as 0 is an int type, If It was long then would have been lconst_0)
Now operand stack is not empty as we have 0 value in operand stack at top.

• 1: istore_1  ( Note: here 1 means store value to the variable present at index 1 of array of local variable )
The value on the top of the operand stack must be of type int. It is popped from the operand stack and stores that value into local variable present at index 1 in array of local variables.
Now we have j = 0 which is present at index 1 in array of local variables of this current frame. As we popped operand stack now operand stack is empty again.

• 2: iload_1 ( Note: here 1 means load value of the variable present at index 1 of array of local variable)
The value of the local variable present at index 1 in array of local variable which is j whose value is 0 is pushed onto the operand stack.
Now operand stack is not empty, we have 0 value at top in operand stack.

• 3: iinc 1, 1 ( Note: here 1 and 1 means increment value of the variable present at index 1 of array of local variable by 1)
Increments int value present at index 1 of array of local variables by 1 so j becomes 1now.
We should not forget we still have 0 value at top in operand stack.

• 6: istore_1 ( Note: here 1 means store value to the variable present at index 1 of array of local variable )
Same instruction as we had at 1:
So again, the value on the top of the operand stack i.e. 0 is popped from the operand stack and stores that value into local variable present at index 1 in array of local variables i.e. j.
Now again we have j = 0 . As we popped operand stack now operand stack is empty again.

• 7: goto    2
Goes to instruction 2:iload_1 again repeats all these steps.
• That is why j always prints 0

Example 2: Refer diagram Example 2: TestOne
Example 2 explanation:
• First compile this example 2 program using javac then you get TestOne.class file the use javap command like this
• C:\>javap -c TestOne.class
you get following output about main method:
public static void main(java.lang.String[]);
Code:
0: iconst_0
1: istore_1
2: iinc   1, 1
5: goto   2

• At the beginning when frame is created operand stack is always empty. At this moment operand stack is empty.
• In array of local variables we have args at index 0 and j at index 1.

• 0:iconst_0
This instruction pushes the int constant 0 onto the operand stack. Now operand stack is not empty as we have 0 value in operand stack at top.

• 1: istore_1  ( Note: here 1 means store value to the variable present at index 1 of array of local variable )
The value on the top of the operand stack is popped from the operand stack and stores that value into local variable present at index 1 in array of local variables.
Now we have j = 0. As we popped operand stack now operand stack is empty again.

• 2: iinc 1, 1 ( Note: here 1 and 1 means increment value of the variable present at index 1 of array of local variable by 1)
Increments int value present at index 1 of array of local variables by 1 so j becomes 1 now.

• 5: goto   2
Goes to instruction 2: iinc 1, 1 again repeats all these steps.
• That is why this program prints incrementing value of j.

Hope I got It correct?

TestStackDiagram.png
TestOneStackDiagram.png

Alion Bada
Greenhorn
Posts: 22
1
• • • • OMG WHAT AN EXCELLENT AND NICE ANSWER Thank you very much Ganesh Patekar !!
I fully understand now.

[Edit: gp]I removed unnecessary quoted part Don't get me started about those stupid light bulbs.

# Similar Threads

• 