JAVA BASICS: We are going to learn a lot about Java meaningfully this time. JAVA INSTALLATION: After installing java, we need to set JAVA_HOME to point to the JDK if we would like to utilize all tools provided in the JDK. set JAVA_HOME = C:\Program Files\Java\jdk-24 JAVA COMPILER AND EXECUTION: Compilation path: Java compiler is used to compile the java files to byte code i..e .class files after checking the syntax and semantics. By default, if we compile a java source file via command line, it creates the class file in the same directory where the source file resides. If we need a separate directory for the class files, we could specify it using -d option. Navigate to C:\Users\Sneha\eclipse-workspace\basic_practice\src\com\basic_practice Then, type javac -d C:\Users\Sneha\eclipse-workspace\basic_practice\bin OmGanesha.java Setting CLASSPATH: JRE actually executes the .class files. But for it to understand where the class files reside, we must specify the CLASSPATH i.e. the path for the class files. Default location to search for the class files is the current directory. CLASSPATH could be specified in one of two ways: 1. <jdk tool> -classpath classpath1;classpath2 Where jdk tool is A command-line tool, such as java, javac, javadoc, or apt Example: java -classpath C:\Users\Sneha\eclipse-workspace\basic_practice\bin com.basic_practice.OmGanesha 2. Setting the environment variable. Or set -classpath classpath1;classpath2 (via command line) CLASSPATH = C:\Users\Sneha\eclipse-workspace\basic_practice\bin (via Windows GUI) Option 1 has higher preference over option 2. Note that option 2 is also temporary for that cmd line session. It does not permanently create any entry in Windows environment variables found in system settings. Helpful articles for classpath: https://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean JVM, JDK, JRE: JVM- It stands for Java virtual machine. After the java compiler compiles and converts a java file into corresponding class file(byte code), the JVM is responsible for loading, converting and executing this byte code. JVM has: Class loader to load, link and initialize the class file into the RAM. Process is called dynamic class cloading. Byte code verifier which checks for access restriction violations in the code Execution engine which converts the byte code into executable machine code. It contains an interpreter, compiler, and garbage collection area. Runtime Data Areas contain method areas, PC registers, stack areas, and threads. JRE: It contains all the tools responsible for execution of a java program. It contains the JVM. In addition to JVM, it contains other tools as well. Deployment solutions: Included as part of the JRE installation are deployment technologies like Java Web Start and Java plug-in that simplify the activation of applications and provide advanced support for future Java updates. Development toolkits: JRE also contains development tools that are designed to help developers improve their user interface. Some of these toolkits include the following: o Java 2D: An application programming interface (API) used for drawing twodimensional graphics in Java language. Developers can create rich user interfaces, special effects, games, and animations. o Abstract Window Toolkit (AWT): A graphical user interface (GUI) that is used to create objects, buttons, scroll bars, and windows. o Swing: Another lightweight GUI that uses a rich set of widgets to offer flexible, userfriendly customizations. Integration libraries: Java Runtime Environment provides several integration libraries and class libraries to assist developers in creating seamless data connections between their applications and services. Some of these libraries include the following: o Java IDL: Uses Common Object Request Brokerage Architecture (CORBA) to support distributed objects written in the Java programming language. o Java Database Connectivity (JDBC) API: Provides tools for developers to write applications with access to remote relationship databases, flat files, and spreadsheets. o Java Naming and Directory Interface (JNDI): A programming interface and directory service that lets clients create portable applications that can fetch information from databases by using naming conventions. Language and utility libraries: Included with JRE are Java.lang. and Java.util. packages that are fundamental for the design of Java applications, package versioning, management, and monitoring. Some of these packages include the following o Collections framework: A unified architecture that is made up of a collection of interfaces that are designed to improve the storage and process of application data. o Concurrency utilities: A powerful framework package with high-performance threading utilities. o Preferences API: A lightweight, cross-platform persistent API that enables multiple users on the same machine to define their own group of application preferences. o Logging: Produces log reports—such as security failures, configuration errors, and performance issues—for further analysis. o Java Archive (JAR): A platform-independent file format that enables multiple files to be bundled in JAR file format, significantly improving download speed and reducing file size. JDK: JDK includes all the Java tools, executables, and binaries that are needed to write and run Java programs. This includes JRE, a compiler, a debugger, an archiver, and other tools that are used in Java development. It is the superset of JRE. JNI: JNI is a programming framework that enables Java code running in JVM to communicate with (that is, to call and be called by) applications that are associated with a piece of hardware and specific operating system platform. These applications are called native applications and can often be written in other languages. Native methods are used to move native code written in other languages into a Java application. IMMUTABILITY: All wrapper classes – Integer, Char, Boolean, Double, Byte, Long, Short, Float are immutable. String is also immutable. Instances/objects of these classes once created cannot change their state. That means the data these objects hold cannot be modified. Why is a string immutable? 1. Since strings objects are re-used across multiple references, if they are made mutable and the value changes in the context of one reference, all the remaining references would now point to a changed string value which would be incorrect. 2. Performance - JVM stores all string objects in string pools in heap memory. Since heap memory is saved by re-using the same string object for multiple references, there is better performance. This process is called interning. 3. Thread-safety and security- Even if multiple threads try to modify the same string object, they would be unsuccessful. Data like usernames, passwords are mostly stored in strings. Immutability allows more security for such sensitive data as the string values cannot be modified in any manner. Even if a thread or some other caller changes a string value, a new String object is created to store the updated string value. The original string remains intact. 4. Caching- Strings are used for hashcode calculations of keys of hashmaps, hashsets, hashtables etc. hashcode() method in String class could be overridden so that hashcode calculation logic could be implemented and the generated hashcode during the first call to the method could be used for caching. On the other hand, mutable Strings would produce two different hashcodes at the time of insertion and retrieval if contents of String was modified after the operation, potentially losing the value object in the Map. API: We shall learn what an API is. Useful links: https://www.geeksforgeeks.org/what-is-an-api/ https://www.ibm.com/think/topics/api https://www.youtube.com/watch?v=cpRcK4GS068&list=PLcgRuP1JhcBP8Kh0MC53GH_pxqfOhTVLa https://konghq.com/blog/learning-center/different-api-types-and-use-cases https://www.wallarm.com/what/what-is-api https://www.wallarm.com/what/what-is-javaapi#:~:text=API%20(Application%20Programming%20Interface)%20in,exchange%20of%20data%20an d%20communication. https://www.youtube.com/watch?v=WXsD0ZgxjRw&t=666s It stands for application programming interface. It is an interface that is designed to perform a task underneath while abstracting the details of implementation from the caller that calls the API. It has a contract – a set of requirements that must be fulfilled by the caller to be able to use it. Suppose, a string needs to be converted to lower case. Based on the programming language, APIs are provided to do the task for the caller whilst hiding how they perform the task. Suppose a user wants to play a song on her phone. She clicks the play button on the GUI which in turn leads to an API call to fetch the songs from the web/phone and play them. She does not have to worry about things like how the play button led to playing the songs. There are Java APIs, for example, which are needed by programmers to do specific actions like communicating to a third party application or some internal task during application development. Programmers call the Java APIs for the required task and the APIs do the task whilst hiding the implementation details. APIs are designed for effortless communication between two entities. How is an API different from a webservice? https://www.youtube.com/watch?v=8nOTE8owKy0 https://www.youtube.com/watch?v=e3bz4dxoUII All webservices are APIs but not all APIs are webservices. APIs could be available internally within an application/program. Webservices are designed to be made available over a network such as the internet/web or a private network. Note that webservices are getting old, they are becoming a thing of the past because newer technologies to develop APIs have come up. Ex. GraphQL, Event-driven APIs etc. Take a look at the different methodologies to develop APIs other than webservices later. Basic types of APIs: Web APIs: Web APIs enable the transfer of data and functionality over the internet using HTTP protocol. Data (or database) APIs, used to connect applications and database management systems Operating system (or local) APIs, used to define how apps use operating system services and resources Remote APIs, used to define how applications on different devices interact Types of APIs based on architecture/protocol: https://medium.com/@yusufacarr18/whats-api-api-types-most-popular-api-services-rest-vs-soapwhat-s-the-difference-1bd6a685afa1 1. SOAP – Simple Object Access Protocol. It is a webservice which uses SOAP based messages(primarily derived from XML) to enable communication between two entities via different protocols like HTTP, TCP, SMTP over the web. It uses WSDL for specification of the contract for the API. It is completely agnostic(independent) when it comes to the programming language and processing platform. Also, it offers stronger security than REST but it is more complex and cumbersome to use. It is mainly used for B2B. 2. RESThttps://www.geeksforgeeks.org/rest-api-introduction/ https://blog.postman.com/rest-api-examples/ https://aws.amazon.com/what-is/restful-api/ https://restfulapi.net/ https://www.ibm.com/think/topics/rest-apis Representational State Transfer. For an API to be considered RESTful webservice, it needs to satisfy the following criteria: Client-server architecture Statelessness Cache ability Layered System-A layered system that organizes each type of server (those responsible for security, load-balancing, etc.) involved the retrieval of requested information into hierarchies, invisible to the client. Code on demand-the ability to send executable code from the server to the client when requested, extending client functionality. Because REST APIs download and execute code in the form of applets or scripts, there’s more client functionality. Oftentimes, a server will send back a static representation of resources in the form of XML or JSON. Servers can also send executable codes to the client when necessary. Uniform interface-A uniform interface between components so that information is transferred in a standard form. This requires that: o resources requested are identifiable and separate from the representations sent to the client. o resources can be manipulated by the client via the representation they receive because the representation contains enough information to do so. o self-descriptive messages returned to the client have enough information to describe how the client should process it. o hypertext/hypermedia is available, meaning that after accessing a resource the client should be able to use hyperlinks to find all other currently available actions they can take. Most popular output format used by REST is JSON because it is quite user-friendly and could be used in different programming languages with the same result. It is also quite light-weight. REST is ideal for mobile development, integration of third party applications in a website, IoT. 3. RPC APIs Remote procedure call (RPC) is a protocol that provides the high-level communications paradigm used in the operating system. RPC presumes the existence of a low-level transport protocol, such as transmission control protocol/internet protocol (TCP/IP) or user datagram protocol (UDP), for carrying the message data between communicating programs. RPC implements a logical client-to-server communications system designed specifically for the support of network applications. The RPC protocol enables users to work with remote procedures as if the procedures were local. XML-RPC, gRPC and JSON-RPC are types of RPC APIs. 4. GraphQL API 5. Webhook APIs 6. WebSocket WebSocket APIs enable bidirectional communication between client and server. This type of API does not require a new connection to be established for each communication—once the connection is established it allows for continuous exchange. This makes Web Socket APIs ideal for real-time communication. Types of Web APIs based on targeted audience: Internal/Private API: It is a type of API that is used only by certain people and is hidden from external users. This type of API is defined for specific departments of the company and is hidden from third parties. It is mostly used for reuse and production. Open/Public API: APIs that are open to developers and everyone else. They are also known as publicly available or Public APIs. It is divided into two: paid and free. Partner API: APIs that enable the systems of companies that conduct business with a certain business partnership to coordinate in their joint operations are called Partner APIs. They are not open to everyone. For example, communication between an e-commerce site and a cargo company is done using Partner API. Composite API: Composite APIs are APIs that combine multiple data or service APIs, allowing developers to access several endpoints in a single call. Curl command: IMPLEMENTING A REST API: Before implementing a REST webservice, we must understand some basic concepts. Follow the links in notepad for this information. We are going to implement REST API using Java Spring. So, let’s dive into Spring. SPRING CORE: Spring framework is mainly used to develop enterprise java applications. If you need to understand what an enterprise application means, follow the links in the notepad. It is light-weight and modular. It offers a POJO based programming model and uses AOP. It uses other frameworks for various purposes – thus there is support for a wide variety of other frameworks in it. It follows MVC architecture. It provides APIs to handle exceptions, manage transactions etc. in the application. Main features-Dependency Injection and Inversion of Control
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )