Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Primitive types

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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 ?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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);

  3. The Following User Says Thank You to Norm For This Useful Post:

    sudesh (January 28th, 2012)

  4. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

  5. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Primitive types

    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

  6. The Following User Says Thank You to chronoz13 For This Useful Post:

    sudesh (January 28th, 2012)

  7. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    11
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Primitive types

    Quote Originally Posted by sudesh View Post
    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.

Similar Threads

  1. Tutorial: Primitive Data Types
    By newbie in forum Java Programming Tutorials
    Replies: 3
    Last Post: July 5th, 2012, 11:56 PM
  2. [SOLVED] Converting primitive data types causing NumberFormatException
    By Melawe in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 10th, 2011, 12:30 AM
  3. Primitive text editor
    By jclaxton in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: October 15th, 2011, 12:01 PM
  4. Primitive data type
    By stuart harper in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 17th, 2011, 12:14 PM
  5. Double & Float primitive types
    By Zeek in forum Java Theory & Questions
    Replies: 10
    Last Post: September 12th, 2011, 07:31 AM