INTRODUCTION TO JAVA. Introduction to Java Java programming language ◦ Object-Oriented Programming ◦ Compiled to byte code of JVM Java virtual machine.

  • Published on

  • View

  • Download


<ul><li><p>INTRODUCTION TO JAVA</p></li><li><p>Introduction to JavaJava programming languageObject-Oriented ProgrammingCompiled to byte code of JVMJava virtual machine (JVM)Java interpreter interpret the compiled byte codeSoftware simulated CPU architectureCross-platform: support Linux, Windows, PalmOSetc.Java runtime environment (JRE)Predefined set of java classes available to useCore Java APIs basic utilities, I/O, graphics, network</p></li><li><p>Java is portable,As long as there is a JVM compiled for that particular processor and OSTypical program, like C or C++, is compiled for a particular processor architecture and OS.Write once, run everywhere!Suns motto for Java</p></li><li><p>Bottom line: slow but safeNot suitable for high-performance computationScientific computation, games, OS kernelCompiled to byte codes of JVM, not native machine language instructionsNew release of Java is improving the speed a lotJust-in-time (JIT) compiler: convert byte codes to native machine language on the flyVery safeNo pointerAutomatic garbage collectionCheck array access bound</p></li><li><p>JavaJava is an object-oriented language, with a syntax similar to CStructured around objects and methodsA method is an action or something you do with the objectAvoid those overly complicated features of C++:Operator overloading, pointer, templates, etc. </p></li><li><p>Getting and using javaJ2SDK freely download from ( text editors support javaNotepad, wordpadJust save to .java fileHave IDEs that comparable to Visual StudioEditPlus (simple)JCreator (simple)Eclipse (more complicated)</p></li><li><p>Compile and run an applicationWrite java class Hello containing a main() method and save in file Hello.javaThe file name MUST be the same as class nameCompile with: javac Hello.javaCreates compiled .class file: Hello.classRun the program: java HelloNotice: use the class name directly, no .class!</p></li><li><p>Hello World!/* Our first Java program */public class Hello {//main()public static void main ( String[] args ) {System.out.println( "hello world!" );} } File name: Hello.javaCommand line argumentsStandard output, print with new line</p></li><li><p>About classFundamental unit of Java programAll java programs are classesEach class define a unique kind of object ( a new data type)Each class defines a set of fields, methods or other classespublic: modifier. This class is publicly available and anyone can use it.</p></li><li><p>Things to noticeJava is case sensitivewhitespace doesnt matter for compilationFile name must be the same as one of the class names, including capitalization!At most one public class per fileIf there is one public class in the file, the filename must be the same as itGenerally one class per file</p></li><li><p>What is an object?Object is a thingAn object has state, behavior and identityInternal variable: store stateMethod: produce behaviorUnique address in memory: identityAn object is a manifestation of a class</p></li><li><p>What is class? Class introduces a new data typeA class describes a set of objects that have identical characteristics (data elements) and behaviors (methods).Existing classes provided by JREUser defined classesOnce a class is established, you can make as many objects of it as you like, or none.</p></li><li><p>Simple example: class PersonA Person has some attributesThe class defines these properties for all peopleEach person gets his own copy of the fieldsAttributes = properties = fields</p></li><li><p>Class Person: definitionclass Person { String name; int height; int weight; public void printInfo(){System.out.println(name+" with height="+height+", weight="+weight); }}class ClassName{ /* class body goes here */ }class: keyword</p></li><li><p>Class Person: usagePerson p1; //declarationp1 = new Person(); //create an object of David; //access its fieldPerson p2 = new Person();;p1.printInfo(); p2.printInfo(); // error here??</p></li><li><p>Class PersonName: Davidheight: 0weight: 0Name: Michaelheight: 0weight: 0p1p2</p></li><li><p>Class Person: variablesPerson x;x=p1;x.printInfo();x=p2;x.printInfo();This gives the same output as previous code ! </p></li><li><p>Class Person: variablesName: David height: 0weight: 0Name: Michaelheight: 0weight: 0p1p2xreferencesobjects</p></li><li><p>ReferenceWe call x, as well as p1 and p2, reference to the objectHandles to access an objectReference itself is not accessible/manipulableDifferent from C/C++, cannot increment/decrement itImplemented as pointer+Java runtime is watching all assignment to references </p></li><li><p>ReferencePerson p1; //only created the reference, not an object. It points to nothing now (null).p1 = new Person();//create the object (allocate storage in memory), and p1 is;//access the object through the reference</p></li><li><p>More on referenceHave distinguished value null, meaning pointing to nothingif( x==null) { }Multiple references can point to one objectWhen no reference point to an object, that object is never accessible again.</p></li><li><p>Class Person: problemp1.weight = 150; // too bad, but possiblep1.weight = -20; // problem!!Need to ensure the validity of value.p1.setWeight(150); // OK, now p1s weight is 150p1.setWeight(-10); ******** Error, weight must be positive number</p></li><li>Class Person: add methodclass Person { ... void setWeight(int w){ if(w</li><li><p>Class Person: new problemp1.setWeight(-10); ******** Error, weight must be positive numberp1.weight = -20; ****** ?How about we forgot to use the set function? Or we just dont want to? Solution: just make the variable inaccessible from outside!</p></li><li>Class Person: private variableclass Person{ private String name; private int weight; private int height; public void setWeight(int w){if(w</li><li><p>Class Personclass Hello{</p><p>public static void main ( String[] args ) {Person p1= new Person();p1.weight = -20;} } &gt;javac weight has private access in Person p1.weight = -20; ^1 error</p></li><li><p>Java Basics: primitive typesOne group of types get special treatment in JavaVariable is not created by new, not a referenceVariable holds the value directly</p></li><li><p>Primitive types</p><p>Primitive typeSizeMinimumMaximumWrapper typeboolean1-bitBooleanchar16-bitUnicode 0Unicode 216- 1Characterbyte 8-bit-128+127Byteshort16-bit-215+215-1Shortint32-bit-231+231-1Integerlong64-bit-263+263-1Longfloat32-bitIEEE754IEEE754Floatdouble64-bit IEEE754IEEE754Double</p></li><li><p>Primitive typesAll numerical types are signed!No unsigned keyword in JavaThe wrapper class allow you to make a non-primitive object to represent the primitive onechar c =a;Character C = new Character(c);Character C = new Character(a);</p></li><li><p>Primitive types - booleanboolean can never convert to or from other data type, not like C or C++boolean is not a integerif(0) doesnt work in javaHave to explicitly state the comparisonif( x ==0) {</p></li><li><p>Primitive types - charChar is unsigned typeThe Character wrapper class has several static methods to work with char, like isDigit(), toUpperCase() etc.</p></li><li><p>Default values for primitive membersWhen a primitive type data is a member of a class, its guaranteed to get a default value even if you dont initialize it.Not true for those local variables!!There will be compile error if you use it without initialization</p><p>Primitive typeDefaultbooleanfalsechar\u0000 (null)byte(byte)0short(short)0int0long0Lfloat0.0fdouble0.0d</p></li><li><p>Example class Hello{</p><p>public static void main ( String[] args ) {int x;System.out.println(x);} } &gt;javac variable x might not have been initialized System.out.println(x); ^1 error</p></li></ul>


View more >