Disclosure: This article may contain affiliate links. When you purchase, we may earn a commission.

21 Examples to convert Data Types in Java - Tutorial

Hello guys, converting one data type to another, for example String to Integer or String too boolean is a common task in Java and every Java programmer should be familiar with how to convert common data types like String, Integer, Long, Float, Double, Date, List, Map, Set, to each other. In the past, I have shared several tutorial where I have shown you how to carry out such conversion in Java and this articile is nothing but a collection of such tutorial so that you can learn all of that knowledge in one place. You can also bookmark this page as I will be adding new data type conversion tutorials into this list as and when I write them, and you can also suggest if you struggle to convert a particular type to another and I will try to cover them here. 


Java Date Type Conversion Tutorial

without wasting anymore of your time, here is a list of common data types and how to convert them like String to Integer, String to Boolean, String to Long, String To Float, Long to Float and vice-versa. I have also included collections and dates to make this article more complete. 


1. How to convert Map to List in Java (tutorial)
Map and List are two different types of objects, Map contains a Mapping while list contains a list of object. So in order to convert a Map to List, you can either create a List of Entries which Map has, or List of keys, or List of values. You can use methods like values() and keySet() , and entrySet() to convert a Map to list of keys, values, and entries in Java. 


2. How to convert float to String in Java? (tutorial)
Converting a float to String is easy, just concatenate float with an empty String and Java will do that for you but there are more sophisticated and performance friendly ways like using String.valueOf() and String.format() method which you will find in this tutorial. You can also use the same technique to convert an integer value to String in Java as shown below:


How to convert Integer to String in Java



3. How to convert Byte array to String in Java? (tutorial)
You can convert a byte array to String easily because String provides a constructor which accepts a byte array. Only thing to keep in mind is character encoding because same byte sequence can represent different characters in different encoding. See the tutorial for an example of how to do convert byte array to String in Java. 


4. How to convert Double to Long in Java? (tutorial)
Double and Long are two of the biggest data types, both require 32-bit of storage, I mean they take 4 bytes in memory but there are some differences. Double represent floating point number while long represent whole numbers or integral numbers. Converting Double to Long is possible because Double provides a method longValue() which gives you long values as shown in this tutorial. In short, you can use methods like longValue() to convert a Double to Long in Java. You can also use the same technique to convert double primitive type to long primitive type in Java. 


5. How to convert String to Int in Java? (example)
This is probably the most common data type conversion you need to do in Java. If you have been coding in Java, there is high chance that you have already done this many times. There are actually multiple ways to convert a String to int in Java like parseInt() and valueOf(). In this article, I have shown you 4 different ways to covert a String value to int data type in Java. 

How to convert String to Int in Java



6. How to convert Array to String in Java? (tutorial)
You can create a String from a character array so you can pass that character array to create a String. 


7. 5 Ways to convert Java 8 Stream to List? (solution)
You can use Collectors.toList() method to convert a Stream to List in Java. In fact you can use this method to convert a Stream to Set and Map also. Collectors class provides many methods to collect Stream elements into List and Set in Java. 


8. Best way to convert Numbers to String in Java? (guide)


9. How to convert binary number to decimal in Java? (tutorial)


10. How to convert java.util.Date to java.sql.Date in Java? (tutorial)


11. 5 examples to convert InputStream to String in Java? (tutorial)


12. How to convert Enum to String in Java? (guide)
You can use either name() method of Enum class to convert an ENum to String or you can define your own toString() method to convert an Enum to String in Java. 


13. How to convert Character to String in Java? (tutorial)


14. How to convert Array to Set and List in Java? (example)


15 3 examples to convert Array to ArrayList in Java (tutorial)


16. How to convert SQL date to Util Date in Java? (example)


17. How to convert ArrayList to Set in Java? (tutorial)


18. How to convert Hexadecimal numbers to octal and binary in Java? (tutorial)


19. How to convert lowercase String to uppercase in Java? (example)


20. How to convert String to Enum in Java? (tutorial)


21. How to convert List to Set in Java? (example)
The key difference between a List and set is duplicates and ordering. List allows duplicates and also keep the elements in the order they are inserted while Set doesn't allow duplicates and also don't maintain any order. Though, you can easily convert a List to Set by using the copy constructor of Collection class which accept a collection. So you can do new HashSet() and pass a List to the constructor to create a Set out of it. 


That's all about how to convert data types like String, Integer, and Date in Java. As I said, this is part of Java fundamentals and every Java programmer, both beginner and experienced should be aware of this. I hope you liked this tutorial but if you have any doubt, feel free to ask in questions, I would be happy to answer any question you may have. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.