Found 34494 Articles for Programming

Floating-point conversion characters in Java

karthikeya Boyini
Updated on 27-Jun-2020 05:14:59

278 Views

Floating-point conversion characters include the following.CharacterDescription%edecimal number in computerized scientific notation%Edecimal number in computerized scientific notation%fdecimal number%gbased on computerized scientific notation or decimal format, %Gbased on computerized scientific notation or decimal format, Example Live Demopublic class Demo {    public static void main(String[] args) throws Exception {       System.out.printf("Integer conversions...");       System.out.printf( "Integer: %d", 889 );       System.out.printf( "Negative Integer: %d", -78 );       System.out.printf( "Octal: %o", 677 );       System.out.printf( "Hexadecimal: %x", 56 );       System.out.printf( "Hexadecimal: %X", 99 );       System.out.printf("Floating-point conversions...");       ... Read More

Integral conversion characters in Java

Samual Sam
Updated on 27-Jun-2020 05:16:34

108 Views

Intergral conversion characters include the following.CharacterDescription%dInteger%oOctal%xHexadecimal%XHexadecimalExample Live Demopublic class Demo {    public static void main(String[] args) throws Exception {       System.out.printf( "Integer: %d", 889 );       System.out.printf( "Negative Integer: %d", -78 );       System.out.printf( "Octal: %o", 677 );       System.out.printf( "Hexadecimal: %x", 56 );       System.out.printf( "Hexadecimal: %X", 99 );    } }OutputInteger: 889 Negative Integer: -78 Octal: 1245 Hexadecimal: 38 Hexadecimal: 63

Unix date format in Java

karthikeya Boyini
Updated on 27-Jun-2020 05:17:18

386 Views

Use the ‘c’ date conversion character to display UNIX date format in Java.System.out.printf("Unix date format: %tc",d);Above, d is a date object.Date d = new Date();Example Live Demoimport java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; public class Demo {    public static void main(String[] args) throws Exception {       Date d = new Date();       System.out.printf("Unix date format: %tc",d);       System.out.printf("Unix date format: %Tc",d);    } }OutputUnix date format: Mon Nov 26 12:24:10 UTC 2018 Unix date format: MON NOV 26 12:24:10 UTC 2018

Display ISO 8601 standard date in Java

Samual Sam
Updated on 27-Jun-2020 05:19:00

745 Views

Use the ‘F’ date conversion character to display ISO 8601 standard date.System.out.printf("ISO 8601 standard date = %tF", d);Above, d is a date object.Date d = new Date();Example Live Demoimport java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; public class Demo {    public static void main(String[] args) throws Exception {       Date d = new Date();       System.out.printf("Four-digit Year = %TY",d);       System.out.printf("Two-digit Year = %ty",d);       System.out.printf("ISO 8601 standard date = %tF", d);    } }OutputFour-digit Year = 2018 Two-digit Year = 18 ISO 8601 standard date = 2018-11-26

Copy characters from string into char Array in Java

karthikeya Boyini
Updated on 26-Jun-2020 16:50:47

850 Views

Let’s say we have the following string.String str = "Demo Text!";To copy some part of the above string, use the getChars() method.// copy characters from string into chArray =str.getChars( 0, 5, chArray, 0 );The following is an example to copy characters from string into char Array in Java.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "Demo Text!";       char chArray[] = new char[ 5 ];       System.out.println("String: "+str);       // copy characters from string into chArray       str.getChars( 0, 5, chArray, 0 ... Read More

Java Program to create Character Array from String Objects

Samual Sam
Updated on 26-Jun-2020 16:56:31

225 Views

Use the toCharArray() method in Java to create character arrays from string objects. The following is our string.String str = "This is it!";Now, let us create character array from the above string.char[] strArr = str.toCharArray();Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "This is it!";       char[] strArr = str.toCharArray();       for(char res: strArr) {          System.out.println(res);       }    } }OutputT h i s i s i t !Let us see another example in which we are creating ... Read More

Java Program to get the Characters in a String as an Array of Bytes

Samual Sam
Updated on 26-Jun-2020 16:30:00

239 Views

To get the characters in a string as an array of bytes, use the getBytes() method.Let’s say we have the following string.String str = "Demo Text!";Convert it to array of bytes.byte[] arr = str.getBytes();Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "Demo Text!";       // getting as an array of bytes       byte[] arr = str.getBytes();       // displaying each character as a Byte value       for(byte res: arr) {          System.out.println(res);       }    } }Output68 101 109 111 32 84 101 120 116 33

Convert byte to String in Java

Samual Sam
Updated on 26-Jun-2020 16:31:54

745 Views

The valueOf() method is used in Java to convert byte to string.Let’s say we have the following byte value.byte val = 40;Converting the above byte to string.String.valueOf(val);Example Live Demopublic class Demo {    public static void main(String[] args) {       byte val = 40;       // converting       String str = String.valueOf(val);       System.out.println("String: "+str);    } }OutputString: 40

Java Program to convert int to Octal String

karthikeya Boyini
Updated on 26-Jun-2020 16:32:50

573 Views

The Integer.toOctalString() method in Java converts int to octal string.Let’s say the following are our integer values.int val1 = 9; int val2 = 20; int val3 = 2;Convert the above int values to octal string.Integer.toOctalString(val1); Integer.toOctalString(val2); Integer.toOctalString(val3);Example Live Demopublic class Demo {    public static void main(String[] args) {       int val1 = 9;       int val2 = 20;       int val3 = 30;       int val4 = 78;       int val5 = 2;       System.out.println("Converting integer "+val1+" to Octal String:       "+Integer.toOctalString(val1));       System.out.println("Converting ... Read More

Java Program to convert int to binary string

Samual Sam
Updated on 26-Jun-2020 16:33:49

4K+ Views

The Integer.toBinaryString() method in Java converts int to binary string.Let’s say the following are our integer values.int val1 = 9; int val2 = 20; int val3 = 2;Convert the above int values to binary string.Integer.toBinaryString(val1); Integer.toBinaryString(val2); Integer.toBinaryString(val3);Example Live Demopublic class Demo {    public static void main(String[] args) {       int val1 = 9;       int val2 = 20;       int val3 = 30;       int val4 = 78;       int val5 = 2;       System.out.println("Converting integer "+val1+" to Binary String: "+Integer.toBinaryString(val1));       System.out.println("Converting integer "+val2+" to ... Read More

Advertisements