200 likes | 304 Views
Knowledge Byte In this section, you will learn about: Evolution and Need for Java Garbage Collection in Java Virtual Machine (JVM) Setting the CLASSPATH Significance of the Java class file. Evolution and Need for Java
E N D
Knowledge Byte • In this section, you will learn about: • Evolution and Need for Java • Garbage Collection in Java Virtual Machine (JVM) • Setting the CLASSPATH • Significance of the Java class file Collaborate
Evolution and Need for Java • You can use Java to develop network-oriented programs because networking features are inbuilt in Java. • In 1991, a team of software developers at Sun Microsystems, USA, was designing a language for consumer electronic devices. • The development team headed by James Gosling wanted to design a portable language using which programs should be developed such that they can run on computers with different platform. • The team considered C++ as the model language for designing Java language. The team deprecated various ambiguous features from this new language. • Initially, this developed language was called Oak, but was later renamed to Java. Collaborate
Year Development 1990 Sun Microsystems developed software to manipulate electronic devices. 1991 A new language named Oak was introduced using the most popular object-oriented language C++ • Evolution and Need for Java (Contd.) • The following table lists the various developments that took place in the evolution of Java: Collaborate
Year Development 1993 The WWW appeared on the Internet that transformed the text-based Internet into graphical Internet. 1994 Sun Microsystems team developed a Web browser called HotJava to locate and run applet programs on the Internet. 1995 Oak was renamed as Java. 1996 Java was established as an Object-oriented programming language. • Evolution and Need for Java (Contd.) Collaborate
Garbage Collection in JVM • Garbage collection is the process that is used to free the memory of the objects that are no longer in use. • When a program stops referencing an object, it is not required any more and can be deleted. • The space that is used by the object is released for use by another objects. • The garbage collection feature implies that the new objects are created and all the unreferenced objects are deallocated from the memory. • The different approaches used for detecting garbage objects are: • Reference-Counting Collectors: Store the references of the objects used within a program • Tracing Collectors: A set of roots is defined from the location where the objects are traced. • Compacting Collectors: Reduce the fragmentation of memory by moving all the free space to one side during garbage collection. Collaborate
Setting the CLASSPATH • The CLASSPATH environment variable instructs the JVM class loader to find the classes that are directly or indirectly invoked, including the system classes. • The -classpath option is used when the SDK tools are called, such as java, javac and javadoc. • The following syntax shows how to set the classpath with an SDK tool: • C:> sdktool -classpath <classpath1>;<classpath2>... • The following syntax shows how to set the classpath using the classpath environment variable: • C:> set CLASSPATH=<classpath1>;<classpath2>... Collaborate
Significance of the Java Class File • The Java class file contains the Java Bytecode. • The class files are platform independent therefore you can run a Java program by loading the class file on any system with the Java Runtime Environment (JRE). • The following command shows how to view the contents of a class file: • javap -c <class_filename> • The javap command prints the instructions that comprise the Java Bytecode, for each of the methods in a class. Collaborate
From the Expert’s Desk • In this section, you will learn: • Best practices on: • Declaring class variables as private and methods as public • Declaring arrays • Declaring class variables using the Hungarian notation • Tips and Tricks on: • Displaying text in a Java program • Setting CLASSPATH • FAQs on Java Fundamentals Collaborate
Best Practices • Declaring class variables as private and methods • as public • You should declare all the class variables as private because data should always remain hidden from the objects of other classes. • Methods should be declared as public because the methods provide an interface to the objects of the other classes. Collaborate
Best Practices • Declaring arrays • Arrays can range from a higher minimum number to a higher maximum number, such as 45 to 90 but it is more efficient to begin arrays with an element 0 because it takes up less memory in the computer to store arrays that begin with 0. • If you write a Java program having the for loop that starts from array element 45 to element 90, the compiler will still allocate the memory to the array starting from elements 0 to array element 44. Collaborate
Best Practices • Declaring Class Variables • You can follow Hungarian notation to declare variables in Java. • The conventions followed in the Hungarian notation are: • The first letter of the variable should indicate the data type used. You can use the letters, i, f, and b to indicate an integer, float, or a boolean variable. For example, iAge, fPrice, and bResult. • The variable name should be meaningful. For example, iAge is an integer variable to store the age of a student. • In case the variable name consists of multiple words, the first letter of each word should be capitalized, for example iTotalMarks and fPriceOfCommodity. Collaborate
Tips and Tricks • Displaying text in a Java program • You can also use the drawString() method to display text in addition to the System.out.println() method in Java. The drawString() method requires three arguments. • The first argument represents the string to be displayed on an applet. The second and third arguments represent the x and y coordinates of the string to be displayed. Collaborate
Tips and Tricks • Setting CLASSPATH • CLASSPATH is an environment variable that enables the Java compiler javac.exe to locate class files to be imported. • You can use the SET CLASSPATH= to set the CLASSPATH variable. The following syntax shows how to set CLASSPATH in Java: • SET CLASSPATH= C:\j2sdk1.4.1_02\bin . Collaborate
FAQs • Why is Java considered ideal for network communication? • Java is considered ideal for network communication because it is a platform independent language. Java enables an application to be executed on any network. The built-in classes of Java support TCP/IP and UDP protocols used for network communication. Java supports the Client-Server model for network communication. Collaborate
FAQs (Contd.) • What will happen if the data type of a variable and the value assigned to the variable are different? • If the data type of a variable and the value assigned to the variable are different then compilation error occurs. The following code shows assigning a character value to an integer variable: • class datatype{ • public static void main(String a[]) • { • int x= 'b'; • System.out.print(x); • } • } • In the preceding code, the ASCII value of the character b is displayed as 98. Collaborate
FAQs (Contd.) • Similarly, if you assign a float or a double value to a character variable, an error • message is displayed. • The following code shows assigning a double value to a character variable: • class datatype{ • public static void main(String a[]) • { • char x= 5.5; • System.out.print(x); • } • } • Java is a strongly typed language and it allows the values of the specific data • types, which are compatible with the type of variable. Collaborate
FAQs (Contd.) • Do all arguments sent to a Java application have to be strings? • Yes, all the arguments sent to a Java application have to be string. If you use some other data type, such as int, you need to convert the value to string. • Can two variables have the same letters but different capitalization, as in variableName and VariableName? • No, Java is a case-sensitive language. It differentiates the two variables, • variableName and VariableName. Collaborate
Challenge • Java is a platform ______ language . • Match the following: • a. Variables i. Reserved words • b. Literals ii. Basic storage unit • c. Keywords iii. Sequence of characters • 3. Make words from the jumbled letters in the box given below and match them with their description. • a. Class members that can be accessed by the subclasses of the class in which they are declared. • b. Class members that are accessible to all the classes of a package by default. • c. Class members that can be accessed only by the objects of the same class. • d. Class members that can be accessed anywhere in the same class. Collaborate
Challenge (Contd.) • e. This method of the String class is used to find the length of a string. • f. Collection of classes that can be reused. • g. Are the reserved words for a language. • h. A method with the same name as the class name IULBPC DFINRE ORETCDPET ELHTGN VPTIARE CKESPAAGS KRDYOESW CNTCSUOORRT Collaborate
Solutions to Challenge • independent • a-ii, b-iii, c-i • a. Protected, b. Friend, c. Private, d. Public, e. length, f. packages, g. keywords, h. constructor Collaborate