0% found this document useful (0 votes)
8 views

Chapter 2 - Java Conversion

The document discusses various ways to convert between Java data types like String, int, long, and Date. It provides examples and explanations of commonly used methods like Integer.parseInt(), String.valueOf(), SimpleDateFormat.parse() for converting between these types in Java. Key points covered include converting String to int, long, Date and vice versa using methods from classes like Integer, Long, DateFormat, SimpleDateFormat.

Uploaded by

coyol91896
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Chapter 2 - Java Conversion

The document discusses various ways to convert between Java data types like String, int, long, and Date. It provides examples and explanations of commonly used methods like Integer.parseInt(), String.valueOf(), SimpleDateFormat.parse() for converting between these types in Java. Key points covered include converting String to int, long, Date and vice versa using methods from classes like Integer, Long, DateFormat, SimpleDateFormat.

Uploaded by

coyol91896
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Chapter 2: Java Conversion

Java Convert String to int


convert String to an int in java using Integer.parseInt() method. To convert String into
Integer, we can use Integer.valueOf() method which returns instance of Integer class.

It is used to perform mathematical operations on the string which contains a number.


Whenever we receive data from TextField or TextArea, entered data is received as a
string. If entered data is in number format, we need to convert the string to an int. To do
so, we use Integer.parseInt() method.

Signature

The parseInt() is the static method of Integer class. The signature of parseInt() method is given
below:

1. public static int parseInt(String s)

Java String to int Example: Integer.parseInt()

int i=Integer.parseInt("200");

Let's see the simple example of converting String to int in Java.

//Java Program to demonstrate the conversion of String into int


//using Integer.parseInt() method
public class StringToIntExample1{
public static void main(String args[]){
//Declaring String variable
String s="200";
//Converting String into int using Integer.parseInt()
int i=Integer.parseInt(s);
//Printing value of i
System.out.println(i);
}}
Output:

200

Understanding String Concatenation Operator

//Java Program to understand the working of string concatenation operator


public class StringToIntExample{
public static void main(String args[]){
//Declaring String variable
String s="200";
//Converting String into int using Integer.parseInt()
int i=Integer.parseInt(s);
System.out.println(s+100);//200100, because "200"+100, here + is a string concatenation operator
System.out.println(i+100);//300, because 200+100, here + is a binary plus operator
}}

1. Output:

2. 200100
3. 300

 Java Convert int to String


We can convert int to String in java using String.valueOf() and Integer.toString() methods.
Alternatively, we can use String.format() method, string concatenation operator etc.

1) String.valueOf()

The String.valueOf() method converts int to String. The valueOf() is the static method of String
class. The signature of valueOf() method is given below:

1. public static String valueOf(int i)

Java int to String Example using String.valueOf()

Let's see the simple code to convert int to String in java.


int i=10;
String s=String.valueOf(i);//Now it will return "10"

Let's see the simple example of converting String to int in java.

public class IntToStringExample1{


public static void main(String args[]){
int i=200;
String s=String.valueOf(i);
System.out.println(i+100);//300 because + is binary plus operator
System.out.println(s+100);//200100 because + is string concatenation operator
}}

Output:

300
200100

2) Integer.toString()

The Integer.toString() method converts int to String. The toString() is the static method of
Integer class. The signature of toString() method is given below:

public static String toString(int i)

Java int to String Example using Integer.toString()

Let's see the simple code to convert int to String in java using Integer.toString() method.

int i=10;
String s=Integer.toString(i);//Now it will return "10"

Let's see the simple example of converting String to int in java.

public class IntToStringExample2{


public static void main(String args[]){
int i=200;
String s=Integer.toString(i);
System.out.println(i+100);//300 because + is binary plus operator
System.out.println(s+100);//200100 because + is string concatenation operator
}}

Output:

300
200100

3) String.format()

The String.format() method is used to format given arguments into String. It is introduced since
Jdk 1.5.

1. public static String format(String format, Object... args)

Java int to String Example using String.format()


public class IntToStringExample3{
public static void main(String args[]){
int i=200;
String s=String.format("%d",i);
System.out.println(s);
}}

Output:

200
 Java String to long
We can convert String to long in java using Long.parseLong() method.

Signature

The parseLong() is the static method of Long class. The signature of parseLong() method is
given below:

1. public static long parseLong(String s)

Java String to long Example


long l=Long.parseLong("200");
public class StringToLongExample
{
public static void main(String args[])
{
String s="9990449935";
long l=Long.parseLong(s);
System.out.println(l);
}}

