Page 118 - ComputerScience_Class_11
P. 118

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 inputs 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 at 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 have to 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();
              3.  To accept an Integer:
                a.  Integer of size 1 byte
                    byte b = sc.nextByte();
                b.  Integer of size 2 bytes
                    short s = sc.nextShort();
                c.  Integer of size 4 bytes
                    int n = sc.nextInt();
                d.  Integer of size 8 bytes
                    long l = sc.nextLong();
              4.  To accept a real number:
                a.  Real number of size 4 bytes
                    float f = sc.nextFloat();
                b.  Real number of size 8 bytes
                    double d = sc.nextDouble();



                  116  Touchpad Computer Science (Ver. 3.0)-XI
   113   114   115   116   117   118   119   120   121   122   123