Found 4338 Articles for Java 8

How to initialize and compare strings?

Vikyath Ram
Updated on 22-Jun-2020 11:39:31

84 Views

Following example compares two strings by using str compareTo (string), str compareToIgnoreCase(String) and str compareTo(object string) of string class and returns the ascii difference of first odd characters of compared strings.Example Live Demopublic class StringCompareEmp{    public static void main(String args[]) {       String str = "Hello World";       String anotherString = "hello world";       Object objStr = str;       System.out.println( str.compareTo(anotherString) );       System.out.println( str.compareToIgnoreCase(anotherString) );       System.out.println( str.compareTo(objStr.toString()));    } }OutputThe above code sample will produce the following result.-32 0 0String compare by equals()This method compares ... Read More

Generating OTP in Java

Fendadis John
Updated on 21-Jun-2020 15:00:56

4K+ Views

Generate OTP is now a requirement on most of the website now-a-days. In case of additional authentication, system generates a OTP password adhering to OTP policy of the company. Following example generates a unique OTP adhering to following conditions −It should contain at least one number.Length should be 4 characters.Exampleimport java.util.Random; public class Tester {    public static void main(String[] args) {       System.out.println(generateOTP(4));    }    private static char[] generateOTP(int length) {       String numbers = "1234567890";       Random random = new Random();       char[] otp = new ... Read More

Flow control in a try catch finally in Java

Vikyath Ram
Updated on 21-Jun-2020 14:57:44

7K+ Views

A method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following −Syntaxtry {    // Protected code } catch (ExceptionName e1) {    // Catch block }The code which is prone to exceptions is placed in the try block. When an exception occurs, that exception occurred is handled by catch block associated with it. Every try block should be immediately followed either by ... Read More

Floating point operators and associativity in Java

Fendadis John
Updated on 21-Jun-2020 14:24:45

393 Views

Following programs shows the float arithmetic can cause dubious result if integer values are used using float variables.Examplepublic class Tester {    public static void main(String[] args) {       float a = 500000000;       float b = -500000000;       float c = 1;       float sumabc1 = a+(b+c);       float sumabc2 =(a+b)+c;       System.out.println("Floating Point Arithmetic");       System.out.println("a + ( b + c ) : " + sumabc1);       System.out.println("(a + b) + c : " + sumabc2);       ... Read More

Difference between super() and this() in Java

Vikyath Ram
Updated on 21-Jun-2020 12:42:48

6K+ Views

Following are the notable differences between super() and this() methods in Java. super()this()Definitionsuper() - refers immediate parent class instance.this() - refers current class instance.InvokeCan be used to invoke immediate parent class method.Can be used to invoke current class method.Constructorsuper() acts as immediate parent class constructor and should be first line in child class constructor.this() acts as current class constructor and can be used in parametrized constructors.OverrideWhen invoking a superclass version of an overridden method the super keyword is used.When invoking a current version of an overridden method the this keyword is used.Example Live Democlass Animal {    String name;    Animal(String name) ... Read More

Array To Stream in Java

Samual Sam
Updated on 18-Jun-2020 10:53:23

384 Views

With Java 8, Arrays class has a stream() methods to generate a Stream using the passed array as its source.DescriptionThe java.util.Arrays.stream() method returns a sequential Stream with the specified array as its source. −Arrays.stream(array)DeclarationFollowing is the declaration for java.util.Arrays.stream() methodpublic static Stream stream(T[] array)Type ParameterT − This is the type of the array elements.Parameterarray − This is the source array to be used.Return ValueThis method returns a stream for the array.ExampleThe following example shows the usage of java.util.Arrays.stream() method.Live Demoimport java.util.Arrays; public class Tester {    public static void main(String args[]) {       int data[] = { 1, ... Read More

Array Declarations in Java

karthikeya Boyini
Updated on 18-Jun-2020 09:24:40

723 Views

Here is the syntax for declaring an array variable −SyntaxdataType[] arrayRefVar;   // preferred way. or dataType arrayRefVar[];  // works but not preferred way.Note − The style dataType[] arrayRefVar is preferred. The style dataType arrayRefVar[] comes from the C/C++ language and was adopted in Java to accommodate C/C++ programmers.ExampleThe following code snippets are examples of this syntax −double[] myList;   // preferred way. or double myList[];   // works but not preferred way.Creating ArraysYou can create an array by using the new operator with the following syntax −SyntaxarrayRefVar = new dataType[arraySize];The above statement does two things −It creates an array ... Read More

Array Copy in Java

Samual Sam
Updated on 13-Sep-2023 15:09:03

27K+ Views

Array in Java can be copied to another array using the following ways.Using variable assignment. This method has side effects as changes to the element of an array reflects on both the places. To prevent this side effect following are the better ways to copy the array elements.Create a new array of the same length and copy each element.Use the clone method of the array. Clone methods create a new array of the same size.Use System.arraycopy() method.  The arraycopy() can be used to copy a subset of an array.ExampleCreate a java class named Tester.Tester.javaLive Demopublic class Tester {    public ... Read More

Addition and Concatenation in Java

karthikeya Boyini
Updated on 18-Jun-2020 09:33:21

1K+ Views

'+' operator in java can be used to add numbers and concatenate strings. Following rules should be considered.Only numbers as operands then result will be a number.Only strings as operands then result will be a concatenated string.If both numbers and strings as operands, then numbers coming before string will be treated as numbers.If both numbers and strings as operands, then numbers coming after string will be treated as a string.Above rule can be overridden using brackets().ExampleCreate a java class named Tester.Tester.javaLive Demopublic class Tester {    public static void main(String args[]) {             //Scenario 1: ... Read More

Java program to print ASCII value of a particular character

Lakshmi Srinivas
Updated on 13-Mar-2020 07:06:25

468 Views

ASCII stands for American Standard Code for Information Interchange. There are 128 standard ASCII codes, each of which can be represented by a 7-digit binary number: 0000000 through 1111111.If you try to store a character into an integer value it stores the ASCII value of the respective character.Exampleimport java.util.Scanner; public class ASCIIValue {    public static void main(String args[]){       System.out.println("Enter a character ::");       Scanner sc = new Scanner(System.in);       char ch = sc.next().charAt(0);       int asciiValue = ch;       System.out.println("ASCII value of the given character is ::"+asciiValue);   ... Read More

Advertisements