Posts

Showing posts from September, 2024

Java fundamental programs to practice: Conditional statement- switch:

               Switch is one of the conditional statements which is useful in selecting one among in multiple options. Syntax: Switch(Option) {   Case 1: statements…..                break; Case 2: statements…..                break; --------------------------- --------------------------- Case n: statements…..                break; } Example for switch conditional statement is given below. Java Program to print the month using switch case: ·        Create a class SwitchMonth with main() function. ·        Get the input from user. Using the switch statement,pass the input. ·        Based on the input given, case stateme...

Java fundamental programs to practice: Conditional statements(if,else)

             Conditional statements are used to check the conditions. It uses the conditional statements listed below. 1.        If statement 2.        If else statement 3.        If else if statement Java fundamental program: To check a number is greater than five(if statement example)               This program reads a input value. Check the value is greater than 5 or not and print the value. import java.util.Scanner; class great {   public static void main(String args[])   {    Scanner scanner = new Scanner(System.in);    System.out.print("Enter a number: ");    int n = scanner.nextInt();    if (n >5)      System.out.println("The given number is greater than five");   } } The output is...

Java fundamental programs for beginners:

       Java is a object oriented programming language. It was developed by James Gosling in sun micro systems. It is a general-purpose language. It uses classes for creating programs. A simple program is given below. Class greeting is used to display a message “Have a nice day” using system.out.println() class   greeting {   public static void main(String args[]) { System.out.println("Have a nice day"); } } Save this file as “greeting.java”. compile and execute this file to get the output. Output: C:\raji\blog>javac greeting.java C:\raji\blog>java greeting Have a nice day Next, an arithmetic operation : Multiplication of two numbers Here, a class arith is created. Two integer values are assigned as input. Using print statement, the two values are multiplied and displayed. class arith {   public static void main(String args[])   {   int a = 10;   int b = 20;   System.out.println("Th...

Print different patterns using java programs

     Patterns are the sequence of some shapes or numbers in some particular order. These can be created by  sequence of looping statements. Here, three programs are listed below. part1 of these type of programs blog is linked here. https://www.blogger.com/blog/post/edit/5251857942821229/5422820259444677124 #Java Program to print right angle triangle:               This program creates a right angle triangle pattern. Steps: ·        Create a class sample1 with main() function. ·        Get the number of lines to print the pattern. ·        Using two for loops starts from value 1 to value n. ·        Repeat until it reaches the ‘n’. ·        Just print the * pattern. class sample1 { public static void main(String args[]) { int no=7;...

Base64 Encoding and Decoding in java8

       Data is everywhere. Anybody can track your data. To secure your data, it should be protected. Java8 offers you a built-in package “java.util.Base64” for security. Base64 encoding and decoding is the process of making binary data into ASCII string data with 64 characters. Here, encoding is the process of converting normal data into Base64 encoding string. Decoding is the process base64 encoding string into normal data. Let us code an example. First, encode a data into Base64 encoding format: Steps to follow: ·        Import the built in package java.util.Base64 ·        Create a class with main() function. ·        Get the data to be encoded. ·        Using the built in function encodeTostring() to encode the string. It gets the and use Base64.getEncoder() function to encode the data. ·        Finally...

How to implement CompletableFuture class in java8?

       CompletableFuture deals with asynchronous programming. It run the process asynchronously. It allows the programs to execute in parallelly. Built in package: java.util.concurrent. Why completableFuture class? CompletableFuture is efficient and provide response in timely manner. It has many functions listed below. supplyAsync ,runAsync : It deals with asynchronous computations to run the tasks. thenApply,thenAccept,thenRun : Multiple tasks executed and linked together to perform the computation. thenCombine,thenCompose,allOf : It combines the tasks. Exception handling: If any error occurs, this will handle. Let the program begins. First, Asynchronous Computation using completableFuture Program in java is given below. Steps: ·        Create a public class CFEg with main() function. ·        Next, include the lambda expression to execute the code. ·     ...

How to Illustrate Type annotations in java8

     Annotations are the information that can be treated as supplement about the program. It is used to associate with the meta data. Generally, it contains the information about classes, methods, constructors, variables and so on… Syntax: @Annotation_Information Eg: @FunctionalInterface Built in package for annotation is java.lang.annotation Where to use annotations??               Mainly, annotations are used in declaration. It can declare a class, member functions, variables and so on. In java8, it deals with types. So, it is called Type annotations. Type annotations are very useful in object creation, type casting an element, implements and thrown statements. Some of the examples are given below. 1.        Object creation: This is used to create an object for a class. Eg: new @Class1 Obj(); where new is the keyword to create an object. @Class1 is the...

How to run JavaScript embedded in java program using Nashorn JavaScript Engine??

  Nashorn JavaScript Engine               It is a JavaScript engine introduced in java8. It was developed by oracle. Now, it is under the OpenJDK community. Purpose: This is used to run the JavaScript code in JVM (Java Virtual Machine). Why Nashorn? It is efficient in execution compared to previous one (Rhino Engine). It easily integrates java and JavaScript. The user can simply use the command line. How to run JavaScript embedded in java program using Nashorn JavaScript Engine??               Here, java Script is a single line which displays a greetings “Happy Programming”. Steps: Import three built in packages from javax.script(.ScriptEngine,.ScriptEngineManager,.ScriptException) Create a public class NashornEg and save the file as “NashornEg.java” Inside the main() function,create an object for ScriptEngineManager as mgr. Get the N...

How to implement Static methods in interface concept in java8???

  The word “Static” is applicable for the interface. It does not be an instance of the class which implements the interface. Why static method??? ·        Scope of the static methods is visible within interface. ·        It belongs to interface only. So, utility methods are declared as static methods. Simple program to implement static methods in interface:               This program creates an interface with a static method “staticMd()” to print the static method message. Even though, a class is declared to implement the interface, the method belongs to the interface only. //Java Program to implement static methods in interface interface MyInter {     static void staticMd() {         System.out.println("This is a static method in an interface MyInter");     } } public cl...

How to display a vehicleList using forEach in java8 programming?

              Java8 offers variety of concepts like lambda expressions, functional interfaces, streams, default methods, collectors and so on… “Foreach” is an added feature for looping to iterate the elements. It uses lambda expression or looping. Java Program to display the vehicle list using ForEach method using Lambda expressions: Steps to follow: ·        This program creates a vehicleList as string. ·        Add the list of vehicles to vehicleList. ·        Using lambda expression, ForEach method dispalys the vehicleList. //Java Program to display the vehicle list using ForEach method using Lambda expressions: import java.util.ArrayList;   import java.util.List;   public class ForEachEg {     public static void main(String[] args) {               ...