Found 2617 Articles for Java

Working with csv files in Java

AmitDiwan
Updated on 17-Aug-2020 09:37:10

282 Views

OpenCSV has to be installed first, which is a parser library for Java. The dependency has to be mentioned in the pom.xml file in the maven project. After that, the below code can be utilized.Exampleimport java.io.FileReader; import java.io.*; public class Demo{    public static void readDataLineByLine(String file){       try{          FileReader my_filereader = new FileReader(file);          CSVReader csvReader = new CSVReader(my_filereader);          String[] nextRecord;          while ((nextRecord = csvReader.readNext()) != null){             for (String cell : nextRecord){         ... Read More

Class and Static Variables in Java

AmitDiwan
Updated on 17-Aug-2020 09:34:45

6K+ Views

Class variables are also known as static variables, and they are declared outside a method, with the help of the keyword ‘static’.Static variable is the one that is common to all the instances of the class. A single copy of the variable is shared among all objects.Example Live Demopublic class Demo{    static int my_count=2;    public void increment(){       my_count++;    }    public static void main(String args[]){       Demo obj_1=new Demo();       Demo obj_2=new Demo();       obj_1.increment();       obj_2.increment();       System.out.println("The count of first object is "+obj_1.my_count); ... Read More

Object Graph in Java Serialization

AmitDiwan
Updated on 17-Aug-2020 09:30:43

619 Views

An object graph contains a set of objects that are automatically serialized given that the object that contains the reference is serialized too. Any object that is serialized and contains an object reference, the object reference will be serialized by the JVM.Example Live Demoimport java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; class One implements Serializable{    Two s2 = new Two(); } class Two implements Serializable{    Three s3 = new Three(); } class Three implements Serializable{    int i = 34;    int j = 67; } public class Demo_Serialize{    public static void main(String args[]) throws Exception{ ... Read More

What’s the connection between Java and Blockchain?

AmitDiwan
Updated on 17-Aug-2020 09:28:52

109 Views

Blockchain has become a buzz word in the recent times. It is being tried to be implemented in every software for various purposes, to check how it would work efficiently given different scenarios. It is a decentralized technology. It basically is data that is digital in nature and every piece of data is known as a transaction. Hence, the date, time and amount of that specific transaction is stored in the blockchain. Every block is unique due to the unique code it has, that is also known as ‘hash’. It is created with the help of different specialized algorithms.Investors are ... Read More

Verification in Java (JVM)

AmitDiwan
Updated on 17-Aug-2020 09:27:42

513 Views

Once the byte code is loaded by the JVM, (with the help of the .class file), the bytecode is checked to see the validity with the help of the verifier. The verifier checks the linking so as to perform operations efficiently. This way, the interpreter performs much efficiently. This process is known as verification.Example Live Demopublic class Demo{    private float my_val;    float my_function(int my_val){       int balance = my_val;       this.my_val += balance;       return this.my_val;    }    public static void main(String[] args){       Demo my_obj = new Demo();   ... Read More

What are C++ features missing in Java?

AmitDiwan
Updated on 17-Aug-2020 09:20:06

1K+ Views

There are many features that can be seen in C++ but not in Java. A few of them have been listed below −No unsigned int option in JavaNo destructor in Java as well as ‘delete’ since garbage collector performs this operation for it.No friend classes or friend functions in Java.There are no pointers in Java.There is no typedef option in Java.Since Java is a purely object oriented language, there are no global variables or global functions.The concept of templates present in C++ can’t be found in Java.The ‘::’ scope resolution operator is not there, since there is no question of ... Read More

Examples of soft references and phantom references?

AmitDiwan
Updated on 17-Aug-2020 09:17:59

179 Views

Soft references are often used to implement memory-sensitive caches. Let us see an example of soft references in Java −Example Live Demoimport java.lang.ref.SoftReference; class Demo{    public void display_msg(){       System.out.println("Hello there");    } } public class Demo_example{    public static void main(String[] args){       Demo my_instance = new Demo();       my_instance.display_msg();       SoftReference my_softref = new SoftReference(my_instance);       my_instance = null;       my_instance = my_softref.get();       my_instance.display_msg();    } }OutputHello there Hello thereA class named Demo contains a function named ‘display_msg’, that displays the relevant ... Read More

Unreachable statement using the non-final variable in Java

AmitDiwan
Updated on 17-Aug-2020 09:06:35

72 Views

Following is an example, wherein we will see unreachable statement using the non-final variable −Exampleclass Demo_example {    int a = 2, b = 3;    void display_msg(){       while (a < b){          System.out.println("The first variable is greater than the second");       }       System.out.println("This is an unreachable statement");    } } public class Demo{    public static void main(String args[]){       Demo_example my_instance = new Demo_example();       my_instance.display_msg();    } }Output“The first variable is greater than the second” displayed infinitelyA class named Demo_example, that defines ... Read More

Unreachable statement using final variable in Java

AmitDiwan
Updated on 17-Aug-2020 09:04:48

124 Views

Unreachable statements are those that don’t get executed when the code is being executed. This could be so because −There is a return statement before the code.There is an infinite loop in the code.The execution of the code is terminated forcibly before it executes.Here, we will see how the unreachable statement can be used with the ‘final’ keyword −Example Live Democlass Demo_example{    final int a = 56, b = 99;    void func_sample(){       while (a < b){          System.out.println("The first value is less than the second.");       }       System.out.println("This ... Read More

Types of References in Java

AmitDiwan
Updated on 17-Aug-2020 09:02:34

685 Views

There are four different kinds of references based on the way in which the data is garbage collected.Strong referencesWeak referencesSoft referencesPhantom referencesStrong referenceIt is the default type of reference object. An object that has active strong reference can’t be garbage collected. It is possible only if the variable that is strongly referenced points to null. Let us see an example −Exampleclass Demo {    //Some functionality } public class Demo_example{    public static void main(String[] args){       Demo my_inst = new Demo();       my_inst = null;    } }Weak referenceThey are not default class of reference ... Read More

Advertisements