390 likes | 568 Views
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 ?.
E N D
CSE 252 Principles of ProgrammingLanguagesLab. Section WEEK 1 INTRODUCTION TO JAVA by: R.A. Kemal Çağrı Serdaroğlu
WEEK 1 • What is Java ProgrammingLanguage? • Java ProgrammingBasics: Java vs C. • UsingClasses at Java API.
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.
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.
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.
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.
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
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.
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.
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 !!!
Java ProgrammingBasics • Everyprogramminglanguages has a syntax.. • Java has a C-likesyntaxandtherewill be similaritieswith C. • Of coursetherewill be differencesbetween C language.
Java vs C (1) ProgrammingLanguageStructure
Java vs C • ProgrammingLanguageStructure
Java vs C • ProgrammingLanguageStructures
Java vs C • ProgrammingLanguageStructure
Java vs C (2) HelloWorldApplication & UserInteraction
Java vs C (2) HelloWorldApplicationandUserInteraction
Java vs C (2) HelloWorldApplicationandUserInteraction
Java vs C (2) HelloWorldApplicationandUserInteraction
Java vs C (2) HelloWorldApplicationandUserInteraction
Java vs C • Comments Commentsaresame !!!
Java vs C • If-SwitchStatements:
Java vs C • Loops
Java vs C • Loops
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); } }
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.
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.
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.
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.
PrimitiveTypes • Primitivetypes at Java are : int, short, long, boolean, char, float, double Example: int a; // (1) a=500; // Assigningto a primitivetype (2)
ReferenceTypes • Othertypesare in javaarereferencetypes. • Referencetypeslooklike a pointer. • Youcannotaccessanyothermemoryaddressusingpointerarithmetic. • In Java, objectsareaccessiblewith a referencetype. • Arraysaretooreferencetypes.
ReferenceTypeExample(1) String s=“Kemal”; // aliastoString s=newString(“Kemal”);
ReferenceTypeExample(2) String s=“Kemal”; // (1) s=“Ali”; //(2)
ReferenceTypeExample(3) Scanner s=newScanner(System.in);// objectcreation (1) Scanner t=newScaner(System.in); // objectcreation(2)
ReferenceTypeExamples(4) Scanner s=newScanner(System.in);// objectcreation (1) s=newScaner(System.in); // objectcreation(2) // Samewithexample 2
Arrays Arraysarereferencetypes at Java. Creation of an array(1): PrimitiveTypeArray: