Java Definitions and Explanations
|
Warning: These notes have been collected from several different sources - each uses different terminology... they may even be contradictory... Java is case sensitive so upper and lower case characters are important. Java is object oriented programming. An object is a unique instance of a class. A class is a description of a possible object. Or you could say that a class is a plan for an object and an object is what results when the plan has been carried out. Example class: a house plan objects: the houses built from the house plan Example class: a cookie cutter objects: the cookies created with the cookie cutter The programmer defines classes that describe future objects when the program is written. As the program runs, objects are created and their methods are activated. The program does its work by creating objects and activating their methods. An object has identity (it acts as a single whole, it has it's own variables) An object has state (it has various properties, which might change... the values held in it's variables) An object has behavior (it can do things and can have things done to it, often using those variables) A good object has both: attributes/properties/descriptors/variables and actions(a verb)/operations/responsibilities/behaviors/methods Variable names are called identifiers (programmer-picked variable name.) Here are the rules for choosing an identifier/variable name: Use only the characters 'a' through 'z', 'A' through 'Z', '0' through '9', character '_', and character '$'. A name can not contain the space character. Do not start with a digit. A name can be any length. Upper and lower case count as different characters. So SUM and Sum are different names. A name can not be a reserved word. (has a predefined meaning in Java) A name must not already be in use in this part of the program.Constants follow the same rules as the names for variables. The reserved word FINAL tells the compiler that the value will not change, it is a constant. (Programmers sometimes use all capital letters for constants; but that is a matter of personal style, not part of the language.) There are types of data that are so fundamental that ways to represent them are built into Java. These are the primitive data types. There are only eight primitive data types. In the phrase primitive data type the word primitive means "a fundamental piece that is used to create other, larger parts." To solve a large problem, you look for the primitive operations that are needed, then use them to build the solution. For each primitive data type, there is a corresponding wrapper class. A wrapper class can be used to convert a primitive data value into an object, and some type of objects into primitive data. To convert to a corresponding wrapper class, merely capitalize the name of the primitive data type such as "byte" to "Byte". An operator is a symbol +, -, *, /, % that calls for an arithmetic operation. An operand is a value that is acted upon by an operator. An expression is a combination of literals, operators, variables, and parentheses used to calculate a value. An integer operation is always done with 32 bits or more. If one or both operand is 64 bits (data type long) then the operation is done with 64 bits. Otherwise the operation is done with 32 bits, even if both operands are smaller. Arithmetic expressions are not the only kind of expression: "This is" + " a string" + " expression" An assignment statement is one way to change the value in a variable. variableName = expression; (The equal sign "=" means "assignment.") variableName: is the name of a variable that has been declared somewhere in the program expression: is a collection of characters that calls for a value An assignment statement asks for the computer to perform two steps, in order: 1. Evaluate the expression on the right of the equal sign, (that is: calculate a value.) 2. Store the value in whatever variable is on the left of the equal sign. Input and output are commonly referred to by an abbreviation: IO Inputting data is usually called reading data. Outputting data is usually called writing data (or printing data if the output stream is connected to a monitor or a printer.) There are three IO streams usually connected to your program: System.in --- the input stream. System.out --- the output stream for normal results. System.err --- the output stream for error messages.A file is a semi-permanent, named collection of information. semi-permanent---files are usually stored on magnetic disk, where they will remain for years even when the power is off. Of course, a file can be deleted (sometimes by accident) so they are semi-permanent, not permanent. named---a file has a name that is used to find it when it is needed. You probably know that MS Windows file names
look like: mydata.txt, program1.java, doom.exe, and so on... collection of information---the purpose of a file is to contain a collection of related information, such as a word processing document, a program source file, a spread sheet, a data base, and so on.Files are the only way for an application program (such as the programs you usually use) to store information semi-permanently. Files are used when information is stored on the hard disk, a floppy disk, and all other physical forms of storage. All information in a file (no matter what file) is kept in binary form. a file can contain any type of information that can be represented with symbols, such as: numbers, characters, text files, images, program source files, machine language and many other types. The system software of a computer system uses a few other semi-permanent forms of storage, such as the ROM (read-only memory) of the computer and the boot sector of a hard disk. However from an application program's point of view the only form of semi-permanent storage is file storage. As far as the hardware of a computer system is concerned, all files look alike. Each file is regarded by hardware as a collection of bytes. The hardware makes no distinction between (say) image files and text files. Its all bytes to the hardware. What those bytes are being used for is up to the software. An object is a section of memory (with variables and methods), and a class is a description of a possible object. When an object is created (instantiated) the description is followed. One way in which objects are created is when the new operator is used with a constructor. The new operator says to create a new object. It is followed by the name of a constructor. A constructor has the same name as the class. The constructor String( ) is part of the definition for the class String. Constructors often are used with values (called parameters) that are to be stored in the data part of the object that is created. There are usually several different constructors in a class, each with different parameters. Sometimes one parameter is more convenient to use than another, depending on how the new object's data is to be initialized. However, all the constructors of a class follow the same plan in creating an object. The two types of things inside of an object---variables and methods---are sometimes called members of that object. The members of an object are accessed using dot notation. ( referenceToAnObject . partOfTheObject ) The example below creates a "String object, referred to by" the variable named stringA. A variable name "len" will hold the value returned when the program is run. The length( ) method is a member of stringA. To refer to this method of the object stringA put the two together with a dot: len = stringA.length( ); 1.The expression on the right of the "=" is evaluated. 2.The resulting value is stored in the variable on the left of the "=" sign. The right-hand side of this particular assignment statement executes the method length( ) which is member of stringA. Executing this member method will return the number of characters in the String object referenced by stringA. This dot notation is also known as a method call. The comparison of if ( stringA == stringB ) will not correctly compare the values held in the Strings as the String object contains only a "reference to the object" not the actual value of the object, such as a primitive data type would. Therefore use method: if (stringA.equals( stringB )) The String referred to by stringA uses an equals( ) method. That method is called with a parameter, a reference to stringB. The method checks if both strings contain identical characters, and if so, evaluates to true ("returns true"). Careful: People often say "String" when they really mean "reference to a String". This is fine, but remember that a variable like stringA is not the object, but only a reference to an object. Therefore, the equals method of the String referenced by stringA is called with a reference to stringB. When a method has a reference to an object, the method can: 1.access the object's instance variables using dot notation, and 2.call the object's methods using dot notation. However it is common for a class have private variables and methods. Only methods of the class can access these. Methods... all method names end with "( )" variableStringName.length( ) variableStringName.equals( ) variableName.readLine( ) Integer.parseInt( variableName ) the method parseInt( ) of the Integer wrapper class. Double.parseDouble( variableName ) will not run under versions of Java earlier than 1.2 An object reference variable can exist without referring to an object. A reference variable can be declared without initialization: String myString; Also, a reference can be set to null: String b = null; * Remember: String a = ""; is NOT the same as: String b = null; the first is like having a blank sheet of paper, versus having no paper at all (an empty string.) An object can exist without an object reference variable referring to it. As in the String myString; example. Such objects are temporary. The process called requirements analysis is the most important (and most difficult) (and highest paid) phase of writing programs. Even for small programs it is important to know what you want before you start writing. To develop a "checking account class", we must decide what "checking account objects" will do and what data they will hold. See " What makes an object?" The class definition will be a blueprint for checking account objects, so any data and behavior that we want for a checking account must be described in the class.
When developing and testing each part of a program... Testing usually involves placing statements that write out information to the terminal as the program is being run. Thoughtful placement of these statements can greatly ease software development. These statements (and other statements intended to be used for testing and program development) are sometimes called scaffolding. They are put in place as the program is being written and tested, and are removed when testing is finished. This is similar to the scaffolding used when a building is being constructed. Well-placed scaffolding greatly aids the carpenters, roofers, painters and other trades that work on the building. Programmers should do the same for themselves. There can be more than one reference to a given object. Each reference is called an alias. The "= =" (equal-equal) operator is an alias detector. It checks if two reference variables refer to the same object. account3 = account1;
if ( account1 == account 3 )
System.out.println("An alias has been detected!");
else
System.out.println("These are different objects!");
|
See Java Links for more info, Object & Classes , Data Types , Operators , Conditionals , Definitions , Keywords , File Structure , Code Examples , Class Skeleton , Java Notes



Use only the characters 'a' through 'z', 'A' through 'Z', '0' through '9', character '_', and character '$'.
A name can not contain the space character.
What makes an 
