Java Articles

Page 83 of 450

Java program to check if binary representation is palindrome

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

A palindrome is a sequence that is same both forwards and backwards. The binary representation of a number is checked for being a palindrome but no leading 0’s are considered. An example of this is given as follows − Number = 5 Binary representation = 101 The binary representation of 5 is a palindrome as it is the same both forwards and backwards. A program that demonstrates this is given as follows. Example public class Example { public static void main(String argc[]) { long num = 5, n1; ...

Read More

Different ways to format long with Java System.out.format

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

The System.out.format is used in Java to format output. Here, let’s say the following is our long. long val = 787890; To format, we have considered the following that justifies the output. System.out.format("%d%n", val); System.out.format("%9d%n", val); System.out.format("%+9d%n", val); System.out.format("%08d%n", val); System.out.format("%, 9d%n", val); The following is the complete example that displays the difference in output as well. Example import java.util.Locale; public class Demo { public static void main(String []args) { long val = 787890; System.out.format("%d%n", val); ...

Read More

Hexadecimal integer literal in Java

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

For Hexadecimal, the 0x or 0X is to be placed in the beginning of a number. Note − Digits 10 to 15 are represented by a to f (A to F) in Hexadecimal Here are some of the examples of hexadecimal integer literal declared and initialized as int. int one = 0X123; int two = 0xABC; Example public class Demo { public static void main(String []args) { int one = 0X123; int two = 0xABC; System.out.println("Hexadecimal: "+one); System.out.println("Hexadecimal: "+two); } } Output Hexadecimal: 291 Hexadecimal: 2748

Read More

Hexadecimal literal of type long in Java

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

Hexadecimal literal of type long is represented as − long hexLong = 0XABL; For Hexadecimal, the 0x or 0X is to be placed in the beginning of a number. Note − Digits 10 to 15 are represented by a to f (A to F) in Hexadecimal Example public class Demo { public static void main(String []args) { long hexLong = 0XABL; System.out.println("Hexadecimal literal of type long: "+hexLong); } } Output Hexadecimal literal of type long:171

Read More

Define integer literals as octal values in Java

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

Literals with a leading zero are octal literals. Any number prefixed with a 0 is considered octal. Octal numbers can only use digits 0-7, just like decimal can use 0-9, and binary can use 0-1. To define integer literals as octal value in Java is effortless. Here is the declaration and initialization. int myOct = 023; Example public class Demo { public static void main(String []args) { int myOct = 023; System.out.println(myOct); } } Output 19 Let us see another example. Example public class Demo { public static void main(String []args) { int myOct = 010; System.out.println(myOct); } } Output 8

Read More

Java program to extract 'k' bits from a given position

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

Extraction of k bits from the given position in a number involves converting the number into its binary representation. An example of this is given as follows −Number = 20 Binary representation = 10100 k = 3 Position = 2 The bits extracted are 010 which represent 2.A program that demonstrates this is given as follows.Examplepublic class Example {    public static void main (String[] args) {       int number = 20, k = 3, pos = 2;       int exNum = ((1 > (pos - 1));       System.out.println("Extract " + k + " ...

Read More

Ternary Operator in Java

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

A ternary operator uses 3 operands and it can be used to replace the if else statement. This can be done to make the code simpler and more compact.The syntax of the ternary operator is given as follows −Expression ? Statement 1 : Statement 2In the above syntax, the expression is a conditional expression that results in true or false. If the value of the expression is true, then statement 1 is executed otherwise statement 2 is executed.A program that demonstrates the ternary operator in Java is given as follows.Examplepublic class Example {    public static void main(String[] args) { ...

Read More

Java program to display Astrological sign or Zodiac sign for given date of birth

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

Each date of birth corresponds to a given Zodiac sign. A table that demonstrates these signs and their corresponding dates is given below −Zodiac SignDateAriesMarch 21 - April 19TaurusApril 20 - May 20GeminiMay 21 - June 20CancerJune 21 - July 22LeoJuly 23 - August 22VirgoAugust 23 - September 22LibraSeptember 23 - October 22ScorpioOctober 23 - November 21SagittariusNovember 22 - December 21CapricornDecember 22 - January 19AquariusJanuary 20 - February 18PiscesFebruary 19 - March 20A program that displays the Astrological sign or Zodiac sign for a given date of birth is given as follows.Examplepublic class Example {    public static void main ...

Read More

Java Program to flip a bit in a BigInteger

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

To flip a bit in a BigInteger in Java, use the flipBit() method. This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit flipped.Exampleimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two;       one = new BigInteger("7");       one = one.flipBit(3);       System.out.println("Result: " +one);    } }OutputResult: 15Let us see another example.Exampleimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger bi1, bi2;       bi1 = new BigInteger("8"); ...

Read More

Java Program to shift bits in a BigInteger

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

To shift bits in a BigInteger, use the shiftLeft() or shiftRight() method.shiftLeft() methodThe java.math.BigInteger.shiftLeft(int n) returns a BigInteger whose value is (this > n). Sign extension is performed. The shift distance, n, may be negative, in which case this method performs a left shift. It computes floor(this / 2n).Exampleimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one;       one = new BigInteger("25");       one = one.shiftRight(3);       System.out.println("Result: " +one);    } }OutputResult: 3

Read More
Showing 821–830 of 4,498 articles
« Prev 1 81 82 83 84 85 450 Next »
Advertisements