❁ I've just started learning how to program with java and i'm having trouble with constants... Help?! ❁?
I'm using the book "The art and science of Java" and up to chapter 3, so i'm doing the problems at the end of the chapter and I'm stuck on question 2 about constants. Here is the question...
Identify which of the following are legal constants in Java. For the ones that are legal, indicate whether they are integers or floating-point constants:
I'm not asking anyone to do this for me, actually I'd prefer if you didn't just answer them because I really want to learn about the constants.
Thank you so much to anyone offering help! ❂❂❂❂❂❂❂❂
Erm, thanks for the help but I'm still a bit confused. Can you give an example of one of the questions?
- AnalProgrammerLv 77 years agoBest Answer
A constant is something that does not change. It can be a number, a character (in single quotes) or a String (in double quotes).
Try this link for a selection of valid numbers.
- 7 years ago
A constant is a value that will not change during the execution of your program
You use the keyword <<final>> with the data type that your constant belongs to:
For example, you can define a constant of type integer like so:
final int MY_CONSTANT=some_value;
You need to initialize a constant inline, which means that you have to give it a value right then and there;
You cannot reassign a value to a constant after it has been declared;
A constant can only hold a 'constant value' or expression that 'evaluates' to a constant value
For instance the number 2 is a constant. Adding another value, 3, to it will also result in a constant (since 3 is also a constant)! Also adding or assigning other constant types to your constant will also yield, you guessed it, a constant ;)
final int const1 = 3
final int const2 = 2+3;
final int const3 = const1+const3;// this works because const1 and const2 are constants
If you declare a non-constant, in other words, a variable, like so:
int variable = Obj.getSomeData(); // get some imaginary data
final int const4 = variable+5; // this will not work
Although 5 is a constant, adding it to a 'non-constant' will give you a non-constant and java will not compile this.
How many times have i used the word 'constant'? LOL!Source(s): Software programmer
- Anonymous7 years ago
Could be a trick question as Java does not support a Constant type. A constant is something that does not change. It is constantly the same. As opposed to a variable which can vary.
In Java you would declare a variable as STATIC FINAL which means it will not change and so is effectively a constant. Traditionally constants are written in UPPER CASE.
public static final int TWENTY = 20;
public static final double PI = 3.14159;
- blankenbeclerLv 43 years ago
Deitel is a good e book. yet i ought to caution you mate. studying a programming language by using your self is extremely puzzling. that is kinda like studying french is you've under no circumstances heard or seen it before by using examining a e book. confident you receives the nouns and verbs staggering, yet your structure will heavily conflict through. In different words, you'll actual under no circumstances quite "study" the thanks to write down code. you quite do opt to pass to college and characteristic a instructor and classmates to study from. You said you do not have time. attempting to study a language like C++ or Java on your man or woman will take a lot longer than the 4 months in a school room. sturdy success.
- How do you think about the answers? You can sign in to vote the answer.
- green meklarLv 77 years ago
g and l are not valid constants. I'm not sure about k.
c is a weird case, you can use it as if it were a constant (and in fact, the compiler will convert it to one automatically), but it is still made up of two literals.
- 7 years ago
Visit w3schools and learn them there for free. Good luck.
- 7 years ago
try in java2s website