Java 1
Java 1
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.
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.
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. Bytecodes 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.
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.
Bytecode
Byte-Codes Byte code is the unique characteristic property of java programming language. It is something like a normal text file. Thats why it cannot be affected by virus? it is a intermediate human readable source & machine readable source. Byte codes are platform independent & thats 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.
Example 1
public class hello { public static void main(String ag[]) { System.out.println(Hello, World!!!); } } Output: Hello, World!!
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
Example -2
xyz.java class hello { public static void main (String ag []) { System.out.println(Hello); } } Output: Hello
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
Example Codes
Example 1
class 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); } }
Example-2 public 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:
Example 3 public class Nest2 { public static void main(String args[]) { int j=0; do for(int i=0;i++<2;) System.out.println(i); while(j++<2) } } Output:
public 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:
Example 4
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); else System.out.println("Not an Armstrong Number+t); } }
Output
Output
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?