1 / 38

CSE 252 Principles of Programming Languages Lab . Section

CSE 252 Principles of Programming Languages Lab . Section. WEEK 1 INTRODUCTION TO JAVA by : R.A. Kemal Çağrı Serdaroğlu. WEEK 1. What is Java Programming Language ? Java Programming Basics : Java vs C. Using Classes at Java API. What is Java Programming Language ?.

kyne
Download Presentation

CSE 252 Principles of Programming Languages Lab . Section

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 252 Principles of ProgrammingLanguagesLab. Section WEEK 1 INTRODUCTION TO JAVA by: R.A. Kemal Çağrı Serdaroğlu

  2. WEEK 1 • What is Java ProgrammingLanguage? • Java ProgrammingBasics: Java vs C. • UsingClasses at Java API.

  3. What is Java ProgrammingLanguage? • Java is an highlevelprogramminglanguage in thetradition of C and C++. • Java is a platform independentprogramminglanguage. Platform independencemeansany program written in a language can runalloperatingsystemplatforms. • Java is an OOP(ObjectOrientedProgramming) Language. • OOP Languagesarebased on objectsandclasses.

  4. JVM (Java VirtualMachine) • Thecompilationphase of Java is differentthanC’s. • VirtualMachinesarehypotheticalcomputerplatforms e.g. a designfor a computerthatdoes not reallyexist on anyactualcomputer. • JVM is a kind of VM that is an implementationenvironment of a Java Application. • Java is a platform independentlanguageso it needs JVM. • JRE(Java RuntimeEnvironment) is an emulationtoolthatcreates a JVM environmentthat can execute Java programs.

  5. JRE and JVM • Forrunning an applicationwritten in Java Language, the JRE must be installed on anycomputer. • JRE and JVM areusedforrunning a java program. • Programs intended to run on a JVM must be compiled into a standardized portable binary format, which typically comes in the form of .class files.

  6. JDK(Java Development Kit) • SDK(Software Development Kit)s aretypical set of developmenttoolsforcreation of applications. • JDK is an extension of a SDK which can be usedforcreation of javaapplications. • JDK is usedforcreating.classfilesfrom.javasourcefiles. So it must be installed at a computerforcreatingjavaapplications.

  7. Basics of a Typical Java Environment Java programs normally undergo five phases - Edit (JDK):Programmer writes program(.javafiles) and stores program on disk - Compile(JDK):Compiler creates bytecodes (.classfiles) from program(.javafiles) - Load(JVM):Class loader stores bytecodes in memory - Verify(JVM) : Verifier ensures bytecodes do not violate securityrequirements -Execute(JVM):Interpreter translates bytecodes into machine language

  8. Learning Java • Twogroupsforlearningforjava. • Basic Java Syntax: Variables – Loops – ConditionalStatements – Class – Interface – Inheritence – Polymorphismetc… • Java API (ApplicationProgrammingInterface): set of classesandinterfacesthatcomeswiththe JDK. Java API is thecollection of libraries(packages) forjava program development. Example of libraries at Java API: File IO, Swing, Math… etc Youwilllearnhowtouse Java API fordeveloping a javaapplicationwiththehelp of Java SyntaxtoolsandObjectOrientedProgrammingconceptssuch as Classes, InheritenceandPolymorphism.

  9. Java ProgrammingBasics • Java is an OOP (ObjectOrientedProgramming) Language.OOP languagesusesobjectsconsisting of data fields(variables) andmethods(funcitons) togetherwiththeirinteractions. • Learning OOP conceptsare not a straight-forwardprocessand a newconceptfor a studentwhoknows C language. C is a functionalprogramminglanguage. • OOP has newprogrammingtechniquessuch as encapsulation, inheritenceandpolymorphism.

  10. Java ProgrammingBasics (4) Java API is an importantsourcefordevelopingprograms. Java API consists of basicclassesandtheelements of JavaAPIhavesome OOP concepts.Hence, thebasicusage of Java API requiresthebasicinformation of OOP. Thedevelopersmustknow OOP conceptsfor program development in Java. Thebasiclearning of Java starts at learning OOP concepts !!!

  11. Java ProgrammingBasics • Everyprogramminglanguages has a syntax.. • Java has a C-likesyntaxandtherewill be similaritieswith C. • Of coursetherewill be differencesbetween C language.

  12. Java vs C (1) ProgrammingLanguageStructure

  13. Java vs C • ProgrammingLanguageStructure

  14. Java vs C • ProgrammingLanguageStructures

  15. Java vs C • ProgrammingLanguageStructure

  16. Java vs C (2) HelloWorldApplication & UserInteraction

  17. Java vs C (2) HelloWorldApplicationandUserInteraction

  18. Java vs C (2) HelloWorldApplicationandUserInteraction

  19. Java vs C (2) HelloWorldApplicationandUserInteraction

  20. Java vs C (2) HelloWorldApplicationandUserInteraction

  21. Java vs C

  22. Java vs C • Comments Commentsaresame !!!

  23. Java vs C • If-SwitchStatements:

  24. Java vs C • Loops

  25. Java vs C • Loops

  26. UsingClasses at Java API Look at thisexample: importjava.util.Scanner;// --- (1) publicclassUserInteraction { publicstaticvoidmain(String[] args) { ScanneruserIn = newScanner(System.in); // --- (2) intnum=userIn.nextInt();// -- (3) System.out.println(num); } }

  27. UsingClasses at Java API • (1) importjava.util.Scanner; Scanneris a classwhich is existed in Java API and it is at java.utilpackageunder Java API. Inour program, wewanttouse it sowemustimportthisclass. importjava.util.*; Ifwewanttousemoreclasses at java.utilpackage, wewillusethestatementabove.

  28. UsingClasses at Java API • (2) ScanneruserIn = newScanner(System.in); Here, wewanttouse a Scannerobject in our program. object themeaningfulstructurewhich has behavioursandvariablesfor a program. Objectsareallocated at thememory.Wemustcreateobjects of theclasses in Java API tohavefunctionalities. class  programmingstructurewhichdefinesthebehaviourandvariabletypes of an object. For Java API classes, wedon’tknowthestructure of a class. Theyservefunctionalitiesforourprogramssoweuseinstances of them.

  29. UsingClasses at Java API • (2) ScanneruserIn = newScanner(System.in); newScanner(System.in)  creates an objectwithScannertype. Scanner is a class. Memory is allocatedforstoringfields at Scannerclass. Ifthislinewill be implementedwithout an assignoperation, the program cannotaccessfieldsormethods of theobject. ScanneruserIn = newScanner(System.in); Thisassignmentoperation is usedforaccessingobjectaftercreation of it. userIn is theaccessorforcreatedScannerobjectandwe can use it foraccessingthisobject’svariablesandmethods.

  30. UsingClasses at Java API • (3) intnum=userIn.nextInt(); userInis theaccessorforournewcreatedobject. Herewewanttouseobject’snextInt() method. userInis here a referencetypeand it is like a pointer at C. However, referencetypeshave no pointerarithmeticandtheyaccessmemorysafely.

  31. PrimitiveTypes • Primitivetypes at Java are : int, short, long, boolean, char, float, double Example: int a; // (1) a=500; // Assigningto a primitivetype (2)

  32. ReferenceTypes • Othertypesare in javaarereferencetypes. • Referencetypeslooklike a pointer. • Youcannotaccessanyothermemoryaddressusingpointerarithmetic. • In Java, objectsareaccessiblewith a referencetype. • Arraysaretooreferencetypes.

  33. ReferenceTypeExample(1) String s=“Kemal”; // aliastoString s=newString(“Kemal”);

  34. ReferenceTypeExample(2) String s=“Kemal”; // (1) s=“Ali”; //(2)

  35. ReferenceTypeExample(3) Scanner s=newScanner(System.in);// objectcreation (1) Scanner t=newScaner(System.in); // objectcreation(2)

  36. ReferenceTypeExamples(4) Scanner s=newScanner(System.in);// objectcreation (1) s=newScaner(System.in); // objectcreation(2) // Samewithexample 2

  37. Arrays Arraysarereferencetypes at Java. Creation of an array(1): PrimitiveTypeArray:

More Related