الثلاثاء، 30 أبريل 2019

Shenandoah: Ultra low-pause garbage collector in Java 12

Java 12 New ZGC - Concurrent Class Unloading

One feature ZGC is currently missing is class unloading before java 12 versions. This should be implemented. Due to the latency sensitive nature of class unloading, this operation should be performed concurrently.

Concurrent Class Unloading Released in JDK 12 for ZGC

The Z Garbage Collector now supports class unloading. By unloading unused classes, data structures related to these classes can be freed, lowering the overall footprint of the application. Class unloading in ZGC happens concurrently, without stopping the execution of Java application threads, and has zero impact on GC pause times. This feature is enabled by default, but can be disabled by using the command line option `-XX:-ClassUnloading`.


Shenandoah - A Low-Pause-Time Garbage Collector

Traditional Class Unloading VS ZGC Concurrnet Class Unloading


Traditional Class Unloading

Step 1: Marking (concurrent)
  Mark metadata (classes, CLDs) when marking objects


Step 2: Reference processing (STW)
  Need to know what is reachable from finalizers before class unloading


Step 3: Unloading (STW)
  Unload code cache
  Unload metadata

ZGC Concurrnet Class Unloading

Step 1: Marking (concurrent)
 Mark metadata (classes, CLDs) when marking objects
 Mark both strong and final reachable graphs


Step 2: Reference processing (concurrent)
 Already know what is reachable from finalizers before class unloading


Step 3: Unloading (concurrent)
 Unload code cache
 Unload metadata

Image of ZGC-Phases

Shenandoah: A Low-Pause-Time Garbage Collector:

Added a new garbage collection (GC) algorithm named Shenandoah which reduces GC pause times by doing evacuation work concurrently with the running Java threads. Pause times with Shenandoah are independent of heap size, meaning you will have the same consistent pause times whether your heap is 100 MB or 100 GB or 1TB.

Modern machines have more memory and more processors than ever before. Service Level Agreement (SLA) applications guarantee response times of 10-500ms. In order to meet the lower end of that goal we need garbage collection algorithms which are efficient enough to allow programs to run in the available memory, but also optimized to never interrupt the running program for more than a handful of milliseconds. Shenandoah is an open-source low-pause time collector for OpenJDK designed to move us closer to those goals.

Shenandoah trades concurrent cpu cycles and space for pause time improvements. We've added an indirection pointer to every Java object which enables the GC threads to compact the heap while the Java threads are running. Marking and compacting are performed concurrently so we only need to pause the Java threads long enough to scan the thread stacks to find and update the roots of the object graph.

The Shenandoah algorithm is described in depth in this PPPJ2016 paper.

As experimental feature, Shenandoah will require -XX:+UnlockExperimentalVMOptions in the command line. The Shenandoah build system disables the build on unsupported configurations automatically. Downstream builders may choose to disable building Shenandoah with --with-jvm-features=-shenandoahgc on otherwise supported platforms.

To enable/use Shenandoah GC, the following JVM options will be needed: -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC.

 

Java 12 Files mismatch Method Example to Compare two Files

1. Java 12 Files mismatch Overview

In this post, We will learn about new method mismatch() added in Java 12 to Files class. Files class is in package java.nio.file.Files.

java.nio.file.Files class consists exclusively of static methods that operate on files, directories, or other types of files. In most cases, the methods defined here will delegate to the associated file system provider to perform the file operations.

Java 12 API Files mismatch


We will learn how to compare two files using Files.mismatch method.

JDK 12 introduces the new way to determine equality between two files.

Java 12 Collectors.teeing() - Working Examples

1. Overview

In this post, We will learn about new method teeing () added in Java 12 to Collectors class. Collectors class is in package java.util.stream.Collector and it is in java.base module.


Java 12 Collector.teeing​() Method with Example


2. Collectors.teeing ()

Returns a Collector that is a composite of two downstream collectors. Every element passed to the resulting collector is processed by both downstream collectors, then their results are merged using the specified merge function into the final result. 


How to run unix/shell command in java like chmod, mkdir, grep or any unix commands

Running shell command in Java

In this tutorial, We are going to learn how to run shell commands from java such as chmod, mkdir, grep files.

run-shell-command-in-java

Find operating system:

Before running the command, we must know the operating system where the application is running.

By calling System.getProperty("os.name") method, we will get the OS name.

private String OS = System.getProperty("os.name").toLowerCase();

This statement, may return the following operating system names.

