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: why it is essential to use (int) ?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default why it is essential to use (int) ?

    int x;
    x= (int) (7.0/2);
    I need to know why it is essential to use (int) in the statement above?
    What is it called?
    How it would be without (int), is it correct?


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: why it is essential to use (int) ?

    Its because your trying to use a float... 7.0 is a float value, But stating (int) is just like parsing it to an int from a float which it currently is.

    7.0 = Decimal point number = Float
    7 = no Decimal Point = int
    you are saying 7.0 divided by 2.. although your saying that the answer will equal the int x.

    So long story short.. you cannot use a float value as an int unless u do either (int) or parse the float.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    42
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: why it is essential to use (int) ?

    Quote Originally Posted by websit View Post
    int x;
    x= (int) (7.0/2);
    I need to know why it is essential to use (int) in the statement above?
    What is it called?
    How it would be without (int), is it correct?
    Because the literal "7.0" is implicit double value, so the result of 7.0/2 will be a double number, theno you need to put a type cast (int) to convert the result into an integer which is assigned to the variable x which is an integer.

    java exception
    Last edited by hns1984; January 11th, 2012 at 06:54 PM.

  4. #4
    Member
    Join Date
    Oct 2011
    Posts
    42
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: why it is essential to use (int) ?

    Quote Originally Posted by macko View Post
    7.0 = Decimal point number = Float
    Wrong, float literal is a double value, implicitly.

    java exception
    Last edited by hns1984; January 11th, 2012 at 06:54 PM.

  5. #5
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: why it is essential to use (int) ?

    ahh, My bad it gets abit confusing when handling all the difference number types of java.