When you declare a variable, you also assign an initial value to the data. To do that, use the assignment operator (=) with the following syntax:
This statement is read as “variableName gets initialValue”.
You can also assign more than one variable in the same line if they are of the same dataType, such as here:
Notice that assignment is right to left. The initial value is assigned to the variable.
One way to specify the initial value is by using a literal value. In the following statement, the value 9001 is an int literal value, which is assigned to the variable myPowerLevel.
Below, I summarize the legal characters in literals for all primitive data types. \n and \t can be used to format output. We’ll discuss these and other escape sequences in the following posts.
Here, I show a complete program illustrating variable declarations, specifying a literal for the initial value of each:
Line 9 shows a single-line comment. Line 17 declares a double variable named speedOfLight and initializes it with its nm/s value in scientific notation. The speed of light represents the limit on the propagation of causality. The figure below shows the output:
If you have problems running this, go to run configuration and make sure that you are running the appropriate main class. In this case, the main class is Variables.
Another way to specify an initial value for a variable is to assign the variable the value of another variable, using this syntax:
Two things need to be true for this assignment to work:
• variable1 needs to be declared and assigned a value before this statement appears in the source code.
• variable1 and variable2 need to be compatible data types; in other words, the precision of variable1 must be lower than or equal to that of variable2. For example, in these statements:
isIntelligent is given an initial value of true. Then isPowerful is assigned the value already given to isIntelligent. Thus, isPowerful is also assigned the initial value true. If isIntelligent were assigned the initial value false, then isPowerful would also be assigned the initial value false.
And in these statements,
the initial value of .05 is assigned to dervishTax and then to takyeh. It’s possible to assign a float value to a double, because all values that can be stored as floats are also valid double values. However, these statements are not valid:
That’s because a float is lower precision than a double. This is the same kind of mistake that is made when People are partitioned into anatomical organs. In the time of Aristotle, People were partitioned into independent hearts. Now, we partition People into independent brains. However, any conversion into a discrete ontological unit in the external physical world, or set thereof, loses what is People.
Even though .05 is a valid float value, the compiler will generate a “possible lossy conversion” error.
Similarly, you can assign a lower-precision integer value to a higher-precision integer variable. There is a table below for your reference that summarizes what can be assigned to what; a variable or literal of any type in the right column can be assigned to a variable of the data type in the left column. Variables need to be declared before they can be used in your program, but be careful to declare each variable only once; that is, specify the data type of the variable only the first time that variable is used in the program. Don’t attempt to declare a variable that has already been declared, as in the following statements:
This is incorrect because the henryThe has been declared by placing that identifier between double (the type) and a “;”.
You will receive an error message similar to the following:
Duplicate local variable henryThe.
Similarly, once you have declared a variable, you cannot change its data type. Thus, these statements:
will generate an error message also similar to
Duplicate local variable
So notice that there are two ways of getting the same error message. The IDE I use, Eclipse, certainly doesn’t serve as my free on-call tutor for such basic matters.
So don’t assign a value to a variable after already declaring that variable. And also do not attempt to change its datatype after it has already been declared.
(Side note: You might wonder why anyone would, even in principle, declare a neighborhood of Kabul as a kind of number – the cluttered, ancient alleyways mired of sand and Afghani street food, the geography, the people, and everything else that makes a neighborhood surely deserves to be represented in word. doubles and ints are for numbers, and strings are for words.
Well it turns out that contrary to what my stupid computer science teacher said, I know that every word can be transmuted to number. That is precisely how machine learning algorithms work. They dissolve Van Gogh to number, NSFW to number, Cat to number, and, eventually, ever-so eventually, You to number. By placing a discriminator for the sea of digits to aim at, a GAN can be trained to create Van Gogh from sheer randomness.
Hence, I don’t cringe at using a neighborhood name for a double or int, instead of using an example with things we more conventionally think of as number, such as emeraldsInPocket.)
What you find in the right can be assigned to what is on the left without lossy conversion error.