120 likes | 226 Views
CS140 Programming with Objects. Dick Steflik. What is Java. Besides being the black/brown stuff we drink it is a simple programming language that lets us develop problem solutions in terms of objects and their behaviors. This is known as the Object-Oriented Paradigm (pronounced para-dyme )
E N D
CS140Programming with Objects Dick Steflik
What is Java • Besides being the black/brown stuff we drink it is a simple programming language that lets us develop problem solutions in terms of objects and their behaviors. • This is known as the Object-Oriented Paradigm (pronounced para-dyme) • There are other programming languages that follow the O-O paradigm (Smalltalk, Eiffle, C++) each with their own benefits and shortcomings • Currently Java and C++ are the most commonly used
Why Java for CS140 and C++ for CS240 • Java has a simpler and more complete object model • Because of its object model C++ is more complex to use • Java is safer (this will be explained later) • Java is more portable • Java excels at being a network tool where C++ excels as a desktop tool
What is Java used for • Desktop application programming • 2 & 3D graphics Programming • Open Wonderland; 3D modeling, avatars, virtual worlds • JOGL (Java Open GL) • Web programming • Enterprise class servers • JBOSS/Wildfly - RedHat, Glassfish - Oracle, Websphere-IBM • Web services • Applets – Java programs that run in a web browser • Dynamic web pages, Java servlets and Java Server Pages • Mobile application programming – Android
Object Orientation • Think of an object as a small, special purpose computer, complete with it’s own memory (that we’ll call it’s state) and its own set of instructions. • Instructions can be used to modify it’s memory and send /receive messages to/from other objects. • An object can only do the things it is programmed to do. • A program, is then nothing more than a collection of objects sending and receiving messages
Making objects • Like most programming languages “everything must be defined before it can be used” • This is done using the class statementpublic class Example { …} • Always remember that a class is pure definition, by itself it can’t do anything. An analog of this is a cookie cutter, by itself it is just a definition of what the final cookie will look like
Making a cookie • To make a cookie we take our cutter and whomp it down on a piece of cookie dough, now we have a cookie • In Java we take our class and whomp it down on some unused memory and we make an object. This is call “instantiation” or creating an instance of the class. • Each instance of a class is unique and must be identified with a unique identifier/name.
Packages • To prevent coding anarchy we usually group related classes into packages, this also is used to promote the idea of “reuse” (more on this later). • Here is an example: package cs140; class student { … } • This identifies the class student as a member of the package cs140. • More on packages later
Variables • Any memory used by our objects should be defined in the class definition as variable. • Variable identifiers should start with a alpha character and then can be any number of alpha or numeric characters long (can use some special characters like _ and &
Primitive types • Every variable must be defined as being of some type. Types are almost identical to the types used in C and C++ • int, long, double, char, boolean • Each type has a defined range of values (see the table of types in section 4.1) • Generally type tell us three things about a variable: it’s range, it’s operations, and its memory requirements
Some examples • int value = 1; //definition and initializationint total; //definition onlychar letterG = ‘G’; //internally these are in unicode not ascii (like C and C++)char aKoreanCharacter = ‘\ud55c’long aBigNumber = 12345678901234567890L;boolean switch = true;float distance = 315.24674; (32 bit, IEEE754 format, real fractional numbers) double pi = 22/7; // (64 bit, IEEE754 format, high precision real fractional number byte smallNumber = (byte)123; // range from -128 to +127short notSoSmallButNotRealBig = 23456; //range -32768 to +32767 • These are Java’s 8 primitive types, they are built into the language and are sometime call atomic types as they can’t be broken down any smaller. • All other types are types that we create via the class statement and are called “reference” types