Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is #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 of letting other methods use the variable lower?

like image 719
Rohan Avatar asked Oct 02 '13 21:10

Rohan


People also ask

What type of word is is?

Is is what is known as a state of being verb. State of being verbs do not express any specific activity or action but instead describe existence. The most common state of being verb is to be, along with its conjugations (is, am, are, was, were, being, been).

Is or a conjunction?

Or is a conjunction that connects two or more possibilities or alternatives. It connects words, phrases and clauses which are the same grammatical type: Which do you prefer? Leather or suede? You can have some freshly baked scones or some chocolate cake or both.

Is in use a word?

Definition of in use : being used All of the computers are currently in use.

What is the origin of the word is?

From Middle English is, from Old English is, from Proto-West Germanic *ist, from Proto-Germanic *isti (a form of Proto-Germanic *wesaną (“to be”)), from Proto-Indo-European *h₁ésti (“is”).


2 Answers

They are totally different. Define is more of a copy paste that the C preprocessor uses. Static is an attribute modifier for a Java class. A static attribute can be changed at runtime.

like image 60
aglassman Avatar answered Sep 17 '22 09:09

aglassman


#define in C causes textual substitution. For example:

#define PI 3.14

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

Now, the scope of #define is much larger than that; it isn't only limited to variables (and there isn't something directly equivalent in Java). When you have a static variable, there is no guarantee that that variable will not change, so a similar "textual substation" cannot be made. 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

Much like what happened with #define, static final constants are replaced with their actual values at compile time. In this sense, static final constants are similar to #define constants.

like image 44
arshajii Avatar answered Sep 19 '22 09:09

arshajii