Click here to load reader

CSE 252 Principles of Programming Languages Lab . Section

  • Upload
    kyne

  • View
    36

  • Download
    0

Embed Size (px)

DESCRIPTION

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 ?. - PowerPoint PPT Presentation

Citation preview

CSE 252 Principles of Programming Languages Lab. Section

WEEK 1INTRODUCTION TO JAVA

by:R.A. Kemal ar SerdaroluCSE 252 Principles of Programming Languages Lab. Section

WEEK 1What is Java Programming Language?Java Programming Basics: Java vs C.Using Classes at Java API.

What is Java Programming Language?Java is an high level programming language in the tradition of C and C++. Java is a platform independent programming language. Platform independence means any program written in a language can run all operating system platforms.Java is an OOP(Object Oriented Programming) Language.OOP Languages are based on objects and classes.

JVM (Java Virtual Machine)The compilation phase of Java is different than Cs.Virtual Machines are hypothetical computer platforms e.g. a design for a computer that does not really exist on any actual computer.JVM is a kind of VM that is an implementation environment of a Java Application.Java is a platform independent language so it needs JVM.JRE(Java Runtime Environment) is an emulation tool that creates a JVM environment that can execute Java programs.JRE and JVMFor running an application written in Java Language, the JRE must be installed on any computer.

JRE and JVM are used for running 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.classfiles.

JDK(Java Development Kit)SDK(Software Development Kit)s are typical set of development tools for creation of applications.JDK is an extension of a SDK which can be used for creation of java applications.JDK is used for creating .class files from .java source files. So it must be installed at a computer for creating java applications.Basics of a Typical Java EnvironmentJava programs normally undergo five phases- Edit (JDK):Programmer writes program(.java files) and stores program on disk- Compile(JDK):Compiler creates bytecodes (.class files) from program(.java files)- Load(JVM):Class loader stores bytecodes in memory- Verify(JVM) : Verifier ensures bytecodes do not violate security requirements-Execute(JVM):Interpreter translates bytecodes into machine language

Learning JavaTwo groups for learning for java.Basic Java Syntax: Variables Loops Conditional Statements Class Interface Inheritence Polymorphism etc

Java API (Application Programming Interface): set of classes and interfaces that comes with the JDK. Java API is the collection of libraries(packages) for java program development. Example of libraries at Java API: File IO, Swing, Math etc

You will learn how to use Java API for developing a java application with the help of Java Syntax tools and Object Oriented Programming concepts such as Classes, Inheritence and Polymorphism.Java Programming Basics Java is an OOP (Object Oriented Programming) Language.OOP languages uses objects consisting of data fields(variables) and methods(funcitons) together with their interactions.Learning OOP concepts are not a straight-forward process and a new concept for a student who knows C language. C is a functional programming language.OOP has new programming techniques such as encapsulation, inheritence and polymorphism.

Java Programming Basics(4) Java API is an important source for developing programs. Java API consists of basic classes and the elements of JavaAPI have some OOP concepts.Hence, the basic usage of Java API requires the basic information of OOP.

The developers must know OOP concepts for program development in Java. The basic learning of Java starts at learning OOP concepts !!!

Java Programming BasicsEvery programming languages has a syntax..

Java has a C-like syntax and there will be similarities with C.

Of course there will be differences between C language.

Java vs C (1) Programming Language Structure

CJavaType of languageFunctional, not OOPOOPBasic programming str.functions.objects.Main methodint main(){return 0;}public class ExampleClass{public static void main(String[] args){}}It is required to define a class.Java vs CProgramming Language Structure

CJavaFile namesNo constraint, ends with .cEnds with .javaFile name must be the name with class name.File NamesExample: ExampleClass.java:public class ExampleClass{public static void main(String[] args){}}Java vs CProgramming Language Structures

CJavaVariable declarationAt the beginning of the blockBefore using itVariable declarationint main(void){int a,b;a=5;b=3;return 0;} public class ExampleClass{public static void main(String[] args){int a;a=5;int b;b=3;}

Java vs CProgramming Language Structure

CJavaAccessing a library#include import java.io Memory addresspointerReferenceJava vs C(2) HelloWorld Application & User Interaction

CJavaHello,World#includeint main(void) {printf("Hello\n");return 0;}public class HelloWorld {public static void main(String[] args) { System.out.println("Hello");}}Java vs C(2) Hello World Application and User Interaction

CJavaprintf (Using not formatted) (1)int a;a=3;printf(A is %d,a);int a;a=3;System.out.print(A is + a);Java vs C(2) HelloWorld Application and User Interaction

CJavaprintf(Using not formatted) (2)int a;printf(A is %d\n,a);int a;a=3;System.out.print(A is + a + \n);-----------------int a;a=3;System.out.println(A is + a);Java vs C(2) Hello World Application and User Interaction

CJavascanf#include int main(void){int num;scanf(%d,&num);printf(%d,num);}import java.util.Scanner;public class UserInteraction {public static void main(String[] args) {Scanner userIn = new Scanner(System.in);int num=userIn.nextInt();System.out.println(num);}}Java vs C(2) HelloWorld Application and User Interaction

CJavaC - functionsint max(int a,int b){}-----------------Example:#include int max(int a,int b){return a>b?a:b;}int main(){int a=max(4,15);printf("%d\n",a);return 0;}static int max(int a,int b){} -------public class FunctionExample { public static void main(String[] args){ int a=max(4,15); System.out.println(a); } static int max(int a,int b){ return a>b?a:b; } }Java vs CCJavainteger typesshort 16 bit.intusually 32 bit 2's complement;longusually 32 bit 2's complementshort 16 bit.intis 32 bit 2's complement;

longis 64 bit 2's complementfloating point typesfloatusually 32 bit;doubleusually 64 bitfloatis 32 bit(same)doubleis 64 bit (same)boolean typeuseint: 0 for false, nonzero for truebooleanis its own type - stores valuetrueorfalsecharacter typecharis usually 8 bit ASCII-------------char c;c=a;charis 16 bit UNICODE------------------------char c;c=a;Java vs CComments

Comments are same !!!Java vs CIf-Switch Statements:

CJavaif(1)if(1){}if(1){} is not acceptedif(true){} is usedif(2)if(x==5){}f(x==5){}if(3)if(x==5 && x!=6 || x