Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 252 Principles of Programming Languages Lab. Section

Similar presentations


Presentation on theme: "CSE 252 Principles of Programming Languages Lab. Section"— Presentation transcript:

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

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

3 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.

4 JVM (Java Virtual Machine)
The compilation phase of Java is different than C’s. 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.

5 JRE and JVM For 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 .class files.

6 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.

7 Basics of a Typical Java Environment
Java 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

8

9 Learning Java Two 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.

10 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.

11 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 !!!

12 Java Programming Basics
Every 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.

13 Java vs C (1) Programming Language Structure C Java Type of language
Functional, not OOP OOP Basic programming str. functions. objects. Main method int main(){ return 0; } public class ExampleClass{ public static void main(String[] args){ It is required to define a class.

14 Java vs C Programming Language Structure C Java
File names No constrai nt, ends with .c Ends with .java File name must be the name with class name. File Names Example: ExampleClass.java: public class ExampleClass{ public static void main(String[] args){ }

15 Java vs C Programming Language Structures C Java Variable declaration
At the beginning of the block Before using it int main(void){ int a,b; a=5; b=3; return 0; } public class ExampleClass{ public static void main(String[] args){ int a; int b;

16 Java vs C Programming Language Structure C Java Accessing a library
#include <stdio.h> import java.io Memory address pointer Reference

17 Java vs C (2) HelloWorld Application & User Interaction C Java
#include<stdio. h> int main(void) {    printf("Hell o\n");    return 0; } public class HelloWorld {    public static void main(String[] args) {         System.out.println("Hello");    } }

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

19 Java vs C (2) HelloWorld Application and User Interaction C Java a=3;
printf(Usin g not formatted) (2) int a; printf(“A is %d\n”,a); a=3; System.out.print(“A is ” + a + “\n”); System.out.println(“A is ” + a);

20 Java vs C (2) Hello World Application and User Interaction C Java
scanf #include <stdio.h> int main(void){ int num; scanf(“%d”,&n um); printf(“%d”,n um); } 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);

21 Java vs C (2) HelloWorld Application and User Interaction C Java
C - function s int max(int a,int b){} Example: #include <stdio.h> 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){ System.out.println(a); static int max(int a,int b){ Java vs C (2) HelloWorld Application and User Interaction

22 Java vs C C Java integer types short 16 bit.
int usually 32 bit 2's complement; long usually 32 bit 2's complement int is 32 bit 2's complement; long is 64 bit 2's complement floating point types float usually 32 bit; double usually 64 bit float is 32 bit(same) double is 64 bit (same) boolean type use int: 0 for false, nonzero for true boolean is its own type - stores value true or false character type char is usually 8 bit ASCII char c; c=’a’; char is 16 bit UNICODE

23 Java vs C Comments Comments are same !!!

24 Java vs C If-Switch Statements: C Java if(1) if(1){}
if(1){} is not accepted if(true){} is used if(2) if(x==5){} İf(x==5){} if(3) if(x==5 && x!=6 || x<7){} if(x==5 && x!==6 || x<7){} if(4) No boolean expression boolean f=true; if(f){ System.out.println(“true is “ + f); }

25 Java vs C Loops C Java For loop int i; for(i=0;i<N;i++){}
for(int i=0;i<N;i++){} While loop (1) while(1){} While(1){} is not accepted!! while(true){} boolean t=true; while(t){}

26 Java vs C Loops C Java While loop(2) int i=3; while(i<5){ i++; }
break while(1){ if(x==0){break;} while(true){ continue int i=10; while(i>0){ if(i==5){continue;} i--;

27 Using Classes at Java API
Look at this example: import java.util.Scanner;// --- (1) public class UserInteraction { public static void main(String[] args) { Scanner userIn = new Scanner(System.in); // --- (2) int num=userIn.nextInt();// -- (3) System.out.println(num); }

28 Using Classes at Java API
(1) import java.util.Scanner; Scanner is a class which is existed in Java API and it is at java.util package under Java API. In our program, we want to use it so we must import this class. import java.util.*; If we want to use more classes at java.util package, we will use the statement above.

29 Using Classes at Java API
(2) Scanner userIn = new Scanner(System.in); Here, we want to use a Scanner object in our program. object  the meaningful structure which has behaviours and variables for a program. Objects are allocated at the memory.We must create objects of the classes in Java API to have functionalities. class  programming structure which defines the behaviour and variable types of an object. For Java API classes, we don’t know the structure of a class. They serve functionalities for our programs so we use instances of them.

30 Using Classes at Java API
(2) Scanner userIn = new Scanner(System.in); new Scanner(System.in)  creates an object with Scanner type. Scanner is a class. Memory is allocated for storing fields at Scanner class. If this line will be implemented without an assign operation, the program cannot access fields or methods of the object. Scanner userIn = new Scanner(System.in); This assignment operation is used for accessing object after creation of it. userIn is the accessor for created Scanner object and we can use it for accessing this object’s variables and methods.

31 Using Classes at Java API
(3) int num=userIn.nextInt(); userIn is the accessor for our new created object. Here we want to use object’s nextInt() method. userIn is here a reference type and it is like a pointer at C. However, reference types have no pointer arithmetic and they access memory safely.

32 Primitive Types Primitive types at Java are : int, short, long, boolean, char, float, double Example: int a; // (1) a=500; // Assigning to a primitive type (2)

33 Reference Types Other types are in java are reference types.
Reference types look like a pointer. You cannot access any other memory address using pointer arithmetic. In Java, objects are accessible with a reference type. Arrays are too reference types.

34 Reference Type Example(1)
String s=“Kemal”; // alias to String s=new String(“Kemal”);

35 Reference Type Example(2)
String s=“Kemal”; // (1) s=“Ali”; //(2)

36 Reference Type Example(3)
Scanner s=new Scanner(System.in);// object creation (1) Scanner t=new Scaner(System.in); // object creation(2)

37 Reference Type Examples(4)
Scanner s=new Scanner(System.in);// object creation (1) s=new Scaner(System.in); // object creation(2) // Same with example 2

38 Arrays Arrays are reference types at Java. Creation of an array (1): Primitive Type Array:


Download ppt "CSE 252 Principles of Programming Languages Lab. Section"

Similar presentations


Ads by Google