Java Help plzz

inranasingha

Well-known member
  • Jan 27, 2008
    5,818
    1,786
    113
    රජයේ රෝහල
    anyone explin plzz,.........
    unledfut.jpg

    unled1ct.jpg
     

    highlander

    Member
    Jul 8, 2009
    932
    59
    0
    Colombo
    anyone explin plzz,.........


    unledfut.jpg

    unled1ct.jpg


    4. in java, there are 'implicit' primitive types for each value type. when you declare a value like 35.5, compiler assumes that it is a double value - that's the implicit primitive data type used to store decimal values.

    so here the problem is, float is a smaller data type than double (float = 32bits, double =64 bits), so it is not possible to represent all double values using floats, coz floats have a much less value range.
    hence the compiler will give u an error.

    imagine what will happen if the compiler doesn't give you an error.
    you try to store a value like 1000000000000000000000000000000000.11 using a float. but since float is too small to keep such big values, only part of that will be stored. so if you run it, it will cause bugs at runtime.
    you can still use casting if you want to forcibly store values.
    eg: float x= (float)35.5;


    8. like the previous one, in k = k*100, the jdk will take '100' as an int. coz its the implicit type used for non-decimal numbers.

    so here we get two different data types to operate. k = short & 100 = int.
    but machines can't do operations with two different data types. so here 'k' also gets automatically converted to an 'int'
    so the answer of k*100 is int.

    but 'k' is short, and it is smaller than int. a similar problem like in Q4 can occur. hence, this gives an error.
    if you want to correct it, you can again cast it forcibly:
    k = (short)(k*100)
     
    • Like
    Reactions: inranasingha

    inranasingha

    Well-known member
  • Jan 27, 2008
    5,818
    1,786
    113
    රජයේ රෝහල
    4. in java, there are 'implicit' primitive types for each value type. when you declare a value like 35.5, compiler assumes that it is a double value - that's the implicit primitive data type used to store decimal values.

    so here the problem is, float is a smaller data type than double (float = 32bits, double =64 bits), so it is not possible to represent all double values using floats, coz floats have a much less value range.
    hence the compiler will give u an error.

    imagine what will happen if the compiler doesn't give you an error.
    you try to store a value like 1000000000000000000000000000000000.11 using a float. but since float is too small to keep such big values, only part of that will be stored. so if you run it, it will cause bugs at runtime.
    you can still use casting if you want to forcibly store values.
    eg: float x= (float)35.5;


    8. like the previous one, in k = k*100, the jdk will take '100' as an int. coz its the implicit type used for non-decimal numbers.

    so here we get two different data types to operate. k = short & 100 = int.
    but machines can't do operations with two different data types. so here 'k' also gets automatically converted to an 'int'
    so the answer of k*100 is int.

    but 'k' is short, and it is smaller than int. a similar problem like in Q4 can occur. hence, this gives an error.
    if you want to correct it, you can again cast it forcibly:
    k = (short)(k*100)

    txx