Beginner to Java - Basic Questions!

Hello! I’m new to programming. Currently, I’m taking a Udemy course on Java hosted by Tim B. I have some questions on concepts I’m trying to understand better. Right now I run all of my code through IntelliJ Ideas.

  1. How do you know when to use int vs long vs double? I understand they hold different values and that double is more suited for decimals, but I don’t understand why long is used in some instructional videos when it seems like an int could be used as well.

  2. What is the difference between private & public? I’m not really understanding when to use one over the other.

Please go easy on me, as I’m sure these are simple concepts to most of you.

Both int and long are integers (whole numbers) only. The difference is their size: In Java an int has 4 bytes, a long 8 (in C it’s a lot more complicated). This gives them different ranges of numbers they can hold. If you need infinite size, check the BigInteger class.

double holds a floating point number, so you can have decimals. Keep in mind though that floating point number are approximations of the values you assign to them. There have been bugs in payments because someone thought it’d make sense to put the amount in dollars into a double (or float) with decimals for cents. It doesn’t, the amounts will be off. :sweat_smile:

See also: Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)

A private variable or method can be used only from within your class, a public one from the outside, too. I assume if you’re just starting your examples have only one class each so the difference isn’t obvious. But that difference (and the third option, protected) becomes important when you have larger applications with multiple classes. Its lets you separate internal data from things other parts of your application can use.

Hi @jswhite1992
Here is the answer to your first question :-

Ans: int is an 4 byte value which is used to store integral values between -2,147,483,648 to 2,147,483,647.
whereas a long is a 8 byte value and is used to store integral values in between -18,446,744,073,709,551,616 to 18,446,744,073,709,551,615.
On the other hand double is a 8 byte floating point value used to store decimal values between
1.7976931348623157 x 10^308 to 4.9406564584124654 x 10^-324.

Here is the answer to your second question:-

Ans: Public and Private are access modifiers. They control the access level to functions defined inside the class. Functions defined inside the Public access modifier are visible to all parts of program
whereas functions defined inside the Private access modifier are visible only inside the class defination.

Feel free to ask me again if you want a more clear idea or any doubt :thinking: :thinking:.

:grinning_face_with_smiling_eyes: :grinning_face_with_smiling_eyes: :grinning_face_with_smiling_eyes: