# Primitive types

• January 27th, 2012, 09:18 PM
sudesh
Primitive types
int low = 2;
int high = 1;

while (high < 10){

System.out.println(high);
int temp = high;
high = high + low;
low = temp ;
}

i am a beginner to java, having said that, in my head I compile the output as 1, 3, 4, 6 whereas complier says 1, 3, 4, 7.
What am i not understanding ? Also how is it possible to initialize 1 variable with anther, for example low = temp ?
• January 28th, 2012, 06:14 AM
Norm
Re: Primitive types
If you do not understand the results of any of the statements, add a println after the statement to print out the value of all the variables used in the statement.

System.out.println("high=" + high);
• January 28th, 2012, 09:50 PM
sudesh
Re: Primitive types
thanks. I did that and this is what i get. Why is "low" constantly changing its value, should it not have a set value of 2, that is what keeps confusing me.

int low = 2;
int high = 1;

while (high < 8){

System.out.println(high);
int temp = high;
high = high + low;
low = temp ;
System.out.println("low="+low);

}

This is the output

1
low=1
3
low=3
4
low=4
7
low=7
• January 28th, 2012, 10:04 PM
chronoz13
Re: Primitive types
Quote:

1, 3, 4, 6
i really dont get what you want here, but based on what you said, you want this output but you get 1, 3, 4, 7, you need to keep track of the 'high' variable, try to count it step-by-step, you will be able to understand why does it give 'low' a value of 7

temp = high
high = high + low
low = temp

therefore low is equal to high
• January 30th, 2012, 12:44 AM
marylandfour
Re: Primitive types
Quote:

Originally Posted by sudesh
int low = 2;
int high = 1;

while (high < 10){

System.out.println(high);
int temp = high;
high = high + low;
low = temp ;
}

i am a beginner to java, having said that, in my head I compile the output as 1, 3, 4, 6 whereas complier says 1, 3, 4, 7.
What am i not understanding ? Also how is it possible to initialize 1 variable with anther, for example low = temp ?

You're program is doing what it should correctly. I would look at the logic of whats happening with each iteration. And I'm not sure what you mean by initialize. If you mean create a variable with another variable, Java is an Object oriented language, so I'm willing to bet you'll need an object or a class.