![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
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 9326 Articles for Object Oriented Programming
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
389 Views
Use the Trim() method to remove leading and trailing whitespace from a string. Let’s say the following is our string with white spaces in the beginning and the end.String str = " a ";Now, use the trim() method.str.trim()The following is the final example with output.Example Live Demopublic class Demo { public static void main(String[] args) { String str = " a "; System.out.println("Text = "+str); System.out.println("Text after using trim() method = " + str.trim()); } }OutputText = a Text after using trim() method = a
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
6K+ Views
Use the + Integer.toHexString() method in Java to convert integer to hexadecimal.Let’s say the following is our integer.int val = 768;Let us convert it to a hexadecimal value.Integer.toHexString(val)The following is the final example with the output.Example Live Demopublic class Demo { public static void main(String[] args) { int val = 768; // integer System.out.println("Integer: "+val); // hex System.out.println("Hex String = " + Integer.toHexString(val)); } }OutputInteger: 768 Hex String = 300
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
1K+ Views
To convert integer to octal in Java, use the Integer.toOctalString() method.Let’s say the following is our integer.int val = 768;Convert the above integer to octal.Integer.toOctalString(val)The following is the final example.Example Live Demopublic class Demo { public static void main(String[] args) { int val = 768; // integer System.out.println("Integer: "+val); // octal System.out.println("Octal String = " + Integer.toOctalString(val)); } }OutputInteger: 768 Octal String = 1400
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
9K+ Views
To convert an integer to binary, use the Integer.toBinaryString() method in Java.Let’s say the following is the integer we want to convert.int val = 999;Converting the above integer to binary.Integer.toBinaryString(val)Example Live Demopublic class Demo { public static void main(String[] args) { int val = 999; // integer System.out.println("Integer: "+val); // binary System.out.println("Binary = " + Integer.toBinaryString(val)); } }OutputInteger: 999 Binary = 1111100111
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
384 Views
The java.text.NumberFormat class is used for formatting numbers and currencies as per a specific Locale. Number formats varies from country to countryTo format a number as percentage you need a percentage NumberFormat instance. To achieved this, use the getPercentageInstance() method.Example Live Demoimport java.text.NumberFormat; import java.util.Locale; public class MainClass { public static void main(String[] args) { // Currency of France is Euro NumberFormat n = NumberFormat.getPercentInstance(Locale.FRANCE); // points double points = 1.78; double totalPoints = points * 1000; System.out.println(n.format(points)); System.out.println(n.format(totalPoints)); } }Output178 % 178 000 %
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
3K+ Views
The getCurrencyInstance() method of the NumberFormat class returns the instance of the NumberFormat class. The java.text.NumberFormat class is used for formatting numbers and currencies as per a specific Locale. Number formats varies from country to countryHere, we have considered a locale.NumberFormat n = NumberFormat.getCurrencyInstance(Locale.FRANCE);Then, we have formatted a double value with the currency.double points = 1.78; System.out.println(n.format(points));The following is the final example.Example Live Demoimport java.text.NumberFormat; import java.util.Locale; public class MainClass { public static void main(String[] args) { // Currency of France is Euro NumberFormat n = NumberFormat.getCurrencyInstance(Locale.FRANCE); // points double points = 1.78; double totalPoints = points * 1000; System.out.println(n.format(points)); ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
384 Views
The java.text.NumberFormat class is used for formatting numbers and currencies as per a specific Locale. Number formats varies from country to country.To get the instance of the NumberFormat class, call getInstance() method in Java.First, we will use the getInstance() method.NumberFormat n = NumberFormat.getInstance();Now, we will format a double.double val = 1.9898798; String formattedVal = n.format(val);The following is the final example.Example Live Demoimport java.text.NumberFormat; public class Demo { public static void main(String args[]) { NumberFormat n = NumberFormat.getInstance(); double val = 1.9898798; System.out.println("Value: "+val); String formattedVal = n.format(val); System.out.println("Formatted Value: "+formattedVal); } }OutputValue: 1.9898798 Formatted Value: 1.99Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
193 Views
Use the Short.parseShort() method in Java to convert a String to a short.Let’s say the following is our string.String str = “76”;Now, the parseShort() method will convert the string to short.byte val = Byte.parseByte(str);Example Live Demopublic class Demo { public static void main(String[] args) { String str = "76"; short val = Short.parseShort(str); System.out.println(val); } }Output76
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
14K+ Views
To add leading zeros to a number, you need to format the output. Let’s say we need to add 4 leading zeros to the following number with 3 digits.int val = 290;For adding 4 leading zeros above, we will use %07d i.e. 4+3 = 7. Here, 3, as shown above, is the number with 3 digits.String.format("%07d", val);The following is the final example.Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { int val = 290; System.out.println("Integer: "+val); String formattedStr = String.format("%07d", val); System.out.println("With leading zeros = " + formattedStr); } }OutputInteger: 290 With leading zeros = 0000290
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
1K+ Views
You can include a precision specifier to the following format specifiers −%f %e %g %sOn floating point, the number of decimal places is known.Let’s say we declared a Formatter object −Formatter f1 = new Formatter();Now, we want 3 decimal places. For that, use 1.3f −f1.format("%1.3f", 29292929.98765432);The above will return the number with 3 decimal places −29292929.988The following is the final example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f1, f2, f3; f1 = new Formatter(); f1.format("%1.3f", 29292929.98765432); System.out.println(f1); f2 = new Formatter(); f2.format("%1.7f", 29292929.98765432); System.out.println(f2); ... Read More