Linux, Windows, SunOS, Mac OS, HP-UX and AIX

الجمعة، 26 أبريل 2019

Java ArrayList add method examples

Java ArrayList add method examples:

Introduction:

We will learn Java ArrayList add method with examples. Add method is used to add the specified values at last position of arraylist and at the specified position.

java.util.ArrayList is a class in Java which is implemented based on array concepts. It has many inbuilt methods to simplify the common functionalities.

In this post, we will see examples of how to use add method in ArrayList. Add method is used to add elements to it.

add method 

ArrayList has two add methods.
1. public boolean add(E e) - Adds the value after the last value in the arraylist.
2. public void add(int index, E element) - Adds the values at specified index. 
add() method first ensures that there is sufficient space in the arraylist. If list does not have space, then it grows the list by adding more spaces in underlying array. Then it add the element to specific array index.
Java ArrayList add method examples

الأربعاء، 24 أبريل 2019

Https Exception Java How To Fix suncertpathbuilderexception or SSLHandshakeException | Fix certificate problem in HTTPS

In this tutorial, How to fix problems in Java on "HTTPS connection with SSL certificate Error".

Introduction:

HTTPS protocol is supported since JDK1.4 (AFAIK), you have nothing special to do.

As a java developer, if you have not been stung by the below-mentioned exception while running a Java application developed by you on your machine that hits an SSL server (https), then be prepared to get a nasty experience at some point of your coding journey.
How To Fix suncertpathbuilderexception SSLHandshakeException

ORA-00972: identifier is too long - Oracle Error - Solution

ORA-00972: identifier is too long 

Introduction:

In this tutorial, We will learn about error "ORA-00972: identifier is too long" in oracle. When this will happen and how to resolve it.


ORA-00972 identifier is too long


Error: 

When you encounter error code ORA-00972, a message saying appears "identifier is too long." This error is self explanative that is some problem with length of identifier.

ORA-00972: identifier is too long 

الأربعاء، 17 أبريل 2019

How to Create a User and Grant Permissions in Oracle

In this post, Learn how to create a user in oracle and How to provide grant permissions to the user in oracle.

How to Create a User in Oracle

Before creating a user in oracle, you must have admin access or system privilege to create new user and to create session.

How to Create a User and Grant Permissions in Oracle


Connect to Oracle as sysdba using following command. We are running this tutorial in Oracle 18C version.

H:\>sqlplus / as sysdba

Output:

H:\>sqlplus / as sysdba

SQL*Plus: Release 18.0.0.0.0 - Production on Tue Apr 16 12:47:26 2019
Version 18.3.0.0.0

Copyright (c) 1982, 2018, Oracle.  All rights reserved.


Connected to:
Oracle Database 18c Enterprise Edition Release 18.0.0.0.0 - Production
Version 18.3.0.0.0

SQL>

Only a user authenticated AS SYSASM/SYSDBA to create the user.

الاثنين، 15 أبريل 2019

Oracle Database 18C Download | Oracle Database 18C Installation - step by step

Oracle Database 18C Download and Installation - step by step

In this tutorial,  We will learn how to download oracle database 18C and how to install Oracle Database 18C on Windows 10 Enterprise for research and education.

Oracle Database 18C Installation - step by step


Minimum Hardware and Software Requirements for Installing Oracle 18c Database:

The minimum hardware requirement for installing Oracle Database 18c on Windows 10 is:

RAM: 2 GB of RAM
Empty Space: 10 GB of empty space on your hard disk drive
Swap Memory: 16 GB SWAP (virtual memory) space if RAM is more than 16 GB. If RAM is less than or equal to 16 GB then 1 times the size of the RAM
Processor: A processor based on either Intel EM64T or AMD64 Architecture
Resolution: Minimum screen resolution of 1024 x 768 pixels.

Java Program to Display Fibonacci Series | Find nth Fibonacci number using recursion

Java Program to Display Fibonacci Series:

The Fibonacci series is a series where the next term is the sum of previous two numbers. The first two numbers of the Fibonacci sequence is 0 followed by 1.

Java Program to Display Fibonacci Series


The Fibonacci numbers are the sums of the "shallow" diagonals (shown in red) of Pascal's triangle as below.



We will learn how to find the Fibonacci series to print the n numbers in the series. Below is the example Fibonacci series for first 20 numbers.


0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765


We can find this in many ways. Below three ways, we will learn in this post.

1) While Loop
2) For Loop
3) Using Recursive

الثلاثاء، 9 أبريل 2019

