Numeric Promotion in Conditional Expression in Java

AmitDiwan
Updated on 04-Jul-2020 09:20:21

171 Views

The conditional operator (? :) leverages the output of one value (which is a bool) to decide which expression has to be evaluated next. Let us see an example −Example Live Demoimport java.io.*; public class Demo{    public static void main (String[] args){       Object my_obj = true ? new Integer(91) : new Float(89);       System.out.println(my_obj);    } }Output91.0A class named Demo contains the main function. Here, an object instance is defined and if it is true, an integer value is displayed otherwise a float value is displayed. Next, they are printed on the console.When promotional expression ... Read More

Java Lambda Expression with Collections

AmitDiwan
Updated on 04-Jul-2020 09:16:54

894 Views

Sorting the elements of a list using lambda expression −Example Live Demoimport java.util.*; public class Demo{    public static void main(String[] args){       ArrayList my_arr = new ArrayList();       my_arr.add(190);       my_arr.add(267);       my_arr.add(12);       my_arr.add(0);       System.out.println("Before sorting, elements in the array list are : " + my_arr);       Collections.sort(my_arr, (o1, o2) -> (o1 > o2) ? -1 : (o1 < o2) ? 1 : 0);       System.out.println("After sorting, elements in the array list are : " + my_arr);    } }OutputBefore sorting, elements ... Read More

Clear Method in Java ConcurrentHashMap

AmitDiwan
Updated on 04-Jul-2020 09:06:38

336 Views

The clear function is used to clear up the mapping between the key value pairs. This way, the ConcurrentHashMap mappings would be cleared.Syntaxpublic void clear()Let us see an example −Example Live Demoimport java.util.concurrent.ConcurrentHashMap; import java.util.*; public class Demo{    public static void main(String[] args){       Map my_map = new ConcurrentHashMap();       my_map.put("This", "35");       my_map.put("is", "78");       my_map.put("sample", "99");       System.out.println("The map contains the below elements " + my_map);       my_map.clear();       System.out.println("The elements after the clear function is called on it " + my_map);    } ... Read More

Java Concurrency Sleep Method

AmitDiwan
Updated on 04-Jul-2020 09:01:30

432 Views

The sleep functionThis sleep function is used to ensure that the currently executing thread goes to sleep for a specific amount of milliseconds which is passed as a parameter to the function. The thread stops executing for that number of milliseconds.Let us see an exampleExample Live Demoimport java.lang.*; public class Demo implements Runnable{    Thread my_t;    public void run(){       for (int i = 0; i < 3; i++){          System.out.println(Thread.currentThread().getName()+ " " + i);          try{             Thread.sleep(100);          }       ... Read More

Jar Files in Java

AmitDiwan
Updated on 04-Jul-2020 08:55:10

764 Views

JAR is a file format that helps in aggregating Java class file, along with its associated metadata and resources into a single file.Creation of a JAR file − The jar cf command can be used, where ‘cf’ means creating the file.jar cf jar_file_name files_to_compressOnce a jar file has been created, a default manifest file is also created. Only one manifest file is present in a specific archive, and it will have the extension ‘mf’ and will be in the pathname. Thismanifest file helps give information about the files that have been compressed/present in the package.Viewing a JAR file − The ... Read More

Iterator vs Collection in Java

AmitDiwan
Updated on 04-Jul-2020 08:53:41

444 Views

IteratorIt is used in Collection Framework so as to retrieve elements as and when they are required.public interface IteratorIt can be used with the ‘next’ function to move and access the next elements. The ‘remove’ function can be used to remove an element from the data structure.It is quicker in comparison to Collections, since the number of operations associated with Iterator is less.Below is an example of an iterator working with a list −Example Live Demomport java.io.*; import java.util.*; public class Demo{    public static void main(String[] args){       ArrayList my_list = new ArrayList();       my_list.add("Its");   ... Read More

Island of Isolation in Java

AmitDiwan
Updated on 04-Jul-2020 08:51:19

1K+ Views

After an object has been used, it is deallocated from the memory using the Garbage Collector class. The objects are destroyed based on the fact that no reference to that object is present. The Garbage Collector class calls the ‘finalize’ function on the object that needs to be destroyed.What is island of isolation?When two objects ‘a’, and ‘b’ reference each other, and they are not referenced by any other object, it is known as island of isolation.It is a group of objects which reference each other but they are not referenced but other objects of other applications at all.Note − ... Read More

Add JSONArray Within JSONObject in Java

raja
Updated on 04-Jul-2020 08:50:58

5K+ Views

A JSONObject can parse text from a String to produce a map-like object and a JSONArray can parse text from a String to produce a vector-like object. We can also add a JSONArray within JSONObject by first creating a JSONArray with few items and add these array of items to the put() method of JSONObject class.Syntaxpublic JSONObject put(java.lang.String key, java.util.Collection value) throws JSONExceptionExampleimport org.json.*; public class AddJSONArrayTest {    public static void main(String[] args) throws JSONException {       JSONArray array = new JSONArray();       array.put("INDIA");       array.put("AUSTRALIA");       array.put("ENGLAND");       JSONObject obj = ... Read More

Is an Array a Primitive Type or an Object in Java

AmitDiwan
Updated on 04-Jul-2020 08:45:15

2K+ Views

Array is considered to be an object in Java. The reason behind this is that an array can be created using the ‘new’ keyword. The ‘new’ keyword/operator is always used to create an object. This is how an array is perceived as an object.The direct parent class or super class of any array is the ‘Object’ class. Every array type in Java belongs to a certain class. This indicates that there are explicit classes for integer array types, float array types, double array types, and so on.Arrays can be dynamically created, and be assigned variables as well.Let us see an ... Read More

Interning of String in Java

AmitDiwan
Updated on 04-Jul-2020 08:42:01

318 Views

String interning is a process wherein a single copy of every distinct string value is stored. In addition to this, the strings can’t be changed too. This way, strings can contain the same data as well as share the same memory. This way, the memory required would be greatly reduced.When the ‘intern’ function is called −It checks the equality between two strings- whether the string object is present in the String Constant pool (SCP) or not.If available, the string is returned by fetching it from the pool. Otherwise, a new String object is created and added to the pool. A ... Read More

Advertisements