Output:

9990449935

 Java long to String

We can convert long to String in java using String.valueOf() and Long.toString() methods.

It is used if we have to display long number in textfield in GUI application because everything is
displayed as a string in form.
1) String.valueOf()

The String.valueOf() is an overloaded method. It can be used to convert long to String. The
valueOf() is the static method of String class. The signature of valueOf() method is given below:

1. public static String valueOf(long i)

Java long to String Example using String.valueOf()


long i=9993939399L;//L is the suffix for long
String s=String.valueOf(i);//Now it will return "9993939399"

public class LongToStringExample1


{
public static void main(String args[])
{
long i=9993939399L;
String s=String.valueOf(i);
System.out.println(s);
}}

Output:

9993939399

2) Long.toString()

The Long.toString() method converts long to String. The toString() is the static method of Long
class. The signature of toString() method is given below:

1. public static String toString(long i)

Java long to String Example using Long.toString()


long i=9993939399L;
String s=Long.toString(i);//Now it will return "9993939399"

public class LongToStringExample2{


public static void main(String args[]){
long i=9993939399L;
String s=Long.toString(i);
System.out.println(s);
}}

Output:

9993939399

Java String to Date

We can convert String to Date in java using parse() method of DateFormat and
SimpleDateFormat classes.

java String to Date Example

import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample1 {
public static void main(String[] args)throws Exception {
String sDate1="31/12/1998";
Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);
System.out.println(sDate1+"\t"+date1);
}
}

Output:

31/12/1998 Thu Dec 31 00:00:00 IST 1998


import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateExample2 {
public static void main(String[] args)throws Exception {
String sDate1="31/12/1998";
String sDate2 = "31-Dec-1998";
String sDate3 = "12 31, 1998";
String sDate4 = "Thu, Dec 31 1998";
String sDate5 = "Thu, Dec 31 1998 23:37:50";
String sDate6 = "31-Dec-1998 23:37:50";
SimpleDateFormat formatter1=new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat formatter2=new SimpleDateFormat("dd-MMM-yyyy");
SimpleDateFormat formatter3=new SimpleDateFormat("MM dd, yyyy");
SimpleDateFormat formatter4=new SimpleDateFormat("E, MMM dd yyyy");
SimpleDateFormat formatter5=new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");
SimpleDateFormat formatter6=new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
Date date1=formatter1.parse(sDate1);
Date date2=formatter2.parse(sDate2);
Date date3=formatter3.parse(sDate3);
Date date4=formatter4.parse(sDate4);
Date date5=formatter5.parse(sDate5);
Date date6=formatter6.parse(sDate6);
System.out.println(sDate1+"\t"+date1);
System.out.println(sDate2+"\t"+date2);
System.out.println(sDate3+"\t"+date3);
System.out.println(sDate4+"\t"+date4);
System.out.println(sDate5+"\t"+date5);
System.out.println(sDate6+"\t"+date6);
}
}

Output:
31/12/1998 Thu Dec 31 00:00:00 IST 1998
31-Dec-1998 Thu Dec 31 00:00:00 IST 1998
12 31, 1998 Thu Dec 31 00:00:00 IST 1998
Thu, Dec 31 1998 Thu Dec 31 00:00:00 IST 1998
Thu, Dec 31 1998 23:37:50 Thu Dec 31 23:37:50 IST 1998
31-Dec-1998 23:37:50 Thu Dec 31 23:37:50 IST 1998

 Java Convert Date to String

convert Date to String in java using format() method of java.text.DateFormat class.

format() method of DateFormat

The format() method of DateFormat class is used to convert Date into String. DateFormat is
an abstract class. The child class of DateFormat is SimpleDateFormat. It is the
implementation of DateFormat class. The signature of format() method is given below:

String format(Date d)

Java Date to String Example

Let's see the simple code to convert Date to String in java.

Date date = Calendar.getInstance().getTime();


DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
String strDate = dateFormat.format(date);

Example:596Java Try Catch

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
public class DateToStringExample1 {
public static void main(String args[])
{
Date date = Calendar.getInstance().getTime();
DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
String strDate = dateFormat.format(date);
System.out.println("Converted String: " + strDate);

}
}

Output:

