Assignment Operator, Initial Values, Literals

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:

Screen Shot 2018-12-10 at 8.49.47 AM

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:

Screen Shot 2018-12-10 at 8.53.34 AM.png

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.

Screen Shot 2018-12-10 at 9.26.53 AM

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.

Screen Shot 2018-12-10 at 9.33.10 AM

Screen Shot 2018-12-10 at 9.37.18 AM

Screen Shot 2018-12-10 at 9.39.29 AM

Screen Shot 2018-12-10 at 9.41.57 AM

Screen Shot 2018-12-10 at 9.43.47 AM

Screen Shot 2018-12-10 at 9.45.27 AM

Here, I show a complete program illustrating variable declarations, specifying a literal for the initial value of each:

Screen Shot 2018-12-10 at 12.50.18 PM

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:

Screen Shot 2018-12-10 at 1.09.27 PM

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.

Screen Shot 2018-12-10 at 1.13.12 PM

Screen Shot 2018-12-10 at 1.16.05 PM

Another way to specify an initial value for a variable is to assign the variable the value of another variable, using this syntax:

Screen Shot 2018-12-10 at 1.18.18 PM

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:

Screen Shot 2018-12-10 at 1.26.04 PM

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,

Screen Shot 2018-12-10 at 1.49.24 PM

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:

Screen Shot 2018-12-11 at 9.43.45 AM

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:

Screen Shot 2018-12-11 at 9.57.35 AM

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:

Screen Shot 2018-12-11 at 10.04.43 AM

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.)

Screen Shot 2018-12-11 at 10.40.38 AM

What you find in the right can be assigned to what is on the left without lossy conversion error.

 

 

 

 

 

 

 

 

 

Java Inheritance Part 2

The syntax for defining a subclass class that inherits from another class is to add an extends clause in the class header:

Screen Shot 2018-06-30 at 1.54.33 PM

The extends keyword specifies that the subclass inherits members of the superclass. That means that the subclass begins with a set of predefined methods and fields inherited from its hierarchy of superclasses.

JFrame allows us to create graphical applications. So we can use that to create our subclass of StatisticalDispersion and all of its inheriting subclasses.

Here, StatisticalDispersion is the subclass under JFrame. And it inherits from all the classes that JFrame inherits from, all the way back to the Object class.

Screen Shot 2018-06-30 at 3.24.57 PM

The StatisticalDispersion Class Hierarchy

We are coding a class named StatisticalDispersion that extends the JFrame class, so we use the following header:

Screen Shot 2018-06-30 at 3.44.19 PM

Because our StatisticalDispersion class extends JFrame, it inherits more than 300 methods and more than 30 fields. That’s because the JFrame class is a subclass of Frame, which is a subclass of Window, which is a subclass of Container, which is a subclass of Component, which is a subclass of Object.

A hierarchy is composed of subclasses which inherit methods and fields. Here, we make all of them available to the StatisticalDispersion class.

Subclasses do not necessarily need to use their inherited methods, but these are available if needed. The programmer does not need to write methods and define fields in classes which have already inherited them.

JFrame is the direct superclass of our StatisticalDispersion class. As you can see in the image, StatisticalDispersion refers to it. And as you can see in the code, JFrame follows the extends clause.

A class can have multiple direct subclasses but only one direct superclass. One can have many offspring but yet can only develop from one zygote.