questions about Java programming

Status
Not open for further replies.

fleahuncut

Beta member
Messages
1
What is the difference between a constant and a variable? Aslo, in a program where is the constant and/or variable found?
 
A constant is a value that is set once at compile time and cannot change, whereas a variable can change whilst the program is running. There isn't a specific place to find constants and variables as they can be global to the program, local to a method or a member of a class.
 
What void said, but in addition, with Java, constants will be tagged with the "final" keyword. Eg:

public static final String numberPattern = "0123456789";

Then with variables, they are (in some sense) everything without the "final" keyword. Eg:

private String hi = "hi";
Integer zero = new Integer( 0 );
protected JFrame frame = new JFrame( );
int counter = 0;

etc.
 
for a more simpler look a few common variables are

int (integer)
double (floating point aka anything with a decimal)
string (a sentence or group of characters)
char (one character)

all of these are made constant with the FINAL tag. sorta oxymoronic really. a constant variable..........
 
all of these are made constant with the FINAL tag. sorta oxymoronic really. a constant variable..........
Hmm, o'course it is, since that phrase should never be used. :happy: It could be a constant reference, pointer, value, number... but not a variable, as you pointed out!
 
Status
Not open for further replies.
Back
Top Bottom