week 1 introduction to java by: r.a. kemal Ça ğ rı serdaro ğ lu cse 252 principles of...

Click here to load reader

Upload: david-williams

Post on 23-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

  • Slide 1
  • WEEK 1 INTRODUCTION TO JAVA by: R.A. Kemal a r Serdaro lu CSE 252 Principles of Programming Languages Lab. Section
  • Slide 2
  • WEEK 1 What is Java Programming Language? Java Programming Basics: Java vs C. Using Classes at Java API.
  • Slide 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.
  • Slide 4
  • 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.
  • Slide 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.
  • Slide 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.
  • Slide 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
  • Slide 8
  • Slide 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.
  • Slide 10
  • Java Programming Basics (1) Java is an OOP (Object Oriented Programming) Language.OOP languages uses objects consisting of data fields(variables) and methods(funcitons) together with their interactions. (2) 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. (3) OOP has new programming techniques such as encapsulation, inheritence and polymorphism.
  • Slide 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 !!!
  • Slide 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.
  • Slide 13
  • Java vs C (1) Programming Language Structure CJava Type of languageFunctional, not OOPOOP 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.
  • Slide 14
  • Java vs C (1) Programming Language Structure CJava File names No constrai nt, ends with.c 1)Ends with.java 2)File name must be the name with class name. File Names Example: ExampleClass.java: public class ExampleClass{ public static void main(String[] args){ }
  • Slide 15
  • Java vs C (1) Programming Language Structures CJava Variable declarationAt the beginning of the block Before using it Variable declaration int 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; }
  • Slide 16
  • Java vs C (1) Programming Language Structure CJava Accessing a library #include import java.io Memory address pointerReference
  • Slide 17
  • Java vs C (2) HelloWorld Application & User Interaction CJava Hello,Wor ld #include int main(void) { printf("Hell o\n"); return 0; } public class HelloWorld { public static void main(String[] args) { System.out.println("Hello"); } }
  • Slide 18
  • Java vs C (2) Hello World Application and User Interaction CJava printf (Using not formatted) (1) int a; a=3; printf(A is %d,a); int a; a=3; System.out.print(A is + a);
  • Slide 19
  • Java vs C (2) HelloWorld Application and User Interaction CJava printf(Usin g 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);
  • Slide 20
  • Java vs C (2) Hello World Application and User Interaction CJava scanf #include 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); }
  • Slide 21 b?a:b; }">
  • Java vs C (2) HelloWorld Application and User Interaction CJava C - function s int 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; }
  • Slide 22
  • Java vs C CJava integer types short 16 bit. int usually 32 bit 2's complement; long usually 32 bit 2's complement short 16 bit. 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 ------------------------ char c; c=a;
  • Slide 23
  • Java vs C Comments Comments are same !!!
  • Slide 24
  • Java vs C If-Switch Statements: CJava 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