Java Articles - Page 100 of 440
331 Views
In general, 10 packages are imported using the JShell.The following command shows the packages that were imported by default.jshell> /importOutputimport java.io.* import java.math.* import java.net.* import java.nio.file* import java.sql.* import java.util.* import java.util.regex* import java.util.function* import java.util.prefs* import java.util.stream.*Importing a specific package using JShelljshell> import java.util.*;
1K+ Views
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.Example Live Demopublic 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 ... Read More
14K+ Views
To input multiple values from user in one line, the code is as follows −Example Live Demoimport 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 More
299 Views
While working with Kotlin, Java libraries and frameworks can be used. This can be done with the help of advanced frameworks, without having to change the whole project. Java and Kotlin co-exist and can be present in the same project.Kotlin code can be placed inside Android Studio, without making the whole project in Kotlin.It is an open source platform that also helps in quick development of projects. It is easy, and readable, and considerably requires lesser coding in comparison to Java.Kotlin was developed by JetBrains, and IntelliJ is the IDE which Android Studio also uses.Kotlin plugin can be installed and ... Read More
1K+ Views
To get sum of list with stream filter in Java, the code is as follows −Example Live Demoimport 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(); } ... Read More
434 Views
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 More
5K+ Views
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 More
531 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
229 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
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP