Skip to main content

How to convert String into primitive data types and primitive data types into String


#Topic-7

Previous topic (6) we have discussed about Wrapper class, auto-boxing, UN-boxing and other related topics. If you have not gone through that topic, click this link Wrapper class, auto-boxing, UN-boxing in Java?


In this section we will discuss about How to convert String into primitive data types and primitive data types into String.

How to convert String into primitive data type:
Note: value should be compatible to destination type (we can’t convert “java” into int) 
//1st way
Example of convert String to Integer: 
String str1 = "5";
int result = Integer.parseInt(str1); // Using Integer.parsrInt()
System.out.println(result);

Example of convert String to Double:
String str=”125”;
double a1=Double.parseDouble(str);

System.out.println(a1); 

Example of convert String to Boolean:
String a=”true”;
boolean a=Boolean.parseBoolean(a); 

System.out.println(a);  

Example of convert String to Char array:
String a=”java”;
char[] ch=a.toCharArray(); 

//2nd way
Example of convert String to Integer: 
String str2 = "5";
Integer result2 = Integer.valueOf(str2); // Using Integer.valueOf()
System.out.println(result2);


Example of convert String to Double:
String str=”125”;
double a1=Double.valueOf(a1);

System.out.println(a1); 

Example of convert  String to Boolean:
String a=”true”;
boolean a=Boolean.valueOf(a); 

System.out.println(a); 

How to convert primitive data type into String:
//1st way

Example of convert Integer to String:
int x = 5;

String str = Integer.toString(x);  // using Integer.toString()
System.out.println(str); 

Example of convert Double to String:
double d=150.25;
String s1=Double.toString(d);

System.out.println(s1);

//2nd way
Example of convert Integer to String:
int x = 5;

String str2 = String.valueOf(x);    // using String.valueOf()
System.out.println(str2); 

Example of convert Double to String:
double d=150.25;
String s1=String.valueOf(d);

System.out.println(s1); 


What is Next?
In the next section, we will be going through Class , constructor ans this keyword in Java

If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter or Facebook. Thank you! - Shivananda Rai

Comments

  1. Very informative

    ReplyDelete
  2. Thank you for spreading knowledge...tat so structured ...

    ReplyDelete

Post a Comment

Popular posts from this blog

What is the software’s needed to start JAVA programming

#Topic-2 Last week we have discussed about some basic topics like how to start java , what is the use etc , If you are very new to Java programming then click this link Java Tutorials for Beginners. What is the software’s needed to start JAVA programming. If you are new to Java programming and wish to learn it right now by doing some hands-on practice, you have visited the right place. This tutorial will help you what is the software’s needed to write your first Java program, Throughout this tutorial, you will learn fundamental concepts and steps which are necessary for every Java fresher.  In order to write and run a Java program, you need below software / editor: 1. Java Development Kit (JDK) 2. Command prompt 3. Note pad OR 4.IDEs : These IDEs offer a variety of features, like: building Java applications, Testing, debugging, code inspections, code assistance, visual GUI builder and code editor, and more. While you can find several Java IDEs or integrated developmen...

How to Install Eclipse and Get Started with Java Programming

  #Topic-3 How to Install Eclipse and Get Started with Java Programming. Last topic we have discussed about Java Development Kit (JDK) tool and how to write, compile & run (execute) a java program using notepad and command prompt.If you not go through that ,click this link What is the software’s needed to start JAVA programming . So let’s see How to Install Eclipse and Get Started with Java Programming.   What is Eclipse? Eclipse is famous for Java Integrated Development Environment (IDE), Eclipse is an open-source Integrated Development Environment (IDE). Eclipse is popular for Java application development (Java SE and Java EE) and Android apps. It also supports C/C++, PHP, Python, Perl, and other web project developments via extensible plug-ins. Eclipse is cross-platform and runs under Windows, Linux and Mac OS. The Java Development Tools (JDT) project provides a plug-in that allows Eclipse to be used as a Java IDE, PyDev is a plugin that allows Ecli...

Wrapper class ,auto boxing and unboxing in Java

#Topic-6 Previous topic (5) we have discussed about Variables and Data Types in Java, we have gone through various data types and literals. If you have not gone through that topic, click this link What are the Variables and Data types using in Java? In this section we will discuss about Wrapper class, auto-boxing, UN-boxing and many more! Wrapper class in Java Each of Java's eight primitive data types has a class dedicated to it. These are known as wrapper classes because they "wrap" the primitive data type into an object of that class. The wrapper classes are part of the java.lang package, which is imported by default into all Java programs.  Wrapper class in java are the Object representation of eight primitive types in java. All the wrapper classes in java are  immutable and final. Java  auto boxing and UN-boxing allows easy conversion between primitive types and their corresponding wrapper classes in java programs.All the wrapper classes (Integer, ...