Posts

Different ways to initialize arrays in java

Different ways to initialize array's 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 ...

Determine OS version from Command line in MacOS

Image
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 system_profiler 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..... sw_vers Execute the following command from Terminal sw_vers 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:" 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.

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

How to convert HttpResponse to String in Java

Following code snippet explains how to convert HttpResponse object to String in Java Steps Call httpclient.execute() Get the InputStream from the response Convert the InputStream to ByteArrayOutputStream Convert the ByteArrayOutputStream to String 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()); request.releaseConnection(); inputStream.close(); return strRes; }

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);

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 alert(jQuery.fn.jquery); } 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.