Java While Loop - While loop Example Programs, Nested While Loop Examples

While Loop In Java:

In this tutorial, we will discuss about while loop, its syntax, example, how to make infinite loop.

The Java while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is recommended to use while loop.

Java While loop is an iterative loop and used to execute set of statements for a specified number of times.


Java While Loop - While loop Example Programs, Nested While Loop Examples


We will see now

1. While loop syntax
2. While flowchart
3. Infinite while loop
4. Nested while loop

If, If Else, nested Condition - Statement in java with Examples

If Statement in java 

In this post, We will discuss about If statement, If Else, Multiple if-else, nested Statement or condition in java with examples.



We will learn
  1. If statement
  2. If-Else statement
  3. Multiple if else statement
  4. Nested if else statement
If Statement in java


Java If Statement:

This is very basic topic in every programming language and simple to learn, use it in your programs.
First we see the syntax of If Statement.

Identifiers and Keywords in Java

Java Identifiers and Keywords:

Identifier defined as a set of Java characters and digits and first indexed should be Java letter.

Identifier: Java character or Letter.

Using following method, we can find out Java character. Pass character to below method which returns true.

Character.isJavaIdentifierPart(char c)

we can find out Java Letter using below method which returns true.

Character.isJavaIdentifierPart(int i)

Identifiers and Keywords in Java



Static block Importance in Java

What is static block  in java ?

Static blocks will be executed at the time (during) of class loading if we want to perform any activity we have to define that inside static block.


Static block Importance in Java


E.g. At the time of java class loading, the corresponding native libraries should be loaded. Hence, we have to define this activity inside static block.

Class Test
{
      Static
     {
            System.loadLibrary(“native library path”);
     }
}
We can observe the same in java api classes. Object, Thread classes, etc..

java.util.Date in Java Example

java.util.Date in Java Example

Date class is inbuilt in Java API and package is java.util.Date.

Date class is used to get the current date, time or specific date time. This has methods to format and parse string dates. Implements Serializable, Cloneable, Comparable<Date>.


java.util.Date in Java Example

Constructor of Date class:


public Date()

Example program to get the current date or today's date:

Object class in Java and its methods

Object class in Java and its methods

Object is the Parent class for all class in java api and user defined classes. Object is called as "Root" class for class hierarchy.

This is available when Java invented and from version 1.0. Object class is in package of "java.lang.Object"

If we are not aware user what type of data sending, Object can accept all types of data.



Object class in Java and its methods


Example:


public void method(Object obj){

// Some operations

}

Static initializers and Initializer blocks in Java

Static initializers in Java

The following are the common interview questions and asked in programming written test.

What is Static initializers ?
What is Static block in Java?
Use of Static block ?
What is Initializer blocks in Java?
When Initializer will be invoked?


Initializer blocks:


Initializer block is a block which is started with "{" and enclosed with "}". We can write these blocks anywhere in the program.


Java Program To Convert String to int or Integer

How to convert a String to Integer wrapper class or int primitive type?
Differences between Integer.parseInt(String s) and Integer.valueOf(String s)?

Converting Java String to Integer:

In some cases, we may be getting numbers in format of String so need to convert to integer for our process.

Eg. Given string like "9876" than result would be 9876 int type.


We can perform Java String to Integer operation in 3 ways. 
1) Integer.parseInt(String s) method  
2) Integer. valueOf(String s)  method 
3) Integer Constructor



Convert Java String to Integer or int

Polymorphism in Java

Polymorphism in Java.

In this tutorial, we will discuss about Polymorphism.

Polymorphism represents one cell may have multiple forms, one method can have different behaviors.  In our education, class professor has told that poly indicates "many" and  morph means forms or change. From here Polymorphism word has evolved which has many forms.


Polymorphism in Java


Generally, Polymorphism can be referred between child and parent classes.

Child class can have as same as its parent class behavior or can implement its own behavior.

الاثنين، 8 أبريل 2019

Generics in Java

Generics in Java

Generics concept is introduced in Java 5. we will discuss

a) What are Generics?
b) What will happen if do not have Generics introduced?
c) Why do use Generics?
d) Where Generics are used in Java API?

We will first see case b.


Generics in Java

Final Classes in Java

Final Classes in Java:

A class which is declared as "final" is called "Final Classes".

A class which is already has all its implementation complete so none in the world should not be able to provide additional to original class. To achieve this scenario we should declare it as "final".

For example, Java api String class and System classes are declared as final.


Final Classes in Java

Packages in Java, Syntax, Types and Examples

Packages in Java:

Packages in Java: Packages are defined in Java to know about the classes or interfaces location in projects. No project is defined in world without packages.

In other words, package is set of programs by types which help to organize properly.

Each packages may have its own classed defined. 


"Package" is a keyword which is a combination of words separated by delimiter ".".

In Java, Package is the first statement in every class.


Packages in Java

String Conversions and concatenation Example in Java | Interview Questions

String Conversions and concatenation 

String is nothing but a sequence of characters. To concatenation of string can be done using either concat method in String api class or binary '+' operator.

a) concat(String content) method
2) Binary '+' operator.



String Conversions and concatenation

Autoboxing and Unboxing Conversion with Examples in Java

Autoboxing and Unboxing in Java

Java 1.5 version has introduced Autoboxing and Unboxing in Java.

Autoboxing Conversion :


Converting primitive types to corresponding reference wrapper types called as "Autoboxing" and this happens run time implicitly.




Autoboxing and Unboxing in Java


Following are Autoboxing conversion in Java allow.

1) boolean to type Boolean
2) byte to type Byte
3) short to type Short
4) char to type Character
5) int to type Integer
6) long to type Long
7) float to type Float
8)double to type Double
Boxing conversion may result in an OutOfMemoryError if new instances are created.

Types of Constructors in Java

Java Constructors:

Constructor:  is a special method which is used to initialize object. Constructor similar to the methods declarations. This will be invoked during object initialization.

 
Constructors in Java,

Thump rules for Constructor:

1) Constructor name should same as Class name.
2) Should not have any return type, even void also not allowed.
3) Can not be applied static, abstract, final on constructor.
4) Constructor should not be duplicated. Always should be change in parameters.

Constructor Purpose:

The purpose of the constructor is to initialize the properties to its default values or customized values.

Constructor types in Java:

Constructor are defined as two types.

1) Default parameter(no-argument or zero Parameterized constructor)
2) Parameterized constructor

Sum of two numbers in Java

How To Find Sum of two numbers in Java:

This tutorial we will see how to find out sum of two numbers in Java. Everyone knows how to sum two number using plus(+) symbol in mathematical representation formula.

First we have to read the two number from user what values need to be summed. Scanner class is used to read input from user and have to pass System.in to the Scanner class.

Sum of two numbers in Java


System.in represents standard input from keyboard. Code semantic is given below.

Scanner scanner = new Scanner(System.in);

How to remove an element from ArrayList in Java?

4 Best Ways to Remove Item from ArrayList:

Learn How to remove an element from ArrayList in Java in this post. We can remove the elements from ArrayList using index or its value using following methods of ArrayList.


1) remove(int index): Takes the index of the element
2) remove(Object o): Takes actual value present in the arraylist.
3) removeIf(Predicate<? super E> filter): which takes the predicate that means if the value is matches to the specified object then it will be removed.
4) void remove(): Removes the current element at the time of iteration.

First 3 methods are from ArrayList and last method is from Iterator interface.


How to remove an element from ArrayList in Java

الجمعة، 5 أبريل 2019

Run class in Jar file - How to run a jar file from command line

Run class in Jar file

In this post, We will learn how to run a Jar file from command prompt with main method and without main method. This is very useful in unix environment to run the jobs in nohup mode. But, now we will be focusing running jar file in windows operating system.


Run class in Jar file - How to run a jar file from command line


How to run a jar file from command line

If you have a jar file called myJavaJar.jar located in /myJavafolder and you want to use the class called myJavaClass from it, How do you run this java program from command line?

I thought it would be to go into the directory and say java -cp myJavaJar.jar myJavaClass but that isn't working. 

الخميس، 4 أبريل 2019

Java Program To Print All Palindromes In A Given Range

Program To Print All Palindromes In A Given Range:

In this post, We will learn how to find all palindromes in a given range in java programming language.

First, We will learn what is palindrome?

What is Palindrome?

If a number is same as original number when it is reversed then it is said to be Palindrome.

Dictionary definition: a word, phrase, or sequence that reads the same backwards as forwards.

Java Program to print all palindromes in a given range

Palindrome Examples:

1001, 10101, 111, 99099

Now, We will write a program on how to print all palindromes in a given range of integers.

Objective: Given a range of numbers, print all palindromes in the given range.

Examples: 


Input range 1: {10, 100}
Output 1: 11, 22, 33, 44, 55, 66, 77, 88, 99