Converted String: 2017-24-28 04:24:27


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class DateToStringExample2 {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
String strDate = formatter.format(date);
System.out.println("Date Format with MM/dd/yyyy : "+strDate);

formatter = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");


strDate = formatter.format(date);
System.out.println("Date Format with dd-M-yyyy hh:mm:ss : "+strDate);

formatter = new SimpleDateFormat("dd MMMM yyyy");


strDate = formatter.format(date);
System.out.println("Date Format with dd MMMM yyyy : "+strDate);

formatter = new SimpleDateFormat("dd MMMM yyyy zzzz");


strDate = formatter.format(date);
System.out.println("Date Format with dd MMMM yyyy zzzz : "+strDate);
formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
strDate = formatter.format(date);
System.out.println("Date Format with E, dd MMM yyyy HH:mm:ss z : "+strDate);
}
}

Output:

Date Format with MM/dd/yyyy : 04/13/2015


Date Format with dd-M-yyyy hh:mm:ss : 13-4-2015 10:59:26
Date Format with dd MMMM yyyy : 13 April 2015
Date Format with dd MMMM yyyy zzzz : 13 April 2015 India Standard Time
Date Format with E, dd MMM yyyy HH:mm:ss z : Mon, 13 Apr 2015 22:59:26 IST

 Java Convert String to char

convert String to char in java using charAt() method of String class.

The charAt() method returns a single character only. To get all characters, you can use loop.

Signature

The charAt() method returns a single character of specified index. The signature of charAt()
method is given below:

1. public char charAt(int index)

Java String to char Example: charAt() method


String s="hello";
char c=s.charAt(2);//returns l

Let's see the simple example of converting String to char in java.

public class StringToCharExample1{


public static void main(String args[]){
String s="hello";
char c=s.charAt(0);//returns h
System.out.println("1st character is: "+c);
}}

Output:

1st character is: h


public class StringToCharExample2{
public static void main(String args[]){
String s="hello";
for(int i=0; i<s.length();i++){
char c = s.charAt(i);
System.out.println("char at "+i+" index is: "+c);
}
}}

Output:

char at 0 index is: h


char at 1 index is: e
char at 2 index is: l
char at 3 index is: l
char at 4 index is: o

Java String to char Example: toCharArray() method

public class StringToCharExample3{


public static void main(String args[]){
String s1="hello";
char[] ch=s1.toCharArray();
for(int i=0;i<ch.length;i++){
System.out.println("char at "+i+" index is: "+ch[i]);
}
}}

Output:

char at 0 index is: h


char at 1 index is: e
char at 2 index is: l
char at 3 index is: l
char at 4 index is: o

Java String to char Example: toCharArray() method

public class StringToCharExample3{


public static void main(String args[]){
String s1="hello";
char[] ch=s1.toCharArray();
for(int i=0;i<ch.length;i++){
System.out.println("char at "+i+" index is: "+ch[i]);
}
}}

Output:

char at 0 index is: h


char at 1 index is: e
char at 2 index is: l
char at 3 index is: l
char at 4 index is: o
 Java Convert char to String

We can convert char to String in java using String.valueOf(char) method of String class
and Character.toString(char) method of Character class.

Java char to String Example: String.valueOf() method

char c='S';

String s=String.valueOf(c);
public class CharToStringExample1{
public static void main(String args[]){
char c='S';
String s=String.valueOf(c);
System.out.println("String is: "+s);
}}

Output:.

String is: S

Java char to String Example: Character.toString() method


public class CharToStringExample2{
public static void main(String args[]){
char c='M';
String s=Character.toString(c);
System.out.println("String is: "+s);
}}

 Output:

String is: M

 Java Convert String to Object

We can convert String to Object in java with assignment operator. Each class is internally a
child class of Object class. So you can assign string to Object directly.

You can also convert String to Class type object using Class.forName() method.

Java String to Object Example

String s="hello";
Object obj=s;

public class StringToObjectExample{


public static void main(String args[]){
String s="hello";
Object obj=s;
System.out.println(obj);
}}

Output:

hello

 Java String to Class object Example

code to convert String to Class object in java using Class.forName() method. The
Class.forName() method returns the instance of Class class which can be used to get the
metadata of any class.

public class StringToObjectExample2{


public static void main(String args[])throws Exception{
Class c=Class.forName("java.lang.String");
System.out.println("class name: "+c.getName());
System.out.println("super class name: "+c.getSuperclass().getName());
}}

Output:

Class name: java.lang.String


Super class name: java.lang.Object

 Java Convert Object to String

