What is a constant in Java -


i have been looking on web, stackexchange, wikipedia, youtube, random sites, everywhere haven't got simple answer, or it's been different others have said.

some resources have said, in java, constants variables value cannot changed.

other have said constants objects name cannot changed.

and more have said constants variables name cannot changed.

please tell me in english constant is, parts can/cannot changed , maybe example constants used instead of normal variable.

thanks in advance!

a constant value cannot changed. example 5, 0.5 , "abcd" etc.

lets consider java :

int = 5; // here 5 constant reference (i) can change , point 6. i.e, value 5 immutable, reference (i) mutable. 

adding final keyword above statement :

final int = 5; // here keyword final ensures reference cannot point other value.  

thus i becomes compile time constant , value passed in byte-code itself.

note reference point constant value (immutable object ) or mutable value.

technically speaking, immutable values aren't same constants in case of reference types (except string).


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -