Tuesday, May 26, 2020

Different ways to initialize arrays in java

Array Initialization in Java

Different ways to initialize array's in java
Java Arrays can be populated in a number of ways following summarizes different options.
One item at a time using a loop
Following example we are declaring rolNumms array of size 10 and adding one item at a time using a for a loop.
  //Using For loops
   int rollNums[] = new int[10];
   for (int i=0; i
At the Time of Declaration
We can set the content of the array while declaring the array it self. Both the syntax will result in initializing the array with 10 items. Note that when we provide the initializer while declaring the size of the array cannot be specified.
int rollNums2[] = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9 ,10};
int rollNums3[] = {1, 2, 3, 4, 5, 6, 7, 8, 9 ,10};
Arrays.setAll() and Generator function
We can set the content of the array using the Arrays.setAll() method as well.
int rollNums4 [] = new int [10];
Arrays.setAll(rollNums4, p -> p );
Copying another array
We can also copy an existing array to populate a new array, using java.util.Arrays.copyOf method
int rollNums5[] = Arrays.copyOf(rollNums4, rollNums4.length);

Friday, October 11, 2019

Determine OS version from Command line in MacOS

Sometimes we would like to know about the installed MacOS from Terminal. Or say we are writing scripts to extract the OS related information like it's version and release name etc.

For Mac we have couple of options

System Profiler

Execute the following command from Terminal


As you can see from the screenshot system_profiler will print a lot of information about the Software and Hardware of the Mac.

This is it.....


Execute the following command from Terminal


As you can see from the screenshot sw_vers will print information about the current installed OS Name and Version.

How to change computer name and user name in terminal command prompt in MacOS

Sometimes we want to hide the system name or username that is displayed on the Terminal.

May be to take some screenshot and then paste it in the blog and we want to hide the System name/user Name etc.

For Bash based terminals it can be done by setting PS1 variable.

Example on the terminal

$ export PS1="> "

$ PS1="Mac:~sidd:"

$ export PS1="This is Demo Terminal: $ "
This is Demo Terminal: $

Change your prompt in your  profile file to make it permanent across all Terminals and new sessions.

Thursday, October 10, 2019

How to Install Apache 2 on Ubuntu Linux

Following are the steps to install Apache 2 on Ubuntu Linux and then start it.

Update the System Repository

sudo apt update

Install Apache2 Software with apt command

sudo apt install apache2

Thursday, September 5, 2019

How to convert HttpResponse to String in Java

Following code snippet explains how to convert HttpResponse object to String in Java


  1. Call httpclient.execute()
  2. Get the InputStream from the response
  3. Convert the InputStream to ByteArrayOutputStream
  4. Convert the ByteArrayOutputStream to String
  5. Close the request and InputStream

private String executeAndGetResponse(HttpRequestBase request)
      throws IOException, ClientProtocolException, Exception, UnsupportedEncodingException {
    HttpResponse response = httpClient.execute(request);
    InputStream inputStream = response.getEntity().getContent();
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputStream.read(buffer)) != -1) {
      result.write(buffer, 0, length);
    String strRes = result.toString(StandardCharsets.UTF_8.name());
    return strRes;

Tuesday, September 3, 2019

Jackson Object Mapper to conver String to JsonNode

Following are some of the code snippet for Jackson Object Mapper
Convert String to JsonNode Object
String data;
JsonNode jsonNode = getObjectMapper().readTree(data);
Convert an Object to JSON String
String jsonString = getObjectMapper().writeValueAsString(object);
Convert InputStream to JsonNode Object
InputStream inputStream;
JsonNode jsonNode = getObjectMapper().readTree(inputStream);

Thursday, August 22, 2019

How to check what version of JQuery is loaded in browser

Sometimes we might want to know what version of jQuery a particular site is using. It is not always possible to know the version of jQuery we are using through the jQuery File name or reading the content of the files, specially when the jQuery is minimized.

Following snippet can tell us the version of jQuery loaded by a site.

if (typeof jQuery != 'undefined') {  
    // jQuery is loaded => print the version

Simply go to the console of the browser, once the webpage is loaded, and then type. the above command on the console. It should display the version as an Alert.