- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 34494 Articles for Programming
8K+ Views
When the full name is provided, the initials of the name are printed with the last name is printed in full. An example of this is given as follows −Full name = Amy Thomas Initials with surname is = A. ThomasA program that demonstrates this is given as follows −Example Live Demoimport java.util.*; public class Example { public static void main(String[] args) { String name = "John Matthew Adams"; System.out.println("The full name is: " + name); System.out.print("Initials with surname is: "); int len = name.length(); ... Read More
167 Views
To compare Float in Java, use the following methods −Method 1 − compareTo(newFloat) method in JavaThe java.lang.Float.compareTo() method compares two Float objects. This method returns the value 0 if the new float value is numerically equal to this Float; a value less than 0 if this Float is numerically less than new float; and a value greater than 0 if this Float is numerically greater than new float.Here is an example −Example Live Demoimport java.lang.*; public class Demo { public static void main(String args[]) { Float f1 = new Float("25.2"); Float f2 = new ... Read More
555 Views
To apply modulus (%) operator to floating-point values in an effortless task. Let us see how.We have the following two values −double one = 9.7; double two = 1.2;Let us now apply the modulus operator −one % twoThe following is the complete example that displays the output as well −Example Live Demopublic class Demo { public static void main(String args[]) { double one = 9.7; double two = 1.2; System.out.println( one % two ); } }Output0.09999999999999964
101 Views
The following is the supplied byte array −byte[] b = new byte[]{'x', 'y', 'z'};We have created a custom method “display” here and passed the byte array value. The same method converts byte array to hex string −public static String display(byte[] b1){ StringBuilder strBuilder = new StringBuilder(); for(byte val : b1){ strBuilder.append(String.format("%02x", val&0xff)); } return strBuilder.toString(); }Let us see the entire example now −Example Live Demopublic class Demo { public static void main(String args[]) { byte[] b = new byte[]{'x', 'y', ... Read More
1K+ Views
DateFormat.LONG is a constant for long style pattern.Firstly, we will create date object −Date dt = new Date(); DateFormat dateFormat;Let us format date for different locale with DateFormat.LONG −// ITALY dateFormat = DateFormat.getDateInstance(DateFormat.LONG, Locale.ITALY); // CANADA dateFormat = DateFormat.getDateInstance(DateFormat. LONG, Locale.CANADA);The following is an example −Example Live Demoimport java.text.DateFormat; import java.util.Date; import java.util.Locale; public class Demo { public static void main(String args[]) { Date dt = new Date(); DateFormat dateFormat; // Date Format LONG constant ... Read More
2K+ Views
DateFormat.MEDIUM is a constant for medium style pattern.Firstly, we will create date object −Date dt = new Date(); DateFormat dateFormat;Let us format date for different locale with DateFormat.MEDIUM −// CHINESE dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CHINESE); // CANADA dateFormat = DateFormat.getDateInstance(DateFormat. MEDIUM, Locale.CANADA);The following is an example −Example Live Demoimport java.text.DateFormat; import java.util.Date; import java.util.Locale; public class Demo { public static void main(String args[]) { Date dt = new Date(); DateFormat dateFormat; // Date Format MEDIUM constant ... Read More
272 Views
To parse a number to binary, use the Integer.parseInt() method with binary as the first parameter and radix as 2 passed as a 2nd parameter.Let’s say the following is our integer −int val = 566;Now, use the Integer.parseInt() method −val = Integer.parseInt("11111111", 2);Using the Integer.toString() method will format the above value into binary −String str = Integer.toString(val, 2);The following is an example −Example Live Demopublic class Demo { public static void main(String []args){ int val = 566; val = Integer.parseInt("11111111", 2); System.out.println(val); String str = Integer.toString(val, 2); System.out.println(str); } }Output255 11111111
231 Views
Use System.nanoTime() method in Java to get the current value of the system timer in nanoseconds. It d returns the current value of the most precise available system timer, in nanoseconds.We are getting the system timer value in a long type −long res = System.nanoTime();The following is an example −Example Live Demopublic class Demo { public static void main(String[] args) { long res = System.nanoTime(); System.out.println("Current Value: " + res + " nanoseconds"); } }OutputCurrent Value: 49908709882168 nanoseconds
116 Views
Use arraycopy() method in Java to copy an array from the specified source array.Here, we have two arrays −int arr1[] = { 10, 20, 30, 40}; int arr2[] = { 3, 7, 20, 30};Now, we will use the arraycopy() method to copy the first two elements of the 1st array to the 2nd array −System.arraycopy(arr1, 0, arr2, 2, 2);The following is an example −Example Live Demoimport java.lang.*; public class Demo { public static void main(String[] args) { int arr1[] = { 10, 20, 30, 40}; int arr2[] = { 3, 7, 20, 30}; System.arraycopy(arr1, 0, arr2, 2, 2); System.out.print("New Array = "); System.out.print(arr2[0] + " "); System.out.print(arr2[1] + " "); System.out.print(arr2[2] + " "); System.out.print(arr2[3] + " "); } }OutputNew Array = 3 7 10 20
110 Views
Let’s say we need to round number to 3 decimal places, therefore use the following format −DecimalFormat decFormat = new DecimalFormat("0.000");Now, format the number −decFormat.format(37878.8989)Since we have used the DecimalFloat class above, therefore do import the following package −import java.text.DecimalFormat;The following is an example that rounds a number to 3 decimal places −Example Live Demoimport java.text.DecimalFormat; public class Demo { public static void main(String[] args) { DecimalFormat decFormat = new DecimalFormat("0.000"); System.out.println("Result = " + decFormat.format(37878.8989)); } }OutputResult = 37878.899Let us see another example that rounds a number ... Read More