Java 1 Ppt

28
JAVA Programmin g

Transcript of Java 1 Ppt

Page 1: Java 1 Ppt

JAVA Programming

Page 2: Java 1 Ppt

Contents

• How Java different from other languages?• Why Java?• What is a java virtual machine?-Heart of Java• ByteCode-the magical file.• Saving files in Java-Naming• Example codes without taking input from the

user -Not Preferred by programmers.• Taking input from the user-Mostly Preferred.

Page 3: Java 1 Ppt

How java different from other languages?Java survives & grows in this world because of its own unique identity.

• Java has neither preprocessor directive nor header files. Predefined methods & classes are available to java through the packages. Packages are collection of class files.

• All the methods, variables & all sorts of codes always reside within the class template.• Through wrapper class java provides an object oriented view of primitive data type like

integer, float, byte, char, boolean etc. This wrapper class package has enabled java as a complete object oriented programming language.

• Java does not support global variables. In C++ global variables does not belong to a particular class & can be modified by any function. This is the unsecure mean of data manipulation. Java has restricted this controversial issue by not supporting the global variable.

• Memory management is not provided to the programmer. Unused data is deleted from memory by the invocation of the demon thread named garbage collector. Memory leak problem is quite optimized in java.

• Non existence of memory pointers. Memory pointers are always threat to any automated system. These are used to break the security & can crash a system. Not having these memory pointers is an advantage to java

Page 4: Java 1 Ppt

More points• During any assignment operation java strictly follows type checking. Due to this feature

precision loss or data loss is minimal in java.

• Java strictly follows type safety. Programmer cannot arbitrarily cast on data-type to another one.

• Local variables inside a method can be declared when they are needed.

• Implementation of floating point variables is platform dependent. There fore java provides the keyword strictfp to maintain its platform independent property.

• Union provides memory over lapping feature which is discarded in java.

• No typdef to create alias of data-type.

• Array index out of bound is checked in java to prevent the unauthorized manipulation of memory.

Page 5: Java 1 Ppt

Why Java?• Platform Independence The Write-Once-Run-Anywhere (Java Virtual

Machine for different platforms usually required), concept is strictly followed.

• Introduction of wrapper class makes java a complete object oriented programming language.

• When a java source file is compiled, class file is generated which is collection of byte-codes. These byte-codes are latter interpreted to native codes by JVM. Byte-codes are portable.

• Byte codes are least affected by virus as compared to executable files. If it is affected, then JVM recognize then & never allow them to be executed.

• Built in support for exception handling.• Built in support for multithreading application development.

Page 6: Java 1 Ppt

More Features• Java provides communication with C/C++ source codes through Java

Native Interface.

• By the use of Compiler class java source code can be compiled to native machine codes.

• Package like java.lang.Security Manager provides permission to the object to have an access on variables, files & other resources.

• Built in support for networking.

Page 7: Java 1 Ppt

Java Virtual Machine

What is a virtual machine?• The philosophy behind virtual machine is to

abstract the system hardware of a single computer into several different virtual systems such that it makes the user to feel that different environment or platform is implemented on a single system.

Page 8: Java 1 Ppt

CASE 1:Without Virtual Machine

Page 9: Java 1 Ppt

CASE 2:With Virtual Machine

Page 10: Java 1 Ppt

More on Virtual Machine• Each virtual machine provides different environment or platform

to the user. • According to the platform provided, only a particular set of

processes can be executed in a particular virtual machine. i.e Processes those are executed in Virtual Machine-1 cannot be executed in Virtual Machine-2.

• Implicitly these virtual machine perform appropriate system call for the execution of different processes.

• When different processes are running, virtual machine makes the end user to feel that different processes are running not only in different platform, but also in different CPUs.

• This illusion is created by the implementation of different CPU scheduling algorithm by the underlying OS.

Page 11: Java 1 Ppt

The Java Virtual Machine

Page 12: Java 1 Ppt

Description of each component• These components are abstract in nature. It completely depend upon

the designer to develop these components.

• Class loader Sub System: when a java file is compiled class file is generated. When we invoke the class file for execution class loader subsystem is called by Java Virtual Machine to load the class file into JVM memory space.

• The task of Class loader sub system is to load the classes & interfaces in the memory. In Java Virtual Machine there are two types of loader available. A boot strap loader & other one is user defined class loader.

• The job of the class loader sub-system is not only to load the class file but also it check correctness of the loaded class, allocates required memory to static variables (static variables are explained in class fundamental).

Page 13: Java 1 Ppt

Steps in java virtual machine• The class loader sub-system performs its task in sequential way • 1st: Loading of class file. • 2nd: Checks the correctness of class file. If you have read the class file by

notepad or word-pad in a windows system, then that class file cannot be executed. For this behavior class. loader sub-system is responsible.

• 3rd: allocates memory to static variables. Static variables are otherwise known as class variables. They are the property of classes rather than the property of objects.

• 4th: transformation of symbolic reference into direct reference. • 5th: sets default value of all static variables. Method Area: it is a logical memory

component of JVM. This logical section of memory holds the information about classes & interfaces. Static variables are treated as class variable, because they take memory from method area. The size of the method area is not fixed. It shrinks & expands according to the size of the application.

