#define in C, similar to a static variable in Java?

In C we can write:

#define LOWER 0 

And in Java we can write:

 static int lower = 0; 

Don't these statements have the same purpose as other methods using the variable below?

+8
java c variables static
source share
8 answers

They are completely different. Define is rather the copy that the C. preprocessor uses. Static is the attribute modifier for the Java class. The static attribute can be changed at runtime.

+4
source share

#define in C causes a text replacement. For example:

 #define PI 3.14 ... a = PI * r * r; // becomes: a = 3.14 * r * r 

The #define area is now much larger; it is not limited only to variables (and in Java there is not something that is directly equivalent). When you have a static variable, there is no guarantee that this variable will not change, so such a “text substation” cannot be performed. However, when you declare a static final variable and assign it to a compile-time constant , there is such a guarantee. So:

 public static final double PI = 3.14; ... a = PI * r * r; // becomes: a = 3.14 * r * r, like in C 

As with #define , static final constants are replaced with their actual values ​​at compile time. In this sense, static finite constants are similar to #define constants.

+1
source share

static , no.

final , yes.

 final int LOWER = 0; 

A final variable can only be initialized once.

static variables belong to the class, not the instance.

BUT

They may be similar in function, but the way they work is completely different.

The final variable is still such that it is a variable. A #define in C (++) is actually interpreted during assembly and accordingly modifies the binary output, resulting in vales values ​​being constants at runtime.

#define more efficient.

0
source share

#define is a preprocessor directive - the value that you define swaps where it is mentioned in the code before compilation. It has a number of pitfalls , although, like any global variable, it can conflict with a similar variable somewhere else (by the way).

If you want a constant, use const .

0
source share

The big advantage of defining Java final int LOWER=0; LOWER always remains variable and will be visible as such to the debugger.

In C, the given value is replaced by the precompiler in the code, and the constant name is missing.

0
source share

First tip from C to C ++: “don't use #define”, try using a constant variable instead. '#define' is hard to debug.

0
source share

in #define lower 0 lower is not a variable. At compile time, lower will be replaced with 0 , where it is used throughout the code.

whereas static int lower = 0; defines a variable, and the actual memory reference will be executed at run time to access the value of the variable. Plus, this value can be changed in your code.

to avoid changes, you can define it like this: static final int lower = 0;

Now compilers do a very good job in optimizing. Therefore, during compilation, this lower will be replaced with 0 .

So, you can only “say” that #define lower 0 in C is equivalent to static final int lower = 0; But they do not match. Because the later one was replaced during the optimization phase. If optimization is disabled, it will not be replaced at compile time.

Hope this helps.

0
source share

The definition preprocessor is great for naming constants, but it doesn't provide the same type of check as declaring a Java constant (or C ++). Being a C and C ++ developer for many years, I really like the type checking that C ++ (and Java) provide. We remind you about the project under the contract, etc.

But the flexibility of languages ​​such as Perl, Python, and Ruby, which offer duck printing and a less restrictive approach to type declarations, also makes sense. Good credit for the philosophical discipline offered by rigorous type checking. But when you just try to declare (named) const (ant), all this rigor seems bloated.

So yes, #define is one way to write "constant."

 #define PIE (3.142) //C preprocessor float PIE_() { return 3.142; } //C also const float PIE = 3.142; //C++ const public static final float PIE = 3.142; //Java PIE = 3.142 #Ruby makes variables with initial capital constant PIE = 3.142 #Python doesn't really have constants #Perl is harder, you really have a hard time defining constants sub PIE { 3.142; } 

But read about how hard it is to get a variable constant in Python. http://code.activestate.com/recipes/65207-constants-in-python/?in=user-97991

0
source share

All Articles