Found 9150 Articles for Object Oriented Programming

Convert decimal integer to hexadecimal number in Java

karthikeya Boyini
Updated on 26-Jun-2020 08:06:13

393 Views

To convert decimal integer to hexadecimal, use the Integer.toHexString() method. Let’s say the following is our decimal integer.int decInt = 25;The following is the usage of the Integer.toHexString() method to convert decimal integer to hexadecimal number.String myHex = Integer.toHexString(decInt);The following the complete example.Example Live Demopublic class Demo {    public static void main(String []args) {       int decInt = 25;       System.out.println("Decimal Integer = "+decInt);       String myHex = Integer.toHexString(decInt);       System.out.println("Hexadecimal = "+myHex);    } }OutputDecimal Integer = 25 Hexadecimal = 19

Convert octal number to decimal number in Java

karthikeya Boyini
Updated on 26-Jun-2020 07:26:09

720 Views

To convert octal to decimal number, use the Integer.parseInt() method with radix 8. Here, the radix is for Octal i.e. 8.Let’s say we have the following octal string.String myOctal = "25";To convert it to decimal, use the Integer.parseInt() method.int val = Integer.parseInt(myOctal, 8);The following is the complete example.Example Live Demopublic class Demo {    public static void main(String []args) {       String myOctal = "25";       System.out.println("Octal = "+myOctal);       int val = Integer.parseInt(myOctal, 8);       System.out.println("Decimal = "+val);    } }OutputOctal = 25 Decimal = 21

Java program to decode string to integer

Alshifa Hasnain
Updated on 06-Mar-2025 19:22:31

552 Views

In this article, we will learn to decode string to integer in Java. Converting a string to an integer is a routine task in Java programming. It is helpful when working with user inputs, file operations, and data fetching from databases. Problem Statement The goal is to convert a given string representation of a number into an integer. For ExampleInput String val = "2"; Output Integer = 2 Different Approaches The following are the two different approaches to decode string to integer in Java − Using Integer.decode() Using Integer.parseInt() Using ... Read More

Convert from String to long in Java

karthikeya Boyini
Updated on 26-Jun-2020 07:29:06

301 Views

To convert String to long, the following are the two methods.Method 1The following is an example wherein we use parseLong() method.Example Live Demopublic class Demo {    public static void main(String[] args) {       String myStr = "5";       Long myLong = Long.parseLong(myStr);       System.out.println("Long: "+myLong);    } }OutputLong: 5Method 2The following is an example wherein we have used valueOf() and longValue() method to convert String to long.Example Live Demopublic class Demo {    public static void main(String[] args) {       String myStr = "20";       long res = Long.valueOf(myStr).longValue();       System.out.println("Long: "+res);    } }OutputLong: 20

Java program to multiply long integers and check for overflow

Samual Sam
Updated on 02-Sep-2024 19:29:01

2K+ Views

To check for Long overflow, we need to check the Long.MAX_VALUE with the multiplied long result, Here, Long.MAX_VALUE is the maximum value of the Long type in Java. Let us see an example wherein long values are multiplied and if the result is more than the Long.MAX_VALUE, then an exception is thrown. Steps to multiply long integers and check for overflow Following are the steps to multiply long integers and check for overflow in Java − Initialize long values by defining two long integers, val1 and val2. Multiply val1 and val2 to ... Read More

Java program to add long integers and check for overflow

karthikeya Boyini
Updated on 16-Oct-2024 16:29:42

1K+ Views

In this article, we will add two long integers in Java and check if the sum causes an overflow, which happens when the result exceeds the maximum value that a long data type can hold,  defined by the Long class as Long.MAX_VALUE. If the sum goes beyond this limit, an exception will be thrown to handle the overflow. Otherwise, the sum will be displayed as the result. Steps to add long integers and check for overflow Following are the steps to add long integers and check for overflow − First, we will define two long integers ... Read More

Java Program to check whether the character is ASCII 7 bit

Samual Sam
Updated on 26-Jun-2020 07:11:17

417 Views

To check whether the character is ASCII 7 bit or not, check whether given value’s ASCII value is less than 128 or not.Here, we have a character.char one = '-';Now, we have checked a condition with if-else for ASCII 7-bit character.if (c < 128) { System.out.println("Given value is ASCII 7 bit!"); } else {    System.out.println("Given value is not an ASCII 7 bit!"); }The following is an example wherein we check a character for ASCII 7-bit.Example Live Demopublic class Demo {    public static void main(String []args) {       char c = '-';       System.out.println("Given value = ... Read More

Java Program to check whether the entered value is ASCII 7-bit control character

karthikeya Boyini
Updated on 26-Jun-2020 07:13:37

293 Views

To check whether the entered value is ASCII 7-bit control character, check the characters ASCII value before 32 and 127. These are the control characters.Here, we have a character.char one = ' n ';Now, we have checked a condition with if-else to check for character less than 32 (ASCII) and equal to 127.if (one < 32 || one == 127) { System.out.println("Given value is a control character!"); } else {    System.out.println("Given value is not a control character!"); }Example Live Demopublic class Demo {    public static void main(String []args) {       char one = '';       ... Read More

Java Program to check whether the entered value is ASCII 7 bit alphabetic

Samual Sam
Updated on 15-Nov-2024 18:42:19

170 Views

In this article, we will discover how we can check whether a value entered is an ASCII 7-bit alphabetic character in Java. This process involves whether a character belongs to the ranges 'a' to 'z' or 'A' to 'Z' in the ASCII table. The character can be checked with some simple if statements to determine if it is in either of these two ranges and then corresponding messages are displayed. Problem StatementGiven a character, write a Java program to check whether the entered value is an ASCII 7-bit alphabetic character or not.Input Initial character = 'r' input = '1'Output Character: ... Read More

Java program to check whether the entered value is ASCII 7 bit alphabetic uppercase

karthikeya Boyini
Updated on 15-Nov-2024 18:41:31

155 Views

In this article, we will explore how to verify whether or not a given character is an uppercase letter and, more specifically, falls within the range of A to Z. This is useful when a user’s input consists of some uppercase letter or when a program needs to restrict itself to uppercase letters. We use concepts such as if-else statements, character comparison, and conditional logic to solve this problem. Problem StatementWe need to write a program that checks if a character falls within the uppercase alphabetic range, which spans from 'A' to 'Z'.Input The input will consist of a single character.Output ... Read More

Advertisements