Page 14: Java 1 Ppt

More on java virtual machine• Heap: In java when an object or array is created, memory is allocated to

them from heap. The Java virtual machine through the use of new operator allocates memory from the heap for a new object.

• Java does not provide any technique to the programmer to free the memory that has been allocated by new operator.

• Once memory is allocated from heap to an object or array, programmer cannot free the memory. The JVM has demon thread known as Garbage Collector whose task is to free those objects from heap whose reference is not alive in stack.

• Java stacks: Method codes are stored inside method area. When method starts execution, it needs memory because of the local variables & the arguments it has taken. For this purpose java stack is there.

• All the local variables & the arguments method have taken acquires memory from stack.

Page 15: Java 1 Ppt

More on java virtual machine• PC registers: It keeps track of the sequence of execution of the

program. PC registers or Program Counter registers holds the address of the instruction to be executed next.

• Native method stacks: When a java application invokes a native method, that application is not going to use Java stack only, rather it uses the native method stack also for the execution of native methods. Native methods are generally in C/C++ & executed in side native method stack.

• The libraries required for the execution of native methods are available to the Java Virtual Machine through Java Native Interface.

• Execution Engine: Execution engine is there to generate & execute the java byte code. It contains an interpreter & Just In Time Compiler. Java Run-time Environment.

Page 16: Java 1 Ppt

Bytecode• Byte-Codes Byte code is the unique characteristic property of java

programming language. • It is something like a normal text file. That’s why it cannot be

affected by virus?• it is a intermediate human readable source & machine readable

source. • Byte codes are platform independent & that’s why JVM is platform

dependent to make the java programming as platform independent. • Just like C source codes are portable, byte codes are portable. The

cost of platform independence behaviour of byte codes is paid by its slower execution speed when it is compared with execution of native codes even in just in time compiler.

Page 17: Java 1 Ppt

Example 1

• public class hello{ public static void main(String ag[]) { System.out.println(“Hello, World!!!”); }}Output: Hello, World!!

Page 18: Java 1 Ppt

• 1: Now in the above program we have declared the class containing the main method as public so we have to save it in a file which has the same name as that of class name. e.g. hello.java

• 2: after the file is saved we have to compile the hello.java file using the javac command. e.g. javac hello.java

• 3: After compilation hello.class file will be generated. Then to get the output we have to run the .class file using java command. e.g.: java hello note : if we are not declaring the class containing the main method as public , then we can save the file in any name.

• But when we will compile the .java file then the name of the class file generated will be same to the name of the class containing the main method. To get the output we have to run the program using the name of the .class

Page 19: Java 1 Ppt

Example -2

• xyz.javaclass hello{public static void main (String ag []){System.out.println(“Hello”);}}Output: Hello

Page 20: Java 1 Ppt

• To get the output: 1: javac xyz.java • 2: the class file will be generated in name of hello.class.

Now run it with the java command to get the output. • 3: java hello • Restrictions:- 1:-In java top level class is declared either

through public or no access modifier. 2:-When the class is declared through public access modifier class name must be same as the file name. 3:-When the class is declared through no access modifier class name and file name may same or may not

Page 21: Java 1 Ppt

Example CodesExample 1class Test2{public static void main(String args[]){ int s=0;for(int n =1;n<=10;n++){s=s+n;}System.out.println("Summation of the fist ten natural no. : "+s);}}

Page 22: Java 1 Ppt

Example-2public class Nest1{ public static void main(String args[]){int i=0,j=9;do{ i++;if(j--<i++){break;}} while (i<5);System.out.println(i+"\t"+j);}}Output:

Page 23: Java 1 Ppt

• Example 3public class Nest2{public static void main(String args[]){int j=0;dofor(int i=0;i++<2;)System.out.println(i);while(j++<2)}}Output:

Page 24: Java 1 Ppt

Example 4public class Loop{static String o="";public static void main(String args[]){ z:for(int x=2;x<7;x++){if(x==3)continue;if(x==5)break z;o=o+x;}System.out.println(o);}}

Output:

Page 25: Java 1 Ppt

Taking input from the user• Enter a number and check it is an Armstrong number or not?

import java.io.*;public class Armstrong{public static void main(String args[]) throws Exception{int n,t=0,s=0,r=0;BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));System.out.println("Enter A Number");n=Integer.parseInt(br.readLine());t=n;while(n>0){r=n%10;s=s+(r*r*r);n=n/10;}if(t==s)System.out.println("Armstrong Number is "+t);elseSystem.out.println("Not an Armstrong Number”+t);}}Output

Page 26: Java 1 Ppt

Output

Page 27: Java 1 Ppt

Assignment No 1

• Enter a number from keyboard and check if it is Prime number or not?

• Enter a number and count how many prime number are present within it?

• Enter a Character and find out the ASCII value of that number?

• Enter a Number and check it is a Binary Number or not?

• Enter a Number and add each digit of that number?• Enter a Number and Find Out The Factorial?

Page 28: Java 1 Ppt

Pyramids and loops

** ** * ** * * ** * * * *• Enter a number and prints Floyds Triangle