Found 346 Articles for Java Programming

How to iterate any Map in Java?

Paul Richard
Updated on 22-Jun-2020 11:34:18

116 Views

Following example uses iterator Method of Collection class to iterate through the HashMap.Example Live Demoimport java.util.*; public class Main {    public static void main(String[] args) {       HashMap< String, String> hMap = new HashMap< String, String>();       hMap.put("1", "1st");       hMap.put("2", "2nd");       hMap.put("3", "3rd");       Collection cl = hMap.values();       Iterator itr = cl.iterator();       while (itr.hasNext()) {          System.out.println(itr.next());       }    } }OutputThe above code sample will produce the following result.1st 2nd 3rd

How to initialize and compare strings?

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

72 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 random numbers in Java

Vikyath Ram
Updated on 21-Jun-2020 15:16:08

1K+ Views

We can generate random numbers using three ways in Java.Using java.util.Random class − Object of Random class can be used to generate random numbers using nextInt(), nextDouble() etc. methods.Using java.lang.Math class − Math.random() methods returns a random double whenever invoked.Using java.util.concurrent.ThreadLocalRandom class − ThreadLocalRandom.current().nextInt() method and similar othjer methods return a random numbers whenever invoked.Exampleimport java.util.Random; import java.util.concurrent.ThreadLocalRandom; public class Tester {    public static void main(String[] args) {       generateUsingRandom();       generateUsingMathRandom();       generateUsingThreadLocalRandom();    }    private static void generateUsingRandom() {       Random random = new Random(); ... Read More

Generating password in Java

Rishi Raj
Updated on 21-Jun-2020 15:20:45

11K+ Views

Generate temporary password is now a requirement on almost every website now-a-days. In case a user forgets the password, system generates a random password adhering to password policy of the company. Following example generates a random password adhering to following conditions −It should contain at least one capital case letter.It should contain at least one lower-case letter.It should contain at least one number.Length should be 8 characters.It should contain one of the following special characters: @, $, #, !.Exampleimport java.util.Random; public class Tester{    public static void main(String[] args) {       System.out.println(generatePassword(8));    }    private ... 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

Garbage collection in Java

Fendadis John
Updated on 21-Jun-2020 15:04:53

646 Views

Java Garbage collector tracks the live object and objects which are no more need are marked for garbage collection. It relieves developers to think of memory allocation/deallocation issues.JVM uses the heap, for dynamic allocation. In most of the cases, the operating systems allocate the heap in advance which is then to be managed by the JVM while the program is running. It helps in following ways −Faster object creation as Operating system level synchronization is no more needed for each object. Object Allocation takes some memory and increases the offset.When an object is not required, garbage collector reuses the object's ... Read More

Formatted Output in Java

Fendadis John
Updated on 21-Jun-2020 15:10:18

2K+ Views

String provides format() method can be used to print formatted output in java.System.out.printf() method can be used to print formatted output in java.Following example returns a formatted string value by using a specific locale, format and arguments in format() methodExampleimport java.util.*; public class StringFormat {    public static void main(String[] args) {       double e = Math.E;       System.out.format("%f%n", e);       System.out.format(Locale.GERMANY, "%-10.4f%n%n", e);    } }OutputThe above code sample will produce the following result.2.718282 2, 7183The following is an another sample example of format strings.Examplepublic class HelloWorld {    public static ... Read More

Flow control in a try catch finally in Java

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

6K+ 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

363 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

Flexible nature of java.lang.object

Fendadis John
Updated on 21-Jun-2020 14:35:44

86 Views

The java.lang.Object class is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.Class DeclarationFollowing is the declaration for java.lang.Object class −public class ObjectClass constructorsSr.No.Constructor & Description1Object()This is the Single Constructor.Class methodsSr.No.Method & Description1protected Object clone()This method creates and returns a copy of this object.2boolean equals(Object obj)This method indicates whether some other object is "equal to" this one.3protected void finalize()This method is called by the garbage collector on an object when garbage collection determines that there are no more references to the object.4Class getClass()This method returns the ... Read More

Previous 1 ... 4 5 6 7 8 ... 35 Next
Advertisements