Input range 2: {10, 100}
Output 2: 202 212 222 232 242 252 262 272 282 292 303 313 323 333 343 353 363 373 383 393 

Input range 3: {1000, 1500}
Output 3: 1001 1111 1221 1331 1441 

Program to find all palindromes in a given range:

We will run this program running a for loop from min to max. For each number check whether a number palindrome or not. If number is palindrome then print it.



package examples.java.w3schools.array.programs;

/**
 * Java program to find all palindromes in a given range (min, max)
 * 
 * @author java-w3schools
 *
 */
public class PalindromesEx {

 public static void main(String[] args) {
  int min = 100;
  int max = 1500;

  printPal(min, max);
 }

 /**
  * Returns true if the given num is palindrome
  * 
  * @param num
  * @return
  */
 private static boolean isPalindrome(int num) {
  // Reversing a number
  int reverse = 0;
  for (int i = num; i > 0; i /= 10)
   reverse = reverse * 10 + i % 10;

  // If num and reverse are same, then num is palindrome
  return num == reverse;
 }

 /**
  * Prints palindrome between min and max
  * 
  * @param min
  * @param max
  */
 static void printPal(int min, int max) {
  for (int i = min; i <= max; i++)
   if (isPalindrome(i))
    System.out.print(i + " ");
 }
}



Output:

The above program produces this output.



101 111 121 131 141 151 161 171 181 191 202 212 222 232 242 252 262 272 282 292 303 313 323 333 343 353 363 373 383 393 404 414 424 434 444 454 464 474 484 494 505 515 525 535 545 555 565 575 585 595 606 616 626 636 646 656 666 676 686 696 707 717 727 737 747 757 767 777 787 797 808 818 828 838 848 858 868 878 888 898 909 919 929 939 949 959 969 979 989 999 1001 1111 1221 1331 1441





الأربعاء، 3 أبريل 2019

Java Program To Print All Distinct Elements of a given integer array

Program to find Unique Array Element


We will learn, how to find and print all distinct elements of a given integer array. 

Objective:


Given a set of integers in array, we have to print all unique values from the array. This array may contain duplicate values and the output of our program should be printing only distinct numbers.


Print All Distinct Elements of a given integer array

Examples:



Example 1: array1[] = {1, 2, 3, 4, 5, 6, 7, 8}
Output 1: 1 2 3 4 5 6 7 8

Example 2: array2[] = {20, 10, 30, 20, 67, 100, 90, 20, 90}
Output 2: 10, 20, 30, 67, 90, 100

Example 3: array3[] = {23, 46, 54, 33, 14, 19, 23, 33, 99, 14}
Output 3: 23, 46, 54, 33, 14, 19, 99


This is a common telephonic interview question to understand the thinking of the candidate. You should ask some questions to the interviewer whether the you understand the question correctly. This reveals understanding the problem and how you are trying to solve the problem.

الثلاثاء، 2 أبريل 2019

String equalsIgnoreCase​ method in java with example - Internal Implementation

String equalsIgnoreCase​ method in java:

In this post, We will learn more about java.lang.String.equalsIgnoreCase​() method as following today.

Step 1) What equalsIgnoreCase​ method does or method description
Step 2) Syntax, Parameters, Return type
Step 3) Example 1:  Program to check equalsIgnoreCase​ method
Step 4) Example 2:  Program to find the string is present in the List
Step 5) Difference between equals and equalsIgnoreCase
Step 6) How equalsIgnoreCase​ works internally and implementation code.

Step 1) What equalsIgnoreCase​ method does or method description

equalsIgnoreCase​ method compares this string to the specified object ignoring case. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object ignoring case. It simply ignores the case whether it is lower case or upper case.

String equalsIgnoreCase​ method in java

For example:

Read How String equals methods works in java with example programs.

Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.

الاثنين، 1 أبريل 2019

Program: How to remove vowels from String in java?

Removing vowels from String in java? 

In this post, We will learn a program to remove the vowels from String in java programming.

Vowel characters: a, e, i, o and u

Given a String, that contains the vowels may be all or few. From the string need to delete all vowels and print the string without vowels.

Program: How to remove vowels from String in java?

Examples:

Input 1: hello, welcome to java-w3schools blog
Output 1: hll, wlcm t jv-w3schls blg

Input 2: You are reading a Java program to delete vowels in a given string
Output 2: Y r rdng  Jv prgrm t dlt vwls n  gvn strng

This can be done Java, Python, C#, C++, etc.