Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Java Articles
Page 18 of 450
Unreachable Code Error in Java
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 −Examplepublic 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. "); } } } }Output/Demo.java:11: ...
Read MoreUsing TreeMap to sort User-defined Objects in Java
To sort user-defined object in Java, the code is as follows −Exampleimport 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 MoreUsing predefined class name as Class or Variable name in Java
Using a predefined class name as a class nameLet us see an example −Examplepublic 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 −Examplepublic class String{ public static void main (java.lang.String[] args){ System.out.println("Pre-defined class name can be ...
Read MoreUsing underscore in Numeric Literals in Java
Followig is the code showing how to use underscore in numeric literals in Java −Examplepublic 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 MoreWays to read input from console in Java
Let us see some ways to read input from console in Java −Exampleimport java.util.Scanner; public class Demo{ public static void main(String args[]){ Scanner my_scan = new Scanner(System.in); String my_str = my_scan.nextLine(); System.out.println("The string is "+my_str); int my_val = my_scan.nextInt(); System.out.println("The integer is "+my_val); float my_float = my_scan.nextFloat(); System.out.println("The float value is "+my_float); } }OutputThe string is Joe The integer is 56 The float value is 78.99A class named Demo contains the main function. An instance of the ...
Read MoreStructure and Members of the Java Program
While writing any piece of code in Java, there is a certain set of rules and regulations that need to be followed, that is considered as a standard. For example − A class contains variables, and functions. The functions can be used to work with the variables. Classes can be extended, and improvised too.Basic structureList of packages that are imported; public class { Constructor (can be user defined or implicitly created) { Operations that the constructor should perform; } Data elements/class data members; User-defined functions/methods; public static void main (String ...
Read MoreSum of list with stream filter in Java
To get sum of list with stream filter in Java, the code is as follows −Exampleimport java.util.*; public class Demo { public static void main(String[] args) { List my_list = new ArrayList(); my_list.add(11); my_list.add(35); my_list.add(56); my_list.add(78); my_list.add(91); System.out.println(sum(my_list)); } public static int sum(List my_list) { System.out.println("In the main function, the sum of list with filter is "); return my_list.stream().filter(i -> i > 5).mapToInt(i -> i).sum(); } }OutputIn ...
Read MoreHow to input multiple values from user in one line in Java?
To input multiple values from user in one line, the code is as follows −Exampleimport java.util.Scanner; public class Demo { public static void main(String[] args) { System.out.print("Enter two floating point values : "); Scanner my_scan = new Scanner(System.in); double double_val = my_scan.nextFloat(); int int_val = my_scan.nextInt(); System.out.println("The floating point value is : " + double_val + " and the integer value is : " + int_val); } }Input56.789 99OutputEnter two floating point values : The floating point value is : ...
Read MoreMethod Overloading and Ambiguity in Varargs in Java
There are ambiguities while using variable arguments in Java. This happens because two methods can definitely be valid enough to be called by data values. Due to this, the compiler doesn’t have the knowledge as to which method to call.Examplepublic class Demo { static void my_fun(double ... my_Val){ System.out.print("fun(double ...): " + "Number of args: " + my_Val.length ); for(double x : my_Val) System.out.print(x + " "); System.out.println(); } static void my_fun(boolean ... my_Val){ System.out.print("fun(boolean ...) " + "The number of arguments: ...
Read MoreSearch a string in Matrix Using Split function in Java
To search a string in Matrix using split function, the code is as follows −Exampleimport java.util.*; public class Demo { public static int search_string(String[] my_matrix, String search_string){ for (String input : my_matrix){ String[] my_value = input.split(search_string); if (my_value.length >= 2 || my_value.length == 0){ return 1; } else if (my_value.length == 1 && input.length() != my_value[0].length()){ return 1; } } ...
Read More