Skip to main content

Posts

Showing posts with the label String

Java : How to convert String to Date?

 Introduction In this post, we will learn how to convert Java String to Date  Example: import java.util.Date; public class DateConversion { public static void main(String[] args) {      SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");      try { Date date = sdf.parse("19-09-2020"); System.out.println(date);      } catch (Exception ex) { ex.printStackTrace();      } } } In Java 8 and onward versions, we can use the LocalDate and DateTimeFormatter class to convert a String to a LocalDate object. We will use the parse method of the LocalDate class to perform this conversion Example (using Java 8+ versions) import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class DateConversion { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); try { LocalDate date = LocalDate.parse("19-09-2020",formatter); System.out.println(da

Java - How to convert an array of bytes i.e. byte[] to String ?

Introduction In this post, we will explore how to convert an array of bytes i.e. byte[] to String in Java. For this functionality, we will use the following constructor of java.lang.String class String (byte[] bytes,  String  enc) Example import java.nio.charset.StandardCharsets; public class ByteArrayToString { public static void main(String[] args) { byte[] bytes = "a quick fox jumps over the lazy dog".getBytes(); // convert the bytes to String System.out.println("bytes = " + bytes); System.out.println( "bytes.toString() = " + bytes.toString()); String myString = new String ( bytes , StandardCharsets.UTF_8); System.out.println("myString = " + myString); } } Output bytes = [B@41a4555e bytes.toString() = [B@41a4555e myString = a quick fox jumps over the lazy dog Conclusion In this post, we learned how to convert an array of bytes to String.

Java - How to convert String to InputStream?

Introduction In this post, we will learn how to convert String to InputStream in Java. Example import java.io.ByteArrayInputStream; import java.io.InputStream; public class StringToInputStream { public static void main(String[] args) { String text = "A quick fox jumps over a lazy dog"; try ( InputStream inputStream = convertToInputStream(text) ) { //TODO you business logic goes here } catch (Exception e) { e.printStackTrace(); } } private static InputStream convertToInputStream(String text) throws Exception { return new ByteArrayInputStream(text.getBytes()); } } Conclusion This is the simplest and shortest way to convert a String to InputStream in Java.

Java - How to convert InputStream to String?

Introduction In this tutorial, we will learn how to convert InputStream to String in Java. Example import java.io.InputStream; import java.net.URL; public class InputStreamToString { public static void main(String[] args) { //very frequent asked question in interview , regarding closable interface try(InputStream inputStream = new URL("https://www.oracle.com/index.html").openStream()){ System.out.println(convertToString(inputStream)); }catch (Exception e) { // TODO: handle exception } } private static String convertToString(InputStream inputStream) throws Exception{ return new String(inputStream.readAllBytes()); } } Conclusion Above example is the simplest method to convert an Inpustream to String in Java. There are alternate solutions available like using commonio jar and converting InputStream to ByteStream etc. Hope you enjoy this tutorial. Happy learning 

Java - How to escape % in String.format?

Introduction In this tutorial, we will learn how to skip or display % in a String using String.format MyStringFormatter .java  public class MyStringFormatter { public static void main(String[] args) { System.out.println(String.format( "Your score is %d % " ,65) ); } } Output Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = ' ' at java.base/java.util.Formatter.checkText(Formatter.java:2732) at java.base/java.util.Formatter.parse(Formatter.java:2718) at java.base/java.util.Formatter.format(Formatter.java:2655) at java.base/java.util.Formatter.format(Formatter.java:2609) at java.base/java.lang.String.format(String.java:2897) at MyStringFormatter.main(MyStringFormatter.java:4) Solution To display a % in String when using String.format , you have to escape a % with % i.e. %% public class MyStringFormatter { public static void main(String[] args) { System.out.println(String.fo

How to convert a String to int in Java?

Introduction In this post, we will learn to convert String to int primitive data type. Java provides the Integer wrapper class to convert a String to int. Integer.parseInt(String) String numberString = "134"; int number = Integer.parseInt(numberString ); System.out.println(number); Output : 134 Integer.valueOf(String) String numberString = "134"; int number = Integer.valueOf(numberString ); System.out.println(number); Output : 134 The following code will throw NumberFormatException //134a cannot be converted to Integer String numberString = "134a"; int number = Integer.valueOf(numberString ); System.out.println(number); Exception in thread "main" java.lang.NumberFormatException: For input string: "134a" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.valueOf(Unknown Source) at StringExample.main(StringExample

Top 10 mostly asked Java String Interview Questions

Introduction String is the most widely used class in any language. In this post, we will explore String in Java context. This post will help you to understand the String in-depth and to answer String related questions in a Job interview. 1. What is String? String is a final class in Java and not a primitive data type like int, float, etc. It is defined in java.lang package and therefore available by default. 2. How to create objects of String? There are two ways to create String a. String literals String str = "abc"; b. Using new operator String str = new String("abc"); 3. Where is String stored in memory? String literals are stored in the String constant pool. A string is called immutable or constant i.e. once created then it can not be changed. Whenever we create a String using double quotes (String literal), Java looks into the String constant pool if an object with the same value is present, it returns the reference to that object