We can convert Object to String in java using toString() method of Object class or
String.valueOf(object) method.

convert any object to String in java whether it is user-defined class, StringBuilder, StringBuffer
or anything else.
Here, we are going to see two examples of converting Object into String. In the first example, we
are going to convert Emp class object into String which is an user-defined class. In second
example, we are going to convert StringBuilder to String.

Java Object to String Example: Converting User-defined class


class Emp{}
public class ObjectToStringExample{
public static void main(String args[]){
Emp e=new Emp();
String s=e.toString();
String s2=String.valueOf(e);
System.out.println(s);
System.out.println(s2);
}}

Output:

Emp@2a139a55
Emp@2a139a55

Java Object to String Example: Converting StringBuilder

code to convert StringBuilder object to String in java.

public class ObjectToStringExample2{


public static void main(String args[]){
String s="hello";
StringBuilder sb=new StringBuilder(s);
sb.reverse();
String rev=sb.toString();//converting StringBuilder to String
System.out.println("String is: "+s);
System.out.println("Reverse String is: "+rev);
}}

Output:

String is: hello


Reverse String is: olleh

So, you can convert any Object to String in java using toString() or String.valueOf(object)
methods.
 Java Convert int to long
We can convert int to long in java using assignment operator. There is nothing to do
extra because lower type can be converted to higher type implicitly.

It is also known as implicit type casting or type promotion.

Java int to long Example


public class IntToLongExample1
{
public static void main(String args[])
{
int i=200;
long l=i;
System.out.println(l);
}}

Output:

200

Java int to Long Example

We can convert int value to Long object by instantiating Long class or calling Long.valueOf()
method.

public class IntToLongExample2{


public static void main(String args[]){
int i=100;
Long l= new Long(i);//first way
Long l2=Long.valueOf(i);//second way
System.out.println(l);
System.out.println(l2);
}}

Output:

100
100

 Java Convert long to int

We can convert long to int in java using typecasting. To convert higher data type into lower, we
need to perform typecasting.

Typecasting in java is performed through typecast operator (datatype).

Here, we are going to learn how to convert long primitive type into int and Long object into int.

Java long to int Example


public class LongToIntExample1{
public static void main(String args[]){
long l=500;
int i=(int)l;
System.out.println(i);
}}

Output:

500

Java Long to int Example

We can convert Long object to int by intValue() method of Long class.


public class LongToIntExample2{

public static void main(String args[]){


Long l= new Long(10);
int i=l.intValue();
System.out.println(i);
}}

Output:

10

 Java Convert char to int

We can convert char to int in java using various ways. If we direct assign char variable to int, it
will return ASCII value of given character.

If char variable contains int value, we can get the int value by
calling Character.getNumericValue(char) method. Alternatively, we can use
String.valueOf(char) method.

1) Java char to int Example: Get ASCII value


public class CharToIntExample1{
public static void main(String args[]){
char c='a';
char c2='1';
int a=c;
int b=c2;
System.out.println(a);
System.out.println(b);
}}

Output:

97
49

2) Java char to int Example: Character.getNumericValue()

code to convert char to int in java using Character.getNumericValue(char) method which returns
an integer value.

public class CharToIntExample2{


public static void main(String args[]){
char c='1';
int a=Character.getNumericValue(c);
System.out.println(a);
}}

Output:

3) Java char to int Example: String.valueOf()

another example which returns integer value of specified char value using String.valueOf(char)
method.

public class CharToIntExample3{


public static void main(String args[]){
char c='1';
int a=Integer.parseInt(String.valueOf(c));
System.out.println(a);
}}
Output:

 Java Convert int to char

convert int to char in java using typecasting. To convert higher data type into lower, we need to
perform typecasting. Here, the ASCII character of integer value will be stored in the char
variable.

To get the actual value in char variable, you can add '0' with int variable. Alternatively, you can
use Character.forDigit() method.

Java int to char Example: Typecasting

public class IntToCharExample1{


public static void main(String args[]){
int a=65;
char c=(char)a;
System.out.println(a);
}}

Output:

But if you store 1, it will store ASCII character of given number which is start of heading which
is not printable. So it will not print anything on the console.
public class IntToCharExample2{
public static void main(String args[]){
int a=1;
char c=(char)a;
System.out.println(c);
}}

Output:

If you add '0' with int variable, it will return actual value in the char variable. The ASCII value of
'0' is 48. So, if you add 1 with 48, it becomes 49 which is equal to 1. The ASCII character of 49
is 1.

