Showing posts with the label Core Java

Java - Swap Two Numbers

In this post, you will learn how to code Java Program to Swap Two Numbers using 2 different methods. 1. Using third variable 2. Without using third variable Java Program to Swap Two Numbers Method 1 : using third variable import ; import ; import ; public class Main { public static void main (String []args) throws IOException { InputStreamReader isr = new InputStreamReader(System. in ) ; BufferedReader br = new BufferedReader(isr) ; System. out .println( "Enter first number: " ) ; int firstNumber = Integer. parseInt (br.readLine()) ; System. out .println( "Enter second number: " ) ; int secondNumber = Integer. parseInt (br.readLine()) ; //Before Swapping two numbers System. out .println( "Before Swapping :" ) ; System. out .println( "First number is " +firstNumber) ; System. out .println( "S

Implement functional interfaces using lambda expressions

Java is Object Oriented Programming language , being object-oriented is not bad, but it brings a lot of verbosity to the program. Java 8 introduced new libraries and programming styles, for example,  functional interfaces ,  lambda expressions , and streams . These bring functional-style programming to the object-oriented programming capabilities of Java. Java Functional Interface and Lambda Expression help us in writing smaller and cleaner code by removing a lot of boilerplate code. Java Functional Interface: A functional interface in Java is an interface that contains only a single abstract method.  A functional interface can contain default and static methods which do have an implementation, in addition to the single unimplemented method. Here is a Java functional interface examples: Example 1, The below counts as a functional interface in Java because it only contains a single method, and that method has no implementation: public interface Addition { public Integer calcula

Understand Variables and its Scope in Java

Hello everyone, In this article, we will learn the types of Variables and its Scope in Java. A variable is a container that holds values that are used in a Java program. Every variable must be declared to use a data type. Variable Declaration: For example, int age; float pi; double salary; char code; Variable Initialization: To initialize a variable, you must assign it a valid value. Here, the container named 'score' holds a value of 49. For example, age = 49; pi = 3.14; salary = 456734.345; code = 'A'; Variable declaration and initialization: To initialize a variable, you must assign it a valid value. int age = 49; float pi = 3.14; double salary = 456734.345; char code = 'A'; Types of Variables and its Scope: There are three types of variables.  Instance Variables  Class Variables/Static Variable Local Variables  Instance Variables: An instance variable is a non-static variable defined in a class outside any method, constructor or block in which each instantiat

Spring Boot Rest XML CRUD example – Web service with XML Request and Response

Hello everyone, Today we will learn how to develop Web service with XML Request and Response in Spring Boot.  You can clone the complete source code from our Github Repository.   Technologies used :      Spring Boot 2.5.4    Java 11 Spring Data JPA H2DB Jackson Dataformat XML 2.10.1  Maven 3   Project Structure: – WebConfig implements WebMvcConfigurer. This is where we set the default content type. – Book data model class corresponds to entity and table books. – BookRepository is an interface that extends JpaRepository for CRUD methods and custom finder methods. It will be autowired in BookController. – BookController is a RestController which has request mapping methods for RESTful requests such as: getBookById, createBook, updateBook, deleteBook... – Configuration for Spring Datasource, JPA & Hibernate in – pom.xml contains dependencies for Spring Boot, Jackson Dataformat XML, JPA & H2DB. 2)Maven/Dependency Management [pom.xml]  <? xml version =&quo