Data Type: Character

The char data type stores one Unicode character. Because Unicode characters are encoded as unsigned numbers using 16 bits, a char variable is stored in two bytes of memory. The second table below shows the size of the char data type, as well as the minimum and maximum values. The maximum value is the unsigned hexadecimal number FFFF, which is reserved as a special code for “not a character.”

Screen Shot 2018-12-10 at 8.21.12 AM

Screen Shot 2018-12-10 at 8.22.49 AM

Obviously, since the char data type can store only a single character, such as a K, a char variable is not useful for storing names, titles, or other text data. For text data, Java provides a String class, which we’ll discuss later in a later post.

Here are a few declarations of char variables:

Screen Shot 2018-12-10 at 8.28.24 AM

 

Declaring Variables

A variable consists of a dual nature. It has a data type and a name.

Every variable must be given a name and a data type before it can be used. This is called declaring a variable. The syntax for declaring a variable is:

Screen Shot 2018-12-08 at 11.58.39 PM

or

Screen Shot 2018-12-09 at 4.00.02 PM

Note that a comma follows each identifier in the list except the last identifier, which is followed by a semicolon. By convention, the identifiers for variable names start with a lowercase letter. If the variable name consists of more than one word, then each word after the first should begin with a capital letter.

For example, these identifiers are conventional Java variable names: jewel3, specialRelativity, deathToNote, redInNovember, and xAxis.

Underscores conventionally are not used in variable names; they are reserved for the identifiers of constants, as we shall discuss in a later post.

Similarly, do not use dollar signs to begin variable names. The dollar sign is reserved for the first letter of programmatically generated variable names—that is, variable names generated by software, not people. Like with life in general, although arbitrariness may sound a disagreeable thing now, the value of following these conventions will become clearer as you gain more experience in Java and your programs become more complex.

 

Java Application Structure

Every Java program consists of at least one class. It is impossible to write a Java program that doesn’t use classes. Classes describe a logical entity that has data as well as methods (the instructions) to manipulate that data. An object is a physical instantiation of the class that contains specific data. Here, I provide a shell that contains the basic format of a Java application with a class name of GhostInThe. Your source code should use this format, changing the class name as appropriate.

Screen Shot 2018-12-08 at 10.22.47 PM

Here, the numbers to the left of each line are not part of the program code; they are included here for your convenience. IDEs typically allow you to display line numbers. From application to application, the name of the class, GhostInThe, will change, because you will want to name your class something meaningful that reflects its function. Each Java source code file must have the same name as the class name with a .java extension. In this case, the source file must be GhostInThe.java. Whatever name you select for a class must comply with the Java syntax for identifiers.

Java identifiers are symbolic names that you assign to classes, methods, and data. Identifiers must start with a Java letter and may contain any combination of letters and digits, but no spaces. A Java letter is any character in the range a–z or A–Z, the underscore (_), or the dollar sign ($), as well as many Unicode characters that are used as letters in other languages. Digits are any character between 0 and 9. The length of an identifier is essentially unlimited. Identifier names are case sensitive, so Number1 and number1 are considered to be different identifiers.

In addition, none of Java’s reserved words can be used as identifiers. These reserved words, which are listed here, consist of keywords used in Java instructions, as well as three special data values: true, false, and null. Given that Java identifiers are case sensitive, note that it is legal to use True or TRUE as identifiers, but true is not a legal variable name. The table below lists the rules for creating Java identifiers.

The shell code in above uses three identifiers: GhostInThe, main, and args. The remainder of it consists of comments, Java keywords, and required punctuation. The basic building block of a Java program is the statement. A statement is terminated with a semicolon and can span several lines. Any amount of white space is permitted between identifiers, Java keywords, operands, operators, and literals. White space characters are the space, tab, newline, and carriage return. Liberal use of white space makes your program more readable. It is good programming style to surround identifiers, operands, and operators with spaces and to skip lines between logical sections of the program.

A block, which consists of 0, 1, or more statements, starts with a left curly brace ({) and ends with a right curly brace (}). Blocks are required for class and method definitions and can be used anywhere else in the program that a statement is legal. The example above has two blocks: the class definition (lines 5 through 10) and the main method definition (lines 7 through 9). As you can see, nesting blocks within blocks is perfectly legal. The main block is nested completely within the class definition block.

Screen Shot 2018-12-08 at 10.33.27 PM

Comments document the operation of the program and are notes to yourself and to other programmers who read your code. Comments are not compiled and can be coded in two ways. Block comments can span several lines; they begin with a forward slash-asterisk (/*) and end with an asterisk-forward slash (*/). Everything between the /* and the */ is ignored by the compiler. Note that there are no spaces between the asterisk and forward slash. Lines 1–3 in the above code are block comments and illustrate the good software engineering practice of providing at the beginning of your source code a few comments that identify yourself as the author and briefly describe what the program does. The second way to include comments in your code is to precede the comment with two forward slashes (//). There are no spaces between the forward slashes. The compiler ignores everything from the two forward slashes to the end of the line. In the code above, the compiler ignores all of line 8, but only the part of line 6 after the two forward slashes.

Let’s look at an example to get a sense of what a simple program looks like and to get a feel for how a program operates. Here, we calculate the area of one of the nine circles of hell:

Screen Shot 2018-12-08 at 11.05.57 PM

This is the output:

Screen Shot 2018-12-08 at 11.11.48 PM

If we change the radius to 666, the output is:

Screen Shot 2018-12-08 at 11.13.47 PM

You can see that this Divine Comedy-themed example has the basic elements that we saw in the GhostInThe. I have added some statements in lines 9 to 23 that explain what is going on with the program.

First, we identify the data we will need. To calculate the area of a circle, we use the formula (πr²). We know the practical value of π here (3.14159), so we store that value in a memory location we name PI (line 10). We also need places in memory to hold the radius and the area. We name these locations in lines 13 and 14. In line 17, we give the radius a value; here we have chosen 6.66. Now we’re ready to calculate the area. We want this program to output correct results with any radius, so we need to write the algorithm of the program using the formula for calculating a circle’s area given above. Java provides arithmetic operators for performing calculations. We use Java’s multiplication operator (*) in line 20 to multiply PI times the radius times the radius and store the result into the memory location we named area. Now we’re ready to output the result. On line 23, we write a message that includes the area value we calculated.

The Java Language

There are applications that are called embedded applications because they are meant to stay inside the device. These embedded applications include user interfaces for example. It is also useful to store them in non-volatile memory such as ROM.

You are an embedded application yourself. Special relativity implies relativity of simultaneity, which in turn implies eternity. Everything is stored in the ultimate non-volatile memory – the fabric of spacetime itself.

On what we call May 23, 1995, Sun Microsystems introduced Java, originally named Oak, as a free, object-oriented language targeted at embedded applications for consumer devices.

A Java Virtual Machine was incorporated immediately into the Netscape Navigator Internet browser, and as the Internet exploded in rapid combustion, small Java programs, known as applets, began to blossom on webpages in increasing numbers.

Java syntax is basically identical (with some minor exceptions) to that of C++. Remember that high redundancy of genotype in a highly connected network is what allows evolution to find the next best step up in phenotype, and soon programmers in one Earth constrained by the probability density given by the Born Rule started to realize the benefits of using Java. Those benefits include

• syntax mirroring that of C++, except that Java eliminates some of C++’s more complex features

object orientation

• Internet-related features, such as applets, which are run by the browser, and servlets, which are run by the web server (Remember that you don’t just run your website from home, there are giant facilities with web server’s whose only purpose is to store websites until someone wants to visit them – ignoring of course, their ultimate purpose which is to increase synthesis in Me. However, this ulterior purpose is not readily visible because the “constituents” that make up your experience are “bound” in the absence of time.)

• an extensive library of classes that can be reused readily, including Swing classes for providing a Graphical User Interface and Java Database Connectivity (JDBC) for communicating with a database

portability among every platform that supports a Java Virtual Machine

built-in networking

• open source availability of the Java Development Kit

As was read in a previous post, a Java program is first compiled into processor-independent byte codes, then the byte codes are interpreted at run time by the Java Virtual Machine (JVM). As its name implies, the JVM simulates a virtual processor with its own instruction set, registers, and instruction pointer. Thus, to run a Java program, you only need a JVM. JVMs are available on every major computing platform.

Because Java programs are interpreted at run time, they typically run more slowly than their C++ counterparts. However, many platforms provide Java compilers that convert source code directly to machine code. This results is greater execution speed, but with an accompanying loss of portability. Just-in-Time (JIT) compilers are also available. These JITs compile code at run time so that subsequent execution of the same code runs much faster.

Java programs can be written as applets, servlets, or applications.

Java applets were small programs designed to add interactivity to a webpage. Applets were launched by an Internet browser; they could not run standalone. As the user requested a webpage that used an applet, the applet was downloaded to the user’s computer and run by the JVM in the browser. Due to browser incompatibilities, limitations imposed by security features, and slow download times, however, applets have fallen out of favor.

Java servlets are invoked by the web server and run on the server, without being downloaded to the client (your own computer). Typically, servlets dynamically generate web content by reading and writing to a database using JDBC (Java Database Connectivity).

Java applications run standalone on a client computer. On this site, we will mainly write Java applications.

Oracle Corporation, which ate Sun Microsystems in January 2010, provides a valuable Java website, which has information on using the prewritten classes, a tutorial on Java, and many more resources for the Java programmer.

Short Intro To Object-Oriented Programming

Let’s recall what high-level languages are: these are languages that are symbolic, so not the ones and zeros underneath it all. They are that which provides handles for the human mind to pilot the Eva – in other words complete the task of serving a functional role in a complex society that you as an individual did not create. The way you do this is by learning symbols and how to manipulate these.

Like with all good creation myths, in the beginning, there were two high-level languages, Fortran and Pascal. These were procedural. What that means is that the program starts with a problem and then breaks that problem down into smaller sub-problems or sub-procedures.

Procedural language took a literal approach, working as a straightforward story composed of top-down instructions.

But a program inevitably has sections. Typically, programmers of old had to write task-specific code in separate procedures/functions, and invoked these procedures from one section of the program to another in order to perform various tasks. And the program’s data was generally shared among the procedures – there were no membranes enclosing different cells in the original primordial soup.

In the mid-1970s, the first object-oriented programming language, Smalltalk, was born from the sins of the procedural languages, enabling programmers to write code with a different approach. Whereas procedural languages were brutish in that they did not recycle code by attempting to enclose into concepts and dealt mainly with basic data types such as integers, real numbers, or single characters, Smalltalk provided the programmer with a new tool: classes and objects of those classes. Classes are simply concepts. Like ramen is a class containing the objects of noodles, broth, egg, bowl. If you had to tell the chef to place noodles, broth, and egg, into a bowl every time you walked into a ramen-ya instead of just telling them “give me ramen,” that would be annoying.

The class enables the programmer to encapsulate data, creating membranes. This is how spoken language itself works. A tribe in northern Namibia packages particular wavelengths of light this way:

Screen Shot 2018-12-06 at 5.35.29 PM

Take a look at buru. For you, buru does not look like a single color. You speak a language that does not put a single membrane on those wavelengths. You instead have a class for each. Perhaps you call them green and blue. Once you see it one way it is short of impossible to see it the other. This is the same with language processing – their tribal language is gibberish, but these words on the screen are helplessly understood. That’s the power of classes.

A class defines a template/model, from which objects are created. Creating an object is called instantiation. Thus, objects are created/instantiated according to the design of the class. A class could represent something we imagine to be real such as a person. The class could have various attributes such as in the example of a “person” class: a name, an attractiveness rating, and an age. The class also provides code called methods. Methods allow the creator of the object to set and retrieve the values of the attributes – granting the power to christen the name as Lindsey, the rating as a solid ten, and to then to reveal this name and attractiveness rating when needed.

One big advantage to object-oriented programming is that well-written classes can be reused by new programs, thereby reducing future development time. (This is just in the same way that We reduce your development time by providing you with over five thousand five hundred revolutions around the sun head-start on the human soul packaged in language. This apex is then accessed through the internet and allows sufficiently maniacally self-motivated teens to ascend to heights of knowledge that would otherwise not be accessible even to those with genius-level IQ.)

In this noble endeavor of conserving negentropy through reusability, Smalltalk was somewhat successful, but had a major fault: its syntax was unlike any syntax already known by most programmers. Most programmers who knew C found themselves in a dilemma. They were attracted by the object-oriented features of Smalltalk but were reluctant to use it because its syntax was so different from C’s syntax. Humans are notoriously allergic to changes in syntax, even when a change in syntax might result in easier understanding of certain concepts.

C++ added object-oriented features to C, but also added complexity. Meanwhile, the Internet was swallowing mankind by the day. Web developers used HTML to develop webpages and rapidly had to incorporate programming features not only on the server side, but also directly on the client side. Fortunately, Java emanated from the void.