Java Articles

Page 81 of 450

Java Program to convert decimal integer to hexadecimal number

Samual Sam
Samual Sam
Updated on 11-Mar-2026 475 Views

Use the toHexString() method to convert decimal to hexadecimal. The method returns a string representation of the integer argument as an unsigned integer in base 16. The following characters are used as hexadecimal digits:0123456789abcdef.The following is the syntax.String toHexString(int i)It has only single parameter.i − This is an integer to be converted to a string.Examplepublic class Demo {    public static void main( String args[] ) {       int dec = 45;       System.out.println("Decimal = "+dec);       // converting to hex       System.out.println(Integer.toHexString(dec));    } }OutputDecimal = 45 2d

Read More

How to convert Decimal to Hexadecimal in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 6K+ Views

To convert decimal to hexadecimal, use any of the two methods i.e.Integer.toHexString() − It returns a string representation of the integer argument as an unsigned integer in base 16.Integer.parseInt() − It allows you to set the radix as well, for example, for hexadecimal set it as 16.Let us see an example now to convert decimal to hexadecimal using Integer.toHexString() method.Examplepublic class Demo {    public static void main( String args[] ) {       int dec = 158;       System.out.println(Integer.toHexString(dec));    } }Output9eLet us see an example now to convert decimal to hexadecimal using Integer.parseInt() method.Examplepublic class ...

Read More

Parse and format a number to decimal in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 557 Views

To parse and format a number to decimal, try the following code.Examplepublic class Demo {    public static void main( String args[] ) {       int val = Integer.parseInt("5");       String str = Integer.toString(val);       System.out.println(str);    } }Output5In the above program, we have used the Integer.parseInt() and Integer.toString() method to convert a number to decimal.int val = Integer.parseInt("5"); String str = Integer.toString(val);The toString() method above represents a value in string and formats.

Read More

Parse and format to hexadecimal in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 341 Views

To parse and format to hexadecimal in Java, we have used the BigInteger class. The java.math.BigInteger class provides operations analogues to all of Java's primitive integer operators and for all relevant methods from java.lang.Math.In the below example, we have taken BigInteger and created an object. With that, some values have been set by us in the arguments as the hexadecimal and the radix i.e. 16 for hexadecimal.BigInteger bi = new BigInteger("2ef", 16);Let us see an example.Exampleimport java.math.*; public class Demo {    public static void main( String args[] ) {       BigInteger bi = new BigInteger("2ef", 16);   ...

Read More

Pass an integer by reference in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 2K+ Views

To pass an integer by reference in Java, try the following code −Examplepublic class Demo {    public static void display(int[] arr) {       arr[0] = arr[0] + 50;;    }    public static void main(String[] args) {       int val = 50;       int[] myArr = { val };       display(myArr);       System.out.println(myArr[0]);    } }Output100In the above program, a custom method is created, which pass an int array.int val = 50; int[] myArr = { val }; display(myArr);In the method, we have performed mathematical operation on the value of array.public static void display(int[] arr) {    arr[0] = arr[0] + 50;; }The updated value is then displayed in the main.System.out.println(myArr[0]);

Read More

Reverse an Integer in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 1K+ Views

To reverse an integer in Java, try the following code −Exampleimport java.lang.*; public class Demo {    public static void main(String[] args) {       int i = 239, rev = 0;       System.out.println("Original: " + i);       while(i != 0) {          int digit = i % 10;          rev = rev * 10 + digit;          i /= 10;       }       System.out.println("Reversed: " + rev);    } }OutputOriginal: 239 Reversed: 932In the above program, we have the following int value, which we will reverse.int i = 239;Now, loop through until the value is 0. Find the remainder and perform the following operations to get the reverse of the given integer 239.while(i != 0) {    int digit = i % 10;    rev = rev * 10 + digit;    i /= 10; }

Read More

How to assign int value to char variable in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 2K+ Views

To assign int value to a char variable in Java would consider the ASCII value and display the associated character/ digit.Here, we have a char.char val;Now assign int value to it.val = 77;Now, when you will print the value of “val”, it would display the character associate with the value (ASCII) 77.The following is the complete example.Examplepublic class Demo {    public static void main(String []args) {       char val;       val = 77;       System.out.print("Value: ");       System.out.println(val);    } }OutputValue: M

Read More

Java Program to validate if a String contains only numbers

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 2K+ Views

To validate if a String has only numbers, you can try the following codes. We have used the matches() method in Java here to check for number in a string.Examplepublic class Demo {    public static void main(String []args) {       String str = "978";       System.out.println("Checking for string that has only numbers...");       System.out.println("String: "+str);       if(str.matches("[0-9]+") && str.length() > 2)       System.out.println("String has only numbers!");       else       System.out.println("String consist of characters as well!");    } }OutputChecking for string that has only numbers... String: ...

Read More

Check whether the entered value is a letter or not in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 7K+ Views

To check whether the entered value is a letter or not in Java, use the Character.isLetter() method.We have a value to be checked.char val = 'D';Now let us use the Character.isLetter() method.if (Character.isLetter(val)) {    System.out.println("Character is in Lowercase!"); }else {    System.out.println("Character is in Uppercase!"); }Let us see the complete example now to check for letter.Examplepublic class Demo {    public static void main(String []args) {       System.out.println("Checking whether the given value is a Letter or not...");       char val = 'D';       System.out.println("Value: "+val);       if (Character.isLetter(val)) {     ...

Read More

Java Program to check whether the entered character a digit, white space, lower case or upper case character

Samual Sam
Samual Sam
Updated on 11-Mar-2026 1K+ Views

To check whether the entered character is a digit, whitespace, lowercase or uppercase, you need to check for the ASCII values.Let’s say we have a value in variable “val”, which is to be checked.For Lower Case.if(val >= 97 && val = 65 && val = 48 && val = 97 && val = 65 && val = 48 && val = 97 && val = 65 && val = 48 && val

Read More
Showing 801–810 of 4,498 articles
« Prev 1 79 80 81 82 83 450 Next »
Advertisements