Question

I'm studying Java and I have some unclear matters I'll be very glad if anybody could help.

First question

the approx value of int is : 2.147.483,647

the approx value of long is : 9,223,372,036,854,775,807

This website says:

"An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int"

Which mean that in case I'm not adding the letter L/l to the end of a variable name

such as :

long num=3515; //no letter L/l at the end

So variable num considered as int type not as long type.

So I made this program:

public class caluMaxNum
{
    public static void main(String [] args)
    {
        long max=2147483640; //doesn't have letter L/l so it consider as an int
        for(int i=0;i<=10;i++)
        {
            max++; 
            System.out.println(max);
        }
    }
}

This is the output:

2147483641
2147483642
2147483643
2147483644
2147483645
2147483646
2147483647
2147483648
2147483649
2147483650
2147483651

The approx value of int is : 2,147,483,647 and variable max is int, so how did it print the bold values?

Second question:

About narrowing conversions:

Is my statement true?

byte type (8 bits) and short type (16 bits) could be convert to char type (16 bits) only in cases the values found on byte/short type is one of the following values : 0,1,2,3,4,5,6,7,8,9 Otherwise there will be a run-time error

- third and last question :

when I declare numeric variables types such as:

float num1=34.1;
float num2=34.1F
long num3=43
long num4=953L

What if the letter symbolize could I also declare variable with other types use their first letter? why should it be included what is the difference between num1 to num2 and to num3 to num4?

Était-ce utile?

La solution

  1. While the value used to initialize max may be an int, max is explicitly declared as a long, perfectly consistent w/ your output.

  2. The answer depends on what you mean by "convert".

  3. num1 & num2 are the same; num3 and num4 are completely different values, regardless of what (if any) suffix is used.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top