Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
919 views
in Technique[技术] by (71.8m points)

integer - When coding in java, is the main Data type that is used called int?

During my course, my professor said to only use int, and double in Java. The text book I am reading goes over byte, int, short, double, long, and float. I understand that each of these handle a different amount of data.

My question is, is int primarily used for programming? and if so, do I only practice using int and double? and if not, do I try to use all the above?

Thank you in advance

question from:https://stackoverflow.com/questions/65835032/when-coding-in-java-is-the-main-data-type-that-is-used-called-int

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm going to give a pretty vanilla answer.

By default, a literal number in Java is interpreted as an int. For instance, 123 is always going to be an int.

By default, a floating-point decimal number in Java is interpreted as a double. For instance, 123.45 is always going to be a double.

You can add L to the end of a number to create a long, or f at the end of a decimal number to create a float.

There are a myriad of other numeral containers that can do numbers higher than the limits of either a 32 or a 64-bit signed number, or that can hold more precision than a 64-bit double (such as BigInteger and BigDecimal), so the correct answer to if it's primarily used is that it really depends on what it is you're doing.

Java has its defaults, but you don't have to stick to them.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...