public class IntToCharExample3{


public static void main(String args[]){
int a=1;
char c=(char)(a+'0');
System.out.println(c);
}}

Output:

If you store integer value in a single quote, it will store actual character in char variable.

public class IntToCharExample4{


public static void main(String args[]){
int a='1';
char c=(char)a;
System.out.println(c);
}}

Output:

1
Java int to char Example: Character.forDigit()

To get the actual value, you can also use Character.forDigit() method.

public class IntToCharExample5{


public static void main(String args[]){
int REDIX=10;//redix 10 is for decimal number, for hexa use redix 16
int a=1;
char c=Character.forDigit(a,REDIX);
System.out.println(c);
}}

 Java Convert String to boolean

convert String to boolean in java using Boolean.parseBoolean(string) method.

To convert String into Boolean object, we can use Boolean.valueOf(string) method which returns
instance of Boolean class.

To get boolean true, string must contain "true". Here, case is ignored. So, "true" or "TRUE" will
return boolean true. Any other string value except "true" returns boolean false.

Java String to boolean Example: Boolean.parseBoolean()

The parseBoolean() method converts string into boolean primitive.

The parseBoolean() is the static method of Boolean class. The signature of parseBoolean()
method is given below:

1. public static int parseBoolean(String s)

public class StringToBooleanExample{


public static void main(String args[]){
String s1="true";
String s2="TRue";
String s3="ok";
boolean b1=Boolean.parseBoolean(s1);
boolean b2=Boolean.parseBoolean(s2);
boolean b3=Boolean.parseBoolean(s3);
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
}}

Output:

true
true
false
The Boolean.valueOf() method converts string into Boolean object. Let's see the simple code to
convert String to Boolean in java.

 Java Convert boolean to String

We can convert boolean to String in java using String.valueOf(boolean) method.

Alternatively, we can use Boolean.toString(boolean) method which also converts boolean into
String.

1) String.valueOf()

The String.valueOf() method converts boolean to String. The valueOf() is the static method of
String class. The signature of valueOf() method is given below:

1. public static String valueOf(boolean b)

Java boolean to String Example using String.valueOf()

Let's see the simple example of converting boolean to String in java.

public class BooleanToStringExample1{


public static void main(String args[]){
boolean b1=true;
boolean b2=false;
String s1=String.valueOf(b1);
String s2=String.valueOf(b2);
System.out.println(s1);
System.out.println(s2);
}}

Output:

true
false

2) Boolean.toString()

The Boolean.toString() method converts boolean to String. The toString() is the static method of
Boolean class. The signature of toString() method is given below:

1. public static String toString(boolean b)

Java Convert Date to Timestamp

We can convert Date to Timestamp in java using constructor of java.sql.Timestamp class.

The constructor of Timestamp class receives long value as an argument. So you need to convert
date into long value using getTime() method of java.util.Date class.

You can also format the output of Timestamp using java.text.SimpleDateFormat class.

Constructor of Timestamp class:

1. Timestamp(long l)

getTime() method of Date class:

1. public long getTime()


Java Date to Timestamp Example
import java.sql.Timestamp;
import java.util.Date;
public class DateToTimestampExample1 {
public static void main(String args[]){
Date date = new Date();
Timestamp ts=new Timestamp(date.getTime());
System.out.println(ts);
}
}

Output:

2017-11-02 01:59:30.274

 Java Convert Timestamp to Date

We can convert Timestamp to Date in java using constructor of java.util.Date class.

The constructor of Date class receives long value as an argument. So, you need to convert
Timestamp object into long value using getTime() method of java.sql.Timestamp class.

the constructor of Date class and signature of getTime() method.

1. Date(long l)

getTime() method of Timestamp class:

1. public long getTime()

Java Timestamp to Date Example

the simple example to convert Timestamp to Date in java.

import java.sql.Timestamp;
import java.util.Date;
public class TimestampToDateExample1 {
public static void main(String args[]){
Timestamp ts=new Timestamp(System.currentTimeMillis());
Date date=new Date(ts.getTime());
System.out.println(date);
}
}

Output:

Thu Nov 02 02:29:07 IST 2017

 Java Convert Binary to Decimal

We can convert binary to decimal in java using Integer.parseInt() method or custom logic.

Java Binary to Decimal conversion: Integer.parseInt()

The Integer.parseInt() method converts string to int with given redix. The signature of parseInt()
method is given below:

1. public static int parseInt(String s,int redix)

public class BinaryToDecimalExample1{


public static void main(String args[]){
String binaryString="1010";
int decimal=Integer.parseInt(binaryString,2);
System.out.println(decimal);
}}

Output:

10
public class BinaryToDecimalExample2{
public static void main(String args[]){
System.out.println(Integer.parseInt("1010",2));
System.out.println(Integer.parseInt("10101",2));
System.out.println(Integer.parseInt("11111",2));
}}

Output:

10
21
31
 Java Convert Decimal to Binary

We can convert decimal to binary in java using Integer.toBinaryString() method or custom


logic.

Java Decimal to Binary conversion: Integer.toBinaryString()

The Integer.toBinaryString() method converts decimal to binary string. The signature of


toBinaryString() method is given below:

1. public static String toBinaryString(int decimal)

public class DecimalToBinaryExample1{


public static void main(String args[]){
System.out.println(Integer.toBinaryString(10));
System.out.println(Integer.toBinaryString(21));
System.out.println(Integer.toBinaryString(31));
}}

Output:

1010
10101
11111

 Java Convert Hexadecimal to Decimal

We can convert hexadecimal to decimal in java using Integer.parseInt() method or custom


logic.
Java Hexadecimal to Decimal conversion: Integer.parseInt()

The Integer.parseInt() method converts string to int with given redix. The signature of parseInt()
method is given below:

1. public static int parseInt(String s,int redix)


the simple example of converting hexadecimal to decimal in java.
public class HexToDecimalExample1{
public static void main(String args[]){
String hex="a";
int decimal=Integer.parseInt(hex,16);
System.out.println(decimal);
}}

Output:

10

another example of Integer.parseInt() method.

public class HexToDecimalExample2{


public static void main(String args[]){
System.out.println(Integer.parseInt("a",16));
System.out.println(Integer.parseInt("f",16));
System.out.println(Integer.parseInt("121",16));
}}

Output:

10
15
289

 Java Convert Decimal to Hexadecimal

We can convert decimal to hexadecimal in java using Integer.toHexString() method or custom


logic.
Java Decimal to Hex conversion: Integer.toHexString()

The Integer.toHexString() method converts decimal to hexadecimal. The signature of


toHexString() method is given below:

1. public static String toHexString(int decimal)

example of converting decimal to binary in java.

public class DecimalToHexExample1{


public static void main(String args[]){
System.out.println(Integer.toHexString(10));
System.out.println(Integer.toHexString(15));
System.out.println(Integer.toHexString(289));
}}

Output:.

a
f
121

 Java Convert Octal to Decimal

convert octal to decimal in java using Integer.parseInt() method or custom logic.

Java Octal to Decimal conversion: Integer.parseInt()

The Integer.parseInt() method converts a string to an int with the given radix. If you pass 8 as a
radix, it converts an octal string into decimal. Let us see the signature of parseInt() method:

1. public static int parseInt(String s,int radix)

example of converting octal to decimal in java.

//Java Program to demonstrate the use of Integer.parseInt() method


//for converting Octal to Decimal number
public class OctalToDecimalExample1{
public static void main(String args[]){
//Declaring an octal number
String octalString="121";
//Converting octal number into decimal
int decimal=Integer.parseInt(octalString,8);
//Printing converted decimal number
System.out.println(decimal);
}}

Output:

81
another examp//Shorthand example of Integer.parseInt() method
public class OctalToDecimalExample2{
public static void main(String args[]){
System.out.println(Integer.parseInt("121",8));
System.out.println(Integer.parseInt("23",8));
System.out.println(Integer.parseInt("10",8));
}}

Output:le of Integer.parseInt() method.

81
19
8

 Java Convert Decimal to Octal

convert decimal to octal in java using Integer.toOctalString() method or custom logic.

Java Decimal to Octal conversion: Integer.toOctalString()

The Integer.toOctalString() method converts decimal to octal string. The signature of


toOctalString() method is given below:
1. public static String toOctalString(int decimal)

example of converting decimal to octal in java.

//Java Program to demonstrate the use of Integer.toOctalString() method


public class DecimalToOctalExample1{
public static void main(String args[]){
//Using the predefined Integer.toOctalString() method
//to convert decimal value into octal
System.out.println(Integer.toOctalString(8));
System.out.println(Integer.toOctalString(19));
System.out.println(Integer.toOctalString(81));
}}

Output:

10
23
121

You might also like