Found 9326 Articles for Object Oriented Programming

How to parse JSON in Java?

Arushi
Updated on 22-Jun-2020 11:30:03

3K+ Views

This articles covers how to encode and decode JSON objects using Java programming language. Let's start with preparing the environment to start our programming with Java for JSON.EnvironmentBefore you start with encoding and decoding JSON using Java, you need to install any of the JSON modules available. For this tutorial we have downloaded and installed JSON.simple and have added the location of json-simple-1.1.1.jar file to the environment variable CLASSPATH.Mapping between JSON and Java entitiesJSON.simple maps entities from the left side to the right side while decoding or parsing, and maps entities from the right to the left while encoding.JSONJavastringjava.lang.Stringnumberjava.lang.Numbertrue|falsejava.lang.Booleannullnullarrayjava.util.Listobjectjava.util.MapOn decoding, ... Read More

How to measure the time taken by a function in Java?

Arushi
Updated on 22-Jun-2020 11:30:52

255 Views

java.lang.System.currentTimeMillis() method can be used to compute the time taken by a function in java. Trick is simple. Get the before time and after time using currentTimeMillis() where before time is the time when method is invoked and after time is when method has executed. See the example below −Example Live Demopublic class Tester{    public static void main(String[] args) {       long startTime = System.currentTimeMillis();       longRunningMethod();       long endTime = System.currentTimeMillis();       System.out.println("Time taken: " + (endTime -startTime) + " ms");    }    private static void longRunningMethod() {       try {          Thread.sleep(1000);       } catch (InterruptedException e) {          e.printStackTrace();       }    } }OutputTime taken: 1000 ms

How to compare two arrays in Java?

Paul Richard
Updated on 22-Jun-2020 11:31:37

13K+ Views

Arrays can be compared using following ways in JavaUsing Arrays.equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method.Using Arrays.deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method.Using == on array will not give the desired result and it will compare them as objects. See the example below for each of the comparison way.Example Live Demoimport java.util.Arrays; public class Tester{    public static void main(String[] args) {       int[] array1 = {1, 2, 3};       int[] ... Read More

How to make an object eligible for garbage collection in Java?

Fendadis John
Updated on 30-Jul-2019 22:30:23

152 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 ... Read More

How is Java strictly pass by value?

Arushi
Updated on 22-Jun-2020 11:32:45

211 Views

Call by Value means calling a method with a parameter as value. Through this, the argument value is passed to the parameter.While Call by Reference means calling a method with a parameter as a reference. Through this, the argument reference is passed to the parameter.In call by value, the modification done to the parameter passed does not reflect in the caller's scope while in the call by reference, the modification done to the parameter passed are persistent and changes are reflected in the caller's scope. But Java uses only call by value. It creates a copy of references and pass ... Read More

How is Java platform independent?

Paul Richard
Updated on 30-Jul-2019 22:30:23

411 Views

When you compile Java programs using javac compiler it generates bytecode. We need to execute this bytecode using JVM (Java Virtual machine) Then, JVM translates the Java bytecode to machine understandable code. You can download JVM's (comes along with JDK or JRE) suitable to your operating system and, once you write a Java program you can run it on any system using JVM.

How to iterate any Map in Java?

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

135 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

83 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

Advertisements