SOW # : ANSYS TEST CASES Configuration for PWC-4 BCSE103E: Computer Programming: JAVA Module 1: JAVA Basics Dr. K. Anandavel Anandavel.k@vit.ac.in Mob: 770 880 4923 Room: 313-A43, SJT 1 SOW # : ANSYS TEST CASES Contents History of JAVA Features of JAVA JAVA Programming cycle Byte code Applications Programming structure 2 SOW # : ANSYS TEST CASES History of JAVA James Gosling and Patrick Naughton initiated the Java language project in June 1991. The idea was to develop a language which was platform-independent and which could create embedded software for consumer electronic devices, It took 18 months to develop and had an initial name as Oak, Renamed to Java in 1995, due to copyright issues. Java originally developed by James Gosling at Sun Microsystems and released in 1995 3 SOW # : ANSYS TEST CASES History of JAVA Versions • 1995 version 1.0: – The Java development kit was released for free by the Sun – 8-Packages 212-Classes – Microsoft and other companies licensed Java • 1997 version 1.1: – 23 -Packages 504-Classes – Improvement include better event handling inner classes , improved JVM. – Microsoft developed its own 1.1 compatible Java Virtual Machine for Internet Explorer – Many browsers in use are still compatible only with 1.1 4 SOW # : ANSYS TEST CASES History of JAVA Versions 1999 version 1.2: -It is also called as the Java 2 platform -59 Packages -1520 Classes -Code & tools distributed as the SDK -A Java foundation class based on swings for improved graphics and user interfaces -Collection API included list sets and hash map 5 SOW # : ANSYS TEST CASES History of JAVA Versions . 2000 VERSION 1.3: - 76 Packages - 1842 Classes - Java Sound (API for Digital & MIDI Sound) •2002 VERSION 1.4: - 135 Packages - 2991 Classes - Improved XML support etc.., •2004 VERSION 5.0 (1.5): - 165 Packages - over 3000 Classes - Faster startup metadata formatted Output - Generic to operate on objects of various types 6 SOW # : ANSYS TEST CASES History of JAVA Versions 2006 Java SE 6: – Scripting language support 2011 Java SE 7: – JVM Support for dynamic language – String in switch – Allowing underscores in numeric literals 2014 Java SE 8: – for Each() method in Iterable interface. - default and static methods in Interfaces. - Functional Interfaces and Lambda Expressions. 7 SOW # : ANSYS TEST CASES History of JAVA Versions 2017 Java SE 9: -Stream API Improvements -Multi-Resolution Image API Java SE 10 (20th Mar 2018) Java SE 11 (September 2018) Java SE 12 (March 2019) Java SE 13 (September 2019) Java SE 14 (Mar 2020) Java SE 15 (September 2020) Java SE 16 (Mar 2021) Java SE 17 (September 2021) Java SE 18 (March 2022) Java SE 19 (Sep 2022) 8 SOW # : ANSYS TEST CASES Features of JAVA 9 SOW # : ANSYS TEST CASES Features of JAVA Simple Java is very easy to learn, and its syntax is simple, clean and easy to understand. According to Sun Microsystem, Java language is a simple programming language because: Java syntax is based on C++ (so easier for programmers to learn it after C++). Java has removed many complicated and rarely-used features, for example, explicit pointers, operator overloading, etc. There is no need to remove unreferenced objects because there is an Automatic Garbage Collection in Java. 10 SOW # : ANSYS TEST CASES Features of JAVA Object-oriented Java is an object-oriented programming language. Everything in Java is an object. Object-oriented means we organize our software as a combination of different types of objects that incorporate both data and behavior. Object-oriented programming (OOPs) is a methodology that simplifies software development and maintenance by providing some rules. Basic concepts of OOPs are: Object Class Inheritance Polymorphism Abstraction Encapsulation 11 SOW # : ANSYS TEST CASES Features of JAVA Platform-independent Java is platform independent because it is different from other languages like C, C++, etc. which are compiled into platform specific machines while Java is a write once, run anywhere language. A platform is the hardware or software environment in which a program runs. The Java platform differs from most other platforms in the sense that it is a software-based platform that runs on top of other hardware-based platforms. It has two components: Runtime Environment API(Application Programming Interface) 12 SOW # : ANSYS TEST CASES Features of JAVA Platform-independent Java code can be executed on multiple platforms, for example, Windows, Linux, Sun Solaris, Mac/OS, etc. Java code is compiled by the compiler and converted into bytecode. This bytecode is a platformindependent code because it can be run on multiple platforms, i.e., Write Once and Run Anywhere (WORA). 13 SOW # : ANSYS TEST CASES Features of JAVA Secured Java is best known for its security. With Java, we can develop virusfree systems. Java is secured because: • No explicit pointer • Java Programs run inside a virtual machine sandbox 14 SOW # : ANSYS TEST CASES Features of JAVA Secured • • • • Classloader: Classloader in Java is a part of the Java Runtime Environment (JRE) which is used to load Java classes into the Java Virtual Machine dynamically. It adds security by separating the package for the classes of the local file system from those that are imported from network sources. Bytecode Verifier: It checks the code fragments for illegal code that can violate access rights to objects. Security Manager: It determines what resources a class can access such as reading and writing to the local disk. Java language provides these securities by default. Some security can also be provided by an application developer explicitly through SSL, JAAS, Cryptography, etc. 15 SOW # : ANSYS TEST CASES Features of JAVA Robust Java is robust because: • It uses strong memory management. • There is a lack of pointers that avoids security problems. • Java provides automatic garbage collection which runs on the Java Virtual Machine to get rid of objects which are not being used by a Java application anymore. • There are exception handling and the type checking mechanism in Java. All these points make Java robust. 16 SOW # : ANSYS TEST CASES Features of JAVA Architecture-neutral Java is architecture neutral because there are no implementation dependent features, for example, the size of primitive types is fixed. In C programming, int data type occupies 2 bytes of memory for 32bit architecture and 4 bytes of memory for 64-bit architecture. However, it occupies 4 bytes of memory for both 32 and 64-bit architectures in Java. Portable Java is portable because it facilitates you to carry the Java bytecode to any platform. It doesn't require any implementation. High-performance Java is faster than other traditional interpreted programming languages because Java bytecode is "close" to native code. It is still a little bit slower than a compiled language (e.g., C++). Java is an interpreted language that is why it is slower than compiled languages, e.g., C, C++, etc. 17 SOW # : ANSYS TEST CASES Features of JAVA Distributed Java is distributed because it facilitates users to create distributed applications in Java. RMI and EJB are used for creating distributed applications. This feature of Java makes us able to access files by calling the methods from any machine on the internet. Multi-threaded A thread is like a separate program, executing concurrently. We can write Java programs that deal with many tasks at once by defining multiple threads. The main advantage of multi-threading is that it doesn't occupy memory for each thread. It shares a common memory area. Threads are important for multi-media, Web applications, etc. Dynamic Java is a dynamic language. It supports the dynamic loading of classes. It means classes are loaded on demand. It also supports functions from its native languages, i.e., C and C++. 18 SOW # : ANSYS TEST CASES JAVA Program life cycle Java requires the source code of your program to be compiled first. It gets converted to either machine-specific code or a byte code that is understood by some run-time engine or a java virtual machine. Not only will the program be checked for syntax errors by a Java compiler, but some other libraries of Java code can be added (linked) to your program after the compilation is complete (deployment stage). 19 SOW # : ANSYS TEST CASES JAVA Program life cycle Step1 : Create a source document using any editor and save file as .java (e.g. abc.java) Step2 : Compile the abc.java file using “javac” command or eclipse will compile it automatically. Step3 : Byte Code (abc.class) will be generated on disk. Step4 : This byte code can run on any platform or device having JVM (java.exe convert byte code in machine language) Let’s get familiar with various terminologies used by java programmers 20 SOW # : ANSYS TEST CASES JAVA Program life cycle 21 SOW # : ANSYS TEST CASES JDK, JRE, JVM Terminologies used in Java Programming JDK (Java Development Kit): JDK contains JRE along with various development tools like Java libraries, Java source compilers, Java debuggers, bundling and deployment tools JRE (Java Runtime Environment): It is part of JDK but can be used independently to run any byte code (compiled java program). It can be called as JVM implementation. JVM (Java Virtual Machine): ‘JVM’ is software that can be ported to various hardware platforms. JVM will become an instance of JRE at runtime of java program. Byte codes are the machine language for the JVM. Like a real computing machine, JVM has an instruction set which manipulates various memory areas at run time. Thus for different hardware platforms, one has corresponding the implementation of JVM available as vendor supplied JREs. Java API (Application Programming Interface) : Set of classes’ written using Java programming language which runs on JVM. These classes will help programmers by providing standard methods like reading from the console, writing to the console, saving objects in data structure etc. 22 SOW # : ANSYS TEST CASES JDK, JRE, JVM JDK 23 SOW # : ANSYS TEST CASES JDK, JRE, JVM JDK • • • • • JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a software development environment which is used to develop Java applications and applets. It physically exists, and contains JRE + development tools. JDK is an implementation of any one of the below given Java Platforms released by Oracle Corporation: • Standard Edition Java Platform • Enterprise Edition Java Platform • Micro Edition Java Platform The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as an interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc), etc. to complete the development of a Java Application. 24 SOW # : ANSYS TEST CASES JDK, JRE, JVM JRE • JRE is an acronym for Java Runtime Environment. It is also written as Java RTE. • The Java Runtime Environment is a set of software tools which are used for developing Java applications. • It is used to provide the runtime environment. • It is the implementation of JVM. • It physically exists. It contains a set of libraries + other files that JVM uses at runtime. • The implementation of JVM is also actively released by other companies besides Sun Micro Systems 25 SOW # : ANSYS TEST CASES JDK, JRE, JVM JVM • • • • • • • • JVM (Java Virtual Machine) is an abstract machine. It is called a virtual machine because it doesn't physically exist. It is a specification that provides a runtime environment in which Java bytecode can be executed. It can also run those programs which are written in other languages and compiled to Java bytecode. JVMs are available for many hardware and software platforms. JVM, JRE, and JDK are platform dependent because the configuration of each OS is different from each other. However, Java is platform independent. There are three notions of the JVM: specification, implementation, and instance The JVM performs the following main tasks: • Loads code • Verifies code • Executes code • Provides runtime environment 26 SOW # : ANSYS TEST CASES JDK, JRE, JVM JVM • JVM provides definitions for the: • Memory area • Class file format • Register set • Garbage-collected heap • Fatal error reporting etc. 27 SOW # : ANSYS TEST CASES JDK, JRE, JVM JVM Architecture 28 SOW # : ANSYS TEST CASES JDK, JRE, JVM JVM Architecture Classloader is a subsystem of JVM which is used to load class files. Whenever we run the java program, it is loaded first by the classloader. There are three built-in classloaders in Java. 1.Bootstrap ClassLoader: This is the first classloader which is the super class of Extension classloader. It loads the rt.jar file which contains all class files of Java Standard Edition like java.lang package classes, java.net package classes, java.util package classes, java.io package classes, java.sql package classes etc. 2.Extension ClassLoader: This is the child classloader of Bootstrap and parent classloader of System classloader. It loads the jar files located inside $JAVA_HOME/jre/lib/ext directory. 3.System/Application ClassLoader: This is the child classloader of Extension classloader. It loads the classfiles from classpath. By default, classpath is set to current directory. You can change the classpath using "cp" or "-classpath" switch. It is also known as Application classloader. 29 SOW # : ANSYS TEST CASES JDK, JRE, JVM JVM Architecture Class(Method) Area Class(Method) Area stores per-class structures such as the runtime constant pool, field and method data, the code for methods. Heap It is the runtime data area in which objects are allocated. Stack Java Stack stores frames. It holds local variables and partial results, and plays a part in method invocation and return. Program Counter Register PC (program counter) register contains the address of the Java virtual machine instruction currently being executed. Native Method Stack It contains all the native methods used in the application. 30 SOW # : ANSYS TEST CASES JDK, JRE, JVM JVM Architecture Execution Engine It contains: • A virtual processor • Interpreter: Read bytecode stream then execute the instructions. • Just-In-Time(JIT) compiler: It is used to improve the performance. JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Here, the term "compiler" refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU. Java Native Interface Java Native Interface (JNI) is a framework which provides an interface to communicate with another application written in another language like C, C++, Assembly etc. Java uses JNI framework to send output to the Console or interact with OS libraries. 31 SOW # : ANSYS TEST CASES JAVA-BYTE Code • In java when you compile a program, the java compiler(javac) converts/rewrite your program in another form/language which we call as bytecode. • The .class file that is generated after compilation is nothing but just the bytecode instructions of your program. • The word bytecode and .class are used interchangeably, so if someone says bytecode, it simply means the .class file of program. 32 SOW # : ANSYS TEST CASES JAVA-BYTE Code Byte code • Byte Code can be defined as an intermediate code generated by the compiler after the compilation of source code(JAVA Program). This intermediate code makes Java a platform-independent language • Compiler converts the source code or the Java program into the Byte Code(or machine code), and secondly, the Interpreter executes the byte code on the system. • The Interpreter can also be called JVM(Java Virtual Machine). The byte code is the common piece between the compiler(which creates it) and the Interpreter (which runs it). 33 SOW # : ANSYS TEST CASES JAVA-Applications • • • • • • • • • • Mobile Applications Desktop GUI Applications Web-based Applications Enterprise Applications Scientific Applications Gaming Applications Big Data technologies Business Applications Distributed Applications Cloud-based Applications 34 SOW # : ANSYS TEST CASES JAVA-Programming Structure 35 SOW # : ANSYS TEST CASES JAVA-Programming Structure • Need to follow a standard structure for writing the program which is recommended by the language experts • A java program may contain many classes of which only one class will have a main method • Class will contain data members and methods that operate on the data members of the class Example -01 36 SOW # : ANSYS TEST CASES JAVA-Programming Structure Package is a collection of classes, interfaces and sub packages. In a java program if we are using any pre-defined classes and interfaces then it is the responsibility of the java programmer to import that particular package containing such specific classes and interface. In java by default java.lang.* package is imported by every program. Class is a keyword used for developing user defined data types. Every java program must starts with a prototype of class. The class has been declared public, means all classes can access the class from all packages. Generally, however, we will declare classes in java without specifying a modifier. Class name is the name given to that class. Every class name is treated as one kind of user defined data type. Data Members represents either instance members or static members 37 SOW # : ANSYS TEST CASES JAVA-Programming Structure Constructor function is called when an object of the class is created. It is a block of code that initializes the newly created object. The constructor simply has the same name as the name of the class name. A constructor does not have a return type. Constructor and method are different because the constructor is used to initialize the object of a class while the method is used to perform a task by implementing java code. Constructors cannot be declared as abstract, final, static and synchronized while methods can be declared. Example -2 When we create the object of the bird class as shown above: bird b = new bird(); The new keyword here creates the object of class bird and invokes the constructor to initialize this newly created object. 38 SOW # : ANSYS TEST CASES JAVA-Programming Structure User-defined methods represent either instance (or) static and they will be selected depends on the class name and these methods are used for performing the operations either once (or) repeatedly. All the user-defined methods of a class contain logic for a specific problem 39 SOW # : ANSYS TEST CASES JAVA-Programming Structure User-defined methods 40 SOW # : ANSYS TEST CASES JAVA-Programming Structure Main methods All java program starts its execution with main() method so main() method is known as the backbone of the program. The Java Virtual Machine starts running any java program by executing main() method first. Java’s main() method is not returning any value so its return type must be void. Also main() method executes only once throughout the life of the Java program and before the object creation so its nature must be static. The main() method is accessed in all the java programs, its access specifier must be public (universal). Each and every main() method of java must take an array of objects of String class as an argument. The block of statements are set of executable statements written for calling user-defined methods of the class. If we have multiple java files then the naming convention of class file in java is that, whichever class is containing main() method, that class name will be given as the file name with an extension (dot) .java. 41 SOW # : ANSYS TEST CASES JAVA-Programming Structure Commenting – The compiler ignores comments. 42 SOW # : ANSYS TEST CASES JAVA-Programming Structure JAVA – Core Packages 43 SOW # : ANSYS TEST CASES JAVA-Programming Structure Importing Classes 44 SOW # : ANSYS TEST CASES JAVA-Programming Structure Importing Classes 45 SOW # : ANSYS TEST CASES JAVA-Programming Structure Data members Two types of data members. 1. Instance or non-static data members 2. Static or class data members 46 SOW # : ANSYS TEST CASES JAVA-Programming Structure Methods Two types of methods 1. Instance or non –static methods 2. Static or class method 47 SOW # : ANSYS TEST CASES JAVA-Programming Structure Example- 3 “package sct”: It is package declaration statement. The package statement defines a namespace in which classes are stored. The package is used to organize the classes based on functionality. If you omit the package statement, the class names are put into the default package, which has no name. Package statement cannot appear anywhere in the program. It must be the first line of your program or you can omit it. 48 SOW # : ANSYS TEST CASES JAVA-Programming Structure 2.“public class HelloWorld”: This line has various aspects of java programming. a. public: This is access modifier keyword which tells compiler access to class. Various values of access modifiers can be public, protected,private or default (no value). b. class: This keyword used to declare a class. Name of class (HelloWorld) followed by this keyword. 3. Comments section: We can write comments in java in two ways. a. Line comments: It starts with two forward slashes (//) and continues to the end of the current line. Line comments do not require an ending symbol. b. Block comments start with a forward slash and an asterisk (/*) and end with an asterisk and a forward slash (*/).Block comments can also extend across as many lines as needed 49 SOW # : ANSYS TEST CASES JAVA-Programming Structure 4. “public static void main (String [ ] args)”: Its method (Function) named main with string array as an argument. a. public: Access Modifier b. static: static is a reserved keyword which means that a method is accessible and usable even though no objects of the class exist. c. void: This keyword declares nothing would be returned from the method. The method can return any primitive or object. d. Method content inside curly braces. { } asdfla;sd 5. System.out.println("Hello World from Java") : a. System: It is the name of Java utility class. b. out:It is an object which belongs to System class. c. println: It is utility method name which is used to send any String to the console. d. “Hello World from Java”: It is String literal set as argument to println method 50 SOW # : ANSYS TEST CASES JAVA-Programming Structure Prog Exercise 01 51 {} SOW # : ANSYS TEST CASES JAVA-Programming Structure Prog Exercise 02 classMember.java // Java code to show structures and // members of Java Program public class classMember { // Static member static int staticNum = 0; // Instance member int instanceNum; /* below constructor increments the static number and initialize instance number */ public classMember(int i) //Constructor method { instanceNum = i; staticNum++; } /* The show method display the value in the staticNum and instanceNum */ public void show() //instance method System.out.println("Value of Static Number is:" + staticNum + "\nValue of Instance number is:"+ instanceNum); 52 SOW # : ANSYS TEST CASES JAVA-Programming Structure // To find cube public static int cube() //Static method { return staticNum * staticNum * staticNum; } // Driver code public static void main(String args[]) { classMember gvp1 = new classMember(2); System.out.println("Value after gvp1 object creation: "); gvp1.show(); classMember gvp2 = new classMember(4); System.out.println("Value after gvp2 object creation: "); gvp2.show(); // static method can be accessed by class name int cub=classMember.cube(); System.out.println("Cube of the Static number is: "+ cub); } } 53 SOW # : ANSYS TEST CASES References • • • • • https://www.javatpoint.com/ https://www.geeksforgeeks.org/ https://www.w3schools.com/ https://www.educba.com/ Y. Daniel Liang, Intro to Java Programming, Pearson Education India 54 SOW # : ANSYS TEST CASES THANK YOU !! 55