Found 2617 Articles for Java

Using Variables in JShell of Java 9

AmitDiwan
Updated on 09-Jul-2020 06:47:43

371 Views

In JShell 9, variables can be declared during a session. Once the user has logged into the session, they can declare a variable as follows −jshell> int val = 56 ;Italics indicate the terminal, once the user has logged in to their session.The above line would print the below output. The semicolon in the above line is optional, and it will run fine without the semicolon also.Outputval = = > 56When an integer value is defined by assigning it to a variable name on the JShell, and it is executed, by pressing ‘Enter’ key, it is displayed on the next ... Read More

Using underscore in Numeric Literals in Java

AmitDiwan
Updated on 09-Jul-2020 06:45:34

114 Views

Followig is the code showing how to use underscore in numeric literals in Java −Example Live Demopublic class Demo{    public static void main (String[] args) throws java.lang.Exception{       int my_num_1 = 6_78_00_120;       System.out.println("The number is : " + my_num_1);       long my_num_2 = 2_00_11_001;       System.out.println("The number is : " + my_num_2);       float my_num_3 = 4.01_981F;       System.out.println("The number is : " + my_num_3);       double my_num_4 = 12.89_46_061;       System.out.println("The number is : " + my_num_4);    } }OutputThe number is ... Read More

Using predefined class name as Class or Variable name in Java

AmitDiwan
Updated on 09-Jul-2020 06:43:55

1K+ Views

Using a predefined class name as a class nameLet us see an example −Example Live Demopublic class Number{    public static void main (String[] args){       System.out.println("Pre-defined class name can be used as a class name");    } }OutputPre-defined class name can be used as a class nameThe class Number has a main function that displays a message when it is executed. The main function takes string values as arguments.Using a predefined class name as a variable nameLet us see an example −Example Live Demopublic class String{    public static void main (java.lang.String[] args){       System.out.println("Pre-defined class name ... Read More

Using TreeMap to sort User-defined Objects in Java

AmitDiwan
Updated on 09-Jul-2020 06:42:38

136 Views

To sort user-defined object in Java, the code is as follows −Example Live Demoimport java.io.*; import java.util.*; public class Demo{    static void sort_objects(String my_data){       String[] my_vals = my_data.split(" ");       Map my_map = new TreeMap();       for (int i = 1; i < my_vals.length; i += 2){          int my_age = Integer.parseInt(my_vals[i]);          String name = my_vals[i - 1];          if (my_map.containsKey(my_age)){             ArrayList my_list = my_map.get(my_age);             my_list.add(name);           ... Read More

Unreachable Code Error in Java

AmitDiwan
Updated on 09-Jul-2020 06:40:33

1K+ Views

Unreachable code error occurs when the code can’t be compiled due to a variety of reasons, some of which include: infinite loop, return statement before the unreachable line of code.Let us see an example −Example Live Demopublic class Demo{    public static void main(String args[]){       int val = 5;       for (;;){          if (val == 5){             break;             System.out.println("If the condition is not true, this line would be printed. ");          }       }    } ... Read More

Java program to Find the first non-repeating character from a stream of characters

AmitDiwan
Updated on 09-Jul-2020 06:39:52

927 Views

To find the first non-repeating character from a stream of characters, the Java code is as follows −Example Live Demoimport java.util.ArrayList; import java.util.List; public class Demo{    final static int max_chars = 256;    static void non_repeating_char(){       List my_list = new ArrayList();       boolean[] repeat = new boolean[max_chars];       String my_str = "Thisisasample";       for (int i = 0; i < my_str.length(); i++){          char x = my_str.charAt(i);          if (!repeat[x]){             if (!(my_list.contains(x))){               ... Read More

Java Program to Find the Number Occurring Odd Number of Times

AmitDiwan
Updated on 08-Jul-2020 12:05:54

390 Views

To find the number occurring odd number of times, the Java code is as follows −Example Live Demopublic class Demo {    static int odd_occurs(int my_arr[], int arr_size){       int i;       for (i = 0; i < arr_size; i++){          int count = 0;          for (int j = 0; j < arr_size; j++){             if (my_arr[i] == my_arr[j])             count++;          }          if (count % 2 != 0)   ... Read More

Java Program to Find the closest pair from two sorted arrays

AmitDiwan
Updated on 08-Jul-2020 12:03:33

241 Views

To find the closest pair from two sorted array, the Java code is as follows −Example Live Demopublic class Demo {    void closest_pair(int my_arr_1[], int my_arr_2[], int arr_1_len, int arr_2_len, int sum){       int diff = Integer.MAX_VALUE;       int result_l = 0, result_r = 0;       int l = 0, r = arr_2_len-1;       while (l=0){          if (Math.abs(my_arr_1[l] + my_arr_2[r] - sum) < diff){             result_l = l;             result_r = r;             ... Read More

Java Program to Find sum of even factors of a number

AmitDiwan
Updated on 08-Jul-2020 12:00:21

653 Views

To find sum of even factors of a number, the Java code is as follows −Example Live Demoimport java.util.*; import java.lang.*; public class Demo{    public static int factor_sum(int num){       if (num % 2 != 0)       return 0;       int result = 1;       for (int i = 2; i = 2)       result *= (1 + num);       return result;    }    public static void main(String argc[]){       int num = 36;       System.out.println("The sum of even factors ... Read More

Java Program to find reminder of array multiplication divided by n

AmitDiwan
Updated on 08-Jul-2020 11:58:16

175 Views

To find reminder of array multiplication divided by n, the Java code is as follows −Example Live Demoimport java.util.*; import java.lang.*; public class Demo{    public static int remainder(int my_arr[], int arr_len, int val){       int mul_val = 1;       for (int i = 0; i < arr_len; i++)       mul_val = (mul_val * (my_arr[i] % val)) % val;       return mul_val % val;    }    public static void main(String argc[]){       int[] my_arr = new int []{ 35, 100, 69, 99, 27, 88, 12, 25 }; ... Read More

Advertisements