Page 118 - Cs_withBlue_J_C11_Flipbook
P. 118

5.3.1 Java Virtual Machine (JVM)
              A Java Virtual Machine (JVM) converts Java byte code to machine-dependent code so that it could be executed. It
              enables a computer to run Java programs as well as programs written in other programming languages that are also
              compiled to Java bytecode.

              Features of JVM
              Some features of JVM are as follows:
              •  It allows Java code to execute on any computers, mobiles, etc. Also, it can run on any operating system. It follows
                 the principle of “Write Once, Run Anywhere”.
              •  It manages and optimises the program so that memory usage can be minimised.

              5.3.2 Interpreter
              Java is a language that is both compiled and interpreted. The interpreter converts high-level program statements
              into machine-level code. It reads the input source program and then translates it into machine code instruction by
              instruction.

              5.3.3 Java Source code
              The Java programs written by programmers are called Java source code. They are saved with the extension .java.

              5.3.4 Bytecode
              When Java source code is compiled by the Java compiler (JAVAC), the resultant code is called Java bytecode, which is
              further converted by the Java Virtual Machine to machine-dependent code.

              This bytecode has a resemblance to assembly language. The only difference between bytecode and assembly language
              is that the antecedent is used by Java Virtual Machine which is software and the descendent is used by CPU, i.e.,
              directly by hardware.


                  5.4 BASIC INPUT/OUTPUT USING SCANNER CLASS FROM JDK
              We use the Scanner class for taking values from the users. It is a built-in class to perform basic input and output on all
              primitive data types. To use the facilities of the class, we have to include the “java.util” package. To use a Scanner class,
              follow the steps given below:

              Step 1:  Import the package in the beginning of the program. There are two ways to import as:
                      1.  import java.util.*;
                      2.  import java.util.Scanner;
              Step 2:   Then we have to create the object of the Scanner class. The syntax to create the object is as follows:

                      Scanner sc = new Scanner(System.in);
                      where sc is the name of the object of the Scanner class.
              Step 3:   Use Scanner class methods as per your data types to read input from the user.

                      Say, to read the whole number from the user, we have to use sc.nextInt();

                      Similarly, to read a character from the user, we shall use sc.next().charAt(0);
              We have different syntaxes to input values of different data types. Some of them are as follows:

              1.  To accept a word:
                 String w = sc.next();
              2.  To accept a sentence:
                 String s = sc.nextLine();





                116116  Touchpad Computer Science-XI
   113   114   115   116   117   118   119   120   121   122   123