Joiner Class in Guava for Java

AmitDiwan
Updated on 14-Jul-2020 06:58:05

139 Views

Joiner provides various methods to handle joining operations on string, objects, etc. Let us see an example −Exampleimport com.google.common.base.Joiner; import java.util.*; public class Demo{    public static void main(String[] args){       String[] my_arr = { "hel", null, "lo", "wo", "r", null, "ld" };       System.out.println("The original array is : "+ Arrays.toString(my_arr));       String my_result = Joiner.on('+').skipNulls().join(my_arr);       System.out.println("The joined string is : " + my_result);    } }OutputThe original array is [hel, null, lo, wo, r, null, ld] The joined string is hel+lo+wo+r+ldA class named Demo contains the main function, which defines ... Read More

Print Single and Multiple Variables in Java

AmitDiwan
Updated on 14-Jul-2020 06:57:00

3K+ Views

To print single and multiple variables in Java, the code is as follows −Example Live Demopublic class Demo {    public static void main(String args[]){       String name_1 = "Hello";       String name_2 = "World";       System.out.println("Printing single variable");       System.out.printf("%s", name_1);       System.out.println("Printing multiple variables");       System.out.printf("First Name: %sLast Name: %s",name_1, name_2);    } }OutputPrinting single variable Hello Printing multiple variables First Name: Hello Last Name: WorldA class named Demo contains the main function, which defines two strings. These strings are displayed using the ‘println’ function and using the ‘printf’ function.

Java Program to Convert Floating Point to Binary

AmitDiwan
Updated on 14-Jul-2020 06:52:56

1K+ Views

To convert floating to binary, the Java code is as follows −Example Live Demoimport java.io.*; public class Demo {    static void decimal_to_bin(int n){       int[] bin_num = new int[50];       int i = 0;       while (n > 0){          bin_num[i] = n % 2;          n = n / 2;          i++;       }       for (int j = i - 1; j >= 0; j--)       System.out.print(bin_num[j]);    }    public static void main (String[] args){   ... Read More

Widening Primitive Conversion in Java

AmitDiwan
Updated on 14-Jul-2020 06:51:24

290 Views

Following is an example showing widening primitive conversion −Example Live Demopublic class Demo {    public static void main(String[] args) {       System.out.print("H" + "E");       System.out.print('L');       System.out.print('L');       System.out.print('O');    } }OutputHELLOA class named Demo contains the main function. Here, the ‘print’ function is used to print specific characters in double quotes and then in single quotes. When the process of widening primitive conversion happens, the presence of ‘+’ operator is a must. This ‘+’ operator expects integer on both the left hand and right hand sides.

Call run() Method Directly Instead of start() in Java

AmitDiwan
Updated on 14-Jul-2020 06:50:05

440 Views

Yes, we can do that. Let us see an example −Example Live Democlass my_thread extends Thread{    public void run(){       try{          System.out.println ("The thread " + Thread.currentThread().getId() + " is currently running");       }       catch (Exception e){          System.out.println ("The exception has been caught");       }    } } public class Main{    public static void main(String[] args){       int n = 6;       for (int i=1; i

Memory Consistency Error in Java

AmitDiwan
Updated on 14-Jul-2020 06:48:21

606 Views

When the concept of multithreading is implemented, it is possible that changes made by one thread wouldn’t be visible to the other thread. This indicates that the view of each thread is inconsistent with respect to each other. This is known as memory consistency error.CPU might initiate main memory access in a different order, whereas the threads might access them in a different order.This is usually true when write operation is being performed, thereby avoiding the CPU wait time.The write operation is an atomic one, meaning no other operation would be performed by other threads when a write operation is ... Read More

Thread Interference Error in Java

AmitDiwan
Updated on 14-Jul-2020 06:46:02

235 Views

Let us see an example to understand the concept of Thread Interference error −Example Live Demoimport java.io.*; class Demo_instance{    static int val_1 = 6;    void increment_val(){       for(int j=1;j

Chrome 45 is Here

Samual Sam
Updated on 14-Jul-2020 06:45:37

197 Views

When Google Chrome broke out on our computer screens, it created a new way to see the internet. It was fast, stable, secure and had beautiful themes. It added tabs and also went undercover for us in the incognito mode, it protected us by automatically blocking malicious websites as well.The new Chrome 45 is fierce fighter equipped with silver bullets in all its cylinder chambers, and boy, does it come out all guns blazing!Automatically pauses Flash contentAuto-playing Flash ads consume a lot of battery power as they require substantial processing power to run. And they slow down the web page ... Read More

Configure and Send Emails in WordPress

Samual Sam
Updated on 14-Jul-2020 06:45:07

936 Views

Looking at the popularity and significance of a website one may has now decided to create his own 5-7 page business website. With that we shall need web pages for our business location map as well as a contact page and an official email id. There are various ways through which we can create a contact-us form so that it’s easier for clients to directly contact us through visiting our website page.For that first we need to setup our official email-id with our WordPress website. After that configuring that email-id will take us further in sending and receiving an email. ... Read More

List All Files in a Directory and Nested Sub-Directory using Java

AmitDiwan
Updated on 14-Jul-2020 06:39:02

541 Views

To list all files in a directory and nested sub-directory, the Java program is as follows −Exampleimport java.io.File; public class Demo{    static void print_recursively(File[] my_arr, int my_index, int sub_level){       if(my_index == my_arr.length)       return;       for (int i = 0; i < sub_level; i++)       System.out.print("\t");       if(my_arr[my_index].isFile())       System.out.println(my_arr[my_index].getName());       else if(my_arr[my_index].isDirectory()){          System.out.println("[" + my_arr[my_index].getName() + "]");          print_recursively(my_arr[my_index].listFiles(), 0, sub_level + 1);       }       ... Read More

Advertisements