Java Articles

Page 170 of 450

Print Single and Multiple variables in Java

AmitDiwan
AmitDiwan
Updated on 14-Jul-2020 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.

Read More

Java program to convert floating to binary

AmitDiwan
AmitDiwan
Updated on 14-Jul-2020 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
AmitDiwan
Updated on 14-Jul-2020 305 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.

Read More

Can we call run() method directly instead of start() in Java

AmitDiwan
AmitDiwan
Updated on 14-Jul-2020 458 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

Read More

Memory Consistency Error in Java

AmitDiwan
AmitDiwan
Updated on 14-Jul-2020 630 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
AmitDiwan
Updated on 14-Jul-2020 247 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

Read More

Java program to List all files in a directory and nested sub-directory - Recursive approach

AmitDiwan
AmitDiwan
Updated on 14-Jul-2020 558 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

How to implement DoubleConsumer using lambda expression in Java?

raja
raja
Updated on 14-Jul-2020 325 Views

DoubleConsumer is a functional interface from java.util.function package. This functional interface accepts a single double-valued argument as input and produces no output. This interface can be used as an assignment target for a lambda expression or method reference. DoubleConsumer contains one abstract method: accept() and one default method: andThen().Syntax@FunctionalInterface public interface DoubleConsumer {    void accept(double value); }Example-1import java.util.function.DoubleConsumer; public class DoubleConsumerLambdaTest1 {    public static void main(String args[]) {       DoubleConsumer increment = doubleVal -> {       // lambda expression          System.out.println("Incrementing " + doubleVal + " by one");          System.out.println("Current Value : ...

Read More

How to implement LongConsumer using lambda in Java?

raja
raja
Updated on 13-Jul-2020 214 Views

LongConsumer is a in-built functional interface from java.util.function package. This interface can accept a single long-valued argument as input and doesn't produce any output. It can also be used as the assignment target for a lambda expression or method reference and contains one abstract method: accept() and one default method: andThen().Syntax@FunctionalInterface public interface LongConsumerExample-1import java.util.function.LongConsumer; public class LongConsumerLambdaTest {    public static void main(String[] args) {       LongConsumer displayNextVal = l-> {     // lambda expression          System.out.println("Display the next value to input : "+l);          System.out.println(l+1);       };       LongConsumer displayPrevVal ...

Read More

Regular expression for a hexadecimal number greater than 10 and should be even in length in java.

Maruthi Krishna
Maruthi Krishna
Updated on 13-Jul-2020 417 Views

Following is the regular expression to match hexadecimal number greater than 10 with even length −^(?=.{10,255}$)(?:0x)?\p{XDigit}{2}(?:\p{XDigit}{2})*$Where,^ − Matches the starting of the sentence.(?=.{10,255}$) − String ending with characters with 10 to 255.\p{XDigit}{2} − Two hexa-decimal characters.(?:\p{XDigit}{2})* − 0 or more sequences of double hexa-decimal characters.$ − Matches the end of the sentence.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class JavaExample51 {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       String nums[] = new String[5];       for(int i=0; i

Read More
Showing 1691–1700 of 4,496 articles
« Prev 1 168 169 170 171 172 450 Next »
Advertisements