Difference between following statements

E

Edward A. Falk

In the second case, p is a const pointer to a non-const char; you
can modify what p points to, but you cannot modify p (cannot set
it to point to a different thing).

Oh, that reminds me. You'll also need to set p when you declare it

char * const p = something;

If you just do

char * const p;

You'll never get to assign anything to p.

Or am I behind the times on this? I know that in Java, you can
define something as final and set its value later, but only once.

Since attempting to modify a string literal invokes undefined behavior,
many people recommend declaring a pointer to it as const char *, such
as

const char *str = "string literal";

If you try to modify the contents of the literal through str, the compiler
will complain.

Very good advice.
 
E

Eric Sosman

Oh, that reminds me. You'll also need to set p when you declare it

char * const p = something;

If you just do

char * const p;

You'll never get to assign anything to p.

Or am I behind the times on this? I know that in Java, you can
define something as final and set its value later, but only once.

You're up-to-date. C and Java are different languages:
C has nothing like Java's `final', and Java has nothing like
C's `const'.
 
G

glen herrmannsfeldt

(snip)
Oh, that reminds me. You'll also need to set p when you declare it
char * const p = something;
If you just do
char * const p;
You'll never get to assign anything to p.
Or am I behind the times on this? I know that in Java, you can
define something as final and set its value later, but only once.

I might be behind in Java, but last I knew you couldn't do that for
static final variables.

Then again, Java compilers have to do compile time (static) checking
for variables that are never assigned before the first reference.
If they can figure out at compile time that a variable is only
assigned once, no reason not to allow it.

-- glen
 
E

Edward A. Falk

I might be behind in Java, but last I knew you couldn't do that for
static final variables.

You can assign final members in the constructor as opposed to
in the declaration. I haven't tried with variables.
 
G

glen herrmannsfeldt

Edward A. Falk said:
(snip, someone wrote)
(then I wrote)
You can assign final members in the constructor as opposed to
in the declaration. I haven't tried with variables.

You are getting pretty close to the Java version of the C question
that started the thread. That is, the difference between a pointer
and what it points to.

A static final variable of a primitive type (char, byte, short, int,
long, float, double) can't be changed and, last I knew, had to be
initialized on its declaration.

A static final Object, should mean that you can't change the Object
reference variable to refer to another Object, but you could still
change instance variables of the Object, if otherwise allowed.

-- glen
 
D

David Thompson

Edward A. Falk said:
(snip, someone wrote)

(then I wrote)
At least as of Java 6 (aka 1.6, JLS 3) since about 4 years ago:

You can assign a nonstatic final field once in a ctor, counting a
class-scope {code} block as prepended to all ctors, or initialize in
the declaration, but not both.

Similarly you can assign a static final field once in a static{code}
block or initialize in the declaration but not both.

And you can assign a final local variable (necessarily nonstatic) once
in the code or initialize in the declaration but not both.

Aside: Java uses 'member' to include fields, methods, and nested
classes or interfaces. Of these only fields are variables; methods and
nested classes can also be marked final but with different meaning.
You are getting pretty close to the Java version of the C question
that started the thread. That is, the difference between a pointer
and what it points to.

A static final variable of a primitive type (char, byte, short, int,
long, float, double) can't be changed and, last I knew, had to be
initialized on its declaration.
See above. Once initialized it can't be assigned again.

However, a static final field of primitive or String type, which is
initialized in the declaration by a constant expression, can be
compiled as a constant into another classes that uses it, instead of
an access to the actual field, and thus not pick-up changes without
recompile. <ObAlmostC>This is similar to C++'s but not C's special
treatment of a const variable of integer type initialized by a
constant expression as allowed in a(nother) constant expression, such
as a bound in an array declaration or a switch case label. said:
A static final Object, should mean that you can't change the Object
reference variable to refer to another Object, but you could still
change instance variables of the Object, if otherwise allowed.
Well, if a final variable (local or field, or parameter) points to an
actual java.lang.Object, that type has no fields you can access and
the only state you can affect is the notify list which AFAIK cannot be
observed except through the debugger interface. But Object is the top
of the inheritance hierarchy so such a variable can actually point to
an object of any type (including an array, since arrays in Java are
objects of synthesized types) and if that type has mutable fields
and/or state (and most do) yes you